@trafficgroup/knex-rel 0.0.29 → 0.1.0

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.
@@ -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;
@@ -16,6 +16,15 @@ export interface IVideo {
16
16
  remainingTime: string;
17
17
  results: Record<string, any>;
18
18
  durationSeconds?: number; // Add optional duration field
19
+
20
+ // HLS streaming support
21
+ isHlsEnabled: boolean;
22
+ hlsPlaylist: string | null;
23
+ videoSizeMB: number | null;
24
+
25
+ // Enhanced metadata for hybrid streaming
26
+ streamingMetadata: Record<string, any> | null;
27
+
19
28
  created_at: string;
20
29
  updated_at: string;
21
30
  folder?: IFolder;