date range extraction, minor fixes

This commit is contained in:
Alex Holliday committed 2026-02-25 20:13:07 +00:00
1 parent 885acf6fe6
commit ba6e931c59
5 files changed
+41 -39

No files matched your search

+25
View File
@@ -1,5 +1,30 @@
import type { GroupedCheck, NormalizedCheck, NormalizedUptimeCheck, HasResponseTime } from "@/types/index.js";
/**
* Get a Date object for a given date range string.
* Returns a date in the past based on the range, or undefined for 'all'.
* This is calculated fresh on each call to ensure accuracy.
*/
export const getDateForRange = (dateRange: string): Date | undefined => {
const now = Date.now();
switch (dateRange) {
case "recent":
return new Date(now - 2 * 60 * 60 * 1000); // 2 hours
case "hour":
return new Date(now - 60 * 60 * 1000); // 1 hour
case "day":
return new Date(now - 24 * 60 * 60 * 1000); // 1 day
case "week":
return new Date(now - 7 * 24 * 60 * 60 * 1000); // 7 days
case "month":
return new Date(now - 30 * 24 * 60 * 60 * 1000); // 30 days
case "all":
return undefined;
default:
return undefined;
}
};
const calculatePercentile = <T extends HasResponseTime>(arr: T[], percentile: number): number => {
const sorted = arr.slice().sort((a, b) => a.responseTime - b.responseTime);
const index = (percentile / 100) * (sorted.length - 1);