Skip to content

Open-source status page, incident response and uptime monitoring

Aggregated monitor data and search params as data table filter (#22)

Maximilian Kaske6d38f3d55dd0
diff --git a/apps/web/src/app/monitor/[id]/[region]/page.tsx b/apps/web/src/app/monitor/[id]/[region]/page.tsx
deleted file mode 100644
index e52865c43..000000000
--- a/apps/web/src/app/monitor/[id]/[region]/page.tsx
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { Region } from "@openstatus/tinybird";
-
-import { columns } from "@/components/monitor/columns";
-import { DataTable } from "@/components/monitor/data-table";
-import { getResponseListData } from "@/lib/tb";
-
-export default async function Monitor({
- params,
-}: {
- params: { id: string; region: Region };
-}) {
- const data = await getResponseListData({
- siteId: params.id,
- region: params.region,
- });
- return <DataTable columns={columns} data={data} />;
-}
diff --git a/apps/web/src/app/monitor/[id]/page.tsx b/apps/web/src/app/monitor/[id]/page.tsx
index f95b9e3bd..67a5523cd 100644
--- a/apps/web/src/app/monitor/[id]/page.tsx
+++ b/apps/web/src/app/monitor/[id]/page.tsx
@@ -1,8 +1,33 @@
+import * as z from "zod";
+
+import { availableRegions } from "@openstatus/tinybird";
+
import { columns } from "@/components/monitor/columns";
import { DataTable } from "@/components/monitor/data-table";
import { getResponseListData } from "@/lib/tb";
-export default async function Monitor({ params }: { params: { id: string } }) {
- const data = await getResponseListData({ siteId: params.id });
+//
+
+/**
+ * allowed URL search params
+ */
+const searchParamsSchema = z.object({
+ statusCode: z.coerce.number().optional(),
+ region: z.enum(availableRegions).optional(),
+ cronTimestamp: z.coerce.number().optional(),
+});
+
+export default async function Monitor({
+ params,
+ searchParams,
+}: {
+ params: { id: string };
+ searchParams: { [key: string]: string | string[] | undefined };
+}) {
+ const search = searchParamsSchema.safeParse(searchParams);
+ const data = search.success
+ ? await getResponseListData({ siteId: params.id, ...search.data })
+ : await getResponseListData({ siteId: params.id });
+
return <DataTable columns={columns} data={data} />;
}
diff --git a/apps/web/src/app/play/@modal/(..)monitor/[id]/page.tsx b/apps/web/src/app/play/@modal/(..)monitor/[id]/page.tsx
index a8506fccf..93608a07f 100644
--- a/apps/web/src/app/play/@modal/(..)monitor/[id]/page.tsx
+++ b/apps/web/src/app/play/@modal/(..)monitor/[id]/page.tsx
@@ -1,10 +1,35 @@
+import * as z from "zod";
+
+import { availableRegions } from "@openstatus/tinybird";
+
import { columns } from "@/components/monitor/columns";
import { DataTable } from "@/components/monitor/data-table";
import { getResponseListData } from "@/lib/tb";
import { Modal } from "./modal";
-export default async function Monitor({ params }: { params: { id: string } }) {
- const data = await getResponseListData({ siteId: params.id });
+//
+
+/**
+ * allowed URL search params
+ */
+const searchParamsSchema = z.object({
+ statusCode: z.coerce.number().optional(),
+ region: z.enum(availableRegions).optional(),
+ cronTimestamp: z.coerce.number().optional(),
+});
+
+export default async function Monitor({
+ params,
+ searchParams,
+}: {
+ params: { id: string };
+ searchParams: { [key: string]: string | string[] | undefined };
+}) {
+ const search = searchParamsSchema.safeParse(searchParams);
+ const data = search.success
+ ? await getResponseListData({ siteId: params.id, ...search.data })
+ : await getResponseListData({ siteId: params.id });
+
return (
<Modal>
<DataTable columns={columns} data={data} />
diff --git a/apps/web/src/app/play/page.tsx b/apps/web/src/app/play/page.tsx
index 0f8293dbf..edc896eea 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/monitor/tracker";
-import { getResponseListData } from "@/lib/tb";
+import { getMonitorListData } from "@/lib/tb";
export default async function PlayPage() {
- const data = await getResponseListData({ siteId: "openstatus" });
+ const data = await getMonitorListData({ siteId: "openstatus" });
return (
<div className="flex flex-col items-center justify-center gap-4">
<p className="mb-2 text-2xl font-semibold">Status</p>
diff --git a/apps/web/src/components/monitor/columns.tsx b/apps/web/src/components/monitor/columns.tsx
index dbf93c4f0..41acb30de 100644
--- a/apps/web/src/components/monitor/columns.tsx
+++ b/apps/web/src/components/monitor/columns.tsx
@@ -1,6 +1,7 @@
"use client";
import type { ColumnDef } from "@tanstack/react-table";
+import { format } from "date-fns";
import type { Ping } from "@openstatus/tinybird";
@@ -18,14 +19,7 @@ export const columns: ColumnDef<Ping>[] = [
cell: ({ row }) => {
return (
<div>
- {new Intl.DateTimeFormat("en", {
- year: "numeric",
- month: "numeric",
- day: "numeric",
- hour: "numeric",
- minute: "numeric",
- hour12: false, // dismiss hydration issue ("_PM" and "__PM")
- }).format(row.getValue("timestamp"))}
+ {format(new Date(row.getValue("timestamp")), "dd/MM/yy HH:mm")}
</div>
);
},
diff --git a/apps/web/src/components/monitor/data-table.tsx b/apps/web/src/components/monitor/data-table.tsx
index 371a08ce0..5917a9772 100644
--- a/apps/web/src/components/monitor/data-table.tsx
+++ b/apps/web/src/components/monitor/data-table.tsx
@@ -29,6 +29,7 @@ import { DataTablePagination } from "./data-table-pagination";
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
+ // filters?: ColumnFiltersState; could also be an idea
}
export function DataTable<TData, TValue>({
diff --git a/apps/web/src/components/monitor/tracker.tsx b/apps/web/src/components/monitor/tracker.tsx
index dfc5810b0..35edb9e97 100644
--- a/apps/web/src/components/monitor/tracker.tsx
+++ b/apps/web/src/components/monitor/tracker.tsx
@@ -1,9 +1,12 @@
-// TODO: create Compound Components like Tracker.Legend instead of passing props
+"use client";
+
+import * as React from "react";
import Link from "next/link";
import { cva } from "class-variance-authority";
-import { formatDistance } from "date-fns";
+import { format } from "date-fns";
+import { Eye } from "lucide-react";
-import type { Ping } from "@openstatus/tinybird";
+import type { Monitor } from "@openstatus/tinybird";
import {
HoverCard,
@@ -26,23 +29,22 @@ const tracker = cva("h-10 w-1.5 sm:w-2 rounded-full md:w-2.5", {
});
interface TrackerProps {
- data: Ping[];
+ data: Monitor[];
/**
* Maximium length of the data array
*/
maxSize?: number;
}
+// TODO: instead of slicing and setting placeholder data,
+// just start from end and absolute position the same number of divs
+// with a lighter bg color
export function Tracker({ data, maxSize = 35 }: TrackerProps) {
- const sliceData = data.slice(0, maxSize);
+ const slicedData = data.slice(0, maxSize);
const placeholderData: null[] = Array(
- Math.max(0, maxSize - sliceData.length),
+ Math.max(0, maxSize - slicedData.length),
).fill(null);
- // TODO: use tinybird for data aggregation
- // IDEA: We want to group X events and aggregate data,
- // only on hover you will get the individual informations
-
return (
<div className="max-w-max">
<div className="flex justify-end">
@@ -61,38 +63,50 @@ export function Tracker({ data, maxSize = 35 }: TrackerProps) {
{placeholderData.map((_, i) => {
return <div key={i} className={tracker({ variant: "empty" })} />;
})}
- {sliceData
- .reverse()
- .map(({ statusCode, latency, timestamp, region }, i) => {
- const isOk = statusCode === 200;
- return (
- <HoverCard key={i} openDelay={100} closeDelay={100}>
- <HoverCardTrigger>
- <div className={tracker({ variant: isOk ? "up" : "down" })} />
- </HoverCardTrigger>
- <HoverCardContent side="top" className="w-56">
- <div className="flex justify-between">
- <p className="text-sm font-semibold">
- {isOk ? "Operational" : "Downtime"}
- </p>
- <p className="text-muted-foreground font-mono text-xs">
- {region}
- </p>
- </div>
- <div className="flex justify-between">
- <p className="text-xs font-light">
- {formatDistance(new Date(timestamp), new Date(), {
- addSuffix: true,
- includeSeconds: true,
- })}
- </p>
- <p className="text-muted-foreground text-xs">{latency}ms</p>
- </div>
- </HoverCardContent>
- </HoverCard>
- );
- })}
+ {slicedData.reverse().map((props) => {
+ return <Bar key={props.cronTimestamp} {...props} />;
+ })}
</div>
</div>
);
}
+
+const Bar = ({ count, ok, avgLatency, cronTimestamp }: Monitor) => {
+ const [open, setOpen] = React.useState(false);
+ const ratio = ok / count;
+ const isOk = ratio === 1; // TODO: when operational, downtime, degraded
+
+ return (
+ <HoverCard
+ openDelay={100}
+ closeDelay={100}
+ open={open}
+ onOpenChange={setOpen}
+ >
+ <HoverCardTrigger onClick={() => setOpen(true)}>
+ <div className={tracker({ variant: isOk ? "up" : "down" })} />
+ </HoverCardTrigger>
+ <HoverCardContent side="top" className="w-56">
+ <div className="flex justify-between">
+ <p className="text-sm font-semibold">
+ {isOk ? "Operational" : "Downtime"}
+ </p>
+ <Link
+ href={`/monitor/openstatus?cronTimestamp=${cronTimestamp}`}
+ className="text-muted-foreground hover:text-foreground"
+ >
+ <Eye className="h-4 w-4" />
+ </Link>
+ </div>
+ <div className="flex justify-between">
+ <p className="text-xs font-light">
+ {format(new Date(cronTimestamp), "dd/MM/yy HH:mm")}
+ </p>
+ <p className="text-muted-foreground text-xs">
+ avg. <span className="font-mono">{avgLatency}ms</span>
+ </p>
+ </div>
+ </HoverCardContent>
+ </HoverCard>
+ );
+};
diff --git a/apps/web/src/data/regions-dictionary.ts b/apps/web/src/data/regions-dictionary.ts
new file mode 100644
index 000000000..966ed0dbd
--- /dev/null
+++ b/apps/web/src/data/regions-dictionary.ts
@@ -0,0 +1,96 @@
+/**
+ * AWS data center informations from 17 regions, supported by vercel.
+ * https://vercel.com/docs/concepts/edge-network/regions#region-list
+ */
+export const regionsDict = {
+ arn1: {
+ code: "arn1",
+ name: "eu-north-1",
+ location: "Stockholm, Sweden",
+ },
+ bom1: {
+ code: "bom1",
+ name: "ap-south-1",
+ location: "Mumbai, India",
+ },
+ cdg1: {
+ code: "cdg1",
+ name: "eu-west-3",
+ location: "Paris, France",
+ },
+ cle1: {
+ code: "cle1",
+ name: "us-east-2",
+ location: "Cleveland, USA",
+ },
+ cpt1: {
+ code: "cpt1",
+ name: "af-south-1",
+ location: "Cape Town, South Africa",
+ },
+ dub1: {
+ code: "dub1",
+ name: "eu-west-1",
+ location: "Dublin, Ireland",
+ },
+ fra1: {
+ code: "fra1",
+ name: "eu-central-1",
+ location: "Frankfurt, Germany",
+ },
+ gru1: {
+ code: "gru1",
+ name: "sa-east-1",
+ location: "São Paulo, Brazil",
+ },
+ hkg1: {
+ code: "hkg1",
+ name: "ap-east-1",
+ location: "Hong Kong",
+ },
+ hnd1: {
+ code: "hnd1",
+ name: "ap-northeast-1",
+ location: "Tokyo, Japan",
+ },
+ iad1: {
+ code: "iad1",
+ name: "us-east-1",
+ location: "Washington, D.C., USA",
+ },
+ icn1: {
+ code: "icn1",
+ name: "ap-northeast-2",
+ location: "Seoul, South Korea",
+ },
+ kix1: {
+ code: "kix1",
+ name: "ap-northeast-3",
+ location: "Osaka, Japan",
+ },
+ lhr1: {
+ code: "lhr1",
+ name: "eu-west-2",
+ location: "London, United Kingdom",
+ },
+ pdx1: {
+ code: "pdx1",
+ name: "us-west-2",
+ location: "Portland, USA",
+ },
+ sfo1: {
+ code: "sfo1",
+ name: "us-west-1",
+ location: "San Francisco, USA",
+ },
+ sin1: {
+ code: "sin1",
+ name: "ap-southeast-1",
+ location: "Singapore",
+ },
+ syd1: {
+ code: "syd1",
+ name: "ap-southeast-2",
+ location: "Sydney, Australia",
+ },
+} as const;
diff --git a/apps/web/src/lib/tb.ts b/apps/web/src/lib/tb.ts
index 45ba6dd89..36ccb7b7d 100644
--- a/apps/web/src/lib/tb.ts
+++ b/apps/web/src/lib/tb.ts
@@ -1,19 +1,33 @@
-import type { ResponseListParams } from "@openstatus/tinybird";
-import { getResponseList, Tinybird } from "@openstatus/tinybird";
+import type {
+ MonitorListParams,
+ ResponseListParams,
+} from "@openstatus/tinybird";
+import {
+ getMonitorList,
+ getResponseList,
+ Tinybird,
+} from "@openstatus/tinybird";
import { env } from "@/env.mjs";
-import MOCK from "@/mock/response-list.json";
+import MOCK_RESPONSE from "@/mock/response-list.json";
const tb = new Tinybird({ token: env.TINY_BIRD_API_KEY });
-export async function getResponseListData({
- siteId,
- region,
-}: Pick<ResponseListParams, "siteId" | "region">) {
- let data = MOCK;
+// TODO: add security layer
+export async function getResponseListData(
+ props: Partial<
+ Pick<ResponseListParams, "siteId" | "region" | "cronTimestamp" | "limit">
+ >,
+) {
+ let data = MOCK_RESPONSE;
if (process.env.NODE_ENV !== "development") {
- const res = await getResponseList(tb)({ siteId, region });
+ const res = await getResponseList(tb)(props);
data = res.data;
}
return data;
}
+
+export async function getMonitorListData(props: Partial<MonitorListParams>) {
+ const res = await getMonitorList(tb)(props);
+ return res.data;
+}
diff --git a/packages/tinybird/src/client.ts b/packages/tinybird/src/client.ts
index c44a0a822..98cd48dcf 100644
--- a/packages/tinybird/src/client.ts
+++ b/packages/tinybird/src/client.ts
@@ -1,8 +1,10 @@
import type { Tinybird } from "@chronark/zod-bird";
import {
+ tbBuildMonitorList,
tbBuildResponseList,
tbIngestPingResponse,
+ tbParameterMonitorList,
tbParameterResponseList,
} from "./validation";
@@ -24,7 +26,18 @@ export function getResponseList(tb: Tinybird) {
parameters: tbParameterResponseList,
data: tbBuildResponseList,
opts: {
- revalidate: 5 * 60, // 5 minutes cache validation
+ revalidate: 60, // 60 seconds cache validation
+ },
+ });
+}
+
+export function getMonitorList(tb: Tinybird) {
+ return tb.buildPipe({
+ pipe: "monitor_list__v0",
+ parameters: tbParameterMonitorList,
+ data: tbBuildMonitorList,
+ opts: {
+ revalidate: 60,
},
});
}
diff --git a/packages/tinybird/src/validation.ts b/packages/tinybird/src/validation.ts
index 32eea55f9..47e63bd3e 100644
--- a/packages/tinybird/src/validation.ts
+++ b/packages/tinybird/src/validation.ts
@@ -24,7 +24,7 @@ export const availableRegions = [
] as const;
/**
- * Values for the datasource ping_response_v2
+ * Values for the datasource ping_response__v2
*/
export const tbIngestPingResponse = z.object({
id: z.string(),
@@ -45,7 +45,7 @@ export const tbIngestPingResponse = z.object({
});
/**
- * Values from the pip response_list_v1
+ * Values from the pip response_list__v1
*/
export const tbBuildResponseList = z.object({
id: z.string(),
@@ -65,7 +65,7 @@ export const tbBuildResponseList = z.object({
});
/**
- * Params for pipe response_list_v1
+ * Params for pipe response_list__v1
*/
export const tbParameterResponseList = z.object({
siteId: z.string().optional().default("openstatus"), // REMINDER: remove default once alpha
@@ -74,8 +74,30 @@ export const tbParameterResponseList = z.object({
end: z.number().int().optional(),
limit: z.number().int().optional().default(100), // used for pagination
region: z.enum(availableRegions).optional(),
+ cronTimestamp: z.number().int().optional(),
+});
+
+/**
+ * Params for pipe monitor_list__v0
+ */
+export const tbParameterMonitorList = z.object({
+ siteId: z.string().optional().default("openstatus"), // REMINDER: remove default once alpha
+ limit: z.number().int().optional().default(100), // used for pagination
+ cronTimestamp: z.number().int().optional(),
+});
+
+/**
+ * Values from the pip monitor_list__v0
+ */
+export const tbBuildMonitorList = z.object({
+ count: z.number().int(),
+ ok: z.number().int(),
+ avgLatency: z.number().int(), // in ms
+ cronTimestamp: z.number().int(),
});
export type Ping = z.infer<typeof tbIngestPingResponse>;
export type Region = (typeof availableRegions)[number];
+export type Monitor = z.infer<typeof tbBuildMonitorList>;
export type ResponseListParams = z.infer<typeof tbParameterResponseList>;
+export type MonitorListParams = z.infer<typeof tbParameterMonitorList>;