File size: 9,206 Bytes
5c0175e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Processing Queue view — live scan progress per step.
"use client";

import { useQuery } from "@tanstack/react-query";
import { Card } from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";
import { Badge } from "@/components/ui/badge";
import {
  Radio, Clock, CheckCircle2, AlertTriangle, ListChecks, History,
} from "lucide-react";
import { useAppStore } from "@/lib/store";
import { SourceBadge } from "@/components/shared/badges";
import type { QueueItemView } from "@/lib/types";

const STEP_LABELS: Record<string, string> = {
  metadata: "Container Metadata",
  filename: "Filename Intelligence",
  stream: "Stream Analysis",
  alpha: "Alpha Channel",
  mask: "Mask Track",
  frame: "Frame Analysis",
  depth: "Depth Detection",
  classify: "Classification",
};

export function QueueView() {
  const setView = useAppStore((s) => s.setView);
  const queueQ = useQuery({
    queryKey: ["queue"],
    queryFn: async () => {
      const res = await fetch("/api/queue");
      if (!res.ok) throw new Error("queue fetch failed");
      return res.json() as Promise<{
        active: QueueItemView[];
        recentlyDone: { id: string; videoId: string; videoName: string; finishedAt: string | null }[];
        recentErrors: { id: string; videoId: string; videoName: string; errorMessage: string | null; finishedAt: string | null }[];
        totals: { queued: number; running: number };
      }>;
    },
    refetchInterval: 1500,
  });

  const q = queueQ.data;

  return (
    <div className="space-y-4">

      {/* Summary cards */}

      <div className="grid grid-cols-2 md:grid-cols-4 gap-3">

        <Card className="glass-card p-4">

          <div className="flex items-center justify-between">

            <div>

              <div className="text-xs uppercase tracking-wider text-muted-foreground">Running</div>

              <div className="text-2xl font-bold text-cyan-300 mt-1">{q?.totals?.running ?? 0}</div>

            </div>

            <Radio className="h-5 w-5 text-cyan-300 vr-pulse" />

          </div>

        </Card>

        <Card className="glass-card p-4">

          <div className="flex items-center justify-between">

            <div>

              <div className="text-xs uppercase tracking-wider text-muted-foreground">Queued</div>

              <div className="text-2xl font-bold text-amber-300 mt-1">{q?.totals?.queued ?? 0}</div>

            </div>

            <Clock className="h-5 w-5 text-amber-300" />

          </div>

        </Card>

        <Card className="glass-card p-4">

          <div className="flex items-center justify-between">

            <div>

              <div className="text-xs uppercase tracking-wider text-muted-foreground">Recently Done</div>

              <div className="text-2xl font-bold text-emerald-300 mt-1">{q?.recentlyDone?.length ?? 0}</div>

            </div>

            <CheckCircle2 className="h-5 w-5 text-emerald-300" />

          </div>

        </Card>

        <Card className="glass-card p-4">

          <div className="flex items-center justify-between">

            <div>

              <div className="text-xs uppercase tracking-wider text-muted-foreground">Errors</div>

              <div className="text-2xl font-bold text-red-300 mt-1">{q?.recentErrors?.length ?? 0}</div>

            </div>

            <AlertTriangle className="h-5 w-5 text-red-300" />

          </div>

        </Card>

      </div>



      {/* Active queue */}

      <Card className="glass-card p-5">

        <h3 className="text-sm font-semibold mb-4 flex items-center gap-2">

          <ListChecks className="h-4 w-4 text-cyan-300" /> Active Queue

        </h3>

        {q?.active?.length === 0 ? (

          <div className="text-sm text-muted-foreground text-center py-6">

            Queue is empty. Trigger a scan from the top bar.

          </div>

        ) : (

          <div className="space-y-3">

            {q?.active?.map((item) => (

              <div key={item.id} className="p-3 rounded-lg bg-background/40 border border-border/60">

                <div className="flex items-center justify-between gap-3 mb-2">

                  <div className="min-w-0 flex-1">

                    <button

                      onClick={() => setView("inspector", { videoId: item.videoId })}

                      className="text-sm font-medium hover:text-cyan-300 truncate block text-left"

                    >

                      {item.videoName}

                    </button>

                    <div className="text-xs text-muted-foreground">

                      Started: {item.startedAt ? new Date(item.startedAt).toLocaleTimeString() : "—"}

                    </div>

                    <div className="mt-1">

                      <SourceBadge value={item.source} />

                    </div>

                  </div>

                  <Badge

                    variant="outline"

                    className={`text-xs ${

                      item.status === "running"

                        ? "bg-cyan-500/15 text-cyan-200 border-cyan-500/40"

                        : "bg-amber-500/15 text-amber-200 border-amber-500/40"

                    }`}

                  >

                    {item.status}

                  </Badge>

                </div>

                <Progress value={item.stepProgress} className="h-1.5 mb-3" />

                <div className="grid grid-cols-4 sm:grid-cols-8 gap-1">

                  {item.steps.map((step) => (

                    <div

                      key={step.step}

                      className={`flex flex-col items-center gap-1 p-1.5 rounded text-[10px] ${

                        step.status === "done"

                          ? "bg-emerald-500/10 text-emerald-300"

                          : step.status === "running"

                            ? "bg-cyan-500/15 text-cyan-200 vr-pulse"

                            : step.status === "error"

                              ? "bg-red-500/15 text-red-300"

                              : "bg-muted/30 text-muted-foreground"

                      }`}

                      title={step.label}

                    >

                      <div className="font-medium">{step.label.split(" ")[0]}</div>

                      <div className="capitalize opacity-70">{step.status}</div>

                    </div>

                  ))}

                </div>

              </div>

            ))}

          </div>

        )}

      </Card>



      {/* Recent activity */}

      <div className="grid grid-cols-1 md:grid-cols-2 gap-4">

        <Card className="glass-card p-5">

          <h3 className="text-sm font-semibold mb-3 flex items-center gap-2">

            <History className="h-4 w-4 text-emerald-300" /> Recently Completed

          </h3>

          {q?.recentlyDone?.length === 0 ? (

            <div className="text-xs text-muted-foreground py-4 text-center">No completed scans yet.</div>

          ) : (

            <div className="space-y-2 max-h-64 overflow-y-auto scrollbar-vr">

              {q?.recentlyDone?.map((item) => (

                <div key={item.id} className="flex items-center gap-2 text-xs">

                  <CheckCircle2 className="h-3.5 w-3.5 text-emerald-300 shrink-0" />

                  <button

                    onClick={() => setView("inspector", { videoId: item.videoId })}

                    className="flex-1 truncate text-left hover:text-cyan-300"

                  >

                    {item.videoName}

                  </button>

                  <span className="text-muted-foreground shrink-0">

                    {item.finishedAt ? new Date(item.finishedAt).toLocaleTimeString() : "—"}

                  </span>

                </div>

              ))}

            </div>

          )}

        </Card>



        <Card className="glass-card p-5">

          <h3 className="text-sm font-semibold mb-3 flex items-center gap-2">

            <AlertTriangle className="h-4 w-4 text-red-300" /> Recent Errors

          </h3>

          {q?.recentErrors?.length === 0 ? (

            <div className="text-xs text-muted-foreground py-4 text-center">No errors. All good.</div>

          ) : (

            <div className="space-y-2 max-h-64 overflow-y-auto scrollbar-vr">

              {q?.recentErrors?.map((item) => (

                <div key={item.id} className="flex items-start gap-2 text-xs">

                  <AlertTriangle className="h-3.5 w-3.5 text-red-300 shrink-0 mt-0.5" />

                  <div className="min-w-0 flex-1">

                    <button

                      onClick={() => setView("inspector", { videoId: item.videoId })}

                      className="font-medium truncate block text-left hover:text-cyan-300"

                    >

                      {item.videoName}

                    </button>

                    <div className="text-red-300/80 truncate">{item.errorMessage}</div>

                  </div>

                </div>

              ))}

            </div>

          )}

        </Card>

      </div>

    </div>
  );
}