vmui: prevent reset relative time (#2543)

* fix: prevent time picker reset to previous time

* fix: add default display type
This commit is contained in:
Yury Molodov 2022-05-05 21:21:02 +03:00 committed by Aliaksandr Valialkin
parent eba02163fe
commit 7b37662a80
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
2 changed files with 21 additions and 13 deletions

View File

@ -56,19 +56,21 @@ export type Action =
| { type: "TOGGLE_AUTOCOMPLETE"} | { type: "TOGGLE_AUTOCOMPLETE"}
| { type: "NO_CACHE"} | { type: "NO_CACHE"}
const {relativeDuration, relativeUntil, relativeTimeId} = getRelativeTime();
const duration = relativeDuration || getQueryStringValue("g0.range_input", "1h") as string; const {duration, endInput, relativeTimeId} = getRelativeTime({
const endInput = relativeUntil || formatDateToLocal(getQueryStringValue("g0.end_input", getDateNowUTC()) as Date); defaultDuration: getQueryStringValue("g0.range_input", "1h") as string,
defaultEndInput: new Date(formatDateToLocal(getQueryStringValue("g0.end_input", getDateNowUTC()) as Date)),
});
const query = getQueryArray(); const query = getQueryArray();
export const initialState: AppState = { export const initialState: AppState = {
serverUrl: getDefaultServer(), serverUrl: getDefaultServer(),
displayType: getQueryStringValue("g0.tab", "chart") as DisplayType, displayType: getQueryStringValue("g0.tab", "chart") as DisplayType || "chart",
query: query, // demo_memory_usage_bytes query: query, // demo_memory_usage_bytes
queryHistory: query.map(q => ({index: 0, values: [q]})), queryHistory: query.map(q => ({index: 0, values: [q]})),
time: { time: {
duration, duration,
period: getTimeperiodForDuration(duration, new Date(endInput)), period: getTimeperiodForDuration(duration, endInput),
relativeTime: relativeTimeId, relativeTime: relativeTimeId,
}, },
queryControls: { queryControls: {
@ -152,7 +154,7 @@ export function reducer(state: AppState, action: Action): AppState {
}; };
case "SET_PERIOD": case "SET_PERIOD":
// eslint-disable-next-line no-case-declarations // eslint-disable-next-line no-case-declarations
const duration = getDurationFromPeriod(action.payload); const durationPeriod = getDurationFromPeriod(action.payload);
return { return {
...state, ...state,
queryControls: { queryControls: {
@ -161,8 +163,8 @@ export function reducer(state: AppState, action: Action): AppState {
}, },
time: { time: {
...state.time, ...state.time,
duration, duration: durationPeriod,
period: getTimeperiodForDuration(duration, action.payload.to), period: getTimeperiodForDuration(durationPeriod, action.payload.to),
relativeTime: "" relativeTime: ""
} }
}; };
@ -191,11 +193,17 @@ export function reducer(state: AppState, action: Action): AppState {
} }
}; };
case "RUN_QUERY": case "RUN_QUERY":
// eslint-disable-next-line no-case-declarations
const {duration: durationRunQuery, endInput} = getRelativeTime({
relativeTimeId: state.time.relativeTime,
defaultDuration: state.time.duration,
defaultEndInput: dateFromSeconds(state.time.period.end),
});
return { return {
...state, ...state,
time: { time: {
...state.time, ...state.time,
period: getTimeperiodForDuration(state.time.duration, dateFromSeconds(state.time.period.end)) period: getTimeperiodForDuration(durationRunQuery, endInput)
} }
}; };
case "RUN_QUERY_TO_NOW": case "RUN_QUERY_TO_NOW":

View File

@ -131,13 +131,13 @@ export const relativeTimeOptions: RelativeTimeOption[] = [
...o ...o
})); }));
export const getRelativeTime = (relativeTimeId?: string) => { export const getRelativeTime = ({relativeTimeId, defaultDuration, defaultEndInput}:
{ relativeTimeId?: string, defaultDuration: string, defaultEndInput: Date }) => {
const id = relativeTimeId || getQueryStringValue("g0.relative_time", "") as string; const id = relativeTimeId || getQueryStringValue("g0.relative_time", "") as string;
const target = relativeTimeOptions.find(d => d.id === id); const target = relativeTimeOptions.find(d => d.id === id);
if (!target) return {};
return { return {
relativeTimeId: id, relativeTimeId: id,
relativeDuration: target.duration, duration: target ? target.duration : defaultDuration,
relativeUntil: target.until() endInput: target ? target.until() : defaultEndInput
}; };
}; };