Skip to content

Open-source status page, incident response and uptime monitoring

4.0 KB
Raw Blame
import type {
  AgentToolInput,
  AgentToolOutput,
} from "@openstatus/services/agent-tools";

import { EntityRef } from "@/components/common/entity-ref/entity-ref";
import { TableCellBoolean } from "@/components/data-table/table-cell-boolean";
import { TableCellDate } from "@/components/data-table/table-cell-date";
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 { toEntityId } from "@/data/entity-refs.client";

import type {
  DetailsRow,
  DetailsSection,
  DetailsTableData,
} from "./details-table";

type Input = AgentToolInput<"get_response_log">;
type Output = AgentToolOutput<"get_response_log">;
type Timing = NonNullable<Output["timing"]>;

function MonitorRef({ value }: { value: string }) {
  const id = toEntityId(value);
  return id ? (
    <EntityRef kind="monitor" id={id} />
  ) : (
    <TableCellText value={value} />
  );
}

export function getResponseLogDetails(
  _input: Input,
  output: Output,
): DetailsTableData {
  const request: DetailsRow[] = [
    { label: "Log ID", value: <TableCellText value={output.id} /> },
    {
      label: "Monitor",
      value: <MonitorRef value={output.monitorId} />,
    },
    { label: "Region", value: <TableCellRegion value={output.region} /> },
    { label: "URL", value: <TableCellText value={output.url} /> },
    {
      label: "Status",
      value: <TableCellText value={output.requestStatus} />,
    },
    {
      label: "Status code",
      value: <TableCellNumber value={output.statusCode} />,
    },
    {
      label: "Latency",
      value: <TableCellNumber value={output.latency} unit="ms" />,
    },
    {
      label: "Timestamp",
      value: <TableCellDate value={new Date(output.timestamp)} />,
    },
    { label: "Trigger", value: <TableCellText value={output.trigger} /> },
    { label: "Error", value: <TableCellBoolean value={output.error} /> },
  ];

  const sections: DetailsSection[] = [{ rows: request }];

  if (Object.keys(output.headers).length > 0) {
    sections.push({
      rows: [
        {
          label: "Headers",
          value: (
            <pre className="text-foreground break-all whitespace-pre-wrap">
              {JSON.stringify(output.headers, null, 2)}
            </pre>
          ),
        },
      ],
    });
  }

  if (output.timing) {
    sections.push({
      rows: timingRows(output.timing, output.latency),
    });
  }

  if (output.message) {
    sections.push({
      rows: [
        { label: "Message", value: <TableCellText value={output.message} /> },
      ],
    });
  }

  if (output.body) {
    sections.push({
      rows: [
        {
          label: "Body",
          value: (
            <pre className="text-foreground break-all whitespace-pre-wrap">
              {output.body}
            </pre>
          ),
        },
      ],
    });
  }

  if (output.assertions) {
    sections.push({
      rows: [
        {
          label: "Assertions",
          value: <TableCellText value={output.assertions} />,
        },
      ],
    });
  }

  return { sections };
}

function timingRows(timing: Timing, latency: number): DetailsRow[] {
  const denom = latency || 1;
  return Object.entries(timing).map(([key, value], index) => ({
    label: key.toUpperCase(),
    value: (
      <div className="flex items-center justify-between gap-3">
        <span className="text-muted-foreground">
          {new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 }).format(
            (value / denom) * 100,
          )}
          %
        </span>
        <div className="flex flex-1 items-center justify-end gap-1">
          <span className="text-muted-foreground text-nowrap">{value} ms</span>
          <div
            className="h-3"
            style={{
              width: `${(value / denom) * 100}%`,
              backgroundColor: `var(--chart-${index + 1})`,
            }}
          />
        </div>
      </div>
    ),
  }));
}