Aug 21 - Fix plan usage counter

This commit is contained in:
2026-08-21 09:40:25 -04:00
parent 6371b13c13
commit cb7c244872
4 changed files with 211 additions and 20 deletions
+23 -5
View File
@@ -9,7 +9,7 @@ no second control-DB round-trip is needed.
Quota axes:
inspections → Inspection.inspection_date in current month, status='completed'
issues → Issue.created_at in current month
issues → Issue.reported_at in current month
users → User.active == True (total, not monthly)
facilities → Facility.active == True (total, not monthly)
@@ -18,10 +18,11 @@ all quota checks pass, single-tenant behaviour unchanged.
"""
import logging
from datetime import datetime
from flask import g, current_app
from app.utils.time_utils import now_eastern
logger = logging.getLogger(__name__)
@@ -30,7 +31,15 @@ def _mt_enabled():
def _month_window():
now = datetime.now()
"""[start, end) of the current month, in the timezone the rows are stamped in.
now_eastern(), not datetime.now(): every timestamp in the tenant DB is
written by now_eastern() (rule 2). On a UTC server the two differ by 4-5
hours, so a plain now() puts the month boundary in the wrong place and the
first hours of each month count the wrong rows — a discrepancy that only
appears on the 1st and is gone before anyone investigates it.
"""
now = now_eastern()
start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
if now.month == 12:
end = now.replace(year=now.year + 1, month=1, day=1,
@@ -52,11 +61,20 @@ def count_inspections_this_month():
def count_issues_this_month():
"""Issues filed this month.
`reported_at`, NOT `created_at` — the issues table has no created_at
column. (IssueComment does, in the same module, which is how the wrong name
got here.) Referencing a missing column raises AttributeError while the
query is built, and every caller wraps this in a try/except, so the failure
was invisible: the plan page silently showed 0 for EVERY axis and the
issues quota was never evaluated at all.
"""
from app.models.issue import Issue
start, end = _month_window()
return (Issue.query
.filter(Issue.created_at >= start,
Issue.created_at < end)
.filter(Issue.reported_at >= start,
Issue.reported_at < end)
.count())