Skip to content

Open-source status page, incident response and uptime monitoring

feat: speed checker data export (#1740)

Maximilian Kaskeb273f41d6e9f
diff --git a/apps/web/src/app/(landing)/play/checker/[slug]/client.tsx b/apps/web/src/app/(landing)/play/checker/[slug]/client.tsx
index 31224df2d..a8a5d6176 100644
--- a/apps/web/src/app/(landing)/play/checker/[slug]/client.tsx
+++ b/apps/web/src/app/(landing)/play/checker/[slug]/client.tsx
@@ -21,6 +21,7 @@ import {
import { Input } from "@openstatus/ui";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@openstatus/ui";
import { useState } from "react";
+import { handleExportCSV } from "../utils";
const STATUS_CODES = {
"1": "text-muted-foreground",
@@ -78,12 +79,21 @@ export function Table({ data }: TableProps) {
return (
<div>
- <Input
- value={input}
- onChange={(e) => setInput(e.target.value)}
- placeholder="Search by region, flag, location code, cloud provider or continent"
- className="h-auto! rounded-none p-4 text-base md:text-base"
- />
+ <div className="flex flex-col gap-2 sm:flex-row">
+ <Input
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
+ placeholder="Search by region, flag, location code, cloud provider or continent"
+ className="h-auto! flex-1 rounded-none p-4 text-base md:text-base"
+ />
+ <Button
+ variant="outline"
+ className="h-auto! rounded-none p-4 text-base"
+ onClick={() => handleExportCSV(checks, data.url)}
+ >
+ Export to CSV
+ </Button>
+ </div>
<div className="table-wrapper">
<table>
<thead>
diff --git a/apps/web/src/app/(landing)/play/checker/client.tsx b/apps/web/src/app/(landing)/play/checker/client.tsx
index d57c313c1..77236216e 100644
--- a/apps/web/src/app/(landing)/play/checker/client.tsx
+++ b/apps/web/src/app/(landing)/play/checker/client.tsx
@@ -2,6 +2,7 @@
import { IconCloudProvider } from "@/components/icon-cloud-provider";
import {
+ type Timing,
is32CharHex,
latencyFormatter,
regionCheckerSchema,
@@ -34,8 +35,14 @@ import {
useTransition,
} from "react";
import { searchParamsParsers } from "./search-params";
+import { handleExportCSV } from "./utils";
-type Values = { region: string; latency: number; status: number };
+type Values = {
+ region: string;
+ latency: number;
+ status: number;
+ timing: Timing;
+};
type CheckerContextType = {
values: Values[];
@@ -188,6 +195,7 @@ export function Form({
region: check.region,
latency: check.latency,
status: check.status,
+ timing: check.timing,
};
}
return null;
@@ -395,3 +403,21 @@ export function DetailsButtonLink() {
</Button>
);
}
+
+export function ExportToCSVButton() {
+ const { values } = useCheckerContext();
+
+ if (values.length === 0) {
+ return null;
+ }
+
+ return (
+ <Button
+ variant="outline"
+ className="h-full w-full rounded-none p-4 text-base"
+ onClick={() => handleExportCSV(values)}
+ >
+ Export to CSV
+ </Button>
+ );
+}
diff --git a/apps/web/src/app/(landing)/play/checker/page.tsx b/apps/web/src/app/(landing)/play/checker/page.tsx
index 52941e2bc..172e77dbb 100644
--- a/apps/web/src/app/(landing)/play/checker/page.tsx
+++ b/apps/web/src/app/(landing)/play/checker/page.tsx
@@ -7,6 +7,7 @@ import { mockCheckAllRegions } from "./api/mock";
import {
CheckerProvider,
DetailsButtonLink,
+ ExportToCSVButton,
Form,
ResponseStatus,
ResultTable,
@@ -42,7 +43,10 @@ export default async function Page(props: {
<Form defaultMethod={data?.method} defaultUrl={data?.url} />
<ResponseStatus />
<ResultTable />
- <DetailsButtonLink />
+ <div className="flex gap-2">
+ <ExportToCSVButton />
+ <DetailsButtonLink />
+ </div>
</CheckerProvider>
<CustomMDX source={page.content} />
</section>
diff --git a/apps/web/src/app/(landing)/play/checker/utils.ts b/apps/web/src/app/(landing)/play/checker/utils.ts
new file mode 100644
index 000000000..43032e228
--- /dev/null
+++ b/apps/web/src/app/(landing)/play/checker/utils.ts
@@ -0,0 +1,86 @@
+import {
+ type Timing,
+ getTimingPhases,
+} from "@/components/ping-response-analysis/utils";
+import { toast } from "@/lib/toast";
+import { type Region, regionDict } from "@openstatus/regions";
+
+export type CheckerRow = {
+ region: string;
+ latency: number;
+ status: number;
+ timing: Timing;
+};
+
+function escapeCSV(value: string): string {
+ // Escape values that contain commas, quotes, or newlines by wrapping in quotes
+ if (value.includes(",") || value.includes('"') || value.includes("\n")) {
+ return `"${value.replace(/"/g, '""')}"`;
+ }
+ return value;
+}
+
+export function convertToCSV(rows: CheckerRow[]): string {
+ const headers = [
+ "Region Code",
+ "Location",
+ "Provider",
+ "Latency (ms)",
+ "Status",
+ "DNS (ms)",
+ "Connect (ms)",
+ "TLS (ms)",
+ "TTFB (ms)",
+ "Transfer (ms)",
+ ];
+ const csvRows = rows.map((row) => {
+ const regionConfig = regionDict[row.region as Region];
+ const timing = getTimingPhases(row.timing);
+ return [
+ escapeCSV(regionConfig.code),
+ escapeCSV(regionConfig.location),
+ escapeCSV(regionConfig.provider),
+ row.latency.toString(),
+ row.status.toString(),
+ timing?.dns.toString() ?? "",
+ timing?.connection.toString() ?? "",
+ timing?.tls.toString() ?? "",
+ timing?.ttfb.toString() ?? "",
+ timing?.transfer.toString() ?? "",
+ ].join(",");
+ });
+ return [headers.join(","), ...csvRows].join("\n");
+}
+
+export function downloadCSV(csv: string, filename: string): void {
+ const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
+ const url = URL.createObjectURL(blob);
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = filename;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ URL.revokeObjectURL(url);
+ toast.success("CSV exported successfully");
+}
+
+export function handleExportCSV(
+ rows: CheckerRow[],
+ urlForFilename?: string,
+): void {
+ const csv = convertToCSV(rows);
+ const datePart = new Date().toISOString().split("T")[0];
+
+ let filename: string;
+ if (urlForFilename) {
+ const sanitizedUrl = urlForFilename
+ .replace(/^https?:\/\//, "")
+ .replace(/[^a-zA-Z0-9.-]/g, "_");
+ filename = `checker-${sanitizedUrl}-${datePart}.csv`;
+ } else {
+ filename = `checker-results-${datePart}.csv`;
+ }
+
+ downloadCSV(csv, filename);
+}