Repository navigation
Expand file tree
/
Copy pathdate-range-utils.ts
More file actions
209 lines (180 loc) 路 5.02 KB
/
Copy pathdate-range-utils.ts
File metadata and controls
209 lines (180 loc) 路 5.02 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { type DateRange } from "react-day-picker";
export const DEFAULT_DASHBOARD_AGGREGATION_SELECTION = "24 hours" as const;
export const DASHBOARD_AGGREGATION_PLACEHOLDER = "Custom" as const;
export const DEFAULT_AGGREGATION_SELECTION = "All time" as const;
export const DASHBOARD_AGGREGATION_OPTIONS = [
"5 min",
"30 min",
"1 hour",
"3 hours",
"24 hours",
"7 days",
"1 month",
"3 months",
"1 year",
] as const;
export const TABLE_AGGREGATION_OPTIONS = [
"30 min",
"1 hour",
"6 hours",
"24 hours",
"3 days",
"7 days",
"14 days",
"1 month",
"3 months",
] as const;
export const TABLE_RANGE_DROPDOWN_OPTIONS = [
...TABLE_AGGREGATION_OPTIONS,
DEFAULT_AGGREGATION_SELECTION,
] as const;
export type DashboardDateRangeAggregationOption =
(typeof DASHBOARD_AGGREGATION_OPTIONS)[number];
export type TableDateRange = {
from: Date;
};
export type TableDateRangeAggregationOption =
(typeof TABLE_AGGREGATION_OPTIONS)[number];
export type DashboardDateRange = {
from: Date;
to: Date;
};
export type DateRangeAggregationOption =
| DashboardDateRangeAggregationOption
| TableDateRangeAggregationOption;
export type DashboardDateRangeOptions =
| DashboardDateRangeAggregationOption
| typeof DASHBOARD_AGGREGATION_PLACEHOLDER;
export type TableDateRangeOptions =
| TableDateRangeAggregationOption
| typeof DEFAULT_AGGREGATION_SELECTION;
export type DashboardDateRangeAggregationSettings = Record<
DashboardDateRangeAggregationOption,
{
date_trunc: "year" | "month" | "week" | "day" | "hour" | "minute";
minutes: number;
}
>;
export type TableDateRangeAggregationSettings = Record<
TableDateRangeAggregationOption,
number
>;
export const dateTimeAggregationOptions = [
...TABLE_AGGREGATION_OPTIONS,
...DASHBOARD_AGGREGATION_OPTIONS,
DEFAULT_AGGREGATION_SELECTION,
] as const;
export const dashboardDateRangeAggregationSettings: DashboardDateRangeAggregationSettings =
{
"1 year": {
date_trunc: "month",
minutes: 365 * 24 * 60,
},
"3 months": {
date_trunc: "month",
minutes: 3 * 28 * 24 * 60,
},
"1 month": {
date_trunc: "day",
minutes: 28 * 24 * 60,
},
"7 days": {
date_trunc: "day",
minutes: 7 * 24 * 60,
},
"24 hours": {
date_trunc: "hour",
minutes: 24 * 60,
},
"3 hours": {
date_trunc: "hour",
minutes: 3 * 60,
},
"1 hour": {
date_trunc: "minute",
minutes: 60,
},
"30 min": {
date_trunc: "minute",
minutes: 30,
},
"5 min": {
date_trunc: "minute",
minutes: 5,
},
};
export const tableDateRangeAggregationSettings: TableDateRangeAggregationSettings =
{
"3 months": 3 * 28 * 24 * 60,
"1 month": 28 * 24 * 60,
"14 days": 14 * 24 * 60,
"7 days": 7 * 24 * 60,
"3 days": 3 * 24 * 60,
"24 hours": 24 * 60,
"6 hours": 6 * 60,
"1 hour": 60,
"30 min": 30,
};
export function isValidDashboardDateRangeAggregationOption(
value: unknown,
): value is DashboardDateRangeAggregationOption {
return (
typeof value === "string" &&
DASHBOARD_AGGREGATION_OPTIONS.includes(
value as DashboardDateRangeAggregationOption,
)
);
}
export function isValidTableDateRangeAggregationOption(
value: unknown,
): value is TableDateRangeAggregationOption {
return (
typeof value === "string" &&
TABLE_AGGREGATION_OPTIONS.includes(value as TableDateRangeAggregationOption)
);
}
export const findClosestTableIntervalToDate = (
targetDate: Date,
): TableDateRangeAggregationOption | undefined => {
const currentDate = new Date();
const duration = Math.abs(currentDate.getTime() - targetDate.getTime());
const diffs = TABLE_AGGREGATION_OPTIONS.map((interval) => {
const minutes = tableDateRangeAggregationSettings[interval];
return {
interval,
diff: Math.abs(duration - minutes * 60 * 1000),
};
});
diffs.sort((a, b) => a.diff - b.diff);
return diffs[0]?.interval;
};
export const findClosestDashboardIntervalToDate = (
targetDate: Date,
): DashboardDateRangeAggregationOption | undefined => {
const currentDate = new Date();
const duration = Math.abs(currentDate.getTime() - targetDate.getTime());
const diffs = DASHBOARD_AGGREGATION_OPTIONS.map((interval) => {
const { minutes } = dashboardDateRangeAggregationSettings[interval];
return {
interval,
diff: Math.abs(duration - minutes * 60 * 1000),
};
});
diffs.sort((a, b) => a.diff - b.diff);
return diffs[0]?.interval;
};
export const findClosestDashboardInterval = (
dateRange: DateRange,
): DashboardDateRangeAggregationOption | undefined => {
if (!dateRange.from || !dateRange.to) return undefined;
const duration = dateRange.to.getTime() - dateRange.from.getTime();
const diffs = DASHBOARD_AGGREGATION_OPTIONS.map((interval) => {
const { minutes } = dashboardDateRangeAggregationSettings[interval];
return {
interval,
diff: Math.abs(duration - minutes * 60 * 1000),
};
});
diffs.sort((a, b) => a.diff - b.diff);
return diffs[0]?.interval;
};