Change Loading Logic for Metrics Page (#3166)

Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
Daniel Valdivia
2023-12-22 10:25:54 -06:00
committed by GitHub
parent 7dffd5f079
commit 63b584c83d
4 changed files with 25 additions and 46 deletions

View File

@@ -15,7 +15,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { Fragment, useEffect, useState } from "react"; import React, { Fragment, useEffect, useState } from "react";
import { Grid, ProgressBar } from "mds";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { AppState, useAppDispatch } from "../../../store"; import { AppState, useAppDispatch } from "../../../store";
import { getUsageAsync } from "./dashboardThunks"; import { getUsageAsync } from "./dashboardThunks";
@@ -27,7 +26,7 @@ import HelpMenu from "../HelpMenu";
const Dashboard = () => { const Dashboard = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const [loading, setLoading] = useState<boolean>(true); const [iniLoad, setIniLoad] = useState<boolean>(false);
const usage = useSelector((state: AppState) => state.dashboard.usage); const usage = useSelector((state: AppState) => state.dashboard.usage);
const features = useSelector(selFeatures); const features = useSelector(selFeatures);
@@ -40,31 +39,22 @@ const Dashboard = () => {
} }
useEffect(() => { useEffect(() => {
if (loading) { if (!iniLoad) {
setLoading(false); setIniLoad(true);
dispatch(getUsageAsync()); dispatch(getUsageAsync());
} }
}, [loading, dispatch]); }, [iniLoad, dispatch]);
useEffect(() => { useEffect(() => {
dispatch(setHelpName("metrics")); dispatch(setHelpName("metrics"));
// eslint-disable-next-line react-hooks/exhaustive-deps }, [dispatch]);
}, []);
return ( return (
<Fragment> <Fragment>
{!hideMenu && ( {!hideMenu && (
<PageHeaderWrapper label="Metrics" actions={<HelpMenu />} /> <PageHeaderWrapper label="Metrics" actions={<HelpMenu />} />
)} )}
{loading ? ( <PrDashboard usage={usage} />
<Grid container>
<Grid item xs={12}>
<ProgressBar />
</Grid>
</Grid>
) : (
<PrDashboard usage={usage} />
)}
</Fragment> </Fragment>
); );
}; };

View File

@@ -15,7 +15,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { Fragment, useState } from "react"; import React, { Fragment, useState } from "react";
import { useSelector } from "react-redux";
import { import {
Box, Box,
Button, Button,
@@ -31,7 +30,7 @@ import {
import { IDashboardPanel } from "./types"; import { IDashboardPanel } from "./types";
import { panelsConfiguration } from "./utils"; import { panelsConfiguration } from "./utils";
import { componentToUse } from "./widgetUtils"; import { componentToUse } from "./widgetUtils";
import { AppState, useAppDispatch } from "../../../../store"; import { useAppDispatch, useAppSelector } from "../../../../store";
import { import {
DLayoutColumnProps, DLayoutColumnProps,
DLayoutRowProps, DLayoutRowProps,
@@ -57,16 +56,12 @@ interface IPrDashboard {
const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => { const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const loadingUsage = useSelector( const status = useAppSelector((state) => state.dashboard.status);
(state: AppState) => state.dashboard.loadingUsage, const zoomOpen = useAppSelector((state) => state.dashboard.zoom.openZoom);
const zoomWidget = useAppSelector(
(state) => state.dashboard.zoom.widgetRender,
); );
const zoomOpen = useSelector( const features = useAppSelector(selFeatures);
(state: AppState) => state.dashboard.zoom.openZoom,
);
const zoomWidget = useSelector(
(state: AppState) => state.dashboard.zoom.widgetRender,
);
const features = useSelector(selFeatures);
const obOnly = !!features?.includes("object-browser-only"); const obOnly = !!features?.includes("object-browser-only");
let hideMenu = false; let hideMenu = false;
if (features?.includes("hide-menu")) { if (features?.includes("hide-menu")) {
@@ -78,9 +73,7 @@ const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => {
const [timeStart, setTimeStart] = useState<any>(null); const [timeStart, setTimeStart] = useState<any>(null);
const [timeEnd, setTimeEnd] = useState<any>(null); const [timeEnd, setTimeEnd] = useState<any>(null);
const panelInformation = panelsConfiguration; const panelInformation = panelsConfiguration;
const [curTab, setCurTab] = useState<string>( const [curTab, setCurTab] = useState<string>("info");
usage?.advancedMetricsStatus === "not configured" ? "info" : "usage",
);
const getPanelDetails = (id: number) => { const getPanelDetails = (id: number) => {
return panelInformation.find((panel) => panel.id === id); return panelInformation.find((panel) => panel.id === id);
@@ -186,7 +179,7 @@ const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => {
onClick={() => { onClick={() => {
dispatch(getUsageAsync()); dispatch(getUsageAsync());
}} }}
disabled={loadingUsage} disabled={status === "loading"}
icon={<SyncIcon />} icon={<SyncIcon />}
label={"Sync"} label={"Sync"}
/> />
@@ -210,8 +203,8 @@ const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => {
tabConfig: { label: "Info", id: "info", disabled: false }, tabConfig: { label: "Info", id: "info", disabled: false },
content: ( content: (
<Fragment> <Fragment>
{(!usage || loadingUsage) && <ProgressBar />} {(!usage || status === "loading") && <ProgressBar />}
{usage && !loadingUsage && ( {usage && status === "idle" && (
<Fragment> <Fragment>
{searchBox} {searchBox}
<BasicDashboard usage={usage} /> <BasicDashboard usage={usage} />
@@ -321,13 +314,7 @@ const PrDashboard = ({ apiPrefix = "admin", usage }: IPrDashboard) => {
}, },
]; ];
let tabsOptions: TabItemProps[]; let tabsOptions: TabItemProps[] = [infoTab, ...prometheusTabs];
if (!prometheusOptionsDisabled) {
tabsOptions = [...prometheusTabs, infoTab];
} else {
tabsOptions = [infoTab, ...prometheusTabs];
}
return ( return (
<PageLayout <PageLayout

View File

@@ -23,17 +23,17 @@ import { AdminInfoResponse } from "api/consoleApi";
export interface DashboardState { export interface DashboardState {
zoom: zoomState; zoom: zoomState;
usage: AdminInfoResponse | null; usage: AdminInfoResponse | null;
loadingUsage: boolean; status: "idle" | "loading" | "failed";
widgetLoadVersion: number; widgetLoadVersion: number;
} }
const initialState: DashboardState = { const initialState: DashboardState = {
status: "idle",
zoom: { zoom: {
openZoom: false, openZoom: false,
widgetRender: null, widgetRender: null,
}, },
usage: null, usage: null,
loadingUsage: true,
widgetLoadVersion: 0, widgetLoadVersion: 0,
}; };
export const dashboardSlice = createSlice({ export const dashboardSlice = createSlice({
@@ -55,13 +55,13 @@ export const dashboardSlice = createSlice({
extraReducers: (builder) => { extraReducers: (builder) => {
builder builder
.addCase(getUsageAsync.pending, (state) => { .addCase(getUsageAsync.pending, (state) => {
state.loadingUsage = true; state.status = "loading";
}) })
.addCase(getUsageAsync.rejected, (state) => { .addCase(getUsageAsync.rejected, (state) => {
state.loadingUsage = false; state.status = "failed";
}) })
.addCase(getUsageAsync.fulfilled, (state, action) => { .addCase(getUsageAsync.fulfilled, (state, action) => {
state.loadingUsage = false; state.status = "idle";
state.usage = action.payload; state.usage = action.payload;
}); });
}, },

View File

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import { useDispatch } from "react-redux"; import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { combineReducers, configureStore } from "@reduxjs/toolkit"; import { combineReducers, configureStore } from "@reduxjs/toolkit";
import systemReducer from "./systemSlice"; import systemReducer from "./systemSlice";
@@ -69,6 +69,8 @@ if (process.env.NODE_ENV !== "production" && module.hot) {
export type AppState = ReturnType<typeof rootReducer>; export type AppState = ReturnType<typeof rootReducer>;
export type AppDispatch = typeof store.dispatch; export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export const useAppDispatch = () => useDispatch<AppDispatch>(); export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export default store; export default store;