-
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 les stats d'activité hebdo des forums de la do…
…cumentation (#691) ## Description 🎸 Interroger matomo pour collecter le nombre de visites uniques, le nombre de visites uniques entrantes et le temps passé sur les `forum` de l'espace documentation hebdomadairement 🎸 Management command planifiée chaque lundi 🎸 Capacité à reprendre un traitement échoué, en recherchant la plus récente date d'execution dans `ForumStat` ## Type de changement 🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une fonctionnalité). ### Points d'attention 🦺 Collecte limitée aux `forum` de l'espace documentation 🦺 La collecte des données mensuelles est prévue dans une PR ultérieure 🦺 harmonisation de la déclaration du modèle `Stat` dans le fichier `admin.py`
- Loading branch information
1 parent
48cdd07
commit ab513a8
Showing
11 changed files
with
398 additions
and
9 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_matomo_forum_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
from django.contrib import admin | ||
|
||
from lacommunaute.stats.models import Stat | ||
from lacommunaute.stats.models import ForumStat, Stat | ||
|
||
|
||
@admin.register(Stat) | ||
class StatAdmin(admin.ModelAdmin): | ||
list_display = ("name", "date", "value", "period") | ||
list_filter = ("name", "date", "period") | ||
|
||
|
||
admin.site.register(Stat, StatAdmin) | ||
@admin.register(ForumStat) | ||
class ForumStatAdmin(admin.ModelAdmin): | ||
list_display = ("date", "period", "forum", "visits", "entry_visits", "time_spent") | ||
list_filter = ("date", "period", "forum") | ||
raw_id_fields = ("forum",) |
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
28 changes: 28 additions & 0 deletions
28
lacommunaute/stats/management/commands/collect_matomo_forum_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,28 @@ | ||
from datetime import date | ||
|
||
from dateutil.relativedelta import relativedelta | ||
from django.core.management.base import BaseCommand | ||
|
||
from lacommunaute.stats.models import ForumStat | ||
from lacommunaute.utils.date import get_last_sunday | ||
from lacommunaute.utils.matomo import collect_forum_stats_from_matomo_api | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Collecter les stats des forum dans matomo, jusqu'au dimanche précédent l'execution" | ||
|
||
def handle(self, *args, **options): | ||
period = "week" | ||
|
||
from_date = ForumStat.objects.filter(period=period).order_by("-date").first() | ||
|
||
if from_date: | ||
from_date = from_date.date + relativedelta(days=7) | ||
else: | ||
from_date = date(2023, 10, 2) | ||
|
||
to_date = get_last_sunday(date.today()) | ||
|
||
collect_forum_stats_from_matomo_api(from_date=from_date, to_date=to_date, period=period) | ||
|
||
self.stdout.write(self.style.SUCCESS("That'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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 5.0.6 on 2024-06-24 15:02 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("forum", "0015_alter_forumrating_options"), | ||
("stats", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="ForumStat", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("date", models.DateField(verbose_name="Date")), | ||
( | ||
"period", | ||
models.CharField( | ||
choices=[("month", "Month"), ("week", "Week"), ("day", "Day")], | ||
max_length=10, | ||
verbose_name="Période", | ||
), | ||
), | ||
("visits", models.IntegerField(default=0, verbose_name="Visites")), | ||
("entry_visits", models.IntegerField(default=0, verbose_name="Visites entrantes")), | ||
("time_spent", models.IntegerField(default=0, verbose_name="Temps passé")), | ||
( | ||
"forum", | ||
models.ForeignKey( | ||
null=True, on_delete=django.db.models.deletion.SET_NULL, to="forum.forum", verbose_name="Forum" | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "Stat de forum", | ||
"verbose_name_plural": "Stats de forum", | ||
"ordering": ["date", "period", "forum"], | ||
"unique_together": {("date", "period", "forum")}, | ||
}, | ||
), | ||
] |
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
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,8 @@ | ||
from datetime import date | ||
|
||
from dateutil.relativedelta import relativedelta | ||
|
||
|
||
def get_last_sunday(theday=date.today()): | ||
days_to_subtract = (theday.weekday() + 1) % 7 | ||
return theday - relativedelta(days=days_to_subtract) |
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
Oops, something went wrong.