fix bug in initial db creation caused by prometheus lib rtying to access tables at load time
All checks were successful
/ test (push) Successful in 2m29s
/ deploy (push) Successful in 4m31s

This commit is contained in:
j3d1 2025-03-09 18:47:59 +01:00
parent 6b0def543c
commit b8ccacb614

View file

@ -3,18 +3,20 @@ from prometheus_client.core import CounterMetricFamily, REGISTRY
from django.db.models import Case, Value, When, BooleanField, Count
from inventory.models import Item
class ItemCountCollector(object):
def collect(self):
counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state'])
try:
counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state'])
yield counter
yield counter
if not apps.models_ready or not apps.apps_ready:
return
if not apps.models_ready or not apps.apps_ready:
return
queryset = (
Item.all_objects
queryset = (
Item.all_objects
.annotate(
returned=Case(
When(returned_at__isnull=True, then=Value(False)),
@ -25,11 +27,14 @@ class ItemCountCollector(object):
.values('event__slug', 'returned', 'event_id')
.annotate(amount=Count('id'))
.order_by('event__slug', 'returned') # Optional: order by slug and returned
)
)
for e in queryset:
counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"])
for e in queryset:
counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"])
yield counter
yield counter
except:
pass
REGISTRY.register(ItemCountCollector())
REGISTRY.register(ItemCountCollector())