@7365admin1/layer-common 4.160.0 → 4.161.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 4.161.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 39d9b2b: CCTV Engine live view: a tile now says "Live" only once the first picture arrives. When the video cannot reach the viewer (for example an engine behind a site router), it switches to the still picture refreshed every 2 seconds instead of staying black.
8
+
9
+ ## 4.161.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 00d5467: CCTV Engine gets two new screens. "Smart search" lets people describe what they are looking for in words and see the best matching camera pictures. "Smart detection" (needs the manage zones permission) lets site staff teach the engine what an area looks like, for example clean or rubbish: draw the area on a camera picture, sort the photos the engine collects into results, press Teach, and choose which results raise an alert. Events can now be filtered by the new "Smart detection" kind and show "result (detection name)" with a photo and no clip. The event detail window is now a shared component.
14
+
3
15
  ## 4.160.0
4
16
 
5
17
  ### Minor Changes
@@ -3650,11 +3662,11 @@ Monthly report: a month that had patrols opens the list of that month's patrol r
3650
3662
  `utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
3651
3663
  own rule from two endpoints the console already calls, unprojected:
3652
3664
 
3653
- GET /api/members/user/:user/app/admin the Seven365 staff membership
3654
- GET /api/roles/id/:role that membership's role document
3665
+ GET /api/members/user/:user/app/admin the Seven365 staff membership
3666
+ GET /api/roles/id/:role that membership's role document
3655
3667
 
3656
- owner = member.type === "admin" && role.type === "admin" && role.default === true
3657
- staff = member.type === "admin" && role.type === "admin"
3668
+ owner = member.type === "admin" && role.type === "admin" && role.default === true
3669
+ staff = member.type === "admin" && role.type === "admin"
3658
3670
 
3659
3671
  `role.default` is the marker because it is the only property of a platform
3660
3672
  staff role no API caller can set - `role.controller.ts` validates create and
@@ -0,0 +1,260 @@
1
+ <!--
2
+ New smart detection, in two steps.
3
+ Step 1: name it, pick a camera, drag a box over the area to watch on the camera's latest picture.
4
+ Step 2: list the results (what the area can look like) and which ones should raise an alert.
5
+ The box is saved as fractions of the picture (0..1) so it stays right at any screen size.
6
+ -->
7
+ <template>
8
+ <v-dialog :model-value="modelValue" max-width="820" scrollable persistent @update:model-value="close">
9
+ <v-card>
10
+ <v-card-title class="d-flex align-center">
11
+ <span class="flex-grow-1">New smart detection</span>
12
+ <v-btn icon="mdi-close" variant="text" aria-label="Close" @click="close(false)" />
13
+ </v-card-title>
14
+ <v-card-text>
15
+ <v-stepper-header class="mb-3 elevation-0">
16
+ <v-stepper-item :complete="step > 1" :value="1" title="Area" :color="step === 1 ? 'primary' : undefined" />
17
+ <v-divider />
18
+ <v-stepper-item :value="2" title="Results" :color="step === 2 ? 'primary' : undefined" />
19
+ </v-stepper-header>
20
+
21
+ <template v-if="step === 1">
22
+ <p class="hero-muted">
23
+ Teach the engine to tell apart what an area looks like, for example a bin area that is clean or has rubbish.
24
+ Give it a name, pick the camera, then drag a box over the area on the picture.
25
+ </p>
26
+ <v-row dense>
27
+ <v-col cols="12" sm="6">
28
+ <v-text-field v-model="name" label="Name (e.g. bin_area)" density="comfortable" :error-messages="nameShown" hint="Letters, numbers and underscores. Spaces become underscores." persistent-hint />
29
+ </v-col>
30
+ <v-col cols="12" sm="6">
31
+ <v-select
32
+ v-model="camera"
33
+ :items="cameras"
34
+ item-title="name"
35
+ item-value="name"
36
+ label="Camera"
37
+ density="comfortable"
38
+ :loading="camLoading"
39
+ :no-data-text="camError ? 'Cameras could not be read' : 'This engine has no cameras yet'"
40
+ />
41
+ </v-col>
42
+ </v-row>
43
+
44
+ <template v-if="camera">
45
+ <v-progress-linear v-if="imgLoading" indeterminate class="mb-2" />
46
+ <div
47
+ ref="stage"
48
+ class="cctv-dc__stage"
49
+ role="img"
50
+ :aria-label="`Latest picture from ${camera}. Drag to draw a box over the area.`"
51
+ @pointerdown.prevent="down"
52
+ @pointermove="move"
53
+ @pointerup="up"
54
+ @pointercancel="up"
55
+ >
56
+ <img v-if="image" :src="image" alt="" class="cctv-dc__img" draggable="false" />
57
+ <div v-else-if="!imgLoading" class="cctv-dc__none">No picture available yet. The engine may be offline. You can still draw a box.</div>
58
+ <div v-if="box" class="cctv-dc__box" :style="boxStyle" />
59
+ </div>
60
+ <div class="hero-muted mt-1">
61
+ <template v-if="cropIsUsable(crop)">Area set. Drag again to change it.</template>
62
+ <template v-else>Press and drag on the picture to draw a box around the area. Make it big enough to see clearly.</template>
63
+ </div>
64
+ <CctvEngineNotice v-if="imgError" class="mt-2" tone="info" :error="imgError" title="The latest picture could not be loaded" body="The engine may be offline. You can still draw a box on the grey area." />
65
+ </template>
66
+ </template>
67
+
68
+ <template v-else>
69
+ <p class="hero-muted">
70
+ What can this area look like? Add each possible result as a word, for example <b>clean</b> and <b>rubbish</b>.
71
+ Choose which results should raise an alert and send a notification.
72
+ </p>
73
+ <v-combobox
74
+ :model-value="states"
75
+ multiple
76
+ chips
77
+ closable-chips
78
+ label="Results (press Enter after each)"
79
+ density="comfortable"
80
+ :error-messages="resultsShown"
81
+ hint="At least 2, at most 10. Letters, numbers and underscores."
82
+ persistent-hint
83
+ @update:model-value="setStates"
84
+ />
85
+ <v-select
86
+ v-model="alertStates"
87
+ :items="states"
88
+ multiple
89
+ chips
90
+ clearable
91
+ label="Alert me when the result is..."
92
+ density="comfortable"
93
+ class="mt-3"
94
+ hint="Optional. Leave empty to only record the results."
95
+ persistent-hint
96
+ />
97
+ <v-alert type="info" variant="tonal" density="compact" class="mt-4" icon="mdi-information-outline">
98
+ What happens next: the engine now saves photos of this area when something moves and every 5 minutes.
99
+ Sort at least {{ MIN_PHOTOS_PER_RESULT }} photos into each result, then press Teach.
100
+ </v-alert>
101
+ </template>
102
+
103
+ <CctvEngineNotice v-if="error" class="mt-3" :error="error" />
104
+ </v-card-text>
105
+ <v-card-actions>
106
+ <v-btn v-if="step === 2" variant="text" @click="step = 1">Back</v-btn>
107
+ <v-spacer />
108
+ <v-btn variant="text" @click="close(false)">Cancel</v-btn>
109
+ <v-btn v-if="step === 1" color="primary" :disabled="!!step1Problem" @click="step = 2">Next</v-btn>
110
+ <v-btn v-else color="primary" :loading="saving" :disabled="!!resultsProblemText || saving" @click="create">Create</v-btn>
111
+ </v-card-actions>
112
+ <div v-if="step === 1 && step1Problem" class="hero-muted px-6 pb-3">{{ step1Problem }}</div>
113
+ </v-card>
114
+ </v-dialog>
115
+ </template>
116
+
117
+ <script setup lang="ts">
118
+ import { computed, onBeforeUnmount, ref, watch } from "vue";
119
+ import {
120
+ cropFromDrag, cropIsUsable, DEFAULT_RESULTS, detectionNameProblem, MIN_PHOTOS_PER_RESULT, resultsProblem, toWord,
121
+ type TCrop,
122
+ } from "../../utils/cctv-engine";
123
+
124
+ const props = defineProps<{ modelValue: boolean; engine: string }>();
125
+ const emit = defineEmits<{ (e: "update:modelValue", v: boolean): void; (e: "created", message: string): void }>();
126
+ const { listCameras, latestImage, createClassifier } = useCctvEngine();
127
+
128
+ const step = ref(1);
129
+ const name = ref("");
130
+ const camera = ref("");
131
+ const cameras = ref<any[]>([]);
132
+ const camLoading = ref(false);
133
+ const camError = ref<unknown>(null);
134
+ const image = ref("");
135
+ const imgLoading = ref(false);
136
+ const imgError = ref<unknown>(null);
137
+ const crop = ref<TCrop | null>(null);
138
+ const states = ref<string[]>([...DEFAULT_RESULTS]);
139
+ const alertStates = ref<string[]>(["rubbish"]);
140
+ const saving = ref(false);
141
+ const error = ref<unknown>(null);
142
+ const stage = ref<HTMLElement | null>(null);
143
+
144
+ const nameProblem = computed(() => detectionNameProblem(name.value));
145
+ const nameShown = computed(() => (name.value ? nameProblem.value : ""));
146
+ const step1Problem = computed(() =>
147
+ nameProblem.value || (!camera.value ? "Pick a camera." : !cropIsUsable(crop.value) ? "Draw a box over the area on the picture." : ""),
148
+ );
149
+ const resultsProblemText = computed(() => resultsProblem(states.value, alertStates.value));
150
+ const resultsShown = computed(() => (states.value.length ? resultsProblemText.value : ""));
151
+
152
+ // ---- drawing the box (pointer events, so mouse, pen and touch all work)
153
+ const dragFrom = ref<{ x: number; y: number } | null>(null);
154
+ const box = computed(() => crop.value);
155
+ const boxStyle = computed(() => {
156
+ const c = box.value!;
157
+ return { left: c[0] * 100 + "%", top: c[1] * 100 + "%", width: (c[2] - c[0]) * 100 + "%", height: (c[3] - c[1]) * 100 + "%" };
158
+ });
159
+ function down(ev: PointerEvent) {
160
+ dragFrom.value = { x: ev.clientX, y: ev.clientY };
161
+ (ev.currentTarget as HTMLElement).setPointerCapture?.(ev.pointerId);
162
+ crop.value = null;
163
+ }
164
+ function move(ev: PointerEvent) {
165
+ if (!dragFrom.value || !stage.value) return;
166
+ crop.value = cropFromDrag(stage.value.getBoundingClientRect(), dragFrom.value, { x: ev.clientX, y: ev.clientY });
167
+ }
168
+ function up(ev: PointerEvent) {
169
+ if (dragFrom.value) move(ev);
170
+ dragFrom.value = null;
171
+ }
172
+
173
+ function setStates(v: string[]) {
174
+ const clean = [...new Set((v ?? []).map(toWord).filter(Boolean))];
175
+ states.value = clean;
176
+ alertStates.value = alertStates.value.filter((a) => clean.includes(a));
177
+ }
178
+
179
+ function freeImage() {
180
+ if (image.value) URL.revokeObjectURL(image.value);
181
+ image.value = "";
182
+ }
183
+
184
+ async function loadCameras() {
185
+ cameras.value = [];
186
+ camError.value = null;
187
+ camLoading.value = true;
188
+ try {
189
+ cameras.value = ((await listCameras(props.engine)) as any)?.items ?? [];
190
+ if (cameras.value.length === 1) camera.value = cameras.value[0].name;
191
+ } catch (e) {
192
+ camError.value = e;
193
+ } finally {
194
+ camLoading.value = false;
195
+ }
196
+ }
197
+ async function loadImage() {
198
+ freeImage();
199
+ crop.value = null;
200
+ imgError.value = null;
201
+ if (!camera.value) return;
202
+ imgLoading.value = true;
203
+ try {
204
+ image.value = await latestImage(props.engine, camera.value);
205
+ } catch (e) {
206
+ imgError.value = e;
207
+ } finally {
208
+ imgLoading.value = false;
209
+ }
210
+ }
211
+
212
+ async function create() {
213
+ saving.value = true;
214
+ error.value = null;
215
+ try {
216
+ const res: any = await createClassifier(props.engine, {
217
+ name: toWord(name.value),
218
+ camera: camera.value,
219
+ crop: crop.value,
220
+ states: states.value,
221
+ alertStates: alertStates.value,
222
+ });
223
+ emit("created", res?.message || "Smart detection created. The engine is now collecting photos of this area.");
224
+ close(false);
225
+ } catch (e) {
226
+ error.value = e;
227
+ } finally {
228
+ saving.value = false;
229
+ }
230
+ }
231
+
232
+ function reset() {
233
+ step.value = 1;
234
+ name.value = camera.value = "";
235
+ crop.value = null;
236
+ states.value = [...DEFAULT_RESULTS];
237
+ alertStates.value = ["rubbish"];
238
+ error.value = null;
239
+ freeImage();
240
+ }
241
+ function close(v: boolean) {
242
+ emit("update:modelValue", v);
243
+ }
244
+
245
+ watch(() => props.modelValue, (open) => {
246
+ if (open) {
247
+ reset();
248
+ loadCameras();
249
+ }
250
+ }, { immediate: true });
251
+ watch(camera, loadImage);
252
+ onBeforeUnmount(freeImage);
253
+ </script>
254
+
255
+ <style scoped>
256
+ .cctv-dc__stage { position: relative; background: #111; border-radius: 10px; overflow: hidden; min-height: 180px; user-select: none; cursor: crosshair; touch-action: none; }
257
+ .cctv-dc__img { width: 100%; display: block; pointer-events: none; }
258
+ .cctv-dc__none { color: #ddd; padding: 60px 16px; text-align: center; font-size: 13px; aspect-ratio: 16 / 9; }
259
+ .cctv-dc__box { position: absolute; border: 2px solid #fff; background: rgba(30, 136, 229, 0.3); box-shadow: 0 0 0 1px #1e88e5; pointer-events: none; }
260
+ </style>
@@ -0,0 +1,266 @@
1
+ <!--
2
+ One smart detection: sort the photos the engine collected into results, then teach it.
3
+ "Waiting" photos have no result yet (the engine's guess is shown as a hint). Press a result's button to sort one,
4
+ or tick several and discard them. Each result needs at least 5 sorted photos before Teach is available.
5
+ Every action shows the server's own message.
6
+ -->
7
+ <template>
8
+ <div>
9
+ <div class="d-flex align-center flex-wrap ga-2 mb-2">
10
+ <v-btn variant="text" prepend-icon="mdi-arrow-left" @click="$emit('back')">All smart detections</v-btn>
11
+ <v-spacer />
12
+ <v-btn variant="text" color="error" prepend-icon="mdi-delete-outline" @click="confirmDelete = true">Delete</v-btn>
13
+ </div>
14
+
15
+ <v-progress-linear v-if="loading" indeterminate />
16
+ <CctvEngineNotice v-else-if="error" :error="error" retry @retry="load" />
17
+ <template v-else-if="data">
18
+ <h3 class="d-flex align-center flex-wrap ga-2">
19
+ {{ data.name }}
20
+ <v-chip size="small" label :color="status.color">{{ status.title }}</v-chip>
21
+ </h3>
22
+ <p class="hero-muted">Watching camera <b>{{ data.camera }}</b>. Results that raise an alert: <b>{{ (data.alertStates ?? []).join(", ") || "none" }}</b>.</p>
23
+
24
+ <!-- Teach -->
25
+ <v-card variant="tonal" class="pa-3 mb-4">
26
+ <div class="d-flex align-center flex-wrap ga-3">
27
+ <v-btn color="primary" prepend-icon="mdi-school-outline" :disabled="!!blocked || teaching || busy" :loading="teaching" @click="teach">Teach</v-btn>
28
+ <span class="hero-muted flex-grow-1">
29
+ <template v-if="teaching">The engine is learning. This can take a few minutes; this page updates when it is done.</template>
30
+ <template v-else-if="blocked">{{ blocked }}</template>
31
+ <template v-else>Every result has enough photos. Press Teach so the engine learns from them.</template>
32
+ </span>
33
+ </div>
34
+ </v-card>
35
+
36
+ <!-- Alert results -->
37
+ <v-row dense class="mb-2">
38
+ <v-col cols="12" sm="8" md="6">
39
+ <v-select v-model="alerts" :items="data.states" multiple chips clearable label="Alert me when the result is..." density="comfortable" hide-details />
40
+ </v-col>
41
+ <v-col cols="12" sm="4" class="d-flex align-center">
42
+ <v-btn variant="tonal" :disabled="!alertsChanged || busy" @click="saveAlerts">Save alerts</v-btn>
43
+ </v-col>
44
+ </v-row>
45
+
46
+ <!-- Waiting photos -->
47
+ <h4 class="mt-4 d-flex align-center flex-wrap ga-2">
48
+ Waiting to be sorted ({{ data.waiting.length }})
49
+ <v-btn v-if="data.waiting.length" size="x-small" variant="text" @click="toggleAll">{{ picked.size === data.waiting.length ? "Clear selection" : "Select all" }}</v-btn>
50
+ <v-btn v-if="picked.size" size="small" color="error" variant="tonal" prepend-icon="mdi-delete-outline" :disabled="busy" @click="confirmDiscard = true">Discard selected ({{ picked.size }})</v-btn>
51
+ </h4>
52
+ <p v-if="!data.waiting.length" class="hero-muted">
53
+ Nothing waiting. The engine saves a new photo of this area when something moves and every 5 minutes; they will appear here.
54
+ </p>
55
+ <v-row v-else dense>
56
+ <v-col v-for="p in data.waiting" :key="p.file" cols="6" sm="4" md="3">
57
+ <v-card variant="outlined" :class="{ 'cctv-dd__picked': picked.has(p.file) }">
58
+ <div class="cctv-dd__thumb" @click="togglePick(p.file)">
59
+ <img v-if="thumbs[key(p.file)]" :src="thumbs[key(p.file)]" :alt="`Waiting photo from ${when(p.at)}`" />
60
+ <v-icon v-else icon="mdi-image-outline" size="28" />
61
+ <v-checkbox-btn :model-value="picked.has(p.file)" class="cctv-dd__check" density="compact" aria-label="Select photo" @click.stop @update:model-value="togglePick(p.file)" />
62
+ </div>
63
+ <div class="pa-2">
64
+ <div class="hero-muted">{{ when(p.at) }}</div>
65
+ <div v-if="p.guess" class="hero-muted">Engine thinks: <b>{{ p.guess }}</b><template v-if="typeof p.score === 'number'"> ({{ pct(p.score) }})</template></div>
66
+ <div class="d-flex flex-wrap ga-1 mt-1">
67
+ <v-btn v-for="s in data.states" :key="s" size="x-small" variant="tonal" :color="s === p.guess ? 'primary' : undefined" :disabled="busy" @click="sort(p.file, s)">{{ s }}</v-btn>
68
+ </div>
69
+ </div>
70
+ </v-card>
71
+ </v-col>
72
+ </v-row>
73
+
74
+ <!-- Sorted photos -->
75
+ <h4 class="mt-6">Sorted photos</h4>
76
+ <div v-for="s in data.states" :key="s" class="mt-3">
77
+ <div class="d-flex align-center flex-wrap ga-2">
78
+ <b>{{ s }}</b>
79
+ <v-chip size="x-small" label :color="count(s) >= MIN_PHOTOS_PER_RESULT ? 'success' : 'warning'">{{ count(s) }} of {{ MIN_PHOTOS_PER_RESULT }} needed</v-chip>
80
+ <v-chip v-if="(data.alertStates ?? []).includes(s)" size="x-small" label color="error">Alert</v-chip>
81
+ </div>
82
+ <p v-if="!(data.sorted?.[s] ?? []).length" class="hero-muted">No photos sorted as {{ s }} yet.</p>
83
+ <v-row v-else dense class="mt-0">
84
+ <v-col v-for="f in data.sorted[s]" :key="f" cols="4" sm="3" md="2">
85
+ <div class="cctv-dd__thumb cctv-dd__thumb--sm">
86
+ <img v-if="thumbs[key(f, s)]" :src="thumbs[key(f, s)]" :alt="`Photo sorted as ${s}`" />
87
+ <v-icon v-else icon="mdi-image-outline" size="24" />
88
+ <v-btn icon="mdi-close" size="x-small" variant="flat" class="cctv-dd__remove" :disabled="busy" :aria-label="`Remove this photo from ${s}`" @click="discard([f], s)" />
89
+ </div>
90
+ </v-col>
91
+ </v-row>
92
+ </div>
93
+ </template>
94
+
95
+ <v-dialog v-model="confirmDiscard" max-width="420">
96
+ <v-card title="Discard these photos?">
97
+ <v-card-text>{{ picked.size }} waiting photo(s) will be deleted from the engine. This cannot be undone.</v-card-text>
98
+ <v-card-actions><v-spacer /><v-btn variant="text" @click="confirmDiscard = false">Keep them</v-btn><v-btn color="error" @click="discardPicked">Discard</v-btn></v-card-actions>
99
+ </v-card>
100
+ </v-dialog>
101
+ <v-dialog v-model="confirmDelete" max-width="460">
102
+ <v-card :title="`Delete ${name}?`">
103
+ <v-card-text>This removes the smart detection and all its photos and training from the engine. Past results already recorded in Events stay. This cannot be undone.</v-card-text>
104
+ <v-card-actions><v-spacer /><v-btn variant="text" @click="confirmDelete = false">Keep it</v-btn><v-btn color="error" :loading="busy" @click="remove">Delete</v-btn></v-card-actions>
105
+ </v-card>
106
+ </v-dialog>
107
+
108
+ <v-snackbar v-model="snack.open" :color="snack.error ? 'error' : undefined" timeout="6000" multi-line>{{ snack.text }}</v-snackbar>
109
+ </div>
110
+ </template>
111
+
112
+ <script setup lang="ts">
113
+ import { computed, onBeforeUnmount, reactive, ref, watch } from "vue";
114
+ import { classifierStatus, cctvNotice, MIN_PHOTOS_PER_RESULT, teachProblem } from "../../utils/cctv-engine";
115
+
116
+ const props = defineProps<{ engine: string; name: string; teaching?: boolean }>();
117
+ const emit = defineEmits<{ (e: "back"): void; (e: "changed"): void; (e: "taught"): void; (e: "deleted"): void }>();
118
+ const { classifierPhotos, classifierPhoto, sortClassifierPhoto, discardClassifierPhotos, updateClassifier, trainClassifier, deleteClassifier } = useCctvEngine();
119
+
120
+ const data = ref<any>(null);
121
+ const loading = ref(false);
122
+ const error = ref<unknown>(null);
123
+ const busy = ref(false);
124
+ const thumbs = ref<Record<string, string>>({});
125
+ const picked = ref<Set<string>>(new Set());
126
+ const alerts = ref<string[]>([]);
127
+ const confirmDiscard = ref(false);
128
+ const confirmDelete = ref(false);
129
+ const snack = reactive({ open: false, text: "", error: false });
130
+
131
+ const key = (file: string, state?: string) => `${state ?? "_"}/${file}`;
132
+ const count = (s: string) => data.value?.counts?.[s] ?? data.value?.sorted?.[s]?.length ?? 0;
133
+ const when = (v?: string | null) => (v ? new Date(v).toLocaleString() : "Time not known");
134
+ const pct = (s: number) => `${Math.round(s <= 1 ? s * 100 : s)}%`;
135
+ const blocked = computed(() => (data.value ? teachProblem({ states: data.value.states, counts: data.value.counts }) : ""));
136
+ const status = computed(() => classifierStatus({ ...data.value, waiting: data.value?.waiting?.length ?? 0 }, { teaching: props.teaching }));
137
+ const alertsChanged = computed(() => {
138
+ const a = [...alerts.value].sort().join();
139
+ return a !== [...(data.value?.alertStates ?? [])].sort().join();
140
+ });
141
+
142
+ function say(text: string, isError = false) {
143
+ Object.assign(snack, { open: true, text, error: isError });
144
+ }
145
+ const errText = (e: any) => String(e?.data?.message || "").trim() || cctvNotice(e).body;
146
+
147
+ // ---- photos (fetched as blobs because an image tag cannot send the sign-in; 4 at a time)
148
+ async function loadThumbs() {
149
+ const todo: { k: string; file: string; state?: string }[] = [];
150
+ for (const p of data.value.waiting) if (!thumbs.value[key(p.file)]) todo.push({ k: key(p.file), file: p.file });
151
+ for (const [s, files] of Object.entries<string[]>(data.value.sorted ?? {}))
152
+ for (const f of files) if (!thumbs.value[key(f, s)]) todo.push({ k: key(f, s), file: f, state: s });
153
+ let i = 0;
154
+ const worker = async () => {
155
+ while (i < todo.length) {
156
+ const t = todo[i++];
157
+ try {
158
+ const url = await classifierPhoto(props.engine, props.name, t.file, t.state);
159
+ thumbs.value = { ...thumbs.value, [t.k]: url };
160
+ } catch {
161
+ /* placeholder stays */
162
+ }
163
+ }
164
+ };
165
+ await Promise.all([worker(), worker(), worker(), worker()]);
166
+ }
167
+ function pruneThumbs() {
168
+ const live = new Set<string>([
169
+ ...data.value.waiting.map((p: any) => key(p.file)),
170
+ ...Object.entries<string[]>(data.value.sorted ?? {}).flatMap(([s, fs]) => fs.map((f) => key(f, s))),
171
+ ]);
172
+ for (const [k, u] of Object.entries(thumbs.value)) {
173
+ if (!live.has(k)) {
174
+ URL.revokeObjectURL(u);
175
+ delete thumbs.value[k];
176
+ }
177
+ }
178
+ }
179
+ function freeThumbs() {
180
+ Object.values(thumbs.value).forEach((u) => URL.revokeObjectURL(u));
181
+ thumbs.value = {};
182
+ }
183
+
184
+ async function load(quiet = false) {
185
+ if (!quiet) loading.value = true;
186
+ error.value = null;
187
+ try {
188
+ const res: any = await classifierPhotos(props.engine, props.name);
189
+ data.value = { ...res, waiting: res?.waiting ?? [], sorted: res?.sorted ?? {}, states: res?.states ?? [] };
190
+ alerts.value = [...(res?.alertStates ?? [])];
191
+ picked.value = new Set([...picked.value].filter((f) => data.value.waiting.some((p: any) => p.file === f)));
192
+ pruneThumbs();
193
+ loading.value = false;
194
+ loadThumbs();
195
+ } catch (e) {
196
+ error.value = e;
197
+ } finally {
198
+ loading.value = false;
199
+ }
200
+ }
201
+
202
+ // ---- actions: each says what the server said
203
+ async function act(fn: () => Promise<any>, fallback: string, reload = true) {
204
+ busy.value = true;
205
+ try {
206
+ const res: any = await fn();
207
+ say(res?.message || fallback);
208
+ if (reload) {
209
+ await load(true);
210
+ emit("changed");
211
+ }
212
+ return true;
213
+ } catch (e) {
214
+ say(errText(e), true);
215
+ return false;
216
+ } finally {
217
+ busy.value = false;
218
+ }
219
+ }
220
+ const sort = (file: string, state: string) => act(() => sortClassifierPhoto(props.engine, props.name, file, state), `Sorted as ${state}.`);
221
+ const discard = (files: string[], state?: string) => act(() => discardClassifierPhotos(props.engine, props.name, files, state), "Photo removed.");
222
+ async function discardPicked() {
223
+ confirmDiscard.value = false;
224
+ await discard([...picked.value]);
225
+ }
226
+ const saveAlerts = () => act(() => updateClassifier(props.engine, props.name, { alertStates: alerts.value }), "Alerts saved.");
227
+ async function teach() {
228
+ if (await act(() => trainClassifier(props.engine, props.name), "Teaching has started.", false)) emit("taught");
229
+ }
230
+ async function remove() {
231
+ busy.value = true;
232
+ try {
233
+ await deleteClassifier(props.engine, props.name);
234
+ confirmDelete.value = false;
235
+ emit("deleted");
236
+ } catch (e) {
237
+ confirmDelete.value = false;
238
+ say(errText(e), true);
239
+ } finally {
240
+ busy.value = false;
241
+ }
242
+ }
243
+
244
+ const togglePick = (f: string) => {
245
+ const s = new Set(picked.value);
246
+ s.has(f) ? s.delete(f) : s.add(f);
247
+ picked.value = s;
248
+ };
249
+ const toggleAll = () => {
250
+ picked.value = picked.value.size === data.value.waiting.length ? new Set() : new Set(data.value.waiting.map((p: any) => p.file));
251
+ };
252
+
253
+ // When Teach finishes (the list stops saying "teaching"), refresh so the status and counts are current.
254
+ watch(() => props.teaching, (now, was) => was && !now && load(true));
255
+ watch(() => props.name, () => { freeThumbs(); load(); }, { immediate: true });
256
+ onBeforeUnmount(freeThumbs);
257
+ </script>
258
+
259
+ <style scoped>
260
+ .cctv-dd__thumb { position: relative; aspect-ratio: 16 / 9; background: #111; display: flex; align-items: center; justify-content: center; color: #aaa; overflow: hidden; cursor: pointer; }
261
+ .cctv-dd__thumb--sm { cursor: default; }
262
+ .cctv-dd__thumb img { width: 100%; height: 100%; object-fit: cover; }
263
+ .cctv-dd__check { position: absolute; top: 0; left: 0; background: rgba(255, 255, 255, 0.8); border-radius: 0 0 6px 0; }
264
+ .cctv-dd__remove { position: absolute; top: 2px; right: 2px; }
265
+ .cctv-dd__picked { outline: 2px solid #1e88e5; }
266
+ </style>