@trafficgroup/knex-rel 0.0.29 → 0.1.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/dist/dao/VideoMinuteResultDAO.d.ts +101 -0
- package/dist/dao/VideoMinuteResultDAO.js +366 -0
- package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
- package/dist/dao/camera/camera.dao.d.ts +13 -0
- package/dist/dao/camera/camera.dao.js +93 -0
- package/dist/dao/camera/camera.dao.js.map +1 -0
- package/dist/dao/video/video.dao.d.ts +5 -0
- package/dist/dao/video/video.dao.js +46 -0
- package/dist/dao/video/video.dao.js.map +1 -1
- package/dist/index.d.ts +12 -10
- package/dist/index.js +13 -11
- package/dist/index.js.map +1 -1
- package/dist/interfaces/camera/camera.interfaces.d.ts +9 -0
- package/dist/interfaces/camera/camera.interfaces.js +3 -0
- package/dist/interfaces/camera/camera.interfaces.js.map +1 -0
- package/dist/interfaces/folder/folder.interfaces.d.ts +3 -0
- package/dist/interfaces/video/video.interfaces.d.ts +8 -0
- package/migrations/20250910015452_migration.ts +34 -0
- package/migrations/20250911000000_migration.ts +61 -0
- package/migrations/20250917144153_migration.ts +37 -0
- package/package.json +1 -1
- package/plan.md +90 -265
- package/src/dao/VideoMinuteResultDAO.ts +517 -0
- package/src/dao/camera/camera.dao.ts +79 -0
- package/src/dao/video/video.dao.ts +49 -0
- package/src/index.ts +12 -10
- package/src/interfaces/camera/camera.interfaces.ts +9 -0
- package/src/interfaces/folder/folder.interfaces.ts +3 -0
- package/src/interfaces/video/video.interfaces.ts +13 -0
|
@@ -6,6 +6,68 @@ import {
|
|
|
6
6
|
} from "../entities/VideoMinuteResult";
|
|
7
7
|
import KnexManager from "../KnexConnection";
|
|
8
8
|
|
|
9
|
+
// Type definitions for aggregated results
|
|
10
|
+
interface ITMCTurnMovements {
|
|
11
|
+
straight: number;
|
|
12
|
+
left: number;
|
|
13
|
+
right: number;
|
|
14
|
+
"u-turn": number;
|
|
15
|
+
[key: string]: number; // Allow dynamic turn types
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ITMCDirections {
|
|
19
|
+
[direction: string]: ITMCTurnMovements;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ITMCVehicles {
|
|
23
|
+
[vehicleClass: string]: ITMCDirections;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ITMCResult {
|
|
27
|
+
vehicles: ITMCVehicles;
|
|
28
|
+
counts: {
|
|
29
|
+
total_vehicles: number;
|
|
30
|
+
entry_vehicles: number;
|
|
31
|
+
};
|
|
32
|
+
total: number;
|
|
33
|
+
totalcount: number;
|
|
34
|
+
detected_classes: { [vehicleClass: string]: number };
|
|
35
|
+
study_type: "TMC";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface IATRVehicles {
|
|
39
|
+
[vehicleClass: string]: { [lane: string]: number };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface IATRResult {
|
|
43
|
+
vehicles: IATRVehicles;
|
|
44
|
+
lane_counts: { [lane: string]: number };
|
|
45
|
+
total_count: number;
|
|
46
|
+
detected_classes: { [vehicleClass: string]: number };
|
|
47
|
+
study_type: "ATR";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface IGroupedResult {
|
|
51
|
+
groupIndex: number;
|
|
52
|
+
startMinute: number;
|
|
53
|
+
endMinute: number;
|
|
54
|
+
label: string;
|
|
55
|
+
results: ITMCResult | IATRResult;
|
|
56
|
+
minuteCount: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface IGroupedResponse {
|
|
60
|
+
success: boolean;
|
|
61
|
+
data: IGroupedResult[];
|
|
62
|
+
groupingMinutes: number;
|
|
63
|
+
video: {
|
|
64
|
+
uuid: string;
|
|
65
|
+
name: string;
|
|
66
|
+
videoType: string;
|
|
67
|
+
status: string;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
9
71
|
export class VideoMinuteResultDAO implements IBaseDAO<IVideoMinuteResult> {
|
|
10
72
|
private knex = KnexManager.getConnection();
|
|
11
73
|
private tableName = "video_minute_results";
|
|
@@ -268,6 +330,461 @@ export class VideoMinuteResultDAO implements IBaseDAO<IVideoMinuteResult> {
|
|
|
268
330
|
totalPages: Math.ceil(totalCount / limit),
|
|
269
331
|
};
|
|
270
332
|
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Get grouped minute results by video UUID with time block aggregation
|
|
336
|
+
* @param videoUuid - The UUID of the video
|
|
337
|
+
* @param groupingMinutes - Number of minutes to group together (1, 5, 10, 15, 30, 60)
|
|
338
|
+
* @param startMinute - Optional start minute filter
|
|
339
|
+
* @param endMinute - Optional end minute filter
|
|
340
|
+
*/
|
|
341
|
+
async getGroupedMinuteResultsByVideoUuid(
|
|
342
|
+
videoUuid: string,
|
|
343
|
+
groupingMinutes: number = 1,
|
|
344
|
+
startMinute?: number,
|
|
345
|
+
endMinute?: number,
|
|
346
|
+
): Promise<IGroupedResponse> {
|
|
347
|
+
// First, get the video to ensure it exists and get metadata
|
|
348
|
+
const video = await this.knex("video").where("uuid", videoUuid).first();
|
|
349
|
+
|
|
350
|
+
if (!video) {
|
|
351
|
+
throw new Error(`Video with UUID ${videoUuid} not found`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Build the base query for minute results
|
|
355
|
+
const query = this.knex(this.tableName + " as vmr").where(
|
|
356
|
+
"vmr.video_id",
|
|
357
|
+
video.id,
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
if (startMinute !== undefined) {
|
|
361
|
+
query.where("vmr.minute_number", ">=", startMinute);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (endMinute !== undefined) {
|
|
365
|
+
query.where("vmr.minute_number", "<=", endMinute);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// If grouping is 1 minute, just return the regular results
|
|
369
|
+
if (groupingMinutes === 1) {
|
|
370
|
+
const data = await query
|
|
371
|
+
.select("vmr.*")
|
|
372
|
+
.orderBy("vmr.minute_number", "asc");
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
success: true,
|
|
376
|
+
data: data.map((row) => ({
|
|
377
|
+
groupIndex: row.minute_number,
|
|
378
|
+
startMinute: row.minute_number,
|
|
379
|
+
endMinute: row.minute_number,
|
|
380
|
+
label: this.formatTimeLabel(row.minute_number, row.minute_number),
|
|
381
|
+
results: row.results,
|
|
382
|
+
minuteCount: 1,
|
|
383
|
+
})),
|
|
384
|
+
groupingMinutes,
|
|
385
|
+
video: {
|
|
386
|
+
uuid: video.uuid,
|
|
387
|
+
name: video.name,
|
|
388
|
+
videoType: video.videoType,
|
|
389
|
+
status: video.status,
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Use Knex query builder for safe parameter binding
|
|
395
|
+
const groupingQuery = this.knex(this.tableName)
|
|
396
|
+
.select(
|
|
397
|
+
this.knex.raw("FLOOR(minute_number / ?) as group_index", [
|
|
398
|
+
groupingMinutes,
|
|
399
|
+
]),
|
|
400
|
+
this.knex.raw("MIN(minute_number) as start_minute"),
|
|
401
|
+
this.knex.raw("MAX(minute_number) as end_minute"),
|
|
402
|
+
this.knex.raw("COUNT(*) as minute_count"),
|
|
403
|
+
this.knex.raw(
|
|
404
|
+
"array_agg(results ORDER BY minute_number) as all_results",
|
|
405
|
+
),
|
|
406
|
+
)
|
|
407
|
+
.where("video_id", video.id);
|
|
408
|
+
|
|
409
|
+
if (startMinute !== undefined) {
|
|
410
|
+
groupingQuery.where("minute_number", ">=", startMinute);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (endMinute !== undefined) {
|
|
414
|
+
groupingQuery.where("minute_number", "<=", endMinute);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const rows = await groupingQuery
|
|
418
|
+
.groupBy("group_index")
|
|
419
|
+
.orderBy("group_index");
|
|
420
|
+
|
|
421
|
+
// Aggregate the results in TypeScript based on video type
|
|
422
|
+
const aggregatedGroups: IGroupedResult[] = rows.map((row: any) => {
|
|
423
|
+
if (!row || typeof row !== "object") {
|
|
424
|
+
throw new Error("Invalid row data received from database query");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const allResults = Array.isArray(row.all_results) ? row.all_results : [];
|
|
428
|
+
|
|
429
|
+
// Determine video type based on multiple factors
|
|
430
|
+
let studyType = video.videoType || "ATR"; // Default fallback to ATR
|
|
431
|
+
|
|
432
|
+
// Check if minute data has study_type field (ATR usually does)
|
|
433
|
+
if (allResults.length > 0 && allResults[0].study_type) {
|
|
434
|
+
studyType = allResults[0].study_type;
|
|
435
|
+
} else if (allResults.length > 0) {
|
|
436
|
+
// Check data structure to determine type
|
|
437
|
+
const sampleResult = allResults[0];
|
|
438
|
+
if (sampleResult.vehicles) {
|
|
439
|
+
// Check if vehicles structure has directions (NORTH, SOUTH, etc.) - TMC pattern
|
|
440
|
+
const vehicleKeys = Object.keys(sampleResult.vehicles);
|
|
441
|
+
if (vehicleKeys.length > 0) {
|
|
442
|
+
const firstVehicleType = sampleResult.vehicles[vehicleKeys[0]];
|
|
443
|
+
if (firstVehicleType && typeof firstVehicleType === "object") {
|
|
444
|
+
const directions = Object.keys(firstVehicleType);
|
|
445
|
+
if (
|
|
446
|
+
directions.includes("NORTH") ||
|
|
447
|
+
directions.includes("SOUTH") ||
|
|
448
|
+
directions.includes("EAST") ||
|
|
449
|
+
directions.includes("WEST")
|
|
450
|
+
) {
|
|
451
|
+
studyType = "TMC";
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// Aggregate based on determined video type
|
|
459
|
+
let aggregatedResult;
|
|
460
|
+
if (studyType === "TMC") {
|
|
461
|
+
aggregatedResult = this.aggregateTMCResults(allResults);
|
|
462
|
+
} else {
|
|
463
|
+
aggregatedResult = this.aggregateATRResults(allResults);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
groupIndex: row.group_index,
|
|
468
|
+
startMinute: row.start_minute,
|
|
469
|
+
endMinute: row.end_minute,
|
|
470
|
+
label: this.formatTimeLabel(row.start_minute, row.end_minute),
|
|
471
|
+
results: aggregatedResult,
|
|
472
|
+
minuteCount: row.minute_count,
|
|
473
|
+
};
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
return {
|
|
477
|
+
success: true,
|
|
478
|
+
data: aggregatedGroups,
|
|
479
|
+
groupingMinutes,
|
|
480
|
+
video: {
|
|
481
|
+
uuid: video.uuid,
|
|
482
|
+
name: video.name,
|
|
483
|
+
videoType: video.videoType,
|
|
484
|
+
status: video.status,
|
|
485
|
+
},
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Aggregate minute results based on video type (TMC or ATR)
|
|
491
|
+
*/
|
|
492
|
+
private aggregateMinuteResults(minutes: any[], videoType: string): any {
|
|
493
|
+
if (minutes.length === 0) return {};
|
|
494
|
+
|
|
495
|
+
// Initialize the aggregated result structure based on the first minute
|
|
496
|
+
const firstMinuteResults = minutes[0].results;
|
|
497
|
+
const aggregated = this.deepCloneStructure(firstMinuteResults);
|
|
498
|
+
|
|
499
|
+
// For TMC videos, aggregate turning movements
|
|
500
|
+
if (videoType === "TMC") {
|
|
501
|
+
return this.aggregateTMCResults(minutes);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// For ATR videos, aggregate lane counts
|
|
505
|
+
if (videoType === "ATR" || videoType === "ATP") {
|
|
506
|
+
return this.aggregateATRResults(minutes);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Default aggregation for other types
|
|
510
|
+
return this.aggregateGenericResults(minutes);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Aggregate TMC (Turning Movement Count) results
|
|
515
|
+
*/
|
|
516
|
+
private aggregateTMCResults(minutes: any[]): ITMCResult {
|
|
517
|
+
const aggregated: ITMCResult = {
|
|
518
|
+
vehicles: {},
|
|
519
|
+
counts: {
|
|
520
|
+
total_vehicles: 0,
|
|
521
|
+
entry_vehicles: 0,
|
|
522
|
+
},
|
|
523
|
+
total: 0,
|
|
524
|
+
totalcount: 0,
|
|
525
|
+
detected_classes: {},
|
|
526
|
+
study_type: "TMC",
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
for (const minute of minutes) {
|
|
530
|
+
const results = minute; // minute is already the results object from array_agg
|
|
531
|
+
|
|
532
|
+
// Aggregate vehicle movements by class and direction
|
|
533
|
+
if (results.vehicles && typeof results.vehicles === "object") {
|
|
534
|
+
for (const [vehicleClass, directions] of Object.entries(
|
|
535
|
+
results.vehicles,
|
|
536
|
+
)) {
|
|
537
|
+
// Skip the 'total' pseudo vehicle class - validate it's actually aggregate data
|
|
538
|
+
if (vehicleClass === "total" && typeof directions === "number") {
|
|
539
|
+
continue; // This is aggregate total data, not a vehicle class
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (!aggregated.vehicles[vehicleClass]) {
|
|
543
|
+
aggregated.vehicles[vehicleClass] = {};
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (!aggregated.detected_classes[vehicleClass]) {
|
|
547
|
+
aggregated.detected_classes[vehicleClass] = 0;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
for (const [direction, turns] of Object.entries(directions as any)) {
|
|
551
|
+
if (!aggregated.vehicles[vehicleClass][direction]) {
|
|
552
|
+
aggregated.vehicles[vehicleClass][direction] = {
|
|
553
|
+
straight: 0,
|
|
554
|
+
left: 0,
|
|
555
|
+
right: 0,
|
|
556
|
+
"u-turn": 0,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (typeof turns === "object" && turns !== null) {
|
|
561
|
+
for (const [turnType, count] of Object.entries(turns)) {
|
|
562
|
+
const turnCount = (count as number) || 0;
|
|
563
|
+
aggregated.vehicles[vehicleClass][direction][turnType] =
|
|
564
|
+
(aggregated.vehicles[vehicleClass][direction][turnType] ||
|
|
565
|
+
0) + turnCount;
|
|
566
|
+
|
|
567
|
+
// Add to detected_classes count for this vehicle type
|
|
568
|
+
aggregated.detected_classes[vehicleClass] += turnCount;
|
|
569
|
+
|
|
570
|
+
// Add to total counts
|
|
571
|
+
aggregated.total += turnCount;
|
|
572
|
+
aggregated.totalcount += turnCount;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// Also process the 'total' entry for validation but don't count it as a vehicle class
|
|
579
|
+
if (results.vehicles.total) {
|
|
580
|
+
for (const [direction, turns] of Object.entries(
|
|
581
|
+
results.vehicles.total as any,
|
|
582
|
+
)) {
|
|
583
|
+
if (typeof turns === "object" && turns !== null) {
|
|
584
|
+
for (const [turnType, count] of Object.entries(turns)) {
|
|
585
|
+
const turnCount = (count as number) || 0;
|
|
586
|
+
aggregated.counts.total_vehicles += turnCount;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Set entry_vehicles same as total_vehicles for TMC
|
|
595
|
+
aggregated.counts.entry_vehicles =
|
|
596
|
+
aggregated.counts.total_vehicles || aggregated.total;
|
|
597
|
+
|
|
598
|
+
return aggregated;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Aggregate ATR (Automatic Traffic Recorder) results
|
|
603
|
+
*/
|
|
604
|
+
private aggregateATRResults(minutes: any[]): IATRResult {
|
|
605
|
+
const aggregated: IATRResult = {
|
|
606
|
+
vehicles: {},
|
|
607
|
+
lane_counts: {},
|
|
608
|
+
total_count: 0,
|
|
609
|
+
detected_classes: {},
|
|
610
|
+
study_type: "ATR",
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
for (const minute of minutes) {
|
|
614
|
+
const results = minute; // minute is already the results object from array_agg
|
|
615
|
+
|
|
616
|
+
// Aggregate vehicle counts by class and lane
|
|
617
|
+
if (results.vehicles) {
|
|
618
|
+
for (const [rawVehicleClass, lanes] of Object.entries(
|
|
619
|
+
results.vehicles,
|
|
620
|
+
)) {
|
|
621
|
+
// Normalize vehicle class names to standard format for frontend compatibility
|
|
622
|
+
const vehicleClass = this.normalizeATRVehicleClass(rawVehicleClass);
|
|
623
|
+
|
|
624
|
+
if (!aggregated.vehicles[vehicleClass]) {
|
|
625
|
+
aggregated.vehicles[vehicleClass] = {};
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
for (const [laneId, count] of Object.entries(lanes as any)) {
|
|
629
|
+
const numericCount =
|
|
630
|
+
typeof count === "number" ? count : parseInt(String(count)) || 0;
|
|
631
|
+
aggregated.vehicles[vehicleClass][laneId] =
|
|
632
|
+
(aggregated.vehicles[vehicleClass][laneId] || 0) + numericCount;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// Aggregate lane counts
|
|
638
|
+
if (results.lane_counts) {
|
|
639
|
+
for (const [laneId, count] of Object.entries(results.lane_counts)) {
|
|
640
|
+
aggregated.lane_counts[laneId] =
|
|
641
|
+
(aggregated.lane_counts[laneId] || 0) + ((count as number) || 0);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Aggregate total count
|
|
646
|
+
aggregated.total_count += results.total_count || 0;
|
|
647
|
+
|
|
648
|
+
// Aggregate detected classes with normalized names
|
|
649
|
+
if (results.detected_classes) {
|
|
650
|
+
for (const [rawCls, count] of Object.entries(
|
|
651
|
+
results.detected_classes,
|
|
652
|
+
)) {
|
|
653
|
+
const cls = this.normalizeATRVehicleClass(rawCls);
|
|
654
|
+
aggregated.detected_classes[cls] =
|
|
655
|
+
(aggregated.detected_classes[cls] || 0) + ((count as number) || 0);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return aggregated;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Generic aggregation for other video types
|
|
665
|
+
*/
|
|
666
|
+
private aggregateGenericResults(minutes: any[]): any {
|
|
667
|
+
if (minutes.length === 0) return {};
|
|
668
|
+
|
|
669
|
+
// For generic aggregation, sum all numeric values
|
|
670
|
+
const aggregated = JSON.parse(JSON.stringify(minutes[0].results));
|
|
671
|
+
|
|
672
|
+
for (let i = 1; i < minutes.length; i++) {
|
|
673
|
+
this.sumNumericValues(aggregated, minutes[i].results);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return aggregated;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Recursively sum numeric values in nested objects
|
|
681
|
+
*/
|
|
682
|
+
private sumNumericValues(target: any, source: any): void {
|
|
683
|
+
for (const key in source) {
|
|
684
|
+
if (typeof source[key] === "number") {
|
|
685
|
+
target[key] = (target[key] || 0) + source[key];
|
|
686
|
+
} else if (
|
|
687
|
+
typeof source[key] === "object" &&
|
|
688
|
+
source[key] !== null &&
|
|
689
|
+
!Array.isArray(source[key])
|
|
690
|
+
) {
|
|
691
|
+
if (!target[key]) {
|
|
692
|
+
target[key] = {};
|
|
693
|
+
}
|
|
694
|
+
this.sumNumericValues(target[key], source[key]);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Deep clone a result structure, setting all numeric values to 0
|
|
701
|
+
*/
|
|
702
|
+
private deepCloneStructure(obj: any): any {
|
|
703
|
+
if (typeof obj !== "object" || obj === null) {
|
|
704
|
+
return typeof obj === "number" ? 0 : obj;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (Array.isArray(obj)) {
|
|
708
|
+
return obj.map((item) => this.deepCloneStructure(item));
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const cloned: any = {};
|
|
712
|
+
for (const key in obj) {
|
|
713
|
+
cloned[key] = this.deepCloneStructure(obj[key]);
|
|
714
|
+
}
|
|
715
|
+
return cloned;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Normalize ATR vehicle class names to standard format for frontend compatibility
|
|
720
|
+
*/
|
|
721
|
+
private normalizeATRVehicleClass(rawVehicleClass: string): string {
|
|
722
|
+
const normalized = rawVehicleClass.toLowerCase().replace(/[_\s-]/g, "");
|
|
723
|
+
|
|
724
|
+
// Map raw vehicle classes to standard classes
|
|
725
|
+
if (
|
|
726
|
+
normalized.includes("car") ||
|
|
727
|
+
normalized === "vehicle" ||
|
|
728
|
+
normalized === "automobiles"
|
|
729
|
+
) {
|
|
730
|
+
return "cars";
|
|
731
|
+
}
|
|
732
|
+
if (
|
|
733
|
+
normalized.includes("medium") ||
|
|
734
|
+
normalized.includes("pickup") ||
|
|
735
|
+
(normalized.includes("truck") && !normalized.includes("heavy"))
|
|
736
|
+
) {
|
|
737
|
+
return "mediums";
|
|
738
|
+
}
|
|
739
|
+
if (
|
|
740
|
+
normalized.includes("heavy") ||
|
|
741
|
+
normalized.includes("largetruck") ||
|
|
742
|
+
normalized.includes("bigtruck")
|
|
743
|
+
) {
|
|
744
|
+
return "heavy_trucks";
|
|
745
|
+
}
|
|
746
|
+
if (
|
|
747
|
+
normalized.includes("pedestrian") ||
|
|
748
|
+
normalized.includes("person") ||
|
|
749
|
+
normalized.includes("people")
|
|
750
|
+
) {
|
|
751
|
+
return "pedestrians";
|
|
752
|
+
}
|
|
753
|
+
if (
|
|
754
|
+
normalized.includes("bicycle") ||
|
|
755
|
+
normalized.includes("bike") ||
|
|
756
|
+
normalized.includes("cyclist")
|
|
757
|
+
) {
|
|
758
|
+
return "bicycles";
|
|
759
|
+
}
|
|
760
|
+
if (normalized.includes("total") || normalized.includes("all")) {
|
|
761
|
+
return "total";
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Handle specific known ATR classes
|
|
765
|
+
if (rawVehicleClass === "mediums") return "mediums";
|
|
766
|
+
if (rawVehicleClass === "heavy_trucks") return "heavy_trucks";
|
|
767
|
+
|
|
768
|
+
// Default fallback for unknown classes
|
|
769
|
+
return "cars";
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Format time label for display (HH:MM:SS format)
|
|
774
|
+
*/
|
|
775
|
+
private formatTimeLabel(startMinute: number, endMinute: number): string {
|
|
776
|
+
const formatMinute = (min: number): string => {
|
|
777
|
+
const hours = Math.floor(min / 60);
|
|
778
|
+
const minutes = min % 60;
|
|
779
|
+
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}`;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
if (startMinute === endMinute) {
|
|
783
|
+
return `${formatMinute(startMinute)}:00 - ${formatMinute(startMinute)}:59`;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return `${formatMinute(startMinute)}:00 - ${formatMinute(endMinute)}:59`;
|
|
787
|
+
}
|
|
271
788
|
}
|
|
272
789
|
|
|
273
790
|
export default VideoMinuteResultDAO;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Knex } from "knex";
|
|
2
|
+
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
+
import { ICamera } from "../../interfaces/camera/camera.interfaces";
|
|
4
|
+
import KnexManager from "../../KnexConnection";
|
|
5
|
+
|
|
6
|
+
export class CameraDAO implements IBaseDAO<ICamera> {
|
|
7
|
+
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
|
+
|
|
9
|
+
async create(item: ICamera): Promise<ICamera> {
|
|
10
|
+
const [createdCamera] = await this._knex("cameras")
|
|
11
|
+
.insert(item)
|
|
12
|
+
.returning("*");
|
|
13
|
+
return createdCamera;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getById(id: number): Promise<ICamera | null> {
|
|
17
|
+
const camera = await this._knex("cameras").where({ id }).first();
|
|
18
|
+
return camera || null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async getByUuid(uuid: string): Promise<ICamera | null> {
|
|
22
|
+
const camera = await this._knex("cameras").where({ uuid }).first();
|
|
23
|
+
return camera || null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async update(id: number, item: Partial<ICamera>): Promise<ICamera | null> {
|
|
27
|
+
const [updatedCamera] = await this._knex("cameras")
|
|
28
|
+
.where({ id })
|
|
29
|
+
.update(item)
|
|
30
|
+
.returning("*");
|
|
31
|
+
return updatedCamera || null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async delete(id: number): Promise<boolean> {
|
|
35
|
+
const result = await this._knex("cameras").where({ id }).del();
|
|
36
|
+
return result > 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async getAll(page: number, limit: number): Promise<IDataPaginator<ICamera>> {
|
|
40
|
+
const offset = (page - 1) * limit;
|
|
41
|
+
|
|
42
|
+
const [countResult] = await this._knex("cameras").count("* as count");
|
|
43
|
+
const totalCount = +countResult.count;
|
|
44
|
+
const cameras = await this._knex("cameras").limit(limit).offset(offset);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
success: true,
|
|
48
|
+
data: cameras,
|
|
49
|
+
page,
|
|
50
|
+
limit,
|
|
51
|
+
count: cameras.length,
|
|
52
|
+
totalCount,
|
|
53
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async getByName(name: string): Promise<ICamera | null> {
|
|
58
|
+
const camera = await this._knex("cameras").where({ name }).first();
|
|
59
|
+
return camera || null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async getCamerasNearCoordinates(
|
|
63
|
+
longitude: number,
|
|
64
|
+
latitude: number,
|
|
65
|
+
radiusKm: number = 1,
|
|
66
|
+
): Promise<ICamera[]> {
|
|
67
|
+
// Using ST_DWithin for geographic distance calculation
|
|
68
|
+
// This is a PostgreSQL-specific query for geospatial operations
|
|
69
|
+
const cameras = await this._knex("cameras").whereRaw(
|
|
70
|
+
`ST_DWithin(
|
|
71
|
+
ST_MakePoint(longitude, latitude)::geography,
|
|
72
|
+
ST_MakePoint(?, ?)::geography,
|
|
73
|
+
?
|
|
74
|
+
)`,
|
|
75
|
+
[longitude, latitude, radiusKm * 1000], // Convert km to meters
|
|
76
|
+
);
|
|
77
|
+
return cameras;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -196,4 +196,53 @@ export class VideoDAO implements IBaseDAO<IVideo> {
|
|
|
196
196
|
totalPages: Math.ceil(totalCount / limit),
|
|
197
197
|
};
|
|
198
198
|
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Get videos from same folder with same type that have metadata and are COMPLETED
|
|
202
|
+
* Suitable for use as lane annotation templates
|
|
203
|
+
*/
|
|
204
|
+
async getTemplateVideos(
|
|
205
|
+
folderId: number,
|
|
206
|
+
videoType: string,
|
|
207
|
+
): Promise<IVideo[]> {
|
|
208
|
+
try {
|
|
209
|
+
let query = this._knex("video")
|
|
210
|
+
.where("folderId", folderId)
|
|
211
|
+
.where("videoType", videoType)
|
|
212
|
+
.where("status", "COMPLETED")
|
|
213
|
+
.whereNotNull("metadata")
|
|
214
|
+
.whereRaw("metadata != '{}'");
|
|
215
|
+
|
|
216
|
+
// Apply video type specific metadata validation
|
|
217
|
+
if (videoType === "ATR") {
|
|
218
|
+
// ATR videos use lanes array structure
|
|
219
|
+
query = query.whereRaw("jsonb_array_length(metadata->'lanes') > 0");
|
|
220
|
+
} else {
|
|
221
|
+
// TMC/JUNCTION/ROUNDABOUT/PATHWAY videos use objects with pt1/pt2 structure
|
|
222
|
+
// Check if metadata has at least one key with pt1 and pt2 properties
|
|
223
|
+
query = query.whereRaw(`
|
|
224
|
+
EXISTS (
|
|
225
|
+
SELECT 1
|
|
226
|
+
FROM jsonb_each(metadata) as entry(key, value)
|
|
227
|
+
WHERE key != 'lanes'
|
|
228
|
+
AND key != 'finish_line'
|
|
229
|
+
AND jsonb_typeof(value) = 'object'
|
|
230
|
+
AND value ? 'pt1'
|
|
231
|
+
AND value ? 'pt2'
|
|
232
|
+
AND jsonb_typeof(value->'pt1') = 'array'
|
|
233
|
+
AND jsonb_typeof(value->'pt2') = 'array'
|
|
234
|
+
AND jsonb_array_length(value->'pt1') = 2
|
|
235
|
+
AND jsonb_array_length(value->'pt2') = 2
|
|
236
|
+
)
|
|
237
|
+
`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const videos = await query.orderBy("updated_at", "desc").select("*");
|
|
241
|
+
|
|
242
|
+
return videos;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error("Error fetching template videos:", error);
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
199
248
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
// DAOs
|
|
2
|
-
export { UserDAO } from "./dao/user/user.dao";
|
|
3
|
-
export { StudyDAO } from "./dao/study/study.dao";
|
|
4
|
-
export { FolderDAO } from "./dao/folder/folder.dao";
|
|
5
|
-
export { VideoDAO } from "./dao/video/video.dao";
|
|
6
2
|
export { AuthDAO } from "./dao/auth/auth.dao";
|
|
7
|
-
export {
|
|
3
|
+
export { CameraDAO } from "./dao/camera/camera.dao";
|
|
8
4
|
export { ChatDAO } from "./dao/chat/chat.dao";
|
|
5
|
+
export { FolderDAO } from "./dao/folder/folder.dao";
|
|
9
6
|
export { MessageDAO } from "./dao/message/message.dao";
|
|
7
|
+
export { StudyDAO } from "./dao/study/study.dao";
|
|
8
|
+
export { UserDAO } from "./dao/user/user.dao";
|
|
9
|
+
export { UserPushNotificationTokenDAO } from "./dao/user-push-notification-token/user-push-notification-token.dao";
|
|
10
|
+
export { VideoDAO } from "./dao/video/video.dao";
|
|
10
11
|
export { VideoMinuteResultDAO } from "./dao/VideoMinuteResultDAO";
|
|
11
12
|
|
|
12
13
|
// Interfaces
|
|
13
14
|
export { IDataPaginator } from "./d.types";
|
|
14
|
-
export { IUser } from "./interfaces/user/user.interfaces";
|
|
15
|
-
export { IStudy } from "./interfaces/study/study.interfaces";
|
|
16
|
-
export { IFolder } from "./interfaces/folder/folder.interfaces";
|
|
17
|
-
export { IVideo } from "./interfaces/video/video.interfaces";
|
|
18
15
|
export { IAuth } from "./interfaces/auth/auth.interfaces";
|
|
19
|
-
export {
|
|
16
|
+
export { ICamera } from "./interfaces/camera/camera.interfaces";
|
|
20
17
|
export {
|
|
21
18
|
IChat,
|
|
22
19
|
IChatCreate,
|
|
23
20
|
IChatUpdate,
|
|
24
21
|
} from "./interfaces/chat/chat.interfaces";
|
|
22
|
+
export { IFolder } from "./interfaces/folder/folder.interfaces";
|
|
25
23
|
export {
|
|
26
24
|
IMessage,
|
|
27
25
|
IMessageCreate,
|
|
28
26
|
IMessageUpdate,
|
|
29
27
|
} from "./interfaces/message/message.interfaces";
|
|
28
|
+
export { IStudy } from "./interfaces/study/study.interfaces";
|
|
29
|
+
export { IUser } from "./interfaces/user/user.interfaces";
|
|
30
|
+
export { IUserPushNotificationToken } from "./interfaces/user-push-notification-token/user-push-notification-token.interfaces";
|
|
31
|
+
export { IVideo } from "./interfaces/video/video.interfaces";
|
|
30
32
|
export {
|
|
31
33
|
IVideoMinuteResult,
|
|
32
34
|
IVideoMinuteResultInput,
|