diff --git a/components/Chart.js b/components/Chart.js index 847f781cf..d78f1aebf 100644 --- a/components/Chart.js +++ b/components/Chart.js @@ -22,10 +22,12 @@ const Chart = React.memo(function Chart({testName, testGroup = null, title, quer }, [queryParams]) const { data, error } = useSWR( - testGroup ? { query: apiQuery, - testNames: testGroup.tests, - groupKey: name - } : apiQuery, + testGroup ? + { query: apiQuery, + testNames: testGroup.tests, + groupKey: name + } : + apiQuery, testGroup ? MATMultipleFetcher : MATFetcher, swrOptions ) diff --git a/components/aggregation/mat/Form.js b/components/aggregation/mat/Form.js index 9f7b0202d..be43b004c 100644 --- a/components/aggregation/mat/Form.js +++ b/components/aggregation/mat/Form.js @@ -51,6 +51,22 @@ const messages = defineMessages({ id: 'MAT.Form.Label.AxisOption.probe_asn', defaultMessage: '' }, + 'hour': { + id: 'MAT.Form.TimeGrainOption.hour', + defaultMessage: '' + }, + 'day': { + id: 'MAT.Form.TimeGrainOption.day', + defaultMessage: '' + }, + 'week': { + id: 'MAT.Form.TimeGrainOption.week', + defaultMessage: '' + }, + 'month': { + id: 'MAT.Form.TimeGrainOption.month', + defaultMessage: '' + } }) @@ -103,7 +119,8 @@ const defaultDefaultValues = { since: lastMonthToday, until: tomorrow, axis_x: 'measurement_start_day', - axis_y: '' + axis_y: '', + time_grain: 'day' } export const Form = ({ onSubmit, testNames, query }) => { @@ -185,6 +202,26 @@ export const Form = ({ onSubmit, testNames, query }) => { if (!yAxisOptionsFiltered.includes(getValues('axis_y'))) setValue('axis_y', '') }, [setValue, getValues, yAxisOptionsFiltered]) + const since = watch('since') + const until = watch('until') + const timeGrainOptions = useMemo(() => { + if (!since || !until) return ['hour', 'day', 'week', 'month'] + const diff = dayjs(until).diff(dayjs(since), 'day') + if (diff < 8) { + const availableValues = ['hour', 'day'] + if (!availableValues.includes(getValues('time_grain'))) setValue('time_grain', 'hour') + return availableValues + } else if (diff >= 8 && diff < 31) { + const availableValues = ['day', 'week'] + if (!availableValues.includes(getValues('time_grain'))) setValue('time_grain', 'day') + return availableValues + } else if (diff >= 31 ) { + const availableValues = ['day', 'week', 'month'] + if (!availableValues.includes(getValues('time_grain'))) setValue('time_grain', 'day') + return availableValues + } + }, [setValue, getValues, since, until]) + return (