-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreporting_engine.py
More file actions
674 lines (561 loc) · 26.3 KB
/
reporting_engine.py
File metadata and controls
674 lines (561 loc) · 26.3 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
"""
Reporting Engine for STAMPede Detection System
Generates comprehensive reports and analytics from historical data
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
from datetime import datetime, timedelta
import time
import json
import os
from enum import Enum
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import base64
from io import BytesIO
class ReportType(Enum):
DAILY_SUMMARY = "daily_summary"
WEEKLY_ANALYSIS = "weekly_analysis"
MONTHLY_REPORT = "monthly_report"
CUSTOM_PERIOD = "custom_period"
INCIDENT_REPORT = "incident_report"
PERFORMANCE_REPORT = "performance_report"
TREND_ANALYSIS = "trend_analysis"
class ChartType(Enum):
LINE = "line"
BAR = "bar"
PIE = "pie"
SCATTER = "scatter"
HEATMAP = "heatmap"
HISTOGRAM = "histogram"
BOX = "box"
@dataclass
class ReportConfig:
report_type: ReportType
start_time: float
end_time: float
camera_ids: List[int]
include_charts: bool = True
chart_format: str = "png" # png, svg, html
include_raw_data: bool = False
email_recipients: List[str] = None
output_format: str = "pdf" # pdf, html, json, csv
@dataclass
class ReportResult:
report_id: str
report_type: ReportType
generated_at: float
file_path: str
summary: Dict[str, Any]
charts: List[str] # Base64 encoded chart images
raw_data: Optional[Dict[str, Any]] = None
class ReportingEngine:
"""Generates comprehensive reports and analytics"""
def __init__(self, db_manager, output_dir: str = "reports"):
self.db_manager = db_manager
self.output_dir = output_dir
os.makedirs(output_dir, exist_ok=True)
# Set up plotting style
plt.style.use('seaborn-v0_8')
sns.set_palette("husl")
def generate_daily_summary(self, date: datetime, camera_ids: List[int] = None) -> ReportResult:
"""Generate daily summary report"""
start_time = date.replace(hour=0, minute=0, second=0, microsecond=0).timestamp()
end_time = date.replace(hour=23, minute=59, second=59, microsecond=999999).timestamp()
config = ReportConfig(
report_type=ReportType.DAILY_SUMMARY,
start_time=start_time,
end_time=end_time,
camera_ids=camera_ids or []
)
return self._generate_report(config)
def generate_weekly_analysis(self, week_start: datetime, camera_ids: List[int] = None) -> ReportResult:
"""Generate weekly analysis report"""
start_time = week_start.timestamp()
end_time = (week_start + timedelta(days=7)).timestamp()
config = ReportConfig(
report_type=ReportType.WEEKLY_ANALYSIS,
start_time=start_time,
end_time=end_time,
camera_ids=camera_ids or []
)
return self._generate_report(config)
def generate_monthly_report(self, month: int, year: int, camera_ids: List[int] = None) -> ReportResult:
"""Generate monthly report"""
start_date = datetime(year, month, 1)
if month == 12:
end_date = datetime(year + 1, 1, 1)
else:
end_date = datetime(year, month + 1, 1)
config = ReportConfig(
report_type=ReportType.MONTHLY_REPORT,
start_time=start_date.timestamp(),
end_time=end_date.timestamp(),
camera_ids=camera_ids or []
)
return self._generate_report(config)
def generate_custom_report(self, start_time: float, end_time: float,
camera_ids: List[int] = None) -> ReportResult:
"""Generate custom period report"""
config = ReportConfig(
report_type=ReportType.CUSTOM_PERIOD,
start_time=start_time,
end_time=end_time,
camera_ids=camera_ids or []
)
return self._generate_report(config)
def _generate_report(self, config: ReportConfig) -> ReportResult:
"""Generate report based on configuration"""
report_id = f"{config.report_type.value}_{int(time.time())}"
# Get data
detection_data = self._get_detection_data(config)
alert_data = self._get_alert_data(config)
# Generate summary
summary = self._generate_summary(detection_data, alert_data, config)
# Generate charts
charts = []
if config.include_charts:
charts = self._generate_charts(detection_data, alert_data, config)
# Generate raw data if requested
raw_data = None
if config.include_raw_data:
raw_data = {
'detections': detection_data.to_dict('records') if not detection_data.empty else [],
'alerts': alert_data.to_dict('records') if not alert_data.empty else []
}
# Save report
file_path = self._save_report(report_id, summary, charts, raw_data, config)
return ReportResult(
report_id=report_id,
report_type=config.report_type,
generated_at=time.time(),
file_path=file_path,
summary=summary,
charts=charts,
raw_data=raw_data
)
def _get_detection_data(self, config: ReportConfig) -> pd.DataFrame:
"""Get detection data for the specified period"""
records = self.db_manager.get_detection_records(
camera_id=None if not config.camera_ids else config.camera_ids[0],
start_time=config.start_time,
end_time=config.end_time,
limit=50000
)
if not records:
return pd.DataFrame()
# Convert to DataFrame
data = []
for record in records:
data.append({
'timestamp': record.timestamp,
'camera_id': record.camera_id,
'people_count': record.people_count,
'density': record.density,
'max_density': record.max_density,
'avg_density': record.avg_density,
'status': record.status,
'alert_level': record.alert_level,
'risk_score': record.risk_score,
'risk_level': record.risk_level,
'flow_intensity': record.flow_intensity,
'movement_direction': record.movement_direction,
'movement_risk_score': record.movement_risk_score,
'movement_risk_level': record.movement_risk_level,
'area_m2': record.area_m2
})
df = pd.DataFrame(data)
if not df.empty:
df['datetime'] = pd.to_datetime(df['timestamp'], unit='s')
df['hour'] = df['datetime'].dt.hour
df['day_of_week'] = df['datetime'].dt.day_name()
df['date'] = df['datetime'].dt.date
return df
def _get_alert_data(self, config: ReportConfig) -> pd.DataFrame:
"""Get alert data for the specified period"""
records = self.db_manager.get_alert_records(
camera_id=None if not config.camera_ids else config.camera_ids[0],
start_time=config.start_time,
end_time=config.end_time,
limit=10000
)
if not records:
return pd.DataFrame()
# Convert to DataFrame
data = []
for record in records:
data.append({
'timestamp': record.timestamp,
'camera_id': record.camera_id,
'alert_type': record.alert_type,
'alert_level': record.alert_level,
'message': record.message,
'people_count': record.people_count,
'density': record.density,
'risk_score': record.risk_score,
'acknowledged': record.acknowledged,
'acknowledged_by': record.acknowledged_by
})
df = pd.DataFrame(data)
if not df.empty:
df['datetime'] = pd.to_datetime(df['timestamp'], unit='s')
df['hour'] = df['datetime'].dt.hour
df['day_of_week'] = df['datetime'].dt.day_name()
df['date'] = df['datetime'].dt.date
return df
def _generate_summary(self, detection_data: pd.DataFrame,
alert_data: pd.DataFrame, config: ReportConfig) -> Dict[str, Any]:
"""Generate summary statistics"""
summary = {
'report_type': config.report_type.value,
'period': {
'start': datetime.fromtimestamp(config.start_time).isoformat(),
'end': datetime.fromtimestamp(config.end_time).isoformat(),
'duration_hours': (config.end_time - config.start_time) / 3600
},
'cameras': config.camera_ids,
'detection_summary': {},
'alert_summary': {},
'trends': {},
'insights': []
}
# Detection summary
if not detection_data.empty:
summary['detection_summary'] = {
'total_records': len(detection_data),
'avg_people_count': float(detection_data['people_count'].mean()),
'max_people_count': int(detection_data['people_count'].max()),
'avg_density': float(detection_data['density'].mean()),
'max_density': float(detection_data['density'].max()),
'avg_risk_score': float(detection_data['risk_score'].mean()),
'max_risk_score': float(detection_data['risk_score'].max()),
'status_distribution': detection_data['status'].value_counts().to_dict(),
'alert_level_distribution': detection_data['alert_level'].value_counts().to_dict(),
'risk_level_distribution': detection_data['risk_level'].value_counts().to_dict()
}
# Hourly patterns
if 'hour' in detection_data.columns:
hourly_stats = detection_data.groupby('hour').agg({
'people_count': ['mean', 'max'],
'density': ['mean', 'max'],
'risk_score': 'mean'
}).round(2)
summary['detection_summary']['hourly_patterns'] = hourly_stats.to_dict()
# Daily patterns
if 'day_of_week' in detection_data.columns:
daily_stats = detection_data.groupby('day_of_week').agg({
'people_count': ['mean', 'max'],
'density': ['mean', 'max'],
'risk_score': 'mean'
}).round(2)
summary['detection_summary']['daily_patterns'] = daily_stats.to_dict()
# Alert summary
if not alert_data.empty:
summary['alert_summary'] = {
'total_alerts': len(alert_data),
'acknowledged_alerts': int(alert_data['acknowledged'].sum()),
'unacknowledged_alerts': int((~alert_data['acknowledged']).sum()),
'alert_type_distribution': alert_data['alert_type'].value_counts().to_dict(),
'alert_level_distribution': alert_data['alert_level'].value_counts().to_dict(),
'avg_people_count': float(alert_data['people_count'].mean()),
'avg_density': float(alert_data['density'].mean()),
'avg_risk_score': float(alert_data['risk_score'].mean())
}
# Alert trends
if 'hour' in alert_data.columns:
hourly_alerts = alert_data.groupby('hour').size()
summary['alert_summary']['hourly_alert_patterns'] = hourly_alerts.to_dict()
# Trends analysis
if not detection_data.empty and len(detection_data) > 10:
summary['trends'] = self._analyze_trends(detection_data)
# Generate insights
summary['insights'] = self._generate_insights(detection_data, alert_data)
return summary
def _analyze_trends(self, data: pd.DataFrame) -> Dict[str, Any]:
"""Analyze trends in the data"""
trends = {}
if 'timestamp' in data.columns and 'density' in data.columns:
# Sort by timestamp
data_sorted = data.sort_values('timestamp')
# Calculate density trend
if len(data_sorted) > 1:
x = data_sorted['timestamp'].values
y = data_sorted['density'].values
# Linear regression
coeffs = np.polyfit(x, y, 1)
slope = coeffs[0]
trends['density_trend'] = {
'direction': 'increasing' if slope > 0 else 'decreasing',
'slope': float(slope),
'strength': min(1.0, abs(slope) * 1000)
}
# Calculate people count trend
if 'people_count' in data_sorted.columns:
y_people = data_sorted['people_count'].values
coeffs_people = np.polyfit(x, y_people, 1)
slope_people = coeffs_people[0]
trends['people_count_trend'] = {
'direction': 'increasing' if slope_people > 0 else 'decreasing',
'slope': float(slope_people),
'strength': min(1.0, abs(slope_people) * 100)
}
return trends
def _generate_insights(self, detection_data: pd.DataFrame,
alert_data: pd.DataFrame) -> List[str]:
"""Generate insights from the data"""
insights = []
if detection_data.empty:
insights.append("No detection data available for the specified period.")
return insights
# Density insights
max_density = detection_data['density'].max()
avg_density = detection_data['density'].mean()
if max_density > 6.0:
insights.append(f"High density detected: Maximum density reached {max_density:.2f} people/m²")
elif max_density > 4.0:
insights.append(f"Moderate crowding: Maximum density reached {max_density:.2f} people/m²")
else:
insights.append(f"Low density conditions: Maximum density was {max_density:.2f} people/m²")
# People count insights
max_people = detection_data['people_count'].max()
avg_people = detection_data['people_count'].mean()
if max_people > 20:
insights.append(f"Large crowds detected: Maximum {max_people} people observed")
elif max_people > 10:
insights.append(f"Medium crowds detected: Maximum {max_people} people observed")
else:
insights.append(f"Small crowds: Maximum {max_people} people observed")
# Risk insights
if 'risk_score' in detection_data.columns:
max_risk = detection_data['risk_score'].max()
if max_risk > 0.7:
insights.append(f"High risk periods detected: Maximum risk score {max_risk:.2f}")
elif max_risk > 0.4:
insights.append(f"Moderate risk periods: Maximum risk score {max_risk:.2f}")
# Alert insights
if not alert_data.empty:
total_alerts = len(alert_data)
unacknowledged = (~alert_data['acknowledged']).sum()
insights.append(f"Alert activity: {total_alerts} alerts generated")
if unacknowledged > 0:
insights.append(f"Attention needed: {unacknowledged} unacknowledged alerts")
# Time-based insights
if 'hour' in detection_data.columns:
peak_hour = detection_data.groupby('hour')['people_count'].mean().idxmax()
insights.append(f"Peak activity hour: {peak_hour}:00")
return insights
def _generate_charts(self, detection_data: pd.DataFrame,
alert_data: pd.DataFrame, config: ReportConfig) -> List[str]:
"""Generate charts and return as base64 encoded images"""
charts = []
if detection_data.empty:
return charts
# 1. People count over time
if 'timestamp' in detection_data.columns and 'people_count' in detection_data.columns:
chart = self._create_people_count_chart(detection_data)
if chart:
charts.append(chart)
# 2. Density over time
if 'timestamp' in detection_data.columns and 'density' in detection_data.columns:
chart = self._create_density_chart(detection_data)
if chart:
charts.append(chart)
# 3. Hourly patterns
if 'hour' in detection_data.columns:
chart = self._create_hourly_pattern_chart(detection_data)
if chart:
charts.append(chart)
# 4. Status distribution
if 'status' in detection_data.columns:
chart = self._create_status_distribution_chart(detection_data)
if chart:
charts.append(chart)
# 5. Alert timeline
if not alert_data.empty and 'timestamp' in alert_data.columns:
chart = self._create_alert_timeline_chart(alert_data)
if chart:
charts.append(chart)
# 6. Risk score distribution
if 'risk_score' in detection_data.columns:
chart = self._create_risk_distribution_chart(detection_data)
if chart:
charts.append(chart)
return charts
def _create_people_count_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create people count over time chart"""
try:
fig, ax = plt.subplots(figsize=(12, 6))
# Sample data if too many points
if len(data) > 1000:
data_sampled = data.sample(1000).sort_values('timestamp')
else:
data_sampled = data.sort_values('timestamp')
ax.plot(data_sampled['timestamp'], data_sampled['people_count'],
linewidth=1, alpha=0.7, color='blue')
ax.set_title('People Count Over Time')
ax.set_xlabel('Time')
ax.set_ylabel('People Count')
ax.grid(True, alpha=0.3)
# Format x-axis
ax.tick_params(axis='x', rotation=45)
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating people count chart: {e}")
return None
def _create_density_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create density over time chart"""
try:
fig, ax = plt.subplots(figsize=(12, 6))
# Sample data if too many points
if len(data) > 1000:
data_sampled = data.sample(1000).sort_values('timestamp')
else:
data_sampled = data.sort_values('timestamp')
ax.plot(data_sampled['timestamp'], data_sampled['density'],
linewidth=1, alpha=0.7, color='red')
# Add threshold lines
ax.axhline(y=4.0, color='orange', linestyle='--', alpha=0.7, label='Warning (4 people/m²)')
ax.axhline(y=6.0, color='red', linestyle='--', alpha=0.7, label='Danger (6 people/m²)')
ax.set_title('Density Over Time')
ax.set_xlabel('Time')
ax.set_ylabel('Density (people/m²)')
ax.legend()
ax.grid(True, alpha=0.3)
# Format x-axis
ax.tick_params(axis='x', rotation=45)
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating density chart: {e}")
return None
def _create_hourly_pattern_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create hourly pattern chart"""
try:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# People count by hour
hourly_people = data.groupby('hour')['people_count'].mean()
ax1.bar(hourly_people.index, hourly_people.values, alpha=0.7, color='blue')
ax1.set_title('Average People Count by Hour')
ax1.set_xlabel('Hour')
ax1.set_ylabel('Average People Count')
ax1.grid(True, alpha=0.3)
# Density by hour
hourly_density = data.groupby('hour')['density'].mean()
ax2.bar(hourly_density.index, hourly_density.values, alpha=0.7, color='red')
ax2.set_title('Average Density by Hour')
ax2.set_xlabel('Hour')
ax2.set_ylabel('Average Density (people/m²)')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating hourly pattern chart: {e}")
return None
def _create_status_distribution_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create status distribution pie chart"""
try:
fig, ax = plt.subplots(figsize=(8, 8))
status_counts = data['status'].value_counts()
colors = ['green', 'yellow', 'red', 'orange']
ax.pie(status_counts.values, labels=status_counts.index, autopct='%1.1f%%',
colors=colors[:len(status_counts)], startangle=90)
ax.set_title('Status Distribution')
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating status distribution chart: {e}")
return None
def _create_alert_timeline_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create alert timeline chart"""
try:
fig, ax = plt.subplots(figsize=(12, 6))
# Count alerts by hour
data['hour'] = pd.to_datetime(data['timestamp'], unit='s').dt.hour
hourly_alerts = data.groupby('hour').size()
ax.bar(hourly_alerts.index, hourly_alerts.values, alpha=0.7, color='red')
ax.set_title('Alert Timeline by Hour')
ax.set_xlabel('Hour')
ax.set_ylabel('Number of Alerts')
ax.grid(True, alpha=0.3)
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating alert timeline chart: {e}")
return None
def _create_risk_distribution_chart(self, data: pd.DataFrame) -> Optional[str]:
"""Create risk score distribution histogram"""
try:
fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(data['risk_score'], bins=20, alpha=0.7, color='purple', edgecolor='black')
ax.set_title('Risk Score Distribution')
ax.set_xlabel('Risk Score')
ax.set_ylabel('Frequency')
ax.grid(True, alpha=0.3)
return self._fig_to_base64(fig)
except Exception as e:
print(f"[ReportingEngine] Error creating risk distribution chart: {e}")
return None
def _fig_to_base64(self, fig) -> str:
"""Convert matplotlib figure to base64 string"""
buffer = BytesIO()
fig.savefig(buffer, format='png', dpi=150, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close(fig)
return image_base64
def _save_report(self, report_id: str, summary: Dict[str, Any],
charts: List[str], raw_data: Optional[Dict[str, Any]],
config: ReportConfig) -> str:
"""Save report to file"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{report_id}_{timestamp}.json"
filepath = os.path.join(self.output_dir, filename)
report_data = {
'report_id': report_id,
'generated_at': time.time(),
'config': {
'report_type': config.report_type.value,
'start_time': config.start_time,
'end_time': config.end_time,
'camera_ids': config.camera_ids
},
'summary': summary,
'charts': charts,
'raw_data': raw_data
}
with open(filepath, 'w') as f:
json.dump(report_data, f, indent=2, default=str)
print(f"[ReportingEngine] Report saved: {filepath}")
return filepath
def get_report_list(self) -> List[Dict[str, Any]]:
"""Get list of available reports"""
reports = []
for filename in os.listdir(self.output_dir):
if filename.endswith('.json'):
filepath = os.path.join(self.output_dir, filename)
try:
with open(filepath, 'r') as f:
report_data = json.load(f)
reports.append({
'report_id': report_data['report_id'],
'generated_at': report_data['generated_at'],
'report_type': report_data['config']['report_type'],
'file_path': filepath,
'summary': report_data['summary']
})
except Exception as e:
print(f"[ReportingEngine] Error reading report {filename}: {e}")
return sorted(reports, key=lambda x: x['generated_at'], reverse=True)
def get_report(self, report_id: str) -> Optional[Dict[str, Any]]:
"""Get specific report by ID"""
for filename in os.listdir(self.output_dir):
if filename.startswith(report_id):
filepath = os.path.join(self.output_dir, filename)
try:
with open(filepath, 'r') as f:
return json.load(f)
except Exception as e:
print(f"[ReportingEngine] Error reading report {filename}: {e}")
return None