-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
95 lines (77 loc) · 3.46 KB
/
models.py
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
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, Boolean, ForeignKey, JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime
import json
Base = declarative_base()
class Zone(Base):
__tablename__ = 'zones'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
description = Column(String(255))
coordinates = Column(JSON, nullable=False) # GeoJSON format
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
cameras = relationship("ZoneCamera", back_populates="zone")
detections = relationship("Detection", back_populates="zone")
class ZoneCamera(Base):
__tablename__ = 'zone_cameras'
id = Column(Integer, primary_key=True)
zone_id = Column(Integer, ForeignKey('zones.id'))
camera_id = Column(String(100), nullable=False)
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
zone = relationship("Zone", back_populates="cameras")
class Detection(Base):
__tablename__ = 'detections'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, nullable=False)
camera_id = Column(String(100), nullable=False)
zone_id = Column(Integer, ForeignKey('zones.id'))
vehicle_type = Column(String(50))
make = Column(String(50))
model = Column(String(50))
confidence = Column(Float)
image_path = Column(String(255))
extra_data = Column(JSON) # Additional detection data
# Relationships
zone = relationship("Zone", back_populates="detections")
matches = relationship("TargetMatch", back_populates="detection")
class TargetMatch(Base):
__tablename__ = 'target_matches'
id = Column(Integer, primary_key=True)
detection_id = Column(Integer, ForeignKey('detections.id'))
target_make = Column(String(50))
target_model = Column(String(50))
confidence = Column(Float)
matched_at = Column(DateTime, default=datetime.utcnow)
is_verified = Column(Boolean, default=False)
extra_data = Column(JSON) # Additional match data
# Relationships
detection = relationship("Detection", back_populates="matches")
class SystemMetric(Base):
__tablename__ = 'system_metrics'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, default=datetime.utcnow)
metric_type = Column(String(50), nullable=False) # e.g., 'latency', 'memory', 'cpu'
value = Column(Float, nullable=False)
unit = Column(String(20)) # e.g., 'ms', 'MB', '%'
component = Column(String(50)) # e.g., 'detector', 'classifier', 'dashboard'
extra_data = Column(JSON) # Additional metric data
class PerformanceLog(Base):
__tablename__ = 'performance_logs'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, default=datetime.utcnow)
event_type = Column(String(50), nullable=False)
severity = Column(String(20), nullable=False) # 'info', 'warning', 'error', 'critical'
message = Column(String(255))
component = Column(String(50))
extra_data = Column(JSON) # Additional log data
def init_db(db_url):
"""Initialize the database and create all tables"""
engine = create_engine(db_url)
Base.metadata.create_all(engine)
return engine