Skip to content

Commit 440781e

Browse files
authored
PMM-11950 admin requests (#665)
* PMM-11950 fetch only needed requests * PMM-11950 update panel: fetch if admin * PMM-11950 UpdatePanel: use store
1 parent 78efc81 commit 440781e

File tree

3 files changed

+28
-43
lines changed

3 files changed

+28
-43
lines changed

public/app/percona/shared/components/PerconaBootstrapper/PerconaBootstrapper.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react';
22

3+
import { config } from '@grafana/runtime';
34
import { Button, HorizontalGroup, Icon, Modal, useStyles2, useTheme2 } from '@grafana/ui';
4-
import { contextSrv } from 'app/core/services/context_srv';
55
import {
66
fetchServerInfoAction,
77
fetchServerSaasHostAction,
@@ -18,6 +18,7 @@ import { useAppDispatch } from 'app/store/store';
1818

1919
import { Telemetry } from '../../../ui-events/components/Telemetry';
2020
import usePerconaTour from '../../core/hooks/tour';
21+
import { isPmmAdmin } from '../../helpers/permissions';
2122

2223
import { Messages } from './PerconaBootstrapper.messages';
2324
import { getStyles } from './PerconaBootstrapper.styles';
@@ -32,7 +33,8 @@ export const PerconaBootstrapper = ({ onReady }: PerconaBootstrapperProps) => {
3233
const [modalIsOpen, setModalIsOpen] = useState(true);
3334
const [showTour, setShowTour] = useState(false);
3435
const styles = useStyles2(getStyles);
35-
const isLoggedIn = contextSrv.user.isSignedIn;
36+
const { user } = config.bootData;
37+
const { isSignedIn } = user;
3638
const theme = useTheme2();
3739

3840
const dismissModal = () => {
@@ -73,28 +75,31 @@ export const PerconaBootstrapper = ({ onReady }: PerconaBootstrapperProps) => {
7375
};
7476

7577
const bootstrap = async () => {
76-
await getSettings();
78+
if (isPmmAdmin(user)) {
79+
await getSettings();
80+
await dispatch(fetchUserStatusAction());
81+
await dispatch(fetchAdvisors({ disableNotifications: true }));
82+
}
83+
7784
await getUserDetails();
78-
await dispatch(fetchUserStatusAction());
7985
await dispatch(fetchServerInfoAction());
8086
await dispatch(fetchServerSaasHostAction());
81-
await dispatch(fetchAdvisors({ disableNotifications: true }));
8287
onReady();
8388
};
8489

85-
if (isLoggedIn) {
90+
if (isSignedIn) {
8691
bootstrap();
8792
} else {
8893
onReady();
8994
}
90-
}, [dispatch, isLoggedIn, setSteps, onReady]);
95+
}, [dispatch, isSignedIn, setSteps, onReady, user]);
9196

9297
return (
9398
<>
94-
{isLoggedIn && <Telemetry />}
99+
{isSignedIn && <Telemetry />}
95100
<PerconaNavigation />
96101
<PerconaTourBootstrapper />
97-
{isLoggedIn && showTour && (
102+
{isSignedIn && showTour && (
98103
<Modal onDismiss={dismissModal} isOpen={modalIsOpen} title={Messages.title}>
99104
<div className={styles.iconContainer}>
100105
<Icon type="mono" name={theme.isLight ? 'pmm-logo-light' : 'pmm-logo'} className={styles.svg} />

public/app/plugins/panel/pmm-check/components/Failed/Failed.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { cx } from '@emotion/css';
22
import React, { FC, useCallback, useEffect, useState } from 'react';
33

4+
import { config } from '@grafana/runtime';
45
import { Spinner, Tooltip, useStyles2 } from '@grafana/ui';
56
import { CheckService } from 'app/percona/check/Check.service';
67
import { FailedCheckSummary } from 'app/percona/check/types';
78
import { getPerconaSettings, getPerconaUser } from 'app/percona/shared/core/selectors';
89
import { logger } from 'app/percona/shared/helpers/logger';
10+
import { isPmmAdmin } from 'app/percona/shared/helpers/permissions';
911
import { useSelector } from 'app/types';
1012

1113
import { PMM_DATABASE_CHECKS_PANEL_URL, PMM_SETTINGS_URL } from '../../CheckPanel.constants';
@@ -35,7 +37,9 @@ export const Failed: FC = () => {
3537
}, []);
3638

3739
useEffect(() => {
38-
fetchAlerts();
40+
if (isPmmAdmin(config.bootData.user)) {
41+
fetchAlerts();
42+
}
3943
// eslint-disable-next-line react-hooks/exhaustive-deps
4044
}, []);
4145

public/app/plugins/panel/pmm-update/UpdatePanel.tsx

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { FC, MouseEvent, useEffect, useState } from 'react';
22

33
import { Button, IconName, Spinner } from '@grafana/ui';
4-
import { SettingsService } from 'app/percona/settings/Settings.service';
5-
import { logger } from 'app/percona/shared/helpers/logger';
4+
import { getPerconaUser, getPerconaSettings } from 'app/percona/shared/core/selectors';
5+
import { useSelector } from 'app/types';
66

77
import { Messages } from './UpdatePanel.messages';
88
import * as styles from './UpdatePanel.styles';
@@ -14,9 +14,8 @@ export const UpdatePanel: FC<{}> = () => {
1414
const [forceUpdate, setForceUpdate] = useState(false);
1515
const [showModal, setShowModal] = useState(false);
1616
const [errorMessage, setErrorMessage] = useState('');
17-
const [updatesDisabled, setUpdatesDisabled] = useState(false);
18-
const [isLoadingSettings, setLoadingSettings] = useState(true);
19-
const [hasNoAccess, setHasNoAccess] = useState(false);
17+
const { isAuthorized } = useSelector(getPerconaUser);
18+
const { result: settings, loading: isLoadingSettings } = useSelector(getPerconaSettings);
2019
const [
2120
{ installedVersionDetails, lastCheckDate, nextVersionDetails, isUpdateAvailable },
2221
fetchVersionErrorMessage,
@@ -27,25 +26,6 @@ export const UpdatePanel: FC<{}> = () => {
2726
const [output, updateErrorMessage, isUpdated, updateFailed, launchUpdate] = usePerformUpdate();
2827
const isLoading = isLoadingVersionDetails || isLoadingSettings;
2928

30-
const getSettings = async () => {
31-
setLoadingSettings(true);
32-
33-
try {
34-
const { updatesDisabled } = await SettingsService.getSettings(undefined, true);
35-
36-
setUpdatesDisabled(!!updatesDisabled);
37-
} catch (e) {
38-
// @ts-ignore
39-
if (e.response?.status === 401) {
40-
setHasNoAccess(true);
41-
}
42-
43-
logger.error(e);
44-
}
45-
46-
setLoadingSettings(false);
47-
};
48-
4929
const handleCheckForUpdates = (e: MouseEvent) => {
5030
if (e.altKey) {
5131
setForceUpdate(true);
@@ -54,10 +34,6 @@ export const UpdatePanel: FC<{}> = () => {
5434
getCurrentVersionDetails({ force: true });
5535
};
5636

57-
useEffect(() => {
58-
getSettings();
59-
}, []);
60-
6137
useEffect(() => {
6238
setErrorMessage(fetchVersionErrorMessage || updateErrorMessage);
6339

@@ -79,7 +55,7 @@ export const UpdatePanel: FC<{}> = () => {
7955
<>
8056
<div className={styles.panel}>
8157
<CurrentVersion installedVersionDetails={installedVersionDetails} />
82-
{isUpdateAvailable && !isDefaultView && !updatesDisabled && !hasNoAccess && !isLoading && isOnline ? (
58+
{isUpdateAvailable && !isDefaultView && !settings?.updatesDisabled && isAuthorized && !isLoading && isOnline ? (
8359
<AvailableUpdate nextVersionDetails={nextVersionDetails} />
8460
) : null}
8561
{isLoading ? (
@@ -88,7 +64,7 @@ export const UpdatePanel: FC<{}> = () => {
8864
</div>
8965
) : (
9066
<>
91-
{(isUpdateAvailable || forceUpdate) && !updatesDisabled && !hasNoAccess && isOnline ? (
67+
{(isUpdateAvailable || forceUpdate) && !settings?.updatesDisabled && isAuthorized && isOnline ? (
9268
<div className={styles.middleSectionWrapper}>
9369
{/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions */}
9470
<Button onClick={handleUpdate} icon={'fa fa-download' as IconName} variant="secondary">
@@ -98,15 +74,15 @@ export const UpdatePanel: FC<{}> = () => {
9874
) : (
9975
<InfoBox
10076
upToDate={!isDefaultView && !forceUpdate}
101-
hasNoAccess={hasNoAccess}
102-
updatesDisabled={updatesDisabled}
77+
hasNoAccess={!isAuthorized}
78+
updatesDisabled={settings?.updatesDisabled}
10379
isOnline={isOnline}
10480
/>
10581
)}
10682
</>
10783
)}
10884
<LastCheck
109-
disabled={isLoading || updatesDisabled || !isOnline}
85+
disabled={isLoading || settings?.updatesDisabled || !isOnline}
11086
onCheckForUpdates={handleCheckForUpdates}
11187
lastCheckDate={lastCheckDate}
11288
/>

0 commit comments

Comments
 (0)