refactor: Added new props to GetMetricQueryRange to control Step from without global time range. (#3304)

* refactor: added new props to GetMetricQueryRange

* refactor: review comments

* chore: removed the unnecessary props in query-range payload

* chore: name updated

---------

Co-authored-by: Vishal Sharma <makeavish786@gmail.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
Co-authored-by: Palash Gupta <palashgdev@gmail.com>
This commit is contained in:
Rajat Dabade 2023-10-09 14:46:44 +05:30 committed by GitHub
parent e7a5eb7b22
commit 503417719c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 5 deletions

View File

@ -25,6 +25,7 @@ export interface ChartPreviewProps {
headline?: JSX.Element; headline?: JSX.Element;
alertDef?: AlertDef; alertDef?: AlertDef;
userQueryKey?: string; userQueryKey?: string;
allowSelectedIntervalForStepGen?: boolean;
} }
function ChartPreview({ function ChartPreview({
@ -35,6 +36,7 @@ function ChartPreview({
selectedInterval = '5min', selectedInterval = '5min',
headline, headline,
userQueryKey, userQueryKey,
allowSelectedIntervalForStepGen = false,
alertDef, alertDef,
}: ChartPreviewProps): JSX.Element | null { }: ChartPreviewProps): JSX.Element | null {
const { t } = useTranslation('alerts'); const { t } = useTranslation('alerts');
@ -89,6 +91,9 @@ function ChartPreview({
globalSelectedInterval: selectedInterval, globalSelectedInterval: selectedInterval,
graphType, graphType,
selectedTime, selectedTime,
params: {
allowSelectedIntervalForStepGen,
},
}, },
{ {
queryKey: [ queryKey: [
@ -146,6 +151,7 @@ ChartPreview.defaultProps = {
selectedInterval: '5min', selectedInterval: '5min',
headline: undefined, headline: undefined,
userQueryKey: '', userQueryKey: '',
allowSelectedIntervalForStepGen: false,
alertDef: undefined, alertDef: undefined,
}; };

View File

@ -44,7 +44,7 @@ import {
StyledLeftContainer, StyledLeftContainer,
} from './styles'; } from './styles';
import UserGuide from './UserGuide'; import UserGuide from './UserGuide';
import { toChartInterval } from './utils'; import { getUpdatedStepInterval, toChartInterval } from './utils';
function FormAlertRules({ function FormAlertRules({
alertType, alertType,
@ -354,6 +354,16 @@ function FormAlertRules({
<BasicInfo alertDef={alertDef} setAlertDef={setAlertDef} /> <BasicInfo alertDef={alertDef} setAlertDef={setAlertDef} />
); );
const updatedStagedQuery = useMemo((): Query | null => {
const newQuery: Query | null = stagedQuery;
if (newQuery) {
newQuery.builder.queryData[0].stepInterval = getUpdatedStepInterval(
alertDef.evalWindow,
);
}
return newQuery;
}, [alertDef.evalWindow, stagedQuery]);
const renderQBChartPreview = (): JSX.Element => ( const renderQBChartPreview = (): JSX.Element => (
<ChartPreview <ChartPreview
headline={ headline={
@ -363,9 +373,10 @@ function FormAlertRules({
/> />
} }
name="" name=""
query={stagedQuery} query={updatedStagedQuery}
selectedInterval={toChartInterval(alertDef.evalWindow)} selectedInterval={toChartInterval(alertDef.evalWindow)}
alertDef={alertDef} alertDef={alertDef}
allowSelectedIntervalForStepGen
/> />
); );

View File

@ -0,0 +1,14 @@
// Write a test for getUpdatedStepInterval function in src/container/FormAlertRules/utils.ts
import { getUpdatedStepInterval } from './utils';
describe('getUpdatedStepInterval', () => {
it('should return 60', () => {
const result = getUpdatedStepInterval('5m0s');
expect(result).toEqual(60);
});
it('should return 60 for 10m0s', () => {
const result = getUpdatedStepInterval('10m0s');
expect(result).toEqual(60);
});
});

View File

@ -1,4 +1,6 @@
import { Time } from 'container/TopNav/DateTimeSelection/config'; import { Time } from 'container/TopNav/DateTimeSelection/config';
import getStartEndRangeTime from 'lib/getStartEndRangeTime';
import getStep from 'lib/getStep';
// toChartInterval converts eval window to chart selection time interval // toChartInterval converts eval window to chart selection time interval
export const toChartInterval = (evalWindow: string | undefined): Time => { export const toChartInterval = (evalWindow: string | undefined): Time => {
@ -21,3 +23,15 @@ export const toChartInterval = (evalWindow: string | undefined): Time => {
return '5min'; return '5min';
} }
}; };
export const getUpdatedStepInterval = (evalWindow?: string): number => {
const { start, end } = getStartEndRangeTime({
type: 'GLOBAL_TIME',
interval: toChartInterval(evalWindow),
});
return getStep({
start,
end,
inputFormat: 'ns',
});
};

View File

@ -22,6 +22,7 @@ export const prepareQueryRangePayload = ({
params = {}, params = {},
}: GetQueryResultsProps): PrepareQueryRangePayload => { }: GetQueryResultsProps): PrepareQueryRangePayload => {
let legendMap: Record<string, string> = {}; let legendMap: Record<string, string> = {};
const { allowSelectedIntervalForStepGen, ...restParams } = params;
const compositeQuery: QueryRangePayload['compositeQuery'] = { const compositeQuery: QueryRangePayload['compositeQuery'] = {
queryType: query.queryType, queryType: query.queryType,
@ -90,13 +91,17 @@ export const prepareQueryRangePayload = ({
start: parseInt(start, 10) * 1e3, start: parseInt(start, 10) * 1e3,
end: parseInt(end, 10) * 1e3, end: parseInt(end, 10) * 1e3,
step: getStep({ step: getStep({
start: store.getState().globalTime.minTime, start: allowSelectedIntervalForStepGen
end: store.getState().globalTime.maxTime, ? start
: store.getState().globalTime.minTime,
end: allowSelectedIntervalForStepGen
? end
: store.getState().globalTime.maxTime,
inputFormat: 'ns', inputFormat: 'ns',
}), }),
variables, variables,
compositeQuery, compositeQuery,
...params, ...restParams,
}; };
return { legendMap, queryPayload }; return { legendMap, queryPayload };