-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.php
More file actions
4941 lines (4613 loc) · 261 KB
/
status.php
File metadata and controls
4941 lines (4613 loc) · 261 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PERTI System Status Dashboard
*
* Live operational status including:
* - Database metrics (VATSIM_ADL, VATSIM_TMI, VATSIM_GIS, VATSWIM, VATSIM_REF, PERTI)
* - External API health checks (VATSIM, Aviation Weather, NOAA)
* - Recent activity counts (parse/ETA/zone detection/boundary)
* - Resource tree visualization with all daemons
* - Stored procedures (Azure SQL + PostGIS functions)
* - Migration status across all databases
*
* Databases:
* - Azure SQL (VATSIM_ADL): Flight data, trajectories, boundaries
* - Azure SQL (VATSIM_TMI): TMI programs, slots, flight control
* - Azure SQL (VATSWIM): SWIM API data, API keys, audit logs
* - Azure SQL (VATSIM_REF): Reference data (airports, airways, waypoints)
* - PostgreSQL (VATSIM_GIS): PostGIS spatial data, route expansion
* - MySQL (VATSIM_PERTI): Application config, plans, users
*/
include("sessions/handler.php");
if (session_status() == PHP_SESSION_NONE) {
session_start();
ob_start();
}
// Start page timing
$pageStartTime = microtime(true);
include("load/config.php");
include("load/connect.php");
// Check permissions
$perm = false;
if (!defined('DEV')) {
if (isset($_SESSION['VATSIM_CID'])) {
$cid = session_get('VATSIM_CID', '');
$p_check = $conn_sqli->query("SELECT * FROM users WHERE cid='$cid'");
if ($p_check) {
$perm = true;
}
}
} else {
$perm = true;
}
// Current timestamp
$current_time = gmdate('d M Y H:i');
// =============================================================================
// LIVE DATA COLLECTION
// =============================================================================
$liveData = [
'adl_connected' => false,
'mysql_connected' => false,
'tmi_connected' => false,
'gis_connected' => false,
'swim_connected' => false,
'ref_connected' => false,
'active_flights' => 0,
'total_flights_today' => 0,
'queue_pending' => 0,
'queue_processing' => 0,
'queue_complete_1h' => 0,
'queue_failed_1h' => 0,
'queue_total' => 0,
'avg_parse_ms' => 0,
'last_vatsim_refresh' => null,
'trajectories_1h' => 0,
'trajectories_total' => 0,
'zone_transitions_1h' => 0,
// Boundary Detection (Background Job)
'boundary_crossings_1h' => 0,
'boundary_crossings_24h' => 0,
'boundary_artcc_1h' => 0,
'boundary_tracon_1h' => 0,
'boundary_pending' => 0, // Flights needing boundary detection
'last_boundary_detection' => null,
'flights_with_artcc' => 0,
'flights_with_tracon' => 0,
// Planned Crossings (Background Job)
'planned_crossings_1h' => 0,
'planned_crossings_24h' => 0,
'crossings_pending' => 0, // Flights with needs_recalc or no calc
'last_crossing_calc' => null,
'flights_with_crossings' => 0,
'crossing_tiers' => [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0],
// ETA Calculation Stats
'etas_calculated_24h' => 0,
'etas_pending' => 0,
'flights_with_eta' => 0,
'weather_alerts_active' => 0,
'atis_updates_1h' => 0,
'atis_pending' => 0,
'atis_parsed' => 0,
'atis_failed' => 0,
'atis_today' => 0,
'atis_airports_active' => 0,
'waypoints_total' => 0,
'boundaries_total' => 0,
// GIS Database Metrics (PostGIS)
'gis_nav_fixes' => 0,
'gis_airways' => 0,
'gis_airway_segments' => 0,
'gis_artcc_boundaries' => 0,
'gis_sector_boundaries' => 0,
'gis_tracon_boundaries' => 0,
'gis_playbook_routes' => 0,
'gis_airports' => 0,
'gis_postgis_version' => null,
// TMI Database Metrics
'tmi_active_programs' => 0,
'tmi_active_slots' => 0,
'tmi_controlled_flights' => 0,
// SWIM Database Metrics
'swim_active_flights' => 0,
'swim_api_keys_active' => 0,
'swim_audit_log_1h' => 0,
'swim_last_sync' => null,
// REF Database Metrics (Reference Data)
'ref_nav_fixes' => 0,
'ref_airways' => 0,
'ref_playbook_routes' => 0,
'ref_nav_procedures' => 0,
'ref_cdrs' => 0,
];
// Runtime tracking
$runtimes = [
'adl_queries' => 0,
'mysql_queries' => 0,
'api_checks' => 0,
'total' => 0,
];
$apiHealth = [
'vatsim' => ['status' => 'unknown', 'latency' => null, 'message' => 'Not checked'],
'aviationweather' => ['status' => 'unknown', 'latency' => null, 'message' => 'Not checked'],
'noaa' => ['status' => 'unknown', 'latency' => null, 'message' => 'Not checked'],
];
$overallStatus = 'operational';
$statusIssues = [];
$isHibernating = defined('HIBERNATION_MODE') && HIBERNATION_MODE;
// -----------------------------------------------------------------------------
// MySQL Connection Check
// -----------------------------------------------------------------------------
if (isset($conn_sqli) && $conn_sqli) {
$liveData['mysql_connected'] = true;
} else {
$statusIssues[] = 'MySQL connection failed';
$overallStatus = 'degraded';
}
// -----------------------------------------------------------------------------
// TMI (Azure SQL) Connection Check
// -----------------------------------------------------------------------------
if (isset($conn_tmi) && $conn_tmi !== null && $conn_tmi !== false) {
$liveData['tmi_connected'] = true;
// Active TMI programs
$sql = "SELECT COUNT(*) AS cnt FROM dbo.tmi_programs WHERE status = 'ACTIVE'";
$stmt = @sqlsrv_query($conn_tmi, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['tmi_active_programs'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Active slots count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.tmi_slots s
JOIN dbo.tmi_programs p ON p.program_id = s.program_id
WHERE p.status = 'ACTIVE'";
$stmt = @sqlsrv_query($conn_tmi, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['tmi_active_slots'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Controlled flights
$sql = "SELECT COUNT(*) AS cnt FROM dbo.tmi_flight_control fc
JOIN dbo.tmi_programs p ON p.program_id = fc.program_id
WHERE p.status = 'ACTIVE' AND fc.control_status = 'CONTROLLED'";
$stmt = @sqlsrv_query($conn_tmi, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['tmi_controlled_flights'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
}
// -----------------------------------------------------------------------------
// GIS (PostgreSQL/PostGIS) Connection Check
// -----------------------------------------------------------------------------
$conn_gis = get_conn_gis();
if ($conn_gis !== null && $conn_gis !== false) {
$liveData['gis_connected'] = true;
try {
// PostGIS version
$stmt = $conn_gis->query("SELECT PostGIS_Version() AS version");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_postgis_version'] = $row['version'] ?? 'Unknown';
}
// Navigation fixes count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM nav_fixes");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_nav_fixes'] = (int)($row['cnt'] ?? 0);
}
// Airways count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM airways");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_airways'] = (int)($row['cnt'] ?? 0);
}
// Airway segments count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM airway_segments");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_airway_segments'] = (int)($row['cnt'] ?? 0);
}
// ARTCC boundaries count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM artcc_boundaries");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_artcc_boundaries'] = (int)($row['cnt'] ?? 0);
}
// Sector boundaries count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM sector_boundaries");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_sector_boundaries'] = (int)($row['cnt'] ?? 0);
}
// TRACON boundaries count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM tracon_boundaries");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_tracon_boundaries'] = (int)($row['cnt'] ?? 0);
}
// Airports count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM airports");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_airports'] = (int)($row['cnt'] ?? 0);
}
// Playbook routes count
$stmt = $conn_gis->query("SELECT COUNT(*) AS cnt FROM playbook_routes");
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$liveData['gis_playbook_routes'] = (int)($row['cnt'] ?? 0);
}
} catch (PDOException $e) {
// GIS queries failed but connection exists
error_log("Status page GIS query error: " . $e->getMessage());
}
}
// -----------------------------------------------------------------------------
// SWIM (Azure SQL) Connection Check - VATSWIM API database
// -----------------------------------------------------------------------------
if (isset($conn_swim) && $conn_swim !== null && $conn_swim !== false) {
$liveData['swim_connected'] = true;
// Active flights in SWIM
$sql = "SELECT COUNT(*) AS cnt FROM dbo.swim_flights WHERE is_active = 1";
$stmt = @sqlsrv_query($conn_swim, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['swim_active_flights'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Active API keys
$sql = "SELECT COUNT(*) AS cnt FROM dbo.swim_api_keys WHERE is_active = 1";
$stmt = @sqlsrv_query($conn_swim, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['swim_api_keys_active'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Audit log entries in last hour
$sql = "SELECT COUNT(*) AS cnt FROM dbo.swim_audit_log WHERE request_time > DATEADD(HOUR, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_swim, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['swim_audit_log_1h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Last sync time
$sql = "SELECT TOP 1 last_sync_utc FROM dbo.swim_flights ORDER BY last_sync_utc DESC";
$stmt = @sqlsrv_query($conn_swim, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row && isset($row['last_sync_utc'])) {
$dt = $row['last_sync_utc'];
if ($dt instanceof DateTimeInterface) {
$liveData['swim_last_sync'] = $dt->format('Y-m-d H:i:s') . ' UTC';
} else {
$liveData['swim_last_sync'] = $dt;
}
}
sqlsrv_free_stmt($stmt);
}
}
// -----------------------------------------------------------------------------
// REF (Azure SQL) Connection Check - Reference Data database
// -----------------------------------------------------------------------------
if (isset($conn_ref) && $conn_ref !== null && $conn_ref !== false) {
$liveData['ref_connected'] = true;
// Nav fixes (waypoints + navaids) count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.nav_fixes";
$stmt = @sqlsrv_query($conn_ref, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['ref_nav_fixes'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Airways count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.airways";
$stmt = @sqlsrv_query($conn_ref, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['ref_airways'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Playbook routes count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.playbook_routes";
$stmt = @sqlsrv_query($conn_ref, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['ref_playbook_routes'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Nav procedures (SIDs/STARs) count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.nav_procedures";
$stmt = @sqlsrv_query($conn_ref, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['ref_nav_procedures'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Coded departure routes count
$sql = "SELECT COUNT(*) AS cnt FROM dbo.coded_departure_routes";
$stmt = @sqlsrv_query($conn_ref, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['ref_cdrs'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
}
// -----------------------------------------------------------------------------
// ADL (Azure SQL) Live Metrics
// -----------------------------------------------------------------------------
$adlQueryStart = microtime(true);
if (isset($conn_adl) && $conn_adl !== null && $conn_adl !== false) {
$liveData['adl_connected'] = true;
// Flight counts - active and today's total (use view for normalized schema)
$sql = "SELECT
COUNT(CASE WHEN is_active = 1 THEN 1 END) AS active_cnt,
COUNT(*) AS total_cnt
FROM dbo.vw_adl_flights
WHERE snapshot_utc > DATEADD(DAY, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['active_flights'] = $row['active_cnt'] ?? 0;
$liveData['total_flights_today'] = $row['total_cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Parse queue stats (comprehensive)
$sql = "
SELECT
COUNT(*) AS total,
COUNT(CASE WHEN status = 'PENDING' THEN 1 END) AS pending,
COUNT(CASE WHEN status = 'PROCESSING' THEN 1 END) AS processing,
COUNT(CASE WHEN status = 'COMPLETE' AND queued_utc > DATEADD(HOUR, -1, SYSUTCDATETIME()) THEN 1 END) AS complete_1h,
COUNT(CASE WHEN status = 'FAILED' AND queued_utc > DATEADD(HOUR, -1, SYSUTCDATETIME()) THEN 1 END) AS failed_1h,
AVG(CASE WHEN status = 'COMPLETE' THEN DATEDIFF(MILLISECOND, started_utc, completed_utc) END) AS avg_parse_ms
FROM dbo.adl_parse_queue
";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['queue_total'] = $row['total'] ?? 0;
$liveData['queue_pending'] = $row['pending'] ?? 0;
$liveData['queue_processing'] = $row['processing'] ?? 0;
$liveData['queue_complete_1h'] = $row['complete_1h'] ?? 0;
$liveData['queue_failed_1h'] = $row['failed_1h'] ?? 0;
$liveData['avg_parse_ms'] = round($row['avg_parse_ms'] ?? 0);
sqlsrv_free_stmt($stmt);
}
// Last VATSIM refresh time (use view for normalized schema)
$sql = "SELECT TOP 1 snapshot_utc FROM dbo.vw_adl_flights ORDER BY snapshot_utc DESC";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row && isset($row['snapshot_utc'])) {
$dt = $row['snapshot_utc'];
if ($dt instanceof DateTimeInterface) {
$liveData['last_vatsim_refresh'] = $dt->format('Y-m-d H:i:s') . ' UTC';
} else {
$liveData['last_vatsim_refresh'] = $dt;
}
}
sqlsrv_free_stmt($stmt);
}
// Trajectory counts
$sql = "SELECT
COUNT(CASE WHEN recorded_utc > DATEADD(HOUR, -1, SYSUTCDATETIME()) THEN 1 END) AS cnt_1h,
COUNT(*) AS cnt_total
FROM dbo.adl_flight_trajectory";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['trajectories_1h'] = $row['cnt_1h'] ?? 0;
$liveData['trajectories_total'] = $row['cnt_total'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Waypoints count (parsed route data)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_waypoints";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['waypoints_total'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Zone transitions (last hour)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_zone_events WHERE event_utc > DATEADD(HOUR, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['zone_transitions_1h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Boundary Detection Stats (Background Job)
// -------------------------------------------------------------------------
// Boundary crossings by type (last hour)
$sql = "SELECT
b.boundary_type,
COUNT(*) AS cnt
FROM dbo.adl_flight_boundary_log bl
JOIN dbo.adl_boundary b ON b.boundary_id = bl.boundary_id
WHERE bl.entry_time > DATEADD(HOUR, -1, SYSUTCDATETIME())
GROUP BY b.boundary_type";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$total = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$type = $row['boundary_type'];
$cnt = (int)($row['cnt'] ?? 0);
$total += $cnt;
if ($type === 'ARTCC') $liveData['boundary_artcc_1h'] = $cnt;
if ($type === 'TRACON') $liveData['boundary_tracon_1h'] = $cnt;
}
$liveData['boundary_crossings_1h'] = $total;
sqlsrv_free_stmt($stmt);
}
// Boundary crossings (last 24 hours) - use JOIN for consistency with 1h query
$sql = "SELECT COUNT(*) AS cnt
FROM dbo.adl_flight_boundary_log bl
JOIN dbo.adl_boundary b ON b.boundary_id = bl.boundary_id
WHERE bl.entry_time > DATEADD(HOUR, -24, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['boundary_crossings_24h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Flights pending boundary detection (grid changed or no ARTCC)
$sql = "SELECT COUNT(*) AS cnt
FROM dbo.adl_flight_core c
JOIN dbo.adl_flight_position p ON p.flight_uid = c.flight_uid
WHERE c.is_active = 1
AND p.lat IS NOT NULL
AND (
c.current_artcc_id IS NULL
OR c.last_grid_lat IS NULL
OR c.last_grid_lat != CAST(FLOOR(p.lat / 0.5) AS SMALLINT)
OR c.last_grid_lon != CAST(FLOOR(p.lon / 0.5) AS SMALLINT)
)";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['boundary_pending'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Last boundary detection time
$sql = "SELECT TOP 1 entry_time FROM dbo.adl_flight_boundary_log ORDER BY entry_time DESC";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row && $row['entry_time'] instanceof DateTime) {
$liveData['last_boundary_detection'] = $row['entry_time']->format('H:i:s') . 'Z';
}
sqlsrv_free_stmt($stmt);
}
// Flights with ARTCC/TRACON assigned
$sql = "SELECT
SUM(CASE WHEN current_artcc IS NOT NULL THEN 1 ELSE 0 END) AS with_artcc,
SUM(CASE WHEN current_tracon IS NOT NULL THEN 1 ELSE 0 END) AS with_tracon
FROM dbo.adl_flight_core WHERE is_active = 1";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['flights_with_artcc'] = $row['with_artcc'] ?? 0;
$liveData['flights_with_tracon'] = $row['with_tracon'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Planned Crossings Stats (Background Job)
// -------------------------------------------------------------------------
// Planned crossings calculated (last hour)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_planned_crossings WHERE calculated_at > DATEADD(HOUR, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['planned_crossings_1h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Planned crossings calculated (last 24 hours)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_planned_crossings WHERE calculated_at > DATEADD(HOUR, -24, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['planned_crossings_24h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Flights pending crossing calculation
$sql = "SELECT COUNT(*) AS cnt
FROM dbo.adl_flight_core
WHERE is_active = 1
AND crossing_region_flags IS NOT NULL
AND (crossing_last_calc_utc IS NULL OR crossing_needs_recalc = 1)";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['crossings_pending'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Last crossing calc time
$sql = "SELECT TOP 1 calculated_at FROM dbo.adl_flight_planned_crossings ORDER BY calculated_at DESC";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row && $row['calculated_at'] instanceof DateTime) {
$liveData['last_crossing_calc'] = $row['calculated_at']->format('H:i:s') . 'Z';
}
sqlsrv_free_stmt($stmt);
}
// Flights with crossings calculated (count distinct flights in crossings table)
$sql = "SELECT COUNT(DISTINCT pc.flight_uid) AS with_crossings
FROM dbo.adl_flight_planned_crossings pc
JOIN dbo.adl_flight_core c ON c.flight_uid = pc.flight_uid
WHERE c.is_active = 1";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['flights_with_crossings'] = $row['with_crossings'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Crossing tier distribution from core table
$sql = "SELECT
SUM(CASE WHEN crossing_tier = 1 THEN 1 ELSE 0 END) AS tier1,
SUM(CASE WHEN crossing_tier = 2 THEN 1 ELSE 0 END) AS tier2,
SUM(CASE WHEN crossing_tier = 3 THEN 1 ELSE 0 END) AS tier3,
SUM(CASE WHEN crossing_tier = 4 THEN 1 ELSE 0 END) AS tier4,
SUM(CASE WHEN crossing_tier = 5 THEN 1 ELSE 0 END) AS tier5,
SUM(CASE WHEN crossing_tier = 6 THEN 1 ELSE 0 END) AS tier6,
SUM(CASE WHEN crossing_tier = 7 THEN 1 ELSE 0 END) AS tier7
FROM dbo.adl_flight_core
WHERE is_active = 1 AND crossing_tier IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['crossing_tiers'] = [
1 => (int)($row['tier1'] ?? 0),
2 => (int)($row['tier2'] ?? 0),
3 => (int)($row['tier3'] ?? 0),
4 => (int)($row['tier4'] ?? 0),
5 => (int)($row['tier5'] ?? 0),
6 => (int)($row['tier6'] ?? 0),
7 => (int)($row['tier7'] ?? 0),
];
sqlsrv_free_stmt($stmt);
}
// Total boundaries defined
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_boundary";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['boundaries_total'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Active weather alerts
$sql = "SELECT COUNT(*) AS cnt FROM dbo.weather_alerts WHERE valid_time_to > SYSUTCDATETIME()";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['weather_alerts_active'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// ADL Refresh Procedure Step Metrics
// Step 2: New flights in last 15 minutes
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_core WHERE first_seen_utc > DATEADD(MINUTE, -15, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['new_flights_15m'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 2: Updated flights in last 15 minutes
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_core WHERE last_seen_utc > DATEADD(MINUTE, -15, SYSUTCDATETIME()) AND first_seen_utc < DATEADD(MINUTE, -15, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['updated_flights_15m'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 4: Routes queued in last 15 minutes
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_parse_queue WHERE queued_utc > DATEADD(MINUTE, -15, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['routes_queued_15m'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 4b: ETDs calculated (flights with etd_utc set recently)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_times WHERE times_updated_utc > DATEADD(MINUTE, -15, SYSUTCDATETIME()) AND etd_utc IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['etds_calculated_15m'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 4c: SimBrief parsed flights
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_plan WHERE is_simbrief = 1";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['simbrief_flights'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 8: ETAs calculated in last 15 minutes
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_times WHERE times_updated_utc > DATEADD(MINUTE, -15, SYSUTCDATETIME()) AND eta_utc IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['etas_calculated_15m'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// ETAs calculated in last 24 hours
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_times WHERE times_updated_utc > DATEADD(HOUR, -24, SYSUTCDATETIME()) AND eta_utc IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['etas_calculated_24h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Flights with ETA (active flights that have eta_utc calculated)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_times t
JOIN dbo.adl_flight_core c ON c.flight_uid = t.flight_uid
WHERE c.is_active = 1 AND t.eta_utc IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['flights_with_eta'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Flights pending ETA calculation (active flights without eta_utc)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_core c
LEFT JOIN dbo.adl_flight_times t ON t.flight_uid = c.flight_uid
WHERE c.is_active = 1 AND (t.eta_utc IS NULL OR t.flight_uid IS NULL)";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['etas_pending'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 8c: Waypoint ETAs (total waypoints with ETA)
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_waypoints WHERE eta_utc IS NOT NULL";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['waypoint_etas_total'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Step 7: Inactive flights marked recently
$sql = "SELECT COUNT(*) AS cnt FROM dbo.adl_flight_core WHERE is_active = 0 AND last_seen_utc > DATEADD(HOUR, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['inactive_flights_1h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// Last refresh timestamp from snapshot
$sql = "SELECT MAX(snapshot_utc) AS last_refresh FROM dbo.adl_flight_core WHERE is_active = 1";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row && isset($row['last_refresh'])) {
$dt = $row['last_refresh'];
if ($dt instanceof DateTimeInterface) {
$liveData['last_refresh_utc'] = $dt->format('H:i:s');
$liveData['last_refresh_ago'] = round((time() - $dt->getTimestamp()));
}
}
sqlsrv_free_stmt($stmt);
}
// Phase snapshots in last hour
$sql = "SELECT COUNT(*) AS cnt FROM dbo.flight_phase_snapshot WHERE snapshot_utc > DATEADD(HOUR, -1, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['phase_snapshots_1h'] = $row['cnt'] ?? 0;
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Tier Tracking: Parse Tiers (0-4) for route parsing priority
// Uses adl_parse_queue which has the actual tier assignments
// -------------------------------------------------------------------------
$liveData['parse_tiers'] = [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0];
$sql = "SELECT q.parse_tier, COUNT(*) AS cnt
FROM dbo.adl_parse_queue q
INNER JOIN dbo.adl_flight_core c ON q.flight_uid = c.flight_uid
WHERE c.is_active = 1 AND q.parse_tier IS NOT NULL
GROUP BY q.parse_tier
ORDER BY q.parse_tier";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$tier = (int)$row['parse_tier'];
if (isset($liveData['parse_tiers'][$tier])) {
$liveData['parse_tiers'][$tier] = (int)$row['cnt'];
}
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Tier Tracking: Trajectory Tiers (0-7) for position logging frequency
// -------------------------------------------------------------------------
$liveData['trajectory_tiers'] = [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0];
$sql = "SELECT last_trajectory_tier, COUNT(*) AS cnt
FROM dbo.adl_flight_core
WHERE is_active = 1 AND last_trajectory_tier IS NOT NULL
GROUP BY last_trajectory_tier
ORDER BY last_trajectory_tier";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$tier = (int)$row['last_trajectory_tier'];
if (isset($liveData['trajectory_tiers'][$tier])) {
$liveData['trajectory_tiers'][$tier] = (int)$row['cnt'];
}
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Tier Tracking: Parse Queue by Parse Tier
// -------------------------------------------------------------------------
$liveData['queue_by_tier'] = [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0];
$sql = "SELECT parse_tier, COUNT(*) AS cnt
FROM dbo.adl_parse_queue
WHERE status = 'PENDING'
GROUP BY parse_tier
ORDER BY parse_tier";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$tier = (int)$row['parse_tier'];
if (isset($liveData['queue_by_tier'][$tier])) {
$liveData['queue_by_tier'][$tier] = (int)$row['cnt'];
}
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Daily Stats: Routes Parsed in Last 24 Hours by Tier
// Queue cleanup retains 24h for full daily tier breakdown
// -------------------------------------------------------------------------
$liveData['daily_parsed_by_tier'] = [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0];
$liveData['daily_parsed_total'] = 0;
// Get tier breakdown from last 24 hours of completed queue entries
$sql = "SELECT q.parse_tier, COUNT(*) AS cnt
FROM dbo.adl_parse_queue q
WHERE q.status = 'COMPLETE'
AND q.completed_utc >= DATEADD(HOUR, -24, SYSUTCDATETIME())
GROUP BY q.parse_tier
ORDER BY q.parse_tier";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$tier = (int)$row['parse_tier'];
$cnt = (int)$row['cnt'];
if (isset($liveData['daily_parsed_by_tier'][$tier])) {
$liveData['daily_parsed_by_tier'][$tier] = $cnt;
}
$liveData['daily_parsed_total'] += $cnt;
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Daily Stats: Trajectory Points Logged in Last 24 Hours by Tier
// -------------------------------------------------------------------------
$liveData['daily_trajectory_by_tier'] = [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0];
$liveData['daily_trajectory_total'] = 0;
$sql = "SELECT tier, COUNT(*) AS cnt
FROM dbo.adl_flight_trajectory
WHERE recorded_utc >= DATEADD(HOUR, -24, SYSUTCDATETIME())
GROUP BY tier
ORDER BY tier";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$tier = (int)$row['tier'];
$cnt = (int)$row['cnt'];
if (isset($liveData['daily_trajectory_by_tier'][$tier])) {
$liveData['daily_trajectory_by_tier'][$tier] = $cnt;
}
$liveData['daily_trajectory_total'] += $cnt;
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Parse Success Rate (24h)
// -------------------------------------------------------------------------
$liveData['parse_success_24h'] = 0;
$liveData['parse_failed_24h'] = 0;
$liveData['parse_success_rate'] = 0;
$sql = "SELECT
COUNT(CASE WHEN status = 'COMPLETE' THEN 1 END) AS success_cnt,
COUNT(CASE WHEN status = 'FAILED' THEN 1 END) AS failed_cnt
FROM dbo.adl_parse_queue
WHERE queued_utc > DATEADD(HOUR, -24, SYSUTCDATETIME())";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['parse_success_24h'] = (int)($row['success_cnt'] ?? 0);
$liveData['parse_failed_24h'] = (int)($row['failed_cnt'] ?? 0);
$total = $liveData['parse_success_24h'] + $liveData['parse_failed_24h'];
if ($total > 0) {
$liveData['parse_success_rate'] = round(($liveData['parse_success_24h'] / $total) * 100, 1);
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Top Active Airports (departures + arrivals for active flights)
// -------------------------------------------------------------------------
$liveData['top_airports'] = [];
$sql = "SELECT TOP 5 airport, SUM(cnt) AS total
FROM (
SELECT fp.fp_dept_icao AS airport, COUNT(*) AS cnt
FROM dbo.adl_flight_core c
INNER JOIN dbo.adl_flight_plan fp ON c.flight_uid = fp.flight_uid
WHERE c.is_active = 1 AND fp.fp_dept_icao IS NOT NULL
GROUP BY fp.fp_dept_icao
UNION ALL
SELECT fp.fp_dest_icao AS airport, COUNT(*) AS cnt
FROM dbo.adl_flight_core c
INNER JOIN dbo.adl_flight_plan fp ON c.flight_uid = fp.flight_uid
WHERE c.is_active = 1 AND fp.fp_dest_icao IS NOT NULL
GROUP BY fp.fp_dest_icao
) combined
GROUP BY airport
ORDER BY total DESC";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$liveData['top_airports'][] = [
'icao' => $row['airport'],
'count' => (int)$row['total']
];
}
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// Queue Age Breakdown
// -------------------------------------------------------------------------
$liveData['queue_age'] = ['under_1m' => 0, '1_to_5m' => 0, 'over_5m' => 0];
$sql = "SELECT
COUNT(CASE WHEN DATEDIFF(SECOND, queued_utc, SYSUTCDATETIME()) < 60 THEN 1 END) AS under_1m,
COUNT(CASE WHEN DATEDIFF(SECOND, queued_utc, SYSUTCDATETIME()) BETWEEN 60 AND 300 THEN 1 END) AS m1_to_5,
COUNT(CASE WHEN DATEDIFF(SECOND, queued_utc, SYSUTCDATETIME()) > 300 THEN 1 END) AS over_5m
FROM dbo.adl_parse_queue
WHERE status = 'PENDING'";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['queue_age']['under_1m'] = (int)($row['under_1m'] ?? 0);
$liveData['queue_age']['1_to_5m'] = (int)($row['m1_to_5'] ?? 0);
$liveData['queue_age']['over_5m'] = (int)($row['over_5m'] ?? 0);
sqlsrv_free_stmt($stmt);
}
// -------------------------------------------------------------------------
// SimBrief Stats
// -------------------------------------------------------------------------
$liveData['simbrief_active'] = 0;
$liveData['simbrief_total_active'] = 0;
$liveData['simbrief_rate'] = 0;
$sql = "SELECT
COUNT(CASE WHEN fp.is_simbrief = 1 THEN 1 END) AS simbrief_cnt,
COUNT(*) AS total_cnt
FROM dbo.adl_flight_core c
LEFT JOIN dbo.adl_flight_plan fp ON c.flight_uid = fp.flight_uid
WHERE c.is_active = 1";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$liveData['simbrief_active'] = (int)($row['simbrief_cnt'] ?? 0);
$liveData['simbrief_total_active'] = (int)($row['total_cnt'] ?? 0);
if ($liveData['simbrief_total_active'] > 0) {
$liveData['simbrief_rate'] = round(($liveData['simbrief_active'] / $liveData['simbrief_total_active']) * 100, 1);
}
sqlsrv_free_stmt($stmt);
}
// SimBrief parse success comparison
$liveData['simbrief_parse_success'] = 0;
$liveData['manual_parse_success'] = 0;
$sql = "SELECT
fp.is_simbrief,
COUNT(CASE WHEN pq.status = 'COMPLETE' THEN 1 END) * 100.0 / NULLIF(COUNT(*), 0) AS success_rate
FROM dbo.adl_parse_queue pq
INNER JOIN dbo.adl_flight_plan fp ON pq.flight_uid = fp.flight_uid
WHERE pq.queued_utc > DATEADD(HOUR, -24, SYSUTCDATETIME())
AND pq.status IN ('COMPLETE', 'FAILED')
GROUP BY fp.is_simbrief";
$stmt = @sqlsrv_query($conn_adl, $sql);
if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
if ($row['is_simbrief'] == 1) {