From ab0a92128b07dc69a816a7a7f981a10e84ef6935 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Mon, 24 Jun 2024 16:57:01 +0200 Subject: [PATCH] setup scheduled management command to collect weekly stats on documentation forums --- .../collect_weekly_matomo_forum_stats.sh | 18 ++++++++++++ clevercloud/cron.json | 1 + .../commands/collect_matomo_forum_stats.py | 28 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100755 clevercloud/collect_weekly_matomo_forum_stats.sh create mode 100644 lacommunaute/stats/management/commands/collect_matomo_forum_stats.py diff --git a/clevercloud/collect_weekly_matomo_forum_stats.sh b/clevercloud/collect_weekly_matomo_forum_stats.sh new file mode 100755 index 000000000..494ddf3e4 --- /dev/null +++ b/clevercloud/collect_weekly_matomo_forum_stats.sh @@ -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 diff --git a/clevercloud/cron.json b/clevercloud/cron.json index b3eb86e28..7b16d8dfd 100644 --- a/clevercloud/cron.json +++ b/clevercloud/cron.json @@ -4,6 +4,7 @@ "0 5 * * * $ROOT/clevercloud/collect_daily_matomo_stats.sh", "3 5 * * * $ROOT/clevercloud/collect_daily_dsp_stats.sh", "5 5 1 * * $ROOT/clevercloud/collect_monthly_matomo_stats.sh", + "8 5 * * 1 $ROOT/clevercloud/collect_weekly_matomo_forum_stats.sh", "5 7-21 * * * $ROOT/clevercloud/send_notifs_when_first_reply.sh", "5 6 * * * $ROOT/clevercloud/send_notifs_when_following_replies.sh", "10 6-22 * * * $ROOT/clevercloud/add_user_to_list_when_register.sh", diff --git a/lacommunaute/stats/management/commands/collect_matomo_forum_stats.py b/lacommunaute/stats/management/commands/collect_matomo_forum_stats.py new file mode 100644 index 000000000..009de28d3 --- /dev/null +++ b/lacommunaute/stats/management/commands/collect_matomo_forum_stats.py @@ -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!"))