From 17785282ffef39a4bbe1b081090ca6d2207d6730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Far=C3=ADas=20Santana?= Date: Thu, 25 Apr 2024 15:13:39 +0200 Subject: [PATCH] fix: Sum only records total count for app metrics (#21847) --- posthog/api/app_metrics.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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()