Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import { endOfDay, isSameDay, startOfDay } from "date-fns";
import type { PERIODS } from "@/data/metrics.client";
export function formatMilliseconds(ms: number) {
if (ms > 1000) {
return `${Intl.NumberFormat("en-US", {
style: "unit",
unit: "second",
maximumFractionDigits: 2,
}).format(ms / 1000)}`;
}
return `${Intl.NumberFormat("en-US", {
style: "unit",
unit: "millisecond",
}).format(ms)}`;
}
export function formatPercentage(value: number, fractionDigits = 2) {
if (Number.isNaN(value)) return "100%";
return `${Intl.NumberFormat("en-US", {
style: "percent",
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
}).format(value)}`;
}
export function formatNumber(value: number) {
return `${Intl.NumberFormat("en-US").format(value)}`;
}
// TODO: think of supporting custom formats
export function formatDate(date: Date) {
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
}
export function formatDateTime(date: Date) {
return date.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
});
}
export function formatTime(date: Date) {
return date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
});
}
// Axis ticks only carry what the range can't imply (tooltip has the full date):
// time of day on 1d, the date at midnight and for daily buckets, both otherwise.
export function formatAxisTick(
date: Date,
period: (typeof PERIODS)[number],
interval: number,
) {
const midnight = date.getHours() === 0 && date.getMinutes() === 0;
if (interval >= 1440 || (period === "1d" && midnight)) {
return date.toLocaleDateString("en-US", { month: "short", day: "numeric" });
}
if (period === "1d") return formatTime(date);
return date.toLocaleString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
...(date.getMinutes() ? { minute: "numeric" } : {}),
});
}
export function formatDateRange(from?: Date, to?: Date) {
const sameDay = from && to && isSameDay(from, to);
const isFromStartDay = from && startOfDay(from).getTime() === from.getTime();
const isToEndDay = to && endOfDay(to).getTime() === to.getTime();
if (sameDay) {
if (from && to) {
return `${formatDateTime(from)} - ${formatTime(to)}`;
}
}
if (from && to) {
if (isFromStartDay && isToEndDay) {
return `${formatDate(from)} - ${formatDate(to)}`;
}
return `${formatDateTime(from)} - ${formatDateTime(to)}`;
}
if (to) {
return `Until ${formatDateTime(to)}`;
}
if (from) {
return `Since ${formatDateTime(from)}`;
}
return "All time";
}
export function formatDistanceToNowShort(date: Date): string {
const seconds = Math.max(0, Math.round((Date.now() - date.getTime()) / 1000));
if (seconds < 60) return `${seconds}s ago`;
const minutes = Math.round(seconds / 60);
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.round(minutes / 60);
if (hours < 24) return `${hours}h ago`;
const days = Math.round(hours / 24);
return `${days}d ago`;
}
export function formatDateForInput(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day}T${hours}:${minutes}`;
}