Skip to content

Open-source status page, incident response and uptime monitoring

chore: improve tracker date handling (#445)

Maximilian Kaske52791d2a6977
diff --git a/apps/web/src/app/api/og/route.tsx b/apps/web/src/app/api/og/route.tsx
index dd22f4ac6..ef53d7c99 100644
--- a/apps/web/src/app/api/og/route.tsx
+++ b/apps/web/src/app/api/og/route.tsx
@@ -2,7 +2,7 @@ import { ImageResponse } from "next/server";
import { DESCRIPTION, TITLE } from "@/app/shared-metadata";
import { getMonitorListData } from "@/lib/tb";
-import { blacklistDates, getMonitorList, getStatus } from "@/lib/tracker";
+import { cleanData, getStatus, isInBlacklist } from "@/lib/tracker";
import { cn, formatDate } from "@/lib/utils";
export const runtime = "edge";
@@ -50,9 +50,7 @@ export async function GET(req: Request) {
}))) ||
[];
- const { monitors, uptime } = getMonitorList(data, {
- maxSize: 40,
- });
+ const { bars, uptime } = cleanData({ data, last: LIMIT });
return new ImageResponse(
(
@@ -78,11 +76,11 @@ export async function GET(req: Request) {
{title}
</h1>
<p tw="text-slate-600 text-3xl">{description}</p>
- {monitors && monitors.length > 0 ? (
+ {bars && bars.length > 0 ? (
<div tw="flex flex-col w-full mt-6">
<div tw="flex flex-row items-center justify-between -mb-1 text-black font-light">
<p tw="">{formatDate(new Date())}</p>
- <p tw="mr-1">{uptime}% uptime</p>
+ <p tw="mr-1">{uptime}</p>
</div>
<div tw="flex flex-row relative">
{/* Empty State */}
@@ -94,12 +92,10 @@ export async function GET(req: Request) {
></div>
);
})}
- <div tw="flex flex-row absolute right-0">
- {monitors.map((item, i) => {
+ <div tw="flex flex-row-reverse absolute right-0">
+ {bars.map((item, i) => {
const { variant } = getStatus(item.ok / item.count);
- const isBlackListed = Object.keys(blacklistDates).includes(
- String(item.cronTimestamp),
- );
+ const isBlackListed = Boolean(item.blacklist);
if (isBlackListed) {
return (
<div
diff --git a/apps/web/src/app/play/page.tsx b/apps/web/src/app/play/page.tsx
index cff7f767d..6fd10b20f 100644
--- a/apps/web/src/app/play/page.tsx
+++ b/apps/web/src/app/play/page.tsx
@@ -1,8 +1,8 @@
import { Tracker } from "@/components/tracker";
-import { getMonitorListData } from "@/lib/tb";
+import { getHomeMonitorListData } from "@/lib/tb";
export default async function PlayPage() {
- const data = await getMonitorListData({ monitorId: "openstatusPing" });
+ const data = await getHomeMonitorListData();
return (
<div className="relative flex flex-col items-center justify-center gap-4">
<p className="font-cal mb-1 text-3xl">Status</p>
@@ -13,7 +13,7 @@ export default async function PlayPage() {
{data && (
<Tracker
data={data}
- id="openstatusPing"
+ id="1"
name="Ping"
url="https://www.openstatus.dev/api/ping"
/>
diff --git a/apps/web/src/app/status-page/[domain]/loading.tsx b/apps/web/src/app/status-page/[domain]/loading.tsx
index b8c2a380d..bc129e5d0 100644
--- a/apps/web/src/app/status-page/[domain]/loading.tsx
+++ b/apps/web/src/app/status-page/[domain]/loading.tsx
@@ -1,10 +1,19 @@
+import { Skeleton } from "@openstatus/ui";
+
import { Container } from "@/components/dashboard/container";
import { Header } from "@/components/dashboard/header";
-export default function StatusPageLoading() {
+export default function Loading() {
return (
<div className="grid gap-6">
<Header.Skeleton />
+ <div className="flex flex-col items-center gap-2">
+ <div className="flex items-center gap-3">
+ <Skeleton className="h-7 w-24" />
+ <Skeleton className="h-10 w-10 rounded-full" />
+ </div>
+ <Skeleton className="h-4 w-28" />
+ </div>
<div className="grid gap-4">
<Container.Skeleton />
<Container.Skeleton />
diff --git a/apps/web/src/app/status-page/[domain]/page.tsx b/apps/web/src/app/status-page/[domain]/page.tsx
index c2542fd81..c636bfb4a 100644
--- a/apps/web/src/app/status-page/[domain]/page.tsx
+++ b/apps/web/src/app/status-page/[domain]/page.tsx
@@ -14,8 +14,6 @@ import { Header } from "@/components/dashboard/header";
import { IncidentList } from "@/components/status-page/incident-list";
import { MonitorList } from "@/components/status-page/monitor-list";
import { StatusCheck } from "@/components/status-page/status-check";
-import { getMonitorListData } from "@/lib/tb";
-import { notEmpty } from "@/lib/utils";
import { api } from "@/trpc/server";
const url =
diff --git a/apps/web/src/components/status-page/monitor.tsx b/apps/web/src/components/status-page/monitor.tsx
index a543d939d..21676e7b2 100644
--- a/apps/web/src/components/status-page/monitor.tsx
+++ b/apps/web/src/components/status-page/monitor.tsx
@@ -19,7 +19,6 @@ export const Monitor = async ({
name={monitor.name}
url={monitor.url}
description={monitor.description}
- context="status-page"
/>
);
};
diff --git a/apps/web/src/components/status-page/status-check.tsx b/apps/web/src/components/status-page/status-check.tsx
index ee9ab8af6..1fc6ee3ae 100644
--- a/apps/web/src/components/status-page/status-check.tsx
+++ b/apps/web/src/components/status-page/status-check.tsx
@@ -1,3 +1,4 @@
+import { cva } from "class-variance-authority";
import type { z } from "zod";
import type {
@@ -11,6 +12,21 @@ import { getStatus } from "@/lib/tracker";
import { cn, notEmpty } from "@/lib/utils";
import { Icons } from "../icons";
+const check = cva("border-border rounded-full border p-2", {
+ variants: {
+ variant: {
+ up: "text-green-500 bg-green-500",
+ down: "text-red-500 bg-red-500",
+ degraded: "text-yellow-500 bg-yellow-500",
+ empty: "text-gray-500 bg-gray-500",
+ incident: "text-yellow-500 bg-yellow-500",
+ },
+ },
+ defaultVariants: {
+ variant: "up",
+ },
+});
+
export async function StatusCheck({
incidents,
monitors,
@@ -49,19 +65,15 @@ export async function StatusCheck({
const incident = {
label: "Incident",
variant: "incident",
- twColor: "text-yellow-500",
- twBgColor: "bg-yellow-500",
} as const;
- const { label, variant, twBgColor } = isIncident ? incident : status;
+ const { label, variant } = isIncident ? incident : status;
return (
<div className="flex flex-col items-center gap-2">
<div className="flex items-center gap-3">
<p className="text-lg font-semibold">{label}</p>
- <span
- className={cn("border-border rounded-full border p-2", twBgColor)}
- >
+ <span className={check({ variant })}>
<StatusIcon variant={variant} />
</span>
</div>
diff --git a/apps/web/src/components/tracker.tsx b/apps/web/src/components/tracker.tsx
index 84b62d878..d1e6c80f1 100644
--- a/apps/web/src/components/tracker.tsx
+++ b/apps/web/src/components/tracker.tsx
@@ -19,7 +19,8 @@ import {
} from "@openstatus/ui";
import useWindowSize from "@/hooks/use-window-size";
-import { blacklistDates, getMonitorList, getStatus } from "@/lib/tracker";
+import type { CleanMonitor } from "@/lib/tracker";
+import { blacklistDates, cleanData, getStatus } from "@/lib/tracker";
// What would be cool is tracker that turn from green to red depending on the number of errors
const tracker = cva("h-10 rounded-full flex-1", {
@@ -28,8 +29,8 @@ const tracker = cva("h-10 rounded-full flex-1", {
up: "bg-green-500 data-[state=open]:bg-green-600",
down: "bg-red-500 data-[state=open]:bg-red-600",
degraded: "bg-yellow-500 data-[state=open]:bg-yellow-600",
- empty: "bg-muted-foreground/20",
- blacklist: "bg-green-400",
+ empty: "bg-muted-foreground/20 data-[state=open]:bg-muted-foreground/30",
+ blacklist: "bg-green-400 data-[state=open]:bg-green-600",
},
},
defaultVariants: {
@@ -55,11 +56,9 @@ export function Tracker({
description,
}: TrackerProps) {
const { isMobile } = useWindowSize();
- const maxSize = React.useMemo(() => (isMobile ? 35 : 45), [isMobile]); // TODO: it is better than how it is currently, but creates a small content shift on first render
- const { monitors, placeholder, uptime } = getMonitorList(data, {
- maxSize,
- context,
- });
+ // TODO: it is better than how it was currently, but creates a small content shift on first render
+ const maxSize = React.useMemo(() => (isMobile ? 35 : 45), [isMobile]);
+ const { bars, uptime } = cleanData({ data, last: maxSize });
return (
<div className="flex flex-col">
@@ -70,19 +69,11 @@ export function Tracker({
<MoreInfo {...{ url, id, context, description }} />
) : null}
</div>
- <p className="text-muted-foreground shrink-0 font-light">
- {uptime}% uptime
- </p>
+ <p className="text-muted-foreground shrink-0 font-light">{uptime}</p>
</div>
<div className="relative h-full w-full">
- <div className="flex gap-0.5">
- {Array(Math.abs(placeholder.length - monitors.length))
- .fill(null)
- .map((_, i) => {
- // TODO: use `Bar` component and `HoverCard` with empty state
- return <div key={i} className={tracker({ variant: "empty" })} />;
- })}
- {monitors.map((props) => {
+ <div className="flex flex-row-reverse gap-0.5">
+ {bars.map((props) => {
return (
<Bar key={props.cronTimestamp} context={context} {...props} />
);
@@ -134,19 +125,14 @@ const Bar = ({
ok,
avgLatency,
cronTimestamp,
+ blacklist,
context,
-}: Monitor & Pick<TrackerProps, "context">) => {
+}: CleanMonitor & Pick<TrackerProps, "context">) => {
const [open, setOpen] = React.useState(false);
const ratio = ok / count;
- // FIX: this is an easy way to detect if cronTimestamps have been aggregated
- const isMidnight = String(cronTimestamp).endsWith("00000");
const date = new Date(cronTimestamp);
- const toDate = isMidnight ? date.setDate(date.getDate() + 1) : cronTimestamp;
- const dateFormat = isMidnight ? "dd/MM/yy" : "dd/MM/yy HH:mm";
-
- const isBlackListed = Object.keys(blacklistDates).includes(
- String(cronTimestamp),
- );
+ const toDate = date.setDate(date.getDate() + 1);
+ const dateFormat = "dd/MM/yy";
return (
<HoverCard
@@ -158,15 +144,13 @@ const Bar = ({
<HoverCardTrigger onClick={() => setOpen(true)} asChild>
<div
className={tracker({
- variant: isBlackListed ? "blacklist" : getStatus(ratio).variant,
+ variant: blacklist ? "blacklist" : getStatus(ratio).variant,
})}
/>
</HoverCardTrigger>
<HoverCardContent side="top" className="w-64">
- {isBlackListed ? (
- <p className="text-muted-foreground text-xs">
- {blacklistDates[cronTimestamp]}
- </p>
+ {blacklist ? (
+ <p className="text-muted-foreground text-xs">{blacklist}</p>
) : (
<>
<div className="flex justify-between">
diff --git a/apps/web/src/lib/tracker.ts b/apps/web/src/lib/tracker.ts
index de2d0cfef..859d67ee1 100644
--- a/apps/web/src/lib/tracker.ts
+++ b/apps/web/src/lib/tracker.ts
@@ -5,8 +5,6 @@ export type StatusVariant = "up" | "degraded" | "down" | "empty";
type GetStatusReturnType = {
label: string;
variant: StatusVariant;
- twColor: string;
- twBgColor: string;
};
/**
@@ -19,135 +17,153 @@ export const getStatus = (ratio: number): GetStatusReturnType => {
return {
label: "Missing",
variant: "empty",
- twColor: "text-gray-500",
- twBgColor: "bg-gray-500",
};
if (ratio >= 0.98)
return {
label: "Operational",
variant: "up",
- twColor: "text-green-500",
- twBgColor: "bg-green-500",
};
if (ratio >= 0.5)
return {
label: "Degraded",
variant: "degraded",
- twColor: "text-yellow-500",
- twBgColor: "bg-yellow-500",
};
return {
label: "Downtime",
variant: "down",
- twColor: "text-red-500",
- twBgColor: "bg-red-500",
};
};
-/**
- *
- * @param data Array of monitors from tinybird
- * @returns
- */
-export function getMonitorList(
- data: Monitor[],
- { maxSize, context }: { maxSize: number; context?: string },
-) {
- const slicedData = data.slice(0, maxSize).reverse();
+// TODO: move into Class component sharing the same `data`
- const filledData: Monitor[] =
- context === "play" ? slicedData : fillMissingDates(slicedData);
-
- const placeholderData: null[] = Array(
- Math.max(maxSize, filledData.length), // we might have more data than maxSize as we are adding empty dates in between
- ).fill(null);
-
- const totalUptime = getTotalUptime(filledData);
+export type CleanMonitor = Monitor & {
+ blacklist?: string;
+};
- return {
- monitors: filledData,
- placeholder: placeholderData,
- uptime: totalUptime,
- };
-}
+export function cleanData({ data, last }: { data: Monitor[]; last: number }) {
+ const today = new Date();
-export function getTotalUptime(data: Monitor[]) {
- const reducedData = data.reduce(
- (prev, curr) => {
- prev.ok += curr.ok;
- prev.count += curr.count;
- return prev;
- },
- {
- count: 0,
- ok: 0,
- },
- );
+ const currentDay = new Date(today);
+ currentDay.setUTCDate(today.getDate());
+ currentDay.setUTCHours(0, 0, 0, 0);
- const uptime =
- reducedData.count !== 0
- ? ((reducedData.ok / reducedData.count) * 100).toFixed(2)
- : "";
+ const lastDay = new Date(today);
+ lastDay.setUTCDate(today.getDate() - last);
+ lastDay.setUTCHours(0, 0, 0, 0);
- return uptime;
-}
+ const dateSequence = generateDateSequence(lastDay, currentDay);
-/**
- * It happens that some monitors don't have data for a specific date
- * This function fills the missing dates in between
- * @param data Array of monitors from tinybird
- * @returns
- */
-export function fillMissingDates(data: Monitor[]) {
- if (data.length === 0) {
- return [];
- }
+ const filledData = fillEmptyData(data, dateSequence);
- const startDate = new Date(data[0].cronTimestamp);
- const endDate = new Date(
- new Date(data[data.length - 1].cronTimestamp).setHours(23, 59, 59, 999),
- );
+ const uptime = getTotalUptimeString(filledData);
- // The reason why we cannot use `date-fns` is because it isn't supported on the edge
- // const dateSequence = eachDayOfInterval({start:startDate, end:endDate})
- const dateSequence = generateDateSequence(startDate, endDate);
+ return { bars: filledData, uptime }; // possibly only return filledData?
+}
- const filledData: Monitor[] = [];
+function fillEmptyData(data: Monitor[], dateSequence: Date[]) {
+ const filledData: CleanMonitor[] = [];
let dataIndex = 0;
- for (const currentDate of dateSequence) {
- const currentTimestamp = currentDate.getTime();
+ for (const date of dateSequence) {
+ const timestamp = date.getTime();
+ const cronTimestamp =
+ dataIndex < data.length ? data[dataIndex].cronTimestamp : undefined;
if (
- dataIndex < data.length &&
- (data[dataIndex].cronTimestamp === currentTimestamp ||
- data[dataIndex].cronTimestamp + 60 * 60 * 1000 === currentTimestamp)
+ cronTimestamp &&
+ areDatesEqualByDayMonthYear(new Date(cronTimestamp), date)
) {
- filledData.push(data[dataIndex]);
+ const isBlacklisted = isInBlacklist(cronTimestamp);
+
+ /**
+ * automatically remove the data from the array to avoid wrong uptime
+ * that provides time to remove cursed logs from tinybird via mv migration
+ */
+ if (isBlacklisted) {
+ filledData.push({
+ ...emptyData(timestamp),
+ blacklist: blacklistDates[cronTimestamp],
+ });
+ } else {
+ filledData.push(data[dataIndex]);
+ }
dataIndex++;
} else {
- filledData.push({
- count: 0,
- ok: 0,
- avgLatency: 0,
- cronTimestamp: currentTimestamp,
- });
+ filledData.push(emptyData(timestamp));
}
}
return filledData;
}
-// Function to generate a sequence of dates between two dates
-export function generateDateSequence(startDate: Date, endDate: Date) {
- const dateSequence = [];
+function emptyData(cronTimestamp: number) {
+ return {
+ count: 0,
+ ok: 0,
+ avgLatency: 0,
+ cronTimestamp,
+ };
+}
+
+/**
+ * equal UTC days - fixes issue with daylight saving
+ * @param date1
+ * @param date2
+ * @returns
+ */
+function areDatesEqualByDayMonthYear(date1: Date, date2: Date) {
+ date1.setUTCDate(date1.getDate());
+ date1.setUTCHours(0, 0, 0, 0);
+
+ date2.setUTCDate(date2.getDate());
+ date2.setUTCHours(0, 0, 0, 0);
+
+ return date1.toUTCString() === date2.toUTCString();
+}
+
+/**
+ *
+ * @param startDate
+ * @param endDate
+ * @returns
+ */
+export function generateDateSequence(startDate: Date, endDate: Date): Date[] {
+ const dateSequence: Date[] = [];
const currentDate = new Date(startDate);
while (currentDate <= endDate) {
dateSequence.push(new Date(currentDate));
- currentDate.setDate(currentDate.getDate() + 1);
+ currentDate.setUTCDate(currentDate.getDate() + 1);
}
- return dateSequence;
+ return dateSequence.reverse();
+}
+
+export function getTotalUptime(data: Monitor[]) {
+ const reducedData = data.reduce(
+ (prev, curr) => {
+ prev.ok += curr.ok;
+ prev.count += curr.count;
+ return prev;
+ },
+ {
+ count: 0,
+ ok: 0,
+ },
+ );
+ return reducedData;
+}
+
+export function getTotalUptimeString(data: Monitor[]) {
+ const reducedData = getTotalUptime(data);
+ const uptime = (reducedData.ok / reducedData.count) * 100;
+
+ if (isNaN(uptime)) return "";
+
+ return `${uptime.toFixed(2)}% uptime`;
+}
+
+export function isInBlacklist(timestamp: number) {
+ return Object.keys(blacklistDates).includes(timestamp.toString());
}
/**
@@ -158,7 +174,6 @@ export const blacklistDates: Record<number, string> = {
"OpenStatus faced issues between 24.08. and 27.08., preventing data collection.",
1693008000000:
"OpenStatus faced issues between 24.08. and 27.08., preventing data collection.",
- // Downtime on 18. Oct. 2023 between 20h40 - 23h55 - TOD0: remove error logs
1697587200000:
"OpenStatus migrated from Vercel to Fly to improve the performance of the checker.",
};