-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stats): collecter le nombre de diag parcours iae réalisés quotid…
…iennement (#658) ## Description 🎸 Enregistrer dans la table `Stat`, sous le nom `dsp`, le nombre de `DSP` créés chaque jour. ## Type de changement 🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une fonctionnalité). ### Points d'attention 🦺 execution quotidienne de la management command à 5h03 🦺 stats quotidiennes uniquement pour l'instant 🦺 pas de valeur enregistrée si aucun `DSP` créé dans une jounée. Peut être un cas limite pour la génération du graphe
- Loading branch information
1 parent
804be3c
commit 7205a2b
Showing
8 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash -l | ||
|
||
# Collect Daily Matomo Stats | ||
|
||
# | ||
# About clever cloud cronjobs: | ||
# https://www.clever-cloud.com/doc/tools/crons/ | ||
# | ||
|
||
if [[ "$INSTANCE_NUMBER" != "0" ]]; then | ||
echo "Instance number is ${INSTANCE_NUMBER}. Stop here." | ||
exit 0 | ||
fi | ||
|
||
# $APP_HOME is set by default by clever cloud. | ||
cd $APP_HOME | ||
|
||
python manage.py collect_django_stats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
lacommunaute/forum_stats/management/commands/collect_django_stats.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from lacommunaute.surveys.stats import collect_dsp_stats | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Collecter les stats django, jusqu'à la veille de l'execution" | ||
|
||
def handle(self, *args, **options): | ||
from_date, count = collect_dsp_stats() | ||
self.stdout.write(self.style.SUCCESS(f"Collecting DSP stats from {from_date} to yesterday: {count} new stats")) | ||
self.stdout.write(self.style.SUCCESS("That's all, folks!")) |
13 changes: 13 additions & 0 deletions
13
lacommunaute/forum_stats/tests/tests_management_commands.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest # noqa | ||
|
||
from django.core.management import call_command | ||
from lacommunaute.surveys.factories import DSPFactory | ||
from lacommunaute.forum_stats.factories import StatFactory | ||
|
||
|
||
def test_collect_django_stats(db, capsys): | ||
DSPFactory() | ||
StatFactory(for_dsp_snapshot=True) | ||
call_command("collect_django_stats") | ||
captured = capsys.readouterr() | ||
assert captured.out.strip() == "Collecting DSP stats from 2024-05-18 to yesterday: 1 new stats\nThat's all, folks!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from datetime import date, datetime, time | ||
|
||
from dateutil.relativedelta import relativedelta | ||
from django.db.models import Count, Value | ||
from django.db.models.functions import TruncDate | ||
from django.utils import timezone | ||
|
||
from lacommunaute.forum_stats.models import Stat | ||
from lacommunaute.surveys.models import DSP | ||
|
||
|
||
def collect_dsp_stats(): | ||
period = "day" | ||
|
||
from_date = ( | ||
Stat.objects.filter(name="dsp", period=period).latest("date").date + relativedelta(days=1) | ||
if Stat.objects.filter(name="dsp", period=period).exists() | ||
else date(2024, 1, 1) | ||
) | ||
|
||
stats = ( | ||
DSP.objects.filter(created__gte=timezone.make_aware(datetime.combine(from_date, time()))) | ||
.annotate(date=TruncDate("created")) | ||
.values("date") | ||
.annotate(value=Count("id"), name=Value("dsp"), period=Value("day")) | ||
.order_by("date") | ||
) | ||
|
||
return from_date, len(Stat.objects.bulk_create([Stat(**stat) for stat in stats])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest # noqa | ||
from datetime import datetime, date | ||
from dateutil.relativedelta import relativedelta | ||
from lacommunaute.forum_stats.models import Stat | ||
from lacommunaute.surveys.factories import DSPFactory | ||
from lacommunaute.surveys.stats import collect_dsp_stats | ||
|
||
|
||
def test_collect_dsp_stats(db): | ||
# no stat collected | ||
assert collect_dsp_stats() == (date(2024, 1, 1), 0) | ||
|
||
# one stat to collected for one dsp | ||
DSPFactory(months_ago=3) | ||
assert collect_dsp_stats() == (date(2024, 1, 1), 1) | ||
stat = Stat.objects.get() | ||
assert stat.name == "dsp" | ||
assert stat.period == "day" | ||
assert stat.value == 1 | ||
assert stat.date == datetime.now().date() - relativedelta(months=3) | ||
|
||
# two stats to collected for two dsp, one by month | ||
DSPFactory(months_ago=2) | ||
DSPFactory(months_ago=1) | ||
assert collect_dsp_stats() == (stat.date + relativedelta(days=1), 2) | ||
assert Stat.objects.count() == 3 | ||
|
||
# one stat to collected for three dsp in the same month | ||
DSPFactory.create_batch(3) | ||
assert collect_dsp_stats() == (datetime.now().date() - relativedelta(months=1) + relativedelta(days=1), 1) | ||
assert Stat.objects.count() == 4 | ||
|
||
# no new stat to collect | ||
assert collect_dsp_stats() == (datetime.now().date() + relativedelta(days=1), 0) | ||
assert Stat.objects.count() == 4 |