metrics: Make metrics actually usable
All checks were successful
/ test (push) Successful in 2m40s
/ deploy (push) Successful in 4m25s

With this commit, we should get stats for the item count per event per
returned-state.
This commit is contained in:
lagertonne 2024-12-23 11:42:27 +01:00
parent 06c0d6cf44
commit 2886d887f5
8 changed files with 33 additions and 56 deletions

View file

@ -1,8 +0,0 @@
from django.core.management.base import BaseCommand, CommandError
from core.metrics import update_item_count
class Command(BaseCommand):
help = "Refresh the metrics"
def handle(self, *args, **options):
update_item_count()

View file

@ -1,11 +1,35 @@
from prometheus_client import Gauge
from django_prometheus import exports
from inventory.models import *
from django.apps import apps
from prometheus_client.core import CounterMetricFamily, REGISTRY
from django.db.models import Case, Value, When, BooleanField, Count
from inventory.models import Item
g_items_total = Gauge('c3lf_items_total', 'Current Total items')
class ItemCountCollector(object):
def update_item_count():
# Get the count of MyModel objects
count = Item.objects.count()
# Set the gauge to the current count
g_items_total.set(count)
def collect(self):
counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'state'])
yield counter
if not apps.models_ready or not apps.apps_ready:
return
queryset = (
Item.all_objects
.annotate(
returned=Case(
When(returned_at__isnull=False, then=Value(False)),
default=Value(True),
output_field=BooleanField()
)
)
.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"])
yield counter
REGISTRY.register(ItemCountCollector())

View file

@ -70,7 +70,6 @@ INSTALLED_APPS = [
'inventory',
'mail',
'notify_sessions',
'core'
]
REST_FRAMEWORK = {