Skip to content

Commit

Permalink
feat(stats): update StatistiquesPageView
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Oct 2, 2023
1 parent e8ad83d commit faa4cb9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
17 changes: 15 additions & 2 deletions lacommunaute/pages/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def test_context_data(self):
url = reverse("pages:statistiques")
date = timezone.now()
names = ["nb_uniq_engaged_visitors", "nb_uniq_visitors", "nb_uniq_active_visitors"]

# datas for daily stats
for name in names:
StatFactory(name=name, date=date)
undesired_period_stat = StatFactory(
Expand All @@ -29,9 +31,12 @@ def test_context_data(self):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "pages/statistiques.html")

# expected values
# datas for monthly stats
for name in names:
StatFactory(name=name, period=Period.MONTH, date=date)

# expected values for daily stats
self.assertIn("stats", response.context)
self.assertIn("date", response.context["stats"])
self.assertIn("nb_uniq_engaged_visitors", response.context["stats"])
self.assertIn("nb_uniq_visitors", response.context["stats"])
self.assertIn("nb_uniq_active_visitors", response.context["stats"])
Expand All @@ -41,6 +46,14 @@ def test_context_data(self):
self.assertNotIn(undesired_period_stat.date.strftime("%Y-%m-%d"), response.context["stats"]["date"])
self.assertNotIn(undesired_date_stat.date.strftime("%Y-%m-%d"), response.context["stats"]["date"])

# expected values for monthly stats
self.assertIn("period", response.context)
self.assertIn("nb_uniq_visitors", response.context)
self.assertIn("nb_uniq_active_visitors", response.context)
self.assertIn("nb_uniq_engaged_visitors", response.context)
self.assertIn("activation_percent", response.context)
self.assertIn("engagement_percent", response.context)


class LandingPagesListViewTest(TestCase):
def test_context_data(self):
Expand Down
27 changes: 27 additions & 0 deletions lacommunaute/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from django.db.models.functions import Cast
from django.shortcuts import render
from django.utils import timezone
from django.utils.dateformat import format
from django.views.generic.base import TemplateView

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.models import Forum
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__)
Expand All @@ -25,6 +27,30 @@ def contact(request):
class StatistiquesPageView(TemplateView):
template_name = "pages/statistiques.html"

def get_funnel_data(self):
qs = Stat.objects.current_month_datas()

stats = {
"period": None,
"nb_uniq_visitors": 0,
"nb_uniq_active_visitors": 0,
"nb_uniq_engaged_visitors": 0,
}

if qs.filter(name="nb_uniq_visitors").exists():
stats["period"] = format(qs.get(name="nb_uniq_visitors")["date"], "F Y")
stats["nb_uniq_visitors"] = qs.get(name="nb_uniq_visitors")["value"]

if qs.filter(name="nb_uniq_active_visitors").exists():
stats["nb_uniq_active_visitors"] = qs.get(name="nb_uniq_active_visitors")["value"]

if qs.filter(name="nb_uniq_engaged_visitors").exists():
stats["nb_uniq_engaged_visitors"] = qs.get(name="nb_uniq_engaged_visitors")["value"]

stats["activation_percent"] = percent(stats["nb_uniq_active_visitors"], stats["nb_uniq_visitors"])
stats["engagement_percent"] = percent(stats["nb_uniq_engaged_visitors"], stats["nb_uniq_visitors"])
return stats

def get_context_data(self, **kwargs):
indicator_names = [
"nb_uniq_engaged_visitors",
Expand All @@ -40,6 +66,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

Expand Down
35 changes: 10 additions & 25 deletions lacommunaute/templates/pages/statistiques.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,21 @@ <h2>Acquisition</h2>
<div class="col-12 col-lg-6">
<figure class="funnel">
<div>
<span class="display-3">Utilisateurs</span>
<span class="h2 mb-0">3192</span>
<span class="fs-sm">-16%/mois précédent</span>
<span class="display-3 mb-3">Utilisateurs</span>
<span class="h2">{{ nb_uniq_visitors }}</span>
</div>
<div>
<span class="display-3 text-white">Utilisateurs actifs</span>
<span class="h2 mb-0 text-white">3067</span>
<ul class="list-unstyled fs-sm mb-0">
<li>
96% des Utilisateurs
</li>
<li>
+1,4%/mois précédent
</li>
</ul>
<span class="display-3 text-white mb-3">Utilisateurs actifs</span>
<span class="h2 mb-3 text-white">{{ nb_uniq_active_visitors }}</span>
<span class="fs-sm">{{ activation_percent }}% des utilisateurs</span>
</div>
<div>
<span class="display-3 text-white">Utilisateurs engagés</span>
<span class="h2 mb-0 text-white">1539</span>
<ul class="list-unstyled fs-sm mb-0">
<li>
50.2% des Util. Actifs
</li>
<li>
+3%/mois préc.
</li>
</ul>
<span class="display-3 text-white mb-3">Utilisateurs engagés</span>
<span class="h2 mb-3 text-white">{{nb_uniq_engaged_visitors }}</span>
<span class="fs-sm">{{ engagement_percent }}% des util. actifs</span>
</div>
<figcaption class="fs-sm text-muted">
Période : Août 2023
<figcaption class="fs-sm text-muted mt-3">
Période : {{ period }}
</figcaption>
</figure>
</div>
Expand Down

0 comments on commit faa4cb9

Please sign in to comment.