diff --git a/core/core/metrics.py b/core/core/metrics.py index 22041bf..2b1f4a6 100644 --- a/core/core/metrics.py +++ b/core/core/metrics.py @@ -4,6 +4,8 @@ from django.db.models import Case, Value, When, BooleanField, Count from inventory.models import Item from tickets.models import IssueThread +from itertools import groupby + class ItemCountCollector(object): @@ -17,16 +19,16 @@ class ItemCountCollector(object): queryset = ( Item.all_objects - .annotate( - returned=Case( - When(returned_at__isnull=True, then=Value(False)), - default=Value(True), - output_field=BooleanField() - ) + .annotate( + returned=Case( + When(returned_at__isnull=True, 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 + ) + .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: @@ -34,27 +36,23 @@ class ItemCountCollector(object): yield counter + class TicketCountCollector(object): def collect(self): counter = CounterMetricFamily("c3lf_ticket_count", "Current number of tickets", labels=['event', 'event_id']) - yield counter - if not apps.models_ready or not apps.apps_ready: return - queryset = ( - IssueThread.objects - .values('event__slug', 'event_id') - .annotate(amount=Count('id')) - ) - - for e in queryset: - counter.add_metric([e["event__slug"].lower()], e["amount"]) + g = groupby(IssueThread.objects.all().prefetch_related('event', 'state_changes'), + lambda x: (x.event.slug, x.state)) + for (event, state), v in g: + counter.add_metric([event.lower(), state.lower()], len(list(v))) yield counter + try: REGISTRY.register(ItemCountCollector()) REGISTRY.register(TicketCountCollector()) diff --git a/deploy/dev/docker-compose.yml b/deploy/dev/docker-compose.yml index e44c276..30c8c88 100644 --- a/deploy/dev/docker-compose.yml +++ b/deploy/dev/docker-compose.yml @@ -7,6 +7,7 @@ services: environment: - HTTP_HOST=core - DB_FILE=dev.db + - DEBUG_MODE_ACTIVE=yup volumes: - ../../core:/code - ../testdata.py:/code/testdata.py diff --git a/deploy/testing/docker-compose.yml b/deploy/testing/docker-compose.yml index b41dd63..91eb540 100644 --- a/deploy/testing/docker-compose.yml +++ b/deploy/testing/docker-compose.yml @@ -30,6 +30,7 @@ services: - DB_USER=system3 - DB_PASSWORD=system3 - MAIL_DOMAIN=mail:1025 + - DEBUG_MODE_ACTIVE=certainly volumes: - ../../core:/code - ../testdata.py:/code/testdata.py