Skip to content

Open-source status page, incident response and uptime monitoring

2.8 KB
Raw Blame
import type { AgentToolOutput } from "@openstatus/services/agent-tools";
import { cn } from "@openstatus/ui/lib/utils";

import { HoverCardTiming } from "@/components/common/hover-card-timing";
import { TableCellDate } from "@/components/data-table/table-cell-date";
import { TableCellEmpty } from "@/components/data-table/table-cell-empty";
import { TableCellNumber } from "@/components/data-table/table-cell-number";
import { TableCellRegion } from "@/components/data-table/table-cell-region";
import { TableCellText } from "@/components/data-table/table-cell-text";
import { getStatusCodeVariant, textColors } from "@/data/status-codes";

import type { ResultTableData } from "./result-table";

type Output = AgentToolOutput<"list_response_logs">;
type Status = NonNullable<Output["logs"][number]["requestStatus"]>;

const statusColor: Record<Status, string> = {
  success: "bg-success",
  degraded: "bg-warning",
  error: "bg-destructive",
};

export function listResponseLogsTable(
  output: Output,
): ResultTableData<
  "indicator" | "timestamp" | "region" | "status" | "latency" | "timing" | "id"
> {
  const logs = output?.logs ?? [];
  return {
    empty: "No response logs in this window.",
    columns: [
      { key: "indicator", header: "" },
      { key: "timestamp", header: "Timestamp" },
      { key: "region", header: "Region" },
      { key: "status", header: "Status" },
      { key: "latency", header: "Latency" },
      { key: "timing", header: "Timing" },
      { key: "id", header: "ID" },
    ],
    rows: logs.map((log, idx) => ({
      id: log.id ?? `row-${idx}`,
      cells: {
        indicator: log.requestStatus ? (
          <div
            className={cn(
              "h-2.5 w-2.5 rounded-xs",
              statusColor[log.requestStatus],
            )}
          />
        ) : (
          <TableCellEmpty>Unknown</TableCellEmpty>
        ),
        timestamp: (
          <TableCellDate
            value={new Date(log.timestamp)}
            className="text-foreground font-mono"
          />
        ),
        region: <TableCellRegion value={log.region} className="font-mono" />,
        status:
          log.statusCode != null ? (
            <TableCellNumber
              value={log.statusCode}
              className={textColors[getStatusCodeVariant(log.statusCode)]}
            />
          ) : (
            <TableCellEmpty>No status</TableCellEmpty>
          ),
        latency: <TableCellNumber value={log.latency} unit="ms" />,
        timing: log.timing ? (
          <HoverCardTiming timing={log.timing} latency={log.latency} />
        ) : (
          <TableCellEmpty>No timing</TableCellEmpty>
        ),
        id: (
          <TableCellText
            value={log.id}
            empty="No ID"
            className="text-muted-foreground font-mono"
          />
        ),
      },
    })),
  };
}