first commit
This commit is contained in:
44
ui/lib/utils/timeRange.ts
Normal file
44
ui/lib/utils/timeRange.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
export const TIME_PERIODS = [
|
||||
{ label: "Last hour", value: "1h" },
|
||||
{ label: "Last 6 hours", value: "6h" },
|
||||
{ label: "Last 24 hours", value: "24h" },
|
||||
{ label: "Last 7 days", value: "7d" },
|
||||
{ label: "Last 30 days", value: "30d" },
|
||||
];
|
||||
|
||||
export type TimePeriod = (typeof TIME_PERIODS)[number]["value"];
|
||||
|
||||
/** Returns a fresh { from, to } Date pair for the given relative period string. */
|
||||
export function getRangeForPeriod(period: string): { from: Date; to: Date } {
|
||||
const to = new Date();
|
||||
const from = new Date(to.getTime());
|
||||
switch (period) {
|
||||
case "1h":
|
||||
from.setHours(from.getHours() - 1);
|
||||
break;
|
||||
case "6h":
|
||||
from.setHours(from.getHours() - 6);
|
||||
break;
|
||||
case "24h":
|
||||
from.setHours(from.getHours() - 24);
|
||||
break;
|
||||
case "7d":
|
||||
from.setDate(from.getDate() - 7);
|
||||
break;
|
||||
case "30d":
|
||||
from.setDate(from.getDate() - 30);
|
||||
break;
|
||||
default:
|
||||
from.setHours(from.getHours() - 1);
|
||||
}
|
||||
return { from, to };
|
||||
}
|
||||
|
||||
/** Returns unix timestamps (seconds) for the given relative period string. */
|
||||
export function getUnixRangeForPeriod(period: string): { start: number; end: number } {
|
||||
const { from, to } = getRangeForPeriod(period);
|
||||
return {
|
||||
start: Math.floor(from.getTime() / 1000),
|
||||
end: Math.floor(to.getTime() / 1000),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user