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:
parent
5ca97ceedd
commit
990e1fa1c4
3 changed files with 27 additions and 12 deletions
|
|
@ -7,6 +7,7 @@ import Filter, { IFilter } from 'App/mstore/types/filter';
|
|||
import FilterItem from 'App/mstore/types/filterItem';
|
||||
import { makeAutoObservable, observable } from 'mobx';
|
||||
import { LAST_24_HOURS, LAST_30_DAYS, LAST_7_DAYS } from 'Types/app/period';
|
||||
import { roundToNextMinutes } from '@/utils';
|
||||
|
||||
// @ts-ignore
|
||||
const rangeValue = DATE_RANGE_VALUES.LAST_24_HOURS;
|
||||
|
|
@ -177,6 +178,7 @@ export default class Search {
|
|||
js.rangeValue,
|
||||
js.startDate,
|
||||
js.endDate,
|
||||
15,
|
||||
);
|
||||
js.startDate = startDate;
|
||||
js.endDate = endDate;
|
||||
|
|
@ -190,12 +192,11 @@ export default class Search {
|
|||
rangeName: string,
|
||||
customStartDate: number,
|
||||
customEndDate: number,
|
||||
): {
|
||||
startDate: number;
|
||||
endDate: number;
|
||||
} {
|
||||
roundMinutes?: number,
|
||||
): { startDate: number; endDate: number } {
|
||||
let endDate = new Date().getTime();
|
||||
let startDate: number;
|
||||
const minutes = roundMinutes || 15;
|
||||
|
||||
switch (rangeName) {
|
||||
case LAST_7_DAYS:
|
||||
|
|
@ -206,9 +207,7 @@ export default class Search {
|
|||
break;
|
||||
case CUSTOM_RANGE:
|
||||
if (!customStartDate || !customEndDate) {
|
||||
throw new Error(
|
||||
'Start date and end date must be provided for CUSTOM_RANGE.',
|
||||
);
|
||||
throw new Error('Start date and end date must be provided for CUSTOM_RANGE.');
|
||||
}
|
||||
startDate = customStartDate;
|
||||
endDate = customEndDate;
|
||||
|
|
@ -218,10 +217,12 @@ export default class Search {
|
|||
startDate = endDate - 24 * 60 * 60 * 1000;
|
||||
}
|
||||
|
||||
return {
|
||||
startDate,
|
||||
endDate,
|
||||
};
|
||||
if (rangeName !== CUSTOM_RANGE) {
|
||||
startDate = roundToNextMinutes(startDate, minutes);
|
||||
endDate = roundToNextMinutes(endDate, minutes);
|
||||
}
|
||||
|
||||
return { startDate, endDate };
|
||||
}
|
||||
|
||||
fromJS({ eventsOrder, filters, events, custom, ...filterData }: any) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { DateTime, Interval, Settings } from 'luxon';
|
||||
import Record from 'Types/Record';
|
||||
import { roundToNextMinutes } from '@/utils';
|
||||
|
||||
export const LAST_30_MINUTES = 'LAST_30_MINUTES';
|
||||
export const TODAY = 'TODAY';
|
||||
|
|
@ -30,7 +31,9 @@ function getRange(rangeName, offset) {
|
|||
now.startOf('day'),
|
||||
);
|
||||
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:
|
||||
return Interval.fromDateTimes(
|
||||
now.minus({ minutes: 30 }).startOf('minute'),
|
||||
|
|
|
|||
|
|
@ -622,3 +622,14 @@ export function exportAntCsv(tableColumns, tableData, filename = 'table.csv') {
|
|||
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue