Spaces:
Running
Running
Download web/src/components/analytics/analytics-view.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 6.88 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/analytics/analytics-view.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/analytics/analytics-view.tsx
-
curl -L -o analytics-view.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/analytics/analytics-view.tsx
6.88 kB
| "use client"; | |
| import { useState } from "react"; | |
| import { OperationsHealth } from "@/components/analytics/operations-health"; | |
| import { RunStatusBadge } from "@/components/runs/status-badge"; | |
| import { ErrorState, PageHeader, StatTile } from "@/components/shell/page"; | |
| import { Skeleton } from "@/components/ui/skeleton"; | |
| import { formatDuration, formatNumber, formatPct } from "@/lib/format"; | |
| import { useI18n } from "@/lib/i18n"; | |
| import type { AnalyticsSummary } from "@/lib/types"; | |
| import { useApi } from "@/lib/use-api"; | |
| const STATUS_ORDER = ["completed", "waiting_approval", "running", "rejected", "failed", "unsupported"]; | |
| const STATUS_COLOR: Record<string, string> = { | |
| completed: "var(--chart-1)", | |
| waiting_approval: "var(--chart-2)", | |
| running: "var(--chart-4)", | |
| rejected: "var(--chart-3)", | |
| failed: "var(--destructive)", | |
| unsupported: "var(--chart-5)", | |
| }; | |
| function Bars({ entries }: { entries: [string, number][] }) { | |
| const max = Math.max(1, ...entries.map(([, n]) => n)); | |
| return ( | |
| <ul className="space-y-2"> | |
| {entries.map(([k, n]) => ( | |
| <li key={k} className="grid grid-cols-[9rem_1fr_3rem] items-center gap-3 text-sm"> | |
| <RunStatusBadge status={k} className="justify-self-start" /> | |
| <div className="h-2 rounded-full bg-muted"> | |
| <div | |
| className="h-full rounded-full" | |
| style={{ width: `${(n / max) * 100}%`, background: STATUS_COLOR[k] ?? "var(--chart-5)" }} | |
| /> | |
| </div> | |
| <span className="tabular text-end text-muted-foreground">{formatNumber(n)}</span> | |
| </li> | |
| ))} | |
| </ul> | |
| ); | |
| } | |
| function DailyChart({ daily }: { daily: AnalyticsSummary["daily"] }) { | |
| const { t } = useI18n(); | |
| const totals = daily.map((d) => | |
| Object.entries(d).reduce((s, [k, v]) => (k === "date" ? s : s + Number(v)), 0), | |
| ); | |
| const max = Math.max(1, ...totals); | |
| if (!daily.length) return <p className="text-sm text-muted-foreground">—</p>; | |
| return ( | |
| <div className="flex h-40 items-end gap-1" role="img" aria-label={t("analytics.daily")} dir="ltr"> | |
| {daily.map((d, i) => ( | |
| <div key={d.date} className="group flex h-full flex-1 flex-col justify-end" title={`${d.date}: ${totals[i]}`}> | |
| <div className="flex flex-col-reverse overflow-hidden rounded-t-sm" style={{ height: `${(totals[i] / max) * 100}%` }}> | |
| {STATUS_ORDER.filter((s) => Number(d[s] ?? 0) > 0).map((s) => ( | |
| <div key={s} style={{ flexGrow: Number(d[s]), background: STATUS_COLOR[s] }} /> | |
| ))} | |
| </div> | |
| <span className="mt-1 truncate text-center text-[10px] text-muted-foreground">{d.date.slice(5)}</span> | |
| </div> | |
| ))} | |
| </div> | |
| ); | |
| } | |
| export function AnalyticsView() { | |
| const { t } = useI18n(); | |
| const [days, setDays] = useState(30); | |
| const a = useApi<AnalyticsSummary>("/analytics/summary", { query: { days }, pollMs: 60_000 }); | |
| const d = a.data; | |
| return ( | |
| <div className="space-y-5"> | |
| <PageHeader | |
| title={t("analytics.title")} | |
| subtitle={t("analytics.subtitle")} | |
| actions={ | |
| <select | |
| aria-label={t("analytics.period", { n: days })} | |
| className="h-8 rounded-md border bg-background px-2 text-sm" | |
| value={days} | |
| onChange={(e) => setDays(Number(e.target.value))} | |
| > | |
| {[7, 30, 90].map((n) => ( | |
| <option key={n} value={n}> | |
| {t("analytics.period", { n })} | |
| </option> | |
| ))} | |
| </select> | |
| } | |
| /> | |
| {a.error && !d ? ( | |
| <ErrorState error={a.error} onRetry={a.reload} /> | |
| ) : !d ? ( | |
| <Skeleton className="h-64 w-full" /> | |
| ) : ( | |
| <> | |
| <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4" data-testid="analytics-tiles"> | |
| <StatTile label={t("analytics.runs")} value={formatNumber(d.runs.total)} /> | |
| <StatTile | |
| label={t("analytics.autoResolution")} | |
| value={formatPct(d.auto_resolution_rate)} | |
| hint={t("analytics.autoResolutionHint")} | |
| /> | |
| <StatTile | |
| label={t("analytics.approvalRate")} | |
| value={formatPct(d.approvals.approval_rate)} | |
| hint={`${d.approvals.approved} / ${d.approvals.approved + d.approvals.rejected}`} | |
| /> | |
| <StatTile | |
| label={t("analytics.latency")} | |
| value={d.median_approval_latency_s === null ? "—" : formatDuration(d.median_approval_latency_s)} | |
| /> | |
| <StatTile label={t("analytics.toolCalls")} value={formatNumber(d.tool_calls.total)} /> | |
| <StatTile | |
| label={t("analytics.cost")} | |
| value={`$${d.cost_usd.toFixed(2)}`} | |
| hint={d.cost_per_run_usd !== null ? `$${d.cost_per_run_usd.toFixed(3)} ${t("analytics.costPerRun")}` : undefined} | |
| /> | |
| <StatTile label={t("analytics.approvals")} value={formatNumber(d.approvals.pending)} hint={t("runs.statuses.pending")} /> | |
| </div> | |
| <div className="grid gap-4 lg:grid-cols-2"> | |
| <section className="rounded-lg border bg-card p-4"> | |
| <h2 className="mb-3 text-sm font-semibold">{t("analytics.daily")}</h2> | |
| <DailyChart daily={d.daily} /> | |
| </section> | |
| <section className="rounded-lg border bg-card p-4"> | |
| <h2 className="mb-3 text-sm font-semibold">{t("analytics.runsByStatus")}</h2> | |
| <Bars entries={Object.entries(d.runs.by_status).sort((x, y) => y[1] - x[1])} /> | |
| </section> | |
| <section className="rounded-lg border bg-card p-4"> | |
| <h2 className="mb-3 text-sm font-semibold">{t("analytics.decisions")}</h2> | |
| <Bars entries={Object.entries(d.tool_calls.by_decision).sort((x, y) => y[1] - x[1])} /> | |
| </section> | |
| <section className="rounded-lg border bg-card p-4"> | |
| <h2 className="mb-3 text-sm font-semibold">{t("analytics.denials")}</h2> | |
| {d.top_denial_reasons.length === 0 ? ( | |
| <p className="text-sm text-muted-foreground">{t("analytics.noDenials")}</p> | |
| ) : ( | |
| <ol className="space-y-2 text-sm"> | |
| {d.top_denial_reasons.map((r) => ( | |
| <li key={r.reason} className="flex gap-3"> | |
| <span className="tabular w-8 shrink-0 text-end font-medium">{r.count}</span> | |
| <span className="text-muted-foreground">{r.reason}</span> | |
| </li> | |
| ))} | |
| </ol> | |
| )} | |
| </section> | |
| </div> | |
| </> | |
| )} | |
| <OperationsHealth days={days} /> | |
| </div> | |
| ); | |
| } | |