Openstatus www.openstatus.dev
at 4c0f4c00a38753a5d0dfd7e7b7b7706dec6f1503 91 lines 2.3 kB view raw
1import { endOfDay, startOfDay, startOfHour, subDays } from "date-fns"; 2 3import type { MonitorPeriodicity } from "@openstatus/db/src/schema"; 4 5export const periods = ["1d", "7d", "14d"] as const; // If neeeded (e.g. Pro plans), "7d", "30d" 6export const quantiles = ["p99", "p95", "p90", "p75", "p50"] as const; 7export const intervals = ["1m", "10m", "30m", "1h"] as const; 8export const triggers = ["cron", "api"] as const; 9 10export type Period = (typeof periods)[number]; 11export type Quantile = (typeof quantiles)[number]; 12export type Interval = (typeof intervals)[number]; 13export type Trigger = (typeof triggers)[number]; 14 15export function getDateByPeriod(period: Period) { 16 switch (period) { 17 case "1d": 18 return { 19 from: subDays(startOfHour(new Date()), 1), 20 to: endOfDay(new Date()), 21 }; 22 case "7d": 23 return { 24 from: subDays(startOfDay(new Date()), 7), 25 to: endOfDay(new Date()), 26 }; 27 case "14d": 28 return { 29 from: subDays(startOfDay(new Date()), 14), 30 to: endOfDay(new Date()), 31 }; 32 default: { 33 const _exhaustiveCheck: never = period; 34 throw new Error(`Unhandled period: ${_exhaustiveCheck}`); 35 } 36 } 37} 38 39export function getHoursByPeriod(period: Period) { 40 switch (period) { 41 case "1d": 42 return 24; 43 case "7d": 44 return 168; 45 case "14d": 46 return 336; 47 default: { 48 const _exhaustiveCheck: never = period; 49 throw new Error(`Unhandled period: ${_exhaustiveCheck}`); 50 } 51 } 52} 53 54export function periodFormatter(period: Period) { 55 switch (period) { 56 case "1d": 57 return "Last day"; 58 case "7d": 59 return "Last 7 days"; 60 case "14d": 61 return "Last 14 days"; 62 default: { 63 const _exhaustiveCheck: never = period; 64 return _exhaustiveCheck; 65 } 66 } 67} 68 69export function getMinutesByInterval(interval: MonitorPeriodicity) { 70 switch (interval) { 71 case "30s": 72 // return 0.5; 73 return 1; // FIX TINYBIRD 74 case "1m": 75 return 1; 76 case "5m": 77 return 5; 78 case "10m": 79 return 10; 80 case "30m": 81 return 30; 82 case "1h": 83 return 60; 84 case "other": 85 return 60; // TODO: remove "other" from here 86 default: { 87 const _exhaustiveCheck: never = interval; 88 throw new Error(`Unhandled interval: ${_exhaustiveCheck}`); 89 } 90 } 91}