Skip to content

Open-source status page, incident response and uptime monitoring

🚀 More query rum (#795)

Thibault Le Ouayef737fff8654
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/route-table.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/route-table.tsx
new file mode 100644
index 000000000..493636822
--- /dev/null
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/route-table.tsx
@@ -0,0 +1,68 @@
+import {
+ Table,
+ TableBody,
+ TableCaption,
+ TableCell,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from "@openstatus/ui";
+
+import { api } from "@/trpc/server";
+
+const RouteTable = async () => {
+ const data = await api.rumRouter.GetAggregatedPerPage.query();
+ if (!data) {
+ return null;
+ }
+ return (
+ <div className=" ">
+ <h2 className="text-lg font-semibold">Page Performance</h2>
+ <div className=" ">
+ <Table>
+ <TableCaption>An overview of your page performance.</TableCaption>
+ <TableHeader>
+ <TableRow className="sticky top-0">
+ <TableHead className="max-w-6 w-4">Page</TableHead>
+ <TableHead>Total Events</TableHead>
+ <TableHead>CLS</TableHead>
+ <TableHead>FCP</TableHead>
+ <TableHead>INP</TableHead>
+ <TableHead>LCP</TableHead>
+ <TableHead>TTFB</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {data.map((page) => {
+ return (
+ <TableRow key={`${page.href}`}>
+ <TableCell className="max-w-6 w-2 truncate font-medium">
+ {page.href}
+ </TableCell>
+ <TableCell>{page.total_event}</TableCell>
+ <TableCell className="">
+ {page.clsValue?.toFixed(2)}
+ </TableCell>
+ <TableCell className="">
+ {page.fcpValue ? (page.fcpValue / 1000).toFixed(2) : "-"}
+ </TableCell>
+ <TableCell className="">
+ {page.inpValue ? (page.inpValue / 1000).toFixed(2) : "-"}
+ </TableCell>
+ <TableCell className="">
+ {page.lcpValue ? (page.lcpValue / 1000).toFixed(2) : "-"}
+ </TableCell>
+ <TableCell className="">
+ {page.ttfbValue ? (page.ttfbValue / 1000).toFixed(2) : "-"}
+ </TableCell>
+ </TableRow>
+ );
+ })}
+ </TableBody>
+ </Table>
+ </div>
+ </div>
+ );
+};
+
+export { RouteTable };
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/rum-metric-card.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/rum-metric-card.tsx
index f7e4d8f5b..118b4ee3e 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/rum-metric-card.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/_components/rum-metric-card.tsx
@@ -22,7 +22,7 @@ export const RUMMetricCard = async ({ event }: { event: WebVitalEvents }) => {
{eventConfig.label} ({event})
</p>
<p className="text-foreground text-3xl font-semibold">
- {data?.median || 0}
+ {data?.median.toFixed(2) || 0}
</p>
<CategoryBar
values={prepareWebVitalValues(eventConfig.values)}
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/page.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/page.tsx
index bff1dab6f..a0fc92655 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/page.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/rum/page.tsx
@@ -7,13 +7,13 @@ import { Button } from "@openstatus/ui";
import { EmptyState } from "@/components/dashboard/empty-state";
import { api } from "@/trpc/server";
+import { RouteTable } from "./_components/route-table";
import { RUMMetricCard } from "./_components/rum-metric-card";
export const dynamic = "force-dynamic";
export default async function RUMPage() {
const workspace = await api.workspace.getWorkspace.query();
-
if (!workspace) {
return notFound();
}
@@ -39,10 +39,18 @@ export default async function RUMPage() {
}
return (
- <div className="grid grid-cols-1 gap-2 md:grid-cols-2 lg:grid-cols-4">
- {webVitalEvents.map((event) => (
- <RUMMetricCard key={event} event={event} />
- ))}
- </div>
+ <>
+ <div className="grid grid-cols-1 gap-2 md:grid-cols-2 lg:grid-cols-5">
+ {webVitalEvents
+ // Remove FID from the list of events because it's deprecated by google
+ .filter((v) => v !== "FID")
+ .map((event) => (
+ <RUMMetricCard key={event} event={event} />
+ ))}
+ </div>
+ <div>
+ <RouteTable />
+ </div>
+ </>
);
}
diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx
index e74c809cf..0b987e3d9 100644
--- a/apps/web/src/app/layout.tsx
+++ b/apps/web/src/app/layout.tsx
@@ -42,7 +42,10 @@ export default function RootLayout({
return (
<html lang="en">
<body className={`${inter.className} ${calSans.variable}`}>
- <OpenStatusProvider dsn="openstatus" />
+ {/* Only include RUM in prod */}
+ {process.env.NODE_ENV === "production" && (
+ <OpenStatusProvider dsn="openstatus" />
+ )}
<ThemeProvider attribute="class" defaultTheme="light" enableSystem>
<Background>{children}</Background>
<Toaster richColors />
diff --git a/packages/api/src/router/rum/index.ts b/packages/api/src/router/rum/index.ts
index aa47cf16f..fa383d133 100644
--- a/packages/api/src/router/rum/index.ts
+++ b/packages/api/src/router/rum/index.ts
@@ -4,6 +4,16 @@ import { createTRPCRouter, protectedProcedure } from "../../trpc";
const event = z.enum(["CLS", "FCP", "FID", "INP", "LCP", "TTFB"]);
+const RouteData = z.object({
+ href: z.string(),
+ total_event: z.coerce.number(),
+ clsValue: z.number().optional(),
+ fcpValue: z.number().optional(),
+ inpValue: z.number().optional(),
+ lcpValue: z.number().optional(),
+ ttfbValue: z.number().optional(),
+});
+
export const rumRouter = createTRPCRouter({
GetEventMetricsForWorkspace: protectedProcedure
.input(
@@ -39,4 +49,76 @@ export const rumRouter = createTRPCRouter({
return null;
}
}),
+
+ GetAggregatedPerPage: protectedProcedure.query(async (opts) => {
+ const data = await opts.ctx.clickhouseClient.query({
+ query: `
+ select
+ count(*) as total_event,
+ href
+ from
+ cwv
+ where
+ dsn = '${opts.ctx.workspace.dsn}'
+ group by
+ href
+ order by
+ total_event desc
+ limit
+ 20
+ `,
+ format: "JSONEachRow",
+ });
+ const result = await data.json();
+ const schema = z.array(
+ z.object({ href: z.string(), total_event: z.coerce.number() }),
+ );
+ if (!result) {
+ return null;
+ }
+ const totalRoute = schema.parse(result);
+
+ const allData = [];
+ for (const currentRoute of totalRoute) {
+ const pageData = await opts.ctx.clickhouseClient.query({
+ query: `
+ select
+ quantile(0.5)(value) as value,
+ event_name
+ from
+ cwv
+ where
+ dsn = '${opts.ctx.workspace.dsn}'
+ and href = '${currentRoute.href}'
+ group by
+ event_name
+ `,
+ format: "JSONEachRow",
+ });
+ const result = await pageData.json();
+
+ if (!result) {
+ return null;
+ }
+ const schema = z.array(
+ z.object({ event_name: event, value: z.number() }),
+ );
+ const d = schema.parse(result);
+ const r = d.reduce((acc, curr) => {
+ acc = {
+ ...acc,
+ [`${String(curr.event_name).toLowerCase()}Value`]: curr.value,
+ };
+
+ return acc;
+ });
+ allData.push({
+ ...currentRoute,
+ ...r,
+ });
+ }
+ // console.log(allData);
+ // return;
+ return z.array(RouteData).parse(allData);
+ }),
});