Skip to content

Commit

Permalink
fix: Sum only records total count for app metrics (#21847)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias authored Apr 25, 2024
1 parent 494e83d commit 1778528
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions posthog/api/app_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1778528

Please sign in to comment.