diff --git a/lacommunaute/forum_stats/models.py b/lacommunaute/forum_stats/models.py index 496ac70c7..ea5db7041 100644 --- a/lacommunaute/forum_stats/models.py +++ b/lacommunaute/forum_stats/models.py @@ -19,3 +19,9 @@ class Meta: def __str__(self): return f"{self.name} - {self.date} - {self.period}" + + def get_month_stats(self): + qs = Stat.objects.filter(date__year=self.date.year, date__month=self.date.month, period=Period.MONTH).values( + "name", "value" + ) + return {stat["name"]: stat["value"] for stat in qs} diff --git a/lacommunaute/pages/views.py b/lacommunaute/pages/views.py index 2f66ecc06..7b7aac7f1 100644 --- a/lacommunaute/pages/views.py +++ b/lacommunaute/pages/views.py @@ -13,6 +13,7 @@ from lacommunaute.forum_conversation.models import Topic from lacommunaute.forum_stats.models import Stat from lacommunaute.utils.json import extract_values_in_list +from lacommunaute.utils.math import percent logger = logging.getLogger(__name__) @@ -25,6 +26,21 @@ def contact(request): class StatistiquesPageView(TemplateView): template_name = "pages/statistiques.html" + def get_funnel_data(self): + current_period = Stat.objects.filter(period="month").order_by("-date").first() + current_stats = Stat.get_month_stats(current_period) + current_stats["nb_uniq_visitors"] = current_stats.get("nb_uniq_visitors", 0) + current_stats["nb_uniq_active_visitors"] = current_stats.get("nb_uniq_active_visitors", 0) + current_stats["nb_uniq_engaged_visitors"] = current_stats.get("nb_uniq_engaged_visitors", 0) + current_stats["activation_percent"] = percent( + current_stats["nb_uniq_active_visitors"], current_stats["nb_uniq_visitors"] + ) + current_stats["engagement_percent"] = percent( + current_stats["nb_uniq_engaged_visitors"], current_stats["nb_uniq_visitors"] + ) + current_stats["period"] = current_period.date.strftime("%B %Y") + return current_stats + def get_context_data(self, **kwargs): indicator_names = [ "nb_uniq_engaged_visitors", @@ -40,6 +56,7 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["stats"] = extract_values_in_list(datas, indicator_names) + context = {**context, **self.get_funnel_data()} return context diff --git a/lacommunaute/templates/pages/statistiques.html b/lacommunaute/templates/pages/statistiques.html index 3158a82c6..80e6beb4d 100644 --- a/lacommunaute/templates/pages/statistiques.html +++ b/lacommunaute/templates/pages/statistiques.html @@ -30,12 +30,12 @@