-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto refresh with advanced control (#804)
- Loading branch information
Showing
112 changed files
with
1,815 additions
and
3,527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,38 @@ | ||
import {createRequestActionTypes} from '../../store/utils'; | ||
import type {IResponseError} from '../../types/api/error'; | ||
|
||
import type {PreparedMetricsData} from './types'; | ||
|
||
const FETCH_CHART_DATA = createRequestActionTypes('chart', 'FETCH_CHART_DATA'); | ||
const SET_CHART_DATA_WAS_NOT_LOADED = 'chart/SET_DATA_WAS_NOT_LOADED'; | ||
|
||
export const setChartDataLoading = () => { | ||
return { | ||
type: FETCH_CHART_DATA.REQUEST, | ||
} as const; | ||
}; | ||
|
||
export const setChartData = (data: PreparedMetricsData) => { | ||
return { | ||
data, | ||
type: FETCH_CHART_DATA.SUCCESS, | ||
} as const; | ||
}; | ||
|
||
export const setChartError = (error: IResponseError) => { | ||
return { | ||
error, | ||
type: FETCH_CHART_DATA.FAILURE, | ||
} as const; | ||
}; | ||
|
||
export const setChartDataWasNotLoaded = () => { | ||
return { | ||
type: SET_CHART_DATA_WAS_NOT_LOADED, | ||
} as const; | ||
}; | ||
|
||
type ChartAction = | ||
| ReturnType<typeof setChartDataLoading> | ||
| ReturnType<typeof setChartData> | ||
| ReturnType<typeof setChartError> | ||
| ReturnType<typeof setChartDataWasNotLoaded>; | ||
|
||
interface ChartState { | ||
loading: boolean; | ||
wasLoaded: boolean; | ||
data: PreparedMetricsData; | ||
error: IResponseError | undefined; | ||
} | ||
|
||
export const initialChartState: ChartState = { | ||
// Set chart initial state as loading, in order not to mount and unmount component in between requests | ||
// as it leads to memory leak errors in console (not proper useEffect cleanups in chart component itself) | ||
// TODO: possible fix (check needed): chart component is always present, but display: none for chart while loading | ||
loading: true, | ||
wasLoaded: false, | ||
data: {timeline: [], metrics: []}, | ||
error: undefined, | ||
}; | ||
|
||
export const chartReducer = (state: ChartState, action: ChartAction) => { | ||
switch (action.type) { | ||
case FETCH_CHART_DATA.REQUEST: { | ||
return {...state, loading: true}; | ||
} | ||
case FETCH_CHART_DATA.SUCCESS: { | ||
return {...state, loading: false, wasLoaded: true, error: undefined, data: action.data}; | ||
} | ||
case FETCH_CHART_DATA.FAILURE: { | ||
if (action.error?.isCancelled) { | ||
return state; | ||
} | ||
|
||
return { | ||
...state, | ||
error: action.error, | ||
// Clear data, so error will be displayed with empty chart | ||
data: {timeline: [], metrics: []}, | ||
loading: false, | ||
wasLoaded: true, | ||
}; | ||
} | ||
case SET_CHART_DATA_WAS_NOT_LOADED: { | ||
return {...state, wasLoaded: false}; | ||
} | ||
default: | ||
return state; | ||
} | ||
}; | ||
import {api} from '../../store/reducers/api'; | ||
|
||
import {convertResponse} from './convertResponse'; | ||
import type {GetChartDataParams} from './getChartData'; | ||
import {getChartData} from './getChartData'; | ||
import i18n from './i18n'; | ||
|
||
export const chartApi = api.injectEndpoints({ | ||
endpoints: (builder) => ({ | ||
getChartData: builder.query({ | ||
queryFn: async (params: GetChartDataParams, {signal}) => { | ||
try { | ||
const response = await getChartData(params, {signal}); | ||
|
||
// Response could be a plain html for ydb versions without charts support | ||
// Or there could be an error in response with 200 status code | ||
// It happens when request is OK, but chart data cannot be returned due to some reason | ||
// Example: charts are not enabled in the DB ('GraphShard is not enabled' error) | ||
if (Array.isArray(response)) { | ||
const preparedData = convertResponse(response, params.metrics); | ||
return {data: preparedData}; | ||
} | ||
|
||
return { | ||
error: new Error( | ||
typeof response === 'string' ? i18n('not-supported') : response.error, | ||
), | ||
}; | ||
} catch (error) { | ||
return {error}; | ||
} | ||
}, | ||
providesTags: ['All'], | ||
keepUnusedDataFor: 0, | ||
}), | ||
}), | ||
overrideExisting: 'throw', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.