Repository navigation
Expand file tree
/
Copy pathranges.ts
More file actions
83 lines (73 loc) · 2.56 KB
/
Copy pathranges.ts
File metadata and controls
83 lines (73 loc) · 2.56 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { DAY, HOUR, MINUTE, isLiveRange, startOfDay } from '@pulse/core';
import type { Range } from '../data/source';
export type PresetId = '30m' | 'today' | '24h' | '7d' | '30d' | 'custom';
export interface Preset {
id: Exclude<PresetId, 'custom'>;
label: string;
short: string;
key: string;
}
export const PRESETS: readonly Preset[] = [
{ id: '30m', label: 'Last 30 minutes', short: '30m', key: '1' },
{ id: 'today', label: 'Today', short: 'Today', key: '2' },
{ id: '24h', label: 'Last 24 hours', short: '24h', key: '3' },
{ id: '7d', label: 'Last 7 days', short: '7d', key: '4' },
{ id: '30d', label: 'Last 30 days', short: '30d', key: '5' },
];
/** Data retained by the demo (and default server tiers for the hourly view). */
export const MAX_HISTORY_MS = 30 * DAY;
export function startOfLocalDay(ts: number): number {
const d = new Date(ts);
d.setHours(0, 0, 0, 0);
return d.getTime();
}
/**
* Live presets end "now"; the query layer keeps the in-progress bucket for them.
* Day presets start at midnight in `timeZone` (the site's, which the server
* buckets by), or in the browser's zone while the site is not known yet.
*/
export function presetRange(
id: Exclude<PresetId, 'custom'>,
now: number,
timeZone?: string | null,
): Range {
const dayStart = (ts: number) => (timeZone ? startOfDay(ts, timeZone) : startOfLocalDay(ts));
switch (id) {
case '30m':
return { from: now - 30 * MINUTE, to: now };
case 'today':
return { from: dayStart(now), to: now };
case '24h':
return { from: now - 24 * HOUR, to: now };
case '7d':
return { from: dayStart(now - 6 * DAY), to: now };
case '30d':
return { from: dayStart(now - 29 * DAY), to: now };
}
}
/** Moves a window by its own length, never past `now` or the retained history. */
export function shiftRange(r: Range, direction: -1 | 1, now: number): Range {
const span = r.to - r.from;
let from = r.from + direction * span;
let to = r.to + direction * span;
if (to > now) {
to = now;
from = now - span;
}
if (from < now - MAX_HISTORY_MS) {
from = now - MAX_HISTORY_MS;
to = from + span;
}
return { from, to };
}
export function isLive(r: Range, now: number): boolean {
return isLiveRange(r.to, now);
}
/** `<input type="datetime-local">` value for a timestamp in local time. */
export function toLocalInput(ts: number): string {
const d = new Date(ts - new Date(ts).getTimezoneOffset() * MINUTE);
return d.toISOString().slice(0, 16);
}
export function fromLocalInput(value: string): number {
return new Date(value).getTime();
}