Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1x 1x 12x 3x 3x 6x | import { parseServerDate } from "@/util/time";
// ─── Grafana-style time range model ────────────────────────────────────────────
export type QuickRangeKey = "1h" | "24h" | "7d" | "30d" | "90d" | "all";
export interface TimeRange {
/** null = no lower bound (all time). */
since: Date | null;
/** null = up to now (re-evaluated live rather than pinned). */
until: Date | null;
label: string;
}
export const QUICK_RANGES: { key: QuickRangeKey; label: string; hours: number | null }[] = [
{ key: "1h", label: "Last hour", hours: 1 },
{ key: "24h", label: "Last 24 hours", hours: 24 },
{ key: "7d", label: "Last 7 days", hours: 24 * 7 },
{ key: "30d", label: "Last 30 days", hours: 24 * 30 },
{ key: "90d", label: "Last 90 days", hours: 24 * 90 },
{ key: "all", label: "All time", hours: null },
];
export const DEFAULT_QUICK_RANGE: QuickRangeKey = "30d";
export function quickRangeToTimeRange(key: QuickRangeKey): TimeRange {
const preset = QUICK_RANGES.find((r) => r.key === key) ?? QUICK_RANGES[3];
Iif (preset.hours === null) {
return { since: null, until: null, label: preset.label };
}
return {
since: new Date(Date.now() - preset.hours * 3_600_000),
until: null,
label: preset.label,
};
}
export function customTimeRange(since: Date, until: Date): TimeRange {
const fmt = (d: Date) =>
d.toLocaleDateString(undefined, { month: "short", day: "numeric" });
return { since, until, label: `${fmt(since)} – ${fmt(until)}` };
}
export function isWithinRange(iso: string, range: TimeRange): boolean {
const t = parseServerDate(iso).getTime();
if (isNaN(t)) return false;
if (range.since && t < range.since.getTime()) return false;
if (range.until && t > range.until.getTime()) return false;
return true;
}
/** ISO bounds for server-side filtering, resolving an open "until" to now. */
export function rangeBounds(range: TimeRange): {
since: string | undefined;
until: string | undefined;
} {
return {
since: range.since?.toISOString(),
until: range.until?.toISOString(),
};
}
|