extract date range calculation for conisistency

This commit is contained in:
Alex Holliday committed 2026-07-23 10:25:24 -07:00
1 parent 4a90ab8e78
commit 3af00281c4
9 files changed
+78 -119

No files matched your search

+16 -3
View File
@@ -1,6 +1,7 @@
import type { GroupedCheck, HasResponseTime, NormalizedCheck, NormalizedUptimeCheck } from "@/domain/checks/check.type.js";
import { type DateRange } from "@/types/query.js";
export const getDateForRange = (dateRange: string): Date | undefined => {
export const getDateForRange = (dateRange: DateRange): Date => {
const now = Date.now();
switch (dateRange) {
case "recent":
@@ -14,12 +15,24 @@ export const getDateForRange = (dateRange: string): Date | undefined => {
case "month":
return new Date(now - 30 * 24 * 60 * 60 * 1000); // 30 days
case "all":
return undefined;
return new Date(0);
default:
return undefined;
return new Date(0);
}
};
export const getDateFormat = (dateRange: DateRange): string => {
const formatLookup = {
hour: "%Y-%m-%dT%H:%M:00Z",
recent: "%Y-%m-%dT%H:%M:00Z",
day: "%Y-%m-%dT%H:00:00Z",
week: "%Y-%m-%dT00:00:00Z",
month: "%Y-%m-%dT00:00:00Z",
all: "%Y-%m-%dT00:00:00Z",
};
return formatLookup[dateRange];
};
const percentileBy = <T>(arr: T[], percentile: number, value: (item: T) => number): number => {
const sorted = arr.slice().sort((a, b) => value(a) - value(b));
const index = (percentile / 100) * (sorted.length - 1);