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
+22 -10
View File
@@ -219,20 +219,32 @@ def plan():
'allow_custom_domain': tenant.allow_custom_domain,
}
# Live counts (tenant DB)
# Live counts (tenant DB), one axis at a time.
#
# These used to be four calls inside a single dict literal in one
# try/except. Python evaluates every value before assigning, so ONE
# failing counter discarded the whole dict and the page rendered 0
# for all four axes — including users and facilities, which were
# fine. That is exactly how a wrong column name in the issues
# counter presented as "no usage number ever updates".
from app.tenancy.quota import (
count_active_users, count_active_facilities,
count_inspections_this_month, count_issues_this_month,
)
try:
quota_usage = {
'users': count_active_users(),
'facilities': count_active_facilities(),
'inspections': count_inspections_this_month(),
'issues': count_issues_this_month(),
}
except Exception as exc:
logger.error('tenant_settings.plan: quota count failed: %s', exc)
for axis, counter in (
('users', count_active_users),
('facilities', count_active_facilities),
('inspections', count_inspections_this_month),
('issues', count_issues_this_month),
):
try:
quota_usage[axis] = counter()
except Exception as exc:
# None (not 0) so the page shows "—": an unknown count and
# a genuine zero must not look the same.
quota_usage[axis] = None
logger.error('tenant_settings.plan: %s count failed: %s',
axis, exc)
# MT-8: billing fields are already on g.tenant — no extra DB query needed.
billing_enabled = current_app.config.get('BILLING_ENABLED', False)