Spaces:
Paused
Paused
Upload core/models/performance.py with huggingface_hub
Browse files- core/models/performance.py +62 -0
core/models/performance.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Performance Monitoring Models
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
from sqlalchemy import Boolean, Column, DateTime, Float, Index, String
|
| 8 |
+
|
| 9 |
+
from core.models.base import Base, utc_now
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class PerformanceMetric(Base):
|
| 13 |
+
"""
|
| 14 |
+
Performance Metrics Model
|
| 15 |
+
Stores frontend Web Vitals and other performance indicators.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
__tablename__ = "performance_metrics"
|
| 19 |
+
|
| 20 |
+
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
| 21 |
+
timestamp = Column(DateTime, default=utc_now, nullable=False)
|
| 22 |
+
|
| 23 |
+
# Context
|
| 24 |
+
user_id = Column(String, nullable=True, index=True)
|
| 25 |
+
url = Column(String, nullable=False, index=True)
|
| 26 |
+
user_agent = Column(String, nullable=False)
|
| 27 |
+
|
| 28 |
+
# Web Vitals (all in milliseconds except CLS)
|
| 29 |
+
lcp = Column(Float, nullable=True) # Largest Contentful Paint
|
| 30 |
+
fid = Column(Float, nullable=True) # First Input Delay
|
| 31 |
+
cls = Column(Float, nullable=True) # Cumulative Layout Shift
|
| 32 |
+
fcp = Column(Float, nullable=True) # First Contentful Paint
|
| 33 |
+
ttfb = Column(Float, nullable=True) # Time to First Byte
|
| 34 |
+
|
| 35 |
+
# Additional Metrics
|
| 36 |
+
dom_content_loaded = Column(Float, nullable=True)
|
| 37 |
+
load_complete = Column(Float, nullable=True)
|
| 38 |
+
dom_interactive = Column(Float, nullable=True)
|
| 39 |
+
|
| 40 |
+
# Analysis
|
| 41 |
+
is_violation = Column(Boolean, default=False, nullable=False)
|
| 42 |
+
violation_details = Column(String, nullable=True)
|
| 43 |
+
|
| 44 |
+
__table_args__ = (
|
| 45 |
+
Index("idx_perf_timestamp", "timestamp"),
|
| 46 |
+
Index("idx_perf_url_timestamp", "url", "timestamp"),
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def to_dict(self):
|
| 50 |
+
return {
|
| 51 |
+
"id": self.id,
|
| 52 |
+
"timestamp": self.timestamp.isoformat(),
|
| 53 |
+
"url": self.url,
|
| 54 |
+
"metrics": {
|
| 55 |
+
"lcp": self.lcp,
|
| 56 |
+
"fid": self.fid,
|
| 57 |
+
"cls": self.cls,
|
| 58 |
+
"fcp": self.fcp,
|
| 59 |
+
"ttfb": self.ttfb
|
| 60 |
+
},
|
| 61 |
+
"is_violation": self.is_violation
|
| 62 |
+
}
|