Skip to content

Commit

Permalink
Merge pull request #1142 from betagouv/feat/stats-objectgroup-detail
Browse files Browse the repository at this point in the history
Add detailed objects to analyzed object stats
  • Loading branch information
wiwski authored Nov 20, 2024
2 parents f0e199f + 9d8d41b commit a7fc766
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lab/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db.models import F, Sum
from django.utils import timezone

from .models import ObjectGroup, Project, Run
from .models import Object, ObjectGroup, Project, Run

StatPeriodLiteral = Literal["all", "year"]

Expand Down Expand Up @@ -39,11 +39,16 @@ def get_total_projects(period: StatPeriodLiteral):
return qs.count()

@staticmethod
def get_total_object_groups(period: StatPeriodLiteral):
def get_total_analyzed_objects(period: StatPeriodLiteral):
run_qs = Run.objects.filter(end_date__lte=timezone.now())
if period == "year":
run_qs = run_qs.filter(start_date__gte=THIS_YEAR_START_DT)
return ObjectGroup.objects.filter(runs__in=run_qs).count()
object_group_qs = ObjectGroup.objects.filter(runs__in=run_qs)
object_qs = Object.objects.filter(group__in=object_group_qs)
object_group_count = object_group_qs.count()
object_count = object_qs.count()
distinct_object_group_count = object_qs.values("group").distinct().count()
return object_group_count + object_count - distinct_object_group_count

@staticmethod
def get_total_hours(period: StatPeriodLiteral):
Expand All @@ -64,12 +69,12 @@ def resolve_stats(self, info):
return {
"all": LabStatType(
total_projects=LabStatsType.get_total_projects("all"),
total_object_groups=LabStatsType.get_total_object_groups("all"),
total_object_groups=LabStatsType.get_total_analyzed_objects("all"),
total_hours=LabStatsType.get_total_hours("all"),
),
"year": LabStatType(
total_projects=LabStatsType.get_total_projects("year"),
total_object_groups=LabStatsType.get_total_object_groups("year"),
total_object_groups=LabStatsType.get_total_analyzed_objects("year"),
total_hours=LabStatsType.get_total_hours("year"),
),
}
Expand Down
4 changes: 2 additions & 2 deletions lab/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def test_resolve_stats(client):
assert executed == {
"data": {
"stats": {
"all": {"totalProjects": 3, "totalObjectGroups": 3, "totalHours": 18},
"year": {"totalProjects": 2, "totalObjectGroups": 2, "totalHours": 12},
"all": {"totalProjects": 3, "totalObjectGroups": 9, "totalHours": 18},
"year": {"totalProjects": 2, "totalObjectGroups": 6, "totalHours": 12},
}
}
}
Expand Down

0 comments on commit a7fc766

Please sign in to comment.