feat(search): add rounding to next minutes for date ranges

- Introduced `roundToNextMinutes` utility function to round timestamps
  to the next specified minute interval.
- Updated `Search` class to use the rounding function for non-custom
  date ranges.
- Modified `getRange` in `period.js` to align LAST_24_HOURS with
  15-minute intervals.
- Added `roundToNextMinutes` implementation in `utils/index.ts`.
This commit is contained in:
Shekar Siri 2025-04-11 11:59:04 +02:00
parent aef94618f6
commit 1f9bc5520a
3 changed files with 27 additions and 12 deletions

View file

@ -7,6 +7,7 @@ import Filter, { IFilter } from 'App/mstore/types/filter';
import FilterItem from 'App/mstore/types/filterItem'; import FilterItem from 'App/mstore/types/filterItem';
import { makeAutoObservable, observable } from 'mobx'; import { makeAutoObservable, observable } from 'mobx';
import { LAST_24_HOURS, LAST_30_DAYS, LAST_7_DAYS } from 'Types/app/period'; import { LAST_24_HOURS, LAST_30_DAYS, LAST_7_DAYS } from 'Types/app/period';
import { roundToNextMinutes } from '@/utils';
// @ts-ignore // @ts-ignore
const rangeValue = DATE_RANGE_VALUES.LAST_24_HOURS; const rangeValue = DATE_RANGE_VALUES.LAST_24_HOURS;
@ -177,6 +178,7 @@ export default class Search {
js.rangeValue, js.rangeValue,
js.startDate, js.startDate,
js.endDate, js.endDate,
15,
); );
js.startDate = startDate; js.startDate = startDate;
js.endDate = endDate; js.endDate = endDate;
@ -190,12 +192,11 @@ export default class Search {
rangeName: string, rangeName: string,
customStartDate: number, customStartDate: number,
customEndDate: number, customEndDate: number,
): { roundMinutes?: number,
startDate: number; ): { startDate: number; endDate: number } {
endDate: number;
} {
let endDate = new Date().getTime(); let endDate = new Date().getTime();
let startDate: number; let startDate: number;
const minutes = roundMinutes || 15;
switch (rangeName) { switch (rangeName) {
case LAST_7_DAYS: case LAST_7_DAYS:
@ -206,9 +207,7 @@ export default class Search {
break; break;
case CUSTOM_RANGE: case CUSTOM_RANGE:
if (!customStartDate || !customEndDate) { if (!customStartDate || !customEndDate) {
throw new Error( throw new Error('Start date and end date must be provided for CUSTOM_RANGE.');
'Start date and end date must be provided for CUSTOM_RANGE.',
);
} }
startDate = customStartDate; startDate = customStartDate;
endDate = customEndDate; endDate = customEndDate;
@ -218,10 +217,12 @@ export default class Search {
startDate = endDate - 24 * 60 * 60 * 1000; startDate = endDate - 24 * 60 * 60 * 1000;
} }
return { if (rangeName !== CUSTOM_RANGE) {
startDate, startDate = roundToNextMinutes(startDate, minutes);
endDate, endDate = roundToNextMinutes(endDate, minutes);
}; }
return { startDate, endDate };
} }
fromJS({ eventsOrder, filters, events, custom, ...filterData }: any) { fromJS({ eventsOrder, filters, events, custom, ...filterData }: any) {

View file

@ -1,5 +1,6 @@
import { DateTime, Interval, Settings } from 'luxon'; import { DateTime, Interval, Settings } from 'luxon';
import Record from 'Types/Record'; import Record from 'Types/Record';
import { roundToNextMinutes } from '@/utils';
export const LAST_30_MINUTES = 'LAST_30_MINUTES'; export const LAST_30_MINUTES = 'LAST_30_MINUTES';
export const TODAY = 'TODAY'; export const TODAY = 'TODAY';
@ -30,7 +31,9 @@ function getRange(rangeName, offset) {
now.startOf('day'), now.startOf('day'),
); );
case LAST_24_HOURS: case LAST_24_HOURS:
return Interval.fromDateTimes(now.minus({ hours: 24 }), now); const mod = now.minute % 15;
const next = now.plus({ minutes: mod === 0 ? 15 : 15 - mod }).startOf('minute');
return Interval.fromDateTimes(next.minus({ hours: 24 }), next);
case LAST_30_MINUTES: case LAST_30_MINUTES:
return Interval.fromDateTimes( return Interval.fromDateTimes(
now.minus({ minutes: 30 }).startOf('minute'), now.minus({ minutes: 30 }).startOf('minute'),

View file

@ -613,3 +613,14 @@ export function exportAntCsv(tableColumns, tableData, filename = 'table.csv') {
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
saveAsFile(blob, filename); saveAsFile(blob, filename);
} }
export function roundToNextMinutes(timestamp: number, minutes: number): number {
const date = new Date(timestamp);
date.setSeconds(0, 0);
const currentMinutes = date.getMinutes();
const remainder = currentMinutes % minutes;
if (remainder !== 0) {
date.setMinutes(currentMinutes + (minutes - remainder));
}
return date.getTime();
}