import { j as jsxRuntimeExports, d as c, c as a, s, h as m, bw as o, bb as f, L as Link, n as namedRoutes, w as h, bu as ForwardRef, e as gql, ba as useQuery } from "./index-Cr4zbAtA.js";
import { M as Metadata } from "./Metadata-aK5zM6ek.js";
const StatusBadge = ({
  statusCode
}) => {
  if (statusCode === 0) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(f, { color: "gray", children: "Unset" });
  }
  if (statusCode === 1) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(f, { color: "green", children: "OK" });
  }
  if (statusCode === 2) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(f, { color: "red", children: "Error" });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(f, { color: "orange", children: "Invalid" });
};
const TraceListItem = ({
  id,
  spans
}) => {
  let startTimeNano = BigInt(Number.MAX_VALUE);
  let endTimeNano = BigInt(0);
  let rootSpan = null;
  let statusCode = 0;
  const types = /* @__PURE__ */ new Map();
  for (const span of spans) {
    const start = BigInt(span.startTimeNano);
    const end = BigInt(span.endTimeNano);
    if (start < startTimeNano) {
      startTimeNano = start;
      rootSpan = span;
    }
    if (end > endTimeNano) {
      endTimeNano = end;
    }
    if (span.statusCode > statusCode) {
      statusCode = span.statusCode;
    }
    const type = types.get(span.type.name);
    if (type) {
      type.count++;
    } else {
      types.set(span.type.name, {
        name: span.type.name,
        colour: span.type.colour,
        count: 1
      });
    }
  }
  const duration = Number((endTimeNano - startTimeNano) / 1000n) / 1e3;
  const name = rootSpan.name ?? "Unknown";
  const brief = rootSpan.brief ?? null;
  const typesArray = Array.from(types.values()).sort((a2, b) => a2.name.localeCompare(b.name));
  return /* @__PURE__ */ jsxRuntimeExports.jsx(c, { className: "w-full", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", justifyContent: "start", className: "min-w-0 gap-4", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "min-w-0 flex-grow", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "col", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", justifyContent: "between", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "col", alignItems: "start", className: "min-w-0 flex-grow", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(s, { children: name }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", justifyContent: "start", className: "gap-2", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: id }),
            brief ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "|" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(m, { className: "truncate", children: brief })
            ] }) : null
          ] })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(StatusBadge, { statusCode }) })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(o, { className: "my-2" }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", justifyContent: "between", className: "min-w-0", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "flex-grow", children: /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "row", justifyContent: "start", alignItems: "start", className: "min-w-0 gap-2 overflow-x-scroll", children: typesArray.map((type) => /* @__PURE__ */ jsxRuntimeExports.jsxs(f, { style: {
          backgroundColor: `#${type.colour}`,
          color: "#fff"
        }, children: [
          type.count,
          "x ",
          type.name
        ] }, type.name)) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "row", justifyContent: "end", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(m, { children: [
          new Date(Number(startTimeNano / 1000000n)).toLocaleString(),
          " | ",
          duration.toFixed(2),
          " ms"
        ] }) })
      ] })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "col", justifyContent: "start", alignItems: "start", className: "gap-2", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Link, { to: namedRoutes.opentelemetryTrace({
      id
    }), children: /* @__PURE__ */ jsxRuntimeExports.jsx(h, { className: "p-2", children: /* @__PURE__ */ jsxRuntimeExports.jsx(ForwardRef, { className: "h-6 w-6 shrink-0", "aria-hidden": "true" }) }) }) }) })
  ] }) });
};
const TRACES_QUERY = gql`
  query GetTraces @live {
    traces: otelTraces {
      id
      spans {
        id
        name
        brief
        startTimeNano
        endTimeNano
        statusCode
        type {
          name
          colour
        }
      }
    }
  }
`;
const OpenTelemetryTracesPage = () => {
  var _a;
  const tracesQuery = useQuery(TRACES_QUERY);
  const traces = ((_a = tracesQuery.data) == null ? void 0 : _a.traces) ?? [];
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Metadata, { title: "OpenTelemetryTraces", description: "OpenTelemetryTraces page" }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "row", justifyContent: "between", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(s, { children: "OpenTelemetry Traces" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Each individual OpenTelemetry trace ingested." })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { className: "mt-6 space-y-6", flexDirection: "col", children: [
      traces.length === 0 && /* @__PURE__ */ jsxRuntimeExports.jsx(c, { className: "w-full", children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "No traces found" }) }),
      traces.map((trace) => /* @__PURE__ */ jsxRuntimeExports.jsx(TraceListItem, { id: trace.id, spans: trace.spans }, trace.id))
    ] })
  ] });
};
export {
  OpenTelemetryTracesPage as default
};
