File size: 7,438 Bytes
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { useMemo } from "react"
import { Inbox } from "lucide-react"
import { useDashboard } from "@/lib/dashboard"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { Badge } from "@/components/ui/badge"

// deterministic label → hue (matches the spirit of the backend's md5 colouring)
function hueFor(label: string): number {
  let h = 0
  for (let i = 0; i < label.length; i++) h = (h * 31 + label.charCodeAt(i)) % 360
  return h
}

function StatusBadge({ status }: { status: string }) {
  const variant = status.includes("fail") || status.includes("missing")
    ? "danger"
    : status.includes("posted")
      ? "info"
      : status.includes("block")
        ? "warning"
        : "live"
  return <Badge variant={variant}>{status}</Badge>
}

export function ActivityPanel() {
  const { result } = useDashboard()
  const detections = result?.detections ?? []
  const events = result?.events ?? []

  const maxT = useMemo(
    () =>
      Math.max(
        0.001,
        ...detections.map((d) => d.timestamp_sec),
        ...events.map((e) => e.timestamp_sec),
      ),
    [detections, events],
  )

  const labels = useMemo(
    () => Array.from(new Set(detections.map((d) => d.label))),
    [detections],
  )

  if (!result) {
    return (
      <div className="flex flex-col items-center gap-2 py-10 text-center text-muted-foreground">
        <Inbox className="size-7 opacity-40" />
        <p className="text-sm">Run detection to populate the activity timeline.</p>
      </div>
    )
  }

  return (
    <div className="space-y-5">
      {/* timeline track */}
      <div className="space-y-2">
        <div className="flex items-center justify-between font-mono text-[0.625rem] uppercase tracking-wider text-muted-foreground">
          <span>0.0s</span>
          <span>detection timeline</span>
          <span>{maxT.toFixed(1)}s</span>
        </div>
        <div className="relative h-16 overflow-hidden rounded-lg border border-border bg-black/30">
          {/* gridlines */}
          {[0.25, 0.5, 0.75].map((f) => (
            <div
              key={f}
              className="absolute inset-y-0 w-px bg-white/5"
              style={{ left: `${f * 100}%` }}
            />
          ))}
          {/* detection ticks */}
          {detections.map((d, i) => (
            <span
              key={i}
              className="absolute top-1/2 size-1.5 -translate-y-1/2 rounded-full"
              style={{
                left: `${(d.timestamp_sec / maxT) * 100}%`,
                backgroundColor: `oklch(0.75 0.13 ${hueFor(d.label)})`,
                opacity: 0.25 + d.confidence * 0.75,
              }}
            />
          ))}
          {/* event markers */}
          {events.map((e, i) => (
            <div
              key={i}
              className="absolute inset-y-0 w-0.5 bg-primary"
              style={{ left: `${(e.timestamp_sec / maxT) * 100}%` }}
            >
              <span className="absolute -top-0 left-1/2 size-2 -translate-x-1/2 rounded-full bg-primary signal-dot" />
            </div>
          ))}
        </div>
        {/* legend */}
        <div className="flex flex-wrap gap-x-4 gap-y-1">
          {labels.map((l) => (
            <div key={l} className="flex items-center gap-1.5 text-[0.6875rem] text-muted-foreground">
              <span
                className="size-2 rounded-full"
                style={{ backgroundColor: `oklch(0.75 0.13 ${hueFor(l)})` }}
              />
              {l}
            </div>
          ))}
        </div>
      </div>

      {/* tables */}
      <Tabs defaultValue="actions">
        <TabsList>
          <TabsTrigger value="actions">
            Actions
            <span className="ml-1 font-mono text-[0.625rem] text-muted-foreground">
              {events.length}
            </span>
          </TabsTrigger>
          <TabsTrigger value="detections">
            Detections
            <span className="ml-1 font-mono text-[0.625rem] text-muted-foreground">
              {detections.length}
            </span>
          </TabsTrigger>
        </TabsList>

        <TabsContent value="actions">
          {events.length ? (
            <Table>
              <TableHeader>
                <TableRow>
                  <TableHead>Time</TableHead>
                  <TableHead>Rule</TableHead>
                  <TableHead>Action</TableHead>
                  <TableHead>Status</TableHead>
                  <TableHead className="text-right">Frame</TableHead>
                </TableRow>
              </TableHeader>
              <TableBody>
                {events.map((e, i) => (
                  <TableRow key={i}>
                    <TableCell className="font-mono text-xs tabular-nums text-muted-foreground">
                      {e.timestamp_sec.toFixed(1)}s
                    </TableCell>
                    <TableCell className="font-mono text-xs">{e.rule}</TableCell>
                    <TableCell className="text-xs">{e.action}</TableCell>
                    <TableCell>
                      <StatusBadge status={e.status} />
                    </TableCell>
                    <TableCell className="text-right font-mono text-xs tabular-nums text-muted-foreground">
                      {e.frame_index}
                    </TableCell>
                  </TableRow>
                ))}
              </TableBody>
            </Table>
          ) : (
            <p className="py-6 text-center text-sm text-muted-foreground">
              No actions fired this run.
            </p>
          )}
        </TabsContent>

        <TabsContent value="detections">
          <div className="max-h-72 overflow-y-auto">
            <Table>
              <TableHeader>
                <TableRow>
                  <TableHead className="text-right">Frame</TableHead>
                  <TableHead>Label</TableHead>
                  <TableHead className="text-right">Conf.</TableHead>
                  <TableHead className="text-right">Time</TableHead>
                </TableRow>
              </TableHeader>
              <TableBody>
                {detections.slice(0, 200).map((d, i) => (
                  <TableRow key={i}>
                    <TableCell className="text-right font-mono text-xs tabular-nums text-muted-foreground">
                      {d.frame_index}
                    </TableCell>
                    <TableCell>
                      <span className="flex items-center gap-1.5 text-xs">
                        <span
                          className="size-2 rounded-full"
                          style={{ backgroundColor: `oklch(0.75 0.13 ${hueFor(d.label)})` }}
                        />
                        {d.label}
                      </span>
                    </TableCell>
                    <TableCell className="text-right font-mono text-xs tabular-nums">
                      {d.confidence.toFixed(2)}
                    </TableCell>
                    <TableCell className="text-right font-mono text-xs tabular-nums text-muted-foreground">
                      {d.timestamp_sec.toFixed(1)}s
                    </TableCell>
                  </TableRow>
                ))}
              </TableBody>
            </Table>
          </div>
        </TabsContent>
      </Tabs>
    </div>
  )
}