Files
Juan DenisandClaude Fable 5 5417c39100 perf(models): index every foreign-key column
An audit found 102 FK columns with no index — every join, per-parent
listing, and the delete cascades from _delete_cascade_policy scanned
whole tables on them (deployments.app_id, audit_logs.user_id,
dns_records.zone_id, webhook_logs.webhook_id, ...). All FK columns now
declare index=True; migration 096 creates the same indexes on existing
installs, deriving the list from the live schema at upgrade time (so
it is idempotent and cannot drift from what an install actually has).

Ratchet: test_every_fk_column_is_indexed pins the rule with no
allowlist — a new FK column is born indexed or the build fails.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 20:42:53 -04:00

45 lines
1.6 KiB
Python

"""Per-domain daily bandwidth rollups.
One row per (domain, day) aggregated from the nginx access logs by
``BandwidthService.aggregate`` (job kind ``bandwidth.aggregate``). ``app_id``
is a best-effort attribution via the Domain/Application tables and is
nullable so traffic to domains the panel no longer manages is still recorded.
"""
from datetime import datetime
from app import db
class SiteBandwidthDaily(db.Model):
__tablename__ = 'site_bandwidth_daily'
__table_args__ = (
db.UniqueConstraint('domain', 'day', name='uq_site_bandwidth_domain_day'),
)
id = db.Column(db.Integer, primary_key=True)
app_id = db.Column(
db.Integer,
db.ForeignKey('applications.id', ondelete='CASCADE'),
nullable=True,
index=True,
)
domain = db.Column(db.String(255), nullable=False, index=True)
day = db.Column(db.Date, nullable=False, index=True)
bytes_sent = db.Column(db.BigInteger, nullable=False, default=0)
requests = db.Column(db.Integer, nullable=False, default=0)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def to_dict(self):
return {
'id': self.id,
'app_id': self.app_id,
'domain': self.domain,
'day': self.day.isoformat() if self.day else None,
'bytes_sent': int(self.bytes_sent or 0),
'requests': int(self.requests or 0),
'created_at': self.created_at.isoformat() if self.created_at else None,
}
def __repr__(self):
return f'<SiteBandwidthDaily {self.domain} {self.day}>'