Skip to content

Open-source status page, incident response and uptime monitoring

fix: og and extract utility (#268)

Maximilian Kasked3baa4a0d707
diff --git a/apps/web/src/app/api/og/route.tsx b/apps/web/src/app/api/og/route.tsx
index 15941d435..645502d67 100644
--- a/apps/web/src/app/api/og/route.tsx
+++ b/apps/web/src/app/api/og/route.tsx
@@ -1,6 +1,7 @@
import { ImageResponse } from "next/server";
import { getMonitorListData } from "@/lib/tb";
+import { blacklistDates, getMonitorList, getStatus } from "@/lib/tracker";
import { cn, formatDate } from "@/lib/utils";
export const runtime = "edge";
@@ -51,14 +52,9 @@ export async function GET(req: Request) {
}))) ||
[];
- const uptime = data?.reduce(
- (prev, curr) => {
- prev.ok += curr.ok;
- prev.count += curr.count;
- return prev;
- },
- { ok: 0, count: 0 },
- );
+ const { monitors, uptime } = getMonitorList(data, {
+ maxSize: 40,
+ });
return new ImageResponse(
(
@@ -84,13 +80,11 @@ export async function GET(req: Request) {
{title}
</h1>
<p tw="text-slate-600 text-3xl">{description}</p>
- {data && data.length > 0 ? (
+ {monitors && monitors.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.ok / uptime.count) * 100).toFixed(2)}% uptime
- </p>
+ <p tw="mr-1">{uptime}% uptime</p>
</div>
<div tw="flex flex-row relative">
{/* Empty State */}
@@ -103,8 +97,19 @@ export async function GET(req: Request) {
);
})}
<div tw="flex flex-row absolute right-0">
- {data.map((item, i) => {
+ {monitors.map((item, i) => {
const { variant } = getStatus(item.ok / item.count);
+ const isBlackListed = Object.keys(blacklistDates).includes(
+ String(item.cronTimestamp),
+ );
+ if (isBlackListed) {
+ return (
+ <div
+ key={i}
+ tw="h-16 w-3 rounded-full mr-1 bg-green-400"
+ />
+ );
+ }
return (
<div
key={i}
@@ -152,12 +157,3 @@ export async function GET(req: Request) {
},
);
}
-
-// FIXME this is a temporary solution (taken from Tracker)
-const getStatus = (
- ratio: number,
-): { label: string; variant: "up" | "degraded" | "down" } => {
- if (ratio >= 0.98) return { label: "Operational", variant: "up" };
- if (ratio >= 0.5) return { label: "Degraded", variant: "degraded" };
- return { label: "Downtime", variant: "down" };
-};
diff --git a/apps/web/src/components/tracker.tsx b/apps/web/src/components/tracker.tsx
index 3f337acce..352eec802 100644
--- a/apps/web/src/components/tracker.tsx
+++ b/apps/web/src/components/tracker.tsx
@@ -21,6 +21,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import useWindowSize from "@/hooks/use-window-size";
+import { blacklistDates, getMonitorList, 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", {
@@ -38,14 +39,6 @@ const tracker = cva("h-10 rounded-full flex-1", {
},
});
-// We had issues during those dates!
-const blacklist: Record<number, string> = {
- 1692921600000:
- "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.",
-};
-
interface TrackerProps {
data: Monitor[];
url: string;
@@ -65,28 +58,10 @@ export function Tracker({
}: 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 slicedData = data.slice(0, maxSize).reverse();
- const placeholderData: null[] = Array(maxSize).fill(null);
-
- const filledData: Monitor[] =
- context === "play" ? slicedData : fillMissingDates(slicedData);
-
- const reducedData = slicedData.reduce(
- (prev, curr) => {
- prev.ok += curr.ok;
- prev.count += curr.count;
- return prev;
- },
- {
- count: 0,
- ok: 0,
- },
- );
-
- const uptime =
- reducedData.count !== 0
- ? `${((reducedData.ok / reducedData.count) * 100).toFixed(2)}% uptime`
- : "";
+ const { monitors, placeholder, uptime } = getMonitorList(data, {
+ maxSize,
+ context,
+ });
return (
<div className="flex flex-col">
@@ -97,17 +72,17 @@ export function Tracker({
<MoreInfo {...{ url, id, context, description }} />
) : null}
</div>
- <p className="text-muted-foreground font-light">{uptime}</p>
+ <p className="text-muted-foreground font-light">{uptime}% uptime</p>
</div>
<div className="relative h-full w-full">
<div className="flex gap-0.5">
- {Array(placeholderData.length - slicedData.length)
+ {Array(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" })} />;
})}
- {filledData.map((props) => {
+ {monitors.map((props) => {
return (
<Bar key={props.cronTimestamp} context={context} {...props} />
);
@@ -169,7 +144,9 @@ const Bar = ({
const toDate = isMidnight ? date.setDate(date.getDate() + 1) : cronTimestamp;
const dateFormat = isMidnight ? "dd/MM/yy" : "dd/MM/yy HH:mm";
- const isBlackListed = Object.keys(blacklist).includes(String(cronTimestamp));
+ const isBlackListed = Object.keys(blacklistDates).includes(
+ String(cronTimestamp),
+ );
return (
<HoverCard
@@ -188,7 +165,7 @@ const Bar = ({
<HoverCardContent side="top" className="w-64">
{isBlackListed ? (
<p className="text-muted-foreground text-xs">
- {blacklist[cronTimestamp]}
+ {blacklistDates[cronTimestamp]}
</p>
) : (
<>
@@ -232,61 +209,3 @@ const Bar = ({
</HoverCard>
);
};
-
-// FIXME this is a temporary solution
-const getStatus = (
- ratio: number,
-): { label: string; variant: "up" | "degraded" | "down" | "empty" } => {
- if (isNaN(ratio)) return { label: "Missing", variant: "empty" };
- if (ratio >= 0.98) return { label: "Operational", variant: "up" };
- if (ratio >= 0.5) return { label: "Degraded", variant: "degraded" };
- return { label: "Downtime", variant: "down" };
-};
-
-// TODO: is there a way to do it with `date-fns`?
-// Function to fill missing dates in the data array
-function fillMissingDates(data: Monitor[]) {
- if (data.length === 0) {
- return [];
- }
-
- const startDate = new Date(data[0].cronTimestamp);
- const endDate = new Date(data[data.length - 1].cronTimestamp);
- const dateSequence = generateDateSequence(startDate, endDate);
-
- const filledData: Monitor[] = [];
- let dataIndex = 0;
-
- for (const currentDate of dateSequence) {
- const currentTimestamp = currentDate.getTime();
- if (
- dataIndex < data.length &&
- data[dataIndex].cronTimestamp === currentTimestamp
- ) {
- filledData.push(data[dataIndex]);
- dataIndex++;
- } else {
- filledData.push({
- count: 0,
- ok: 0,
- avgLatency: 0,
- cronTimestamp: currentTimestamp,
- });
- }
- }
-
- return filledData;
-}
-
-// Function to generate a sequence of dates between two dates
-function generateDateSequence(startDate: Date, endDate: Date) {
- const dateSequence = [];
- const currentDate = new Date(startDate);
-
- while (currentDate <= endDate) {
- dateSequence.push(new Date(currentDate));
- currentDate.setDate(currentDate.getDate() + 1);
- }
-
- return dateSequence;
-}
diff --git a/apps/web/src/lib/tracker.ts b/apps/web/src/lib/tracker.ts
new file mode 100644
index 000000000..e94aa8cb4
--- /dev/null
+++ b/apps/web/src/lib/tracker.ts
@@ -0,0 +1,124 @@
+import type { Monitor } from "@openstatus/tinybird";
+
+/**
+ * Get the status of a monitor based on its ratio
+ * @param ratio
+ * @returns
+ */
+export const getStatus = (
+ ratio: number,
+): { label: string; variant: "up" | "degraded" | "down" | "empty" } => {
+ if (isNaN(ratio)) return { label: "Missing", variant: "empty" };
+ if (ratio >= 0.98) return { label: "Operational", variant: "up" };
+ if (ratio >= 0.5) return { label: "Degraded", variant: "degraded" };
+ return { label: "Downtime", variant: "down" };
+};
+
+/**
+ *
+ * @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();
+ const placeholderData: null[] = Array(maxSize).fill(null);
+
+ const filledData: Monitor[] =
+ context === "play" ? slicedData : fillMissingDates(slicedData);
+
+ const totalUptime = getTotalUptime(filledData);
+
+ return {
+ monitors: filledData,
+ placeholder: placeholderData,
+ uptime: totalUptime,
+ };
+}
+
+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 uptime =
+ reducedData.count !== 0
+ ? ((reducedData.ok / reducedData.count) * 100).toFixed(2)
+ : "";
+
+ return uptime;
+}
+
+/**
+ * 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 startDate = new Date(data[0].cronTimestamp);
+ const endDate = new Date(data[data.length - 1].cronTimestamp);
+ // 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);
+
+ const filledData: Monitor[] = [];
+ let dataIndex = 0;
+
+ for (const currentDate of dateSequence) {
+ const currentTimestamp = currentDate.getTime();
+ if (
+ dataIndex < data.length &&
+ data[dataIndex].cronTimestamp === currentTimestamp
+ ) {
+ filledData.push(data[dataIndex]);
+ dataIndex++;
+ } else {
+ filledData.push({
+ count: 0,
+ ok: 0,
+ avgLatency: 0,
+ cronTimestamp: currentTimestamp,
+ });
+ }
+ }
+
+ return filledData;
+}
+
+// Function to generate a sequence of dates between two dates
+export function generateDateSequence(startDate: Date, endDate: Date) {
+ const dateSequence = [];
+ const currentDate = new Date(startDate);
+
+ while (currentDate <= endDate) {
+ dateSequence.push(new Date(currentDate));
+ currentDate.setDate(currentDate.getDate() + 1);
+ }
+
+ return dateSequence;
+}
+
+/**
+ * Blacklist dates where we had issues with data collection
+ */
+export const blacklistDates: Record<number, string> = {
+ 1692921600000:
+ "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.",
+};