Skip to content

Commit 5bdf539

Browse files
authored
fix: the refresh button on stats page (#203)
fixes #202
1 parent 5618c54 commit 5bdf539

File tree

8 files changed

+92
-21
lines changed

8 files changed

+92
-21
lines changed

Diff for: src/components/Header/SubHeader.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { IconCodeCircle } from '@tabler/icons-react';
1818
import styles from './styles/LogQuery.module.css';
1919
import headerStyles from './styles/Header.module.css';
2020
import Querier from './Querier';
21+
import { useStatsPageContext } from '@/pages/Stats/Context';
2122

2223
type HeaderLayoutProps = {
2324
children: React.ReactNode;
@@ -39,8 +40,8 @@ export const StatsHeader: FC = () => {
3940
const classes = styles;
4041
const { container, innerContainer } = classes;
4142
const {
42-
methods: { resetTimeInterval },
43-
} = useHeaderContext();
43+
methods: { resetFetchStartTime },
44+
} = useStatsPageContext();
4445
return (
4546
<HeaderLayout>
4647
<Box className={container}>
@@ -52,7 +53,7 @@ export const StatsHeader: FC = () => {
5253

5354
<Box>
5455
<Box className={innerContainer}>
55-
<RefreshNow onRefresh={resetTimeInterval} />
56+
<RefreshNow onRefresh={resetFetchStartTime} />
5657
</Box>
5758
</Box>
5859
</Box>

Diff for: src/hooks/useAlertsEditor.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { getLogStreamAlerts, putLogStreamAlerts } from '@/api/logStream';
33
import { IconFileAlert } from '@tabler/icons-react';
44
import { notifications } from '@mantine/notifications';
55
import useMountedState from './useMountedState';
6+
import { Dayjs } from 'dayjs';
67

7-
export const useAlertsEditor = (streamName: string) => {
8+
export const useAlertsEditor = (streamName: string, fetchStartTime?: Dayjs) => {
89
const [alertQuery, setAlertQuery] = useMountedState<string | undefined>('');
910

1011
const {
@@ -18,7 +19,7 @@ export const useAlertsEditor = (streamName: string) => {
1819
isError: getLogAlertIsError,
1920
isSuccess: getLogAlertIsSuccess,
2021
isLoading: getLogAlertIsLoading,
21-
} = useQuery(['fetch-log-stream-alert', streamName, updateLogStreamIsSuccess], () => getLogStreamAlerts(streamName), {
22+
} = useQuery(['fetch-log-stream-alert', streamName, updateLogStreamIsSuccess, fetchStartTime], () => getLogStreamAlerts(streamName), {
2223
onSuccess: (data) => {
2324
setAlertQuery(JSON.stringify(data?.data));
2425
},

Diff for: src/hooks/useLogStreamStats.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useQuery } from 'react-query';
22
import { getLogStreamStats } from '@/api/logStream';
3+
import { Dayjs } from 'dayjs';
34

4-
export const useLogStreamStats = (streamName: string) => {
5+
export const useLogStreamStats = (streamName: string, fetchStartTime?: Dayjs) => {
56
const {
67
data: getLogStreamStatsData,
78
isSuccess: getLogStreamStatsDataIsSuccess,
89
isError: getLogStreamStatsDataIsError,
910
isLoading: getLogStreamStatsDataIsLoading,
10-
} = useQuery(['fetch-log-stream-stats', streamName], () => getLogStreamStats(streamName), {
11+
} = useQuery(['fetch-log-stream-stats', streamName, fetchStartTime], () => getLogStreamStats(streamName), {
1112
retry: false,
1213
enabled: streamName !== '',
1314
});

Diff for: src/hooks/useRetentionEditor.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { getLogStreamRetention, putLogStreamRetention } from '@/api/logStream';
33
import { IconFileAlert } from '@tabler/icons-react';
44
import { notifications } from '@mantine/notifications';
55
import useMountedState from './useMountedState';
6+
import { Dayjs } from 'dayjs';
67

7-
export const useRetentionEditor = (streamName: string) => {
8+
export const useRetentionEditor = (streamName: string, fetchStartTime?: Dayjs) => {
89
const [retentionQuery, setRetentionQuery] = useMountedState<string | undefined>('');
910

1011
const {
@@ -19,7 +20,7 @@ export const useRetentionEditor = (streamName: string) => {
1920
isLoading: getLogRetentionIsLoading,
2021
isSuccess: getLogRetentionIsSuccess,
2122
} = useQuery(
22-
['fetch-log-stream-retention', streamName, updateLogRetentionIsSuccess],
23+
['fetch-log-stream-retention', streamName, updateLogRetentionIsSuccess, fetchStartTime],
2324
() => getLogStreamRetention(streamName),
2425
{
2526
onSuccess: (data) => {

Diff for: src/pages/Stats/Alerts.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import { useAlertsEditor } from '@/hooks/useAlertsEditor';
99
import { useParams } from 'react-router-dom';
1010
import alertStyles from './styles/Alerts.module.css'
1111
import { CodeHighlight } from '@mantine/code-highlight';
12+
import { useStatsPageContext } from './Context';
1213

1314
const Alerts: FC = () => {
1415
const {
1516
state: { subLogQuery },
1617
} = useHeaderContext();
1718
const { streamName } = useParams();
18-
19-
const { getLogAlertData, getLogAlertIsError, getLogAlertIsLoading } = useAlertsEditor(streamName || '');
19+
const {
20+
state: { fetchStartTime },
21+
} = useStatsPageContext();
22+
const { getLogAlertData, getLogAlertIsError, getLogAlertIsLoading } = useAlertsEditor(streamName || '', fetchStartTime);
2023

2124
const [opened, { open, close }] = useDisclosure(false);
2225
const [Alert, setAlert] = useMountedState({ name: 'Loading....' });

Diff for: src/pages/Stats/Context.tsx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Dispatch, FC, SetStateAction } from 'react';
2+
import { ReactNode, createContext, useCallback, useContext, useMemo, useState } from 'react';
3+
import dayjs, { Dayjs } from 'dayjs';
4+
5+
const Context = createContext({});
6+
7+
const { Provider } = Context;
8+
9+
interface StatsPageProvider {
10+
children: ReactNode;
11+
}
12+
13+
interface LogsPageContextValue {
14+
state: StatsPageContextState;
15+
methods: StatsPageContextMethods;
16+
}
17+
18+
type StatsPageContextState = {
19+
fetchStartTime: Dayjs;
20+
statusFixedDurations: number;
21+
}
22+
23+
type StatsPageContextMethods = {
24+
resetFetchStartTime: () => void;
25+
setStatusFixedDurations: Dispatch<SetStateAction<number>>;
26+
}
27+
28+
const StatsPageProvider: FC<StatsPageProvider> = ({ children }) => {
29+
const [fetchStartTime, setFetchStartTime] = useState<Dayjs>(dayjs());
30+
const [statusFixedDurations, setStatusFixedDurations] = useState<number>(0);
31+
32+
const resetFetchStartTime = useCallback(() => {
33+
setFetchStartTime(dayjs())
34+
}, [])
35+
36+
const state: StatsPageContextState = {
37+
fetchStartTime,
38+
statusFixedDurations
39+
};
40+
41+
const methods: StatsPageContextMethods = {
42+
resetFetchStartTime,
43+
setStatusFixedDurations
44+
}
45+
46+
const value = useMemo(() => ({ state, methods }), [state, methods]);
47+
48+
return <Provider value={value}>{children}</Provider>;
49+
};
50+
51+
export const useStatsPageContext = () => useContext(Context) as LogsPageContextValue;
52+
53+
export default StatsPageProvider;

Diff for: src/pages/Stats/Status.tsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,42 @@ import { useLogStreamStats } from '@/hooks/useLogStreamStats';
1919
import { useParams } from 'react-router-dom';
2020
import statusStyles from './styles/Status.module.css';
2121
import statCardStyles from './styles/StatsCard.module.css';
22+
import { useStatsPageContext } from './Context';
2223

2324
const Status: FC = () => {
2425
const { streamName } = useParams();
2526

26-
const [statusFixedDurations, setStatusFixedDurations] = useMountedState<number>(0);
27+
const {
28+
state: { statusFixedDurations, fetchStartTime },
29+
methods: { setStatusFixedDurations },
30+
} = useStatsPageContext();
31+
2732
const [fetchQueryStatus, setFetchQueryStatus] = useMountedState<string>('');
2833

2934
const { getLogRetentionIsError, getLogRetentionData, getLogRetentionIsSuccess, getLogRetentionIsLoading } =
30-
useRetentionEditor(streamName || '');
35+
useRetentionEditor(streamName || '', fetchStartTime);
3136

3237
const {
3338
getLogStreamStatsData,
3439
getLogStreamStatsDataIsSuccess,
3540
getLogStreamStatsDataIsLoading,
3641
getLogStreamStatsDataIsError,
37-
} = useLogStreamStats(streamName || '');
42+
} = useLogStreamStats(streamName || '', fetchStartTime);
3843
const { fetchQueryMutation } = useQueryResult();
3944

4045
useEffect(() => {
4146
if (streamName) {
4247
getStatus();
4348
setStatusFixedDurations(0);
4449
}
45-
}, [streamName]);
50+
}, [streamName, fetchStartTime]);
4651

4752
const getStatus = async () => {
48-
const now = dayjs();
4953
setStatusFixedDurations(statusFixedDurations + 1);
5054
const LogQuery = {
5155
streamName: streamName || '',
52-
startTime: now.subtract(FIXED_DURATIONS[statusFixedDurations].milliseconds, 'milliseconds').toDate(),
53-
endTime: now.toDate(),
56+
startTime: fetchStartTime.subtract(FIXED_DURATIONS[statusFixedDurations].milliseconds, 'milliseconds').toDate(),
57+
endTime: fetchStartTime.toDate(),
5458
access: [],
5559
};
5660
fetchQueryMutation.mutate({

Diff for: src/routes/elements.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Home from '@/pages/Home';
2-
import LogsPageProvider from '@/pages/Logs/Context';
32
import type { FC } from 'react';
43
import { lazy } from 'react';
54
import SuspensePage from './SuspensePage';
@@ -13,6 +12,12 @@ import {
1312
StatsHeader,
1413
UsersManagementHeader,
1514
} from '@/components/Header/SubHeader';
15+
16+
// page-wise providers
17+
import LogsPageProvider from '@/pages/Logs/Context';
18+
import StatsPageProvider from '@/pages/Stats/Context';
19+
20+
// component-wise providers
1621
import QueryFilterProvider from '@/providers/QueryFilterProvider';
1722

1823
export const HomeElement: FC = () => {
@@ -73,8 +78,10 @@ const Stats = lazy(() => import('@/pages/Stats'));
7378
export const StatsElement: FC = () => {
7479
return (
7580
<SuspensePage>
76-
<StatsHeader />
77-
<Stats />
81+
<StatsPageProvider>
82+
<StatsHeader />
83+
<Stats />
84+
</StatsPageProvider>
7885
</SuspensePage>
7986
);
8087
};

0 commit comments

Comments
 (0)