|
| 1 | +import datetime |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | +from django.core.exceptions import ValidationError |
| 5 | +from django.db.models import Case, CharField, Count, Exists, OuterRef, Value, When |
| 6 | +from django.db.models.functions import TruncDate |
| 7 | + |
| 8 | +from ..errors import INVALID_PARAMETERS, MISSING_PARAMETERS, PMError |
| 9 | +from ..utils import JSONHttpResponse |
| 10 | +from ..models.conference import Conference |
| 11 | +from ..models.issue import Issue |
| 12 | +from .generic_view import GenericView |
| 13 | + |
| 14 | + |
| 15 | +def _parse_iso(value): |
| 16 | + # Python 3.8 fromisoformat doesn't accept the Z suffix that JS toISOString produces. |
| 17 | + if value.endswith('Z'): |
| 18 | + value = value[:-1] + '+00:00' |
| 19 | + return datetime.datetime.fromisoformat(value) |
| 20 | + |
| 21 | + |
| 22 | +class ConferenceSummaryView(GenericView): |
| 23 | + """ |
| 24 | + Returns conference counts grouped by day, with each day's conferences |
| 25 | + bucketed into success/warning/error/ongoing. Replaces the pattern of |
| 26 | + downloading all conferences to the browser and aggregating client-side. |
| 27 | + """ |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def get(cls, request): |
| 31 | + app_id = request.GET.get('appId') |
| 32 | + if not app_id: |
| 33 | + raise PMError(status=400, app_error=MISSING_PARAMETERS) |
| 34 | + |
| 35 | + filters = {'app_id': app_id, 'is_active': True} |
| 36 | + |
| 37 | + created_at_gte = request.GET.get('created_at_gte') |
| 38 | + if created_at_gte: |
| 39 | + try: |
| 40 | + filters['created_at__gte'] = _parse_iso(created_at_gte) |
| 41 | + except ValueError: |
| 42 | + raise PMError(status=400, app_error=INVALID_PARAMETERS) |
| 43 | + |
| 44 | + created_at_lte = request.GET.get('created_at_lte') |
| 45 | + if created_at_lte: |
| 46 | + try: |
| 47 | + filters['created_at__lte'] = _parse_iso(created_at_lte) |
| 48 | + except ValueError: |
| 49 | + raise PMError(status=400, app_error=INVALID_PARAMETERS) |
| 50 | + |
| 51 | + try: |
| 52 | + rows = (Conference.objects |
| 53 | + .filter(**filters) |
| 54 | + .annotate( |
| 55 | + day=TruncDate('created_at'), |
| 56 | + has_error=Exists( |
| 57 | + Issue.objects.filter(conference=OuterRef('pk'), type='e', is_active=True) |
| 58 | + ), |
| 59 | + has_warning=Exists( |
| 60 | + Issue.objects.filter(conference=OuterRef('pk'), type='w', is_active=True) |
| 61 | + ), |
| 62 | + ) |
| 63 | + .annotate( |
| 64 | + status=Case( |
| 65 | + When(ongoing=True, then=Value('ongoing')), |
| 66 | + When(has_error=True, then=Value('error')), |
| 67 | + When(has_warning=True, then=Value('warning')), |
| 68 | + default=Value('success'), |
| 69 | + output_field=CharField(), |
| 70 | + ), |
| 71 | + ) |
| 72 | + .values('day', 'status') |
| 73 | + .annotate(count=Count('id')) |
| 74 | + .order_by('day')) |
| 75 | + except ValidationError: |
| 76 | + raise PMError(status=400, app_error=INVALID_PARAMETERS) |
| 77 | + |
| 78 | + buckets = defaultdict(lambda: {'success': 0, 'warning': 0, 'error': 0, 'ongoing': 0}) |
| 79 | + for row in rows: |
| 80 | + day_key = row['day'].isoformat() if row['day'] else None |
| 81 | + buckets[day_key][row['status']] = row['count'] |
| 82 | + |
| 83 | + data = [ |
| 84 | + { |
| 85 | + 'date': day, |
| 86 | + 'success': counts['success'], |
| 87 | + 'warning': counts['warning'], |
| 88 | + 'error': counts['error'], |
| 89 | + 'ongoing': counts['ongoing'], |
| 90 | + 'total': counts['success'] + counts['warning'] + counts['error'] + counts['ongoing'], |
| 91 | + } |
| 92 | + for day, counts in sorted(buckets.items(), key=lambda x: x[0] or '') |
| 93 | + ] |
| 94 | + |
| 95 | + return JSONHttpResponse({'data': data}) |
0 commit comments