Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats): collecter le nombre de diag parcours iae réalisés quotidiennement #658

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions clevercloud/collect_daily_django_stats.sh
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
1 change: 1 addition & 0 deletions clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"0 3 * * 0 $ROOT/clevercloud/extract_tables.sh",
"*/10 * * * * $ROOT/clevercloud/rebuild_index.sh",
"0 5 * * * $ROOT/clevercloud/collect_daily_matomo_stats.sh",
"3 5 * * * $ROOT/clevercloud/collect_daily_django_stats.sh",
"5 5 1 * * $ROOT/clevercloud/collect_monthly_matomo_stats.sh",
"5 7-21 * * * $ROOT/clevercloud/send_notifs_when_first_reply.sh",
"5 6 * * * $ROOT/clevercloud/send_notifs_when_following_replies.sh",
Expand Down
10 changes: 10 additions & 0 deletions lacommunaute/forum_stats/factories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import factory
import factory.django

Expand All @@ -13,3 +15,11 @@ class StatFactory(factory.django.DjangoModelFactory):

class Meta:
model = Stat

class Params:
for_dsp_snapshot = factory.Trait(
date=datetime.date(2024, 5, 17),
name="dsp",
value=46,
period="day",
)
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 lacommunaute/forum_stats/tests/tests_management_commands.py
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!"
7 changes: 7 additions & 0 deletions lacommunaute/surveys/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import factory
from dateutil.relativedelta import relativedelta

from lacommunaute.surveys.models import DSP
from lacommunaute.surveys.recommendations import get_recommendations
Expand Down Expand Up @@ -75,3 +76,9 @@ class Params:
def recommendations(obj, create, extracted, **kwargs):
if create and isinstance(extracted, int):
obj.recommendations.set(get_recommendations(obj))

@factory.post_generation
def months_ago(obj, create, extracted, **kwargs):
if create and isinstance(extracted, int):
obj.created = obj.created - relativedelta(months=extracted)
obj.save()
29 changes: 29 additions & 0 deletions lacommunaute/surveys/stats.py
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]))
35 changes: 35 additions & 0 deletions lacommunaute/surveys/tests/test_stats.py
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
Loading