diff --git a/posthog/api/app_metrics.py b/posthog/api/app_metrics.py index 8e0c4c6e2c6c0..7107f76a443c5 100644 --- a/posthog/api/app_metrics.py +++ b/posthog/api/app_metrics.py @@ -2,7 +2,7 @@ import uuid from typing import Any -from django.db.models import Sum +from django.db.models import Q, Sum from django.db.models.functions import Coalesce, TruncDay from rest_framework import mixins, request, response, viewsets from rest_framework.decorators import action @@ -82,13 +82,14 @@ def get_batch_export_runs_app_metrics_queryset(self, batch_export_id: str): ``` select date_trunc('day', last_updated_at) as dates, - sum(coalesce(records_completed, 0)) as successes, - sum(coalesce(records_total_count, 0)) - sum(coalesce(records_completed, 0)) as failures + sum(case when status = 'Completed' then coalesce(records_total_count, 0) else 0) as successes, + sum(case when status != 'Completed' then coalesce(records_total_count, 0) else 0) as failures from posthog_batchexportrun where batch_export_id = :batch_export_id and last_updated_at between :date_from and :date_to + and status != 'Running' group by date_trunc('day', last_updated_at) order by @@ -117,8 +118,8 @@ def get_batch_export_runs_app_metrics_queryset(self, batch_export_id: str): .annotate(dates=TruncDay("last_updated_at")) .values("dates") .annotate( - successes=Sum(Coalesce("records_completed", 0)), - failures=Sum(Coalesce("records_total_count", 0)) - Sum(Coalesce("records_completed", 0)), + successes=Sum(Coalesce("records_total_count", 0), filter=Q(status=BatchExportRun.Status.COMPLETED)), + failures=Sum(Coalesce("records_total_count", 0), filter=~Q(status=BatchExportRun.Status.COMPLETED)), ) .order_by("dates") .all()