Skip to content

Commit

Permalink
Gère plus d'erreurs possibles venant de Matomo
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink committed Oct 8, 2021
1 parent b547584 commit e8fd8ac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 55 deletions.
22 changes: 12 additions & 10 deletions templates/tutorialv2/stats/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ <h2 class="ico-after ico-tutorials">
{% endblocktrans %}
{% endif %}
</h2>
<!-- Tab links -->
<div class="tab">
<span class="tablinks" tabindex="0" id="tab-view-graph">{% trans "Pages vues" %}</span>
<span class="tablinks" tabindex="0" id="tab-visit-time-graph">{% trans "Temps moyen de lecture" %}</span>
<span class="tablinks" tabindex="0" id="tab-users-graph">{% trans "Nombre de visiteurs uniques" %}</span>
</div>
<!-- Tab content -->
{% include "misc/graph.part.html" with tab_name="tab-view-graph-content" graph_title="Évolution des pages vues sur le contenu" canvas_id="view-graph" report_key="nb_hits" y_label="Nombre de pages" %}
{% include "misc/graph.part.html" with tab_name="tab-visit-time-graph-content" graph_title="Évolution du temps moyen lecture (en secondes)" canvas_id="visit-time-graph" report_key="avg_time_on_page" y_label="Secondes" %}
{% include "misc/graph.part.html" with tab_name="tab-users-graph-content" graph_title="Évolution du nombre de visiteurs uniques" canvas_id="users-graph" report_key="nb_uniq_visitors" y_label="Nombre de visiteurs" %}

{% if reports %}
<!-- Tab links -->
<div class="tab">
<span class="tablinks" tabindex="0" id="tab-view-graph">{% trans "Pages vues" %}</span>
<span class="tablinks" tabindex="0" id="tab-visit-time-graph">{% trans "Temps moyen de lecture" %}</span>
<span class="tablinks" tabindex="0" id="tab-users-graph">{% trans "Nombre de visiteurs uniques" %}</span>
</div>
<!-- Tab content -->
{% include "misc/graph.part.html" with tab_name="tab-view-graph-content" graph_title="Évolution des pages vues sur le contenu" canvas_id="view-graph" report_key="nb_hits" y_label="Nombre de pages" %}
{% include "misc/graph.part.html" with tab_name="tab-visit-time-graph-content" graph_title="Évolution du temps moyen lecture (en secondes)" canvas_id="visit-time-graph" report_key="avg_time_on_page" y_label="Secondes" %}
{% include "misc/graph.part.html" with tab_name="tab-users-graph-content" graph_title="Évolution du nombre de visiteurs uniques" canvas_id="users-graph" report_key="nb_uniq_visitors" y_label="Nombre de visiteurs" %}
{% endif %}

{% if cumulative_stats %}
{% if display == 'global' %}
Expand Down
17 changes: 17 additions & 0 deletions zds/tutorialv2/tests/tests_views/tests_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ def test_access_for_author(self, mock_post):
self.assertEqual(resp.context_data["urls"][0].url, self.published.content.get_absolute_url_online())
self.assertEqual(len(resp.context_data["urls"]), 3)

def test_access_for_author_matomo_error(self):
# author can access to stats, and if request to Matomo triggers an error,
# display the page with a message error, and not an ugly error 500 page:
url = reverse(
"content:stats-content",
kwargs={"pk": self.published.content_pk, "slug": self.published.content_public_slug},
)
self.client.force_login(self.user_author)
with self.assertLogs(level="ERROR") as error_log:
resp = self.client.get(url)
self.assertTrue(
error_log.output[0].startswith(
"ERROR:zds.tutorialv2.views.statistics:Something failed with Matomo reporting system:"
)
)
self.assertEqual(resp.status_code, 200)

@mock.patch("requests.post")
def test_access_for_staff(self, mock_post):
# staff can access to stats
Expand Down
94 changes: 49 additions & 45 deletions zds/tutorialv2/views/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,20 @@ def get_all_statistics(self, urls, start, end, methods):
response_matomo = requests.post(url=self.matomo_api_url, data=data_request)
data = response_matomo.json()
if isinstance(data, dict) and data.get("result", "") == "error":
data = {}
self.logger.error(data.get("message", "Something failed with Matomo reporting system."))
messages.error(
self.request, data.get("message", _(f"Impossible de récupérer les statistiques du site."))
)
raise Exception(self.logger.error, data.get("message", _("Pas de message d'erreur")))
else:
for index, method_url in enumerate(itertools.product(methods, urls)):
if isinstance(data[index], dict) and data[index].get("result", "") == "error":
raise Exception(self.logger.error, data[index].get("message", _("Pas de message d'erreur")))

for index, method_url in enumerate(itertools.product(methods, urls)):
method = method_url[0]
data_structured[method].append(data[index])
method = method_url[0]
data_structured[method].append(data[index])

return data_structured
return data_structured
except Exception:
data = {}
self.logger.exception(f"Something failed with Matomo reporting system.")
messages.error(self.request, _(f"Impossible de récupérer les statistiques du site."))
raise Exception(self.logger.exception, _("Erreur inconnue"))

return data
return {}

@staticmethod
def get_stat_metrics(data, metric_name):
Expand Down Expand Up @@ -214,40 +211,47 @@ def get_context_data(self, **kwargs):
keywords = {}
report_field = [("nb_uniq_visitors", False), ("nb_hits", False), ("avg_time_on_page", True)]

# Each function sends only one bulk request for all the urls
# Each variable is a list of dictionnaries (one for each url)
all = self.get_all_statistics(
urls,
start_date,
end_date,
["Referrers.getReferrerType", "Referrers.getWebsites", "Referrers.getKeywords", "Actions.getPageUrl"],
)

all_stats = all["Actions.getPageUrl"]
all_ref_websites = all["Referrers.getWebsites"]
all_ref_types = all["Referrers.getReferrerType"]
all_ref_keyword = all["Referrers.getKeywords"]

for index, url in enumerate(urls):
cumul_stats = self.get_cumulative(all_stats[index])
reports[url] = {}
cumulative_stats[url] = {}

for item, is_avg in report_field:
reports[url][item] = self.get_stat_metrics(all_stats[index], item)
if is_avg:
cumulative_stats[url][item] = 0
if cumul_stats.get("total") > 0:
cumulative_stats[url][item] = cumul_stats.get(item, 0) / cumul_stats.get("total")
else:
cumulative_stats[url][item] = cumul_stats.get(item, 0)
try:
# Each function sends only one bulk request for all the urls
# Each variable is a list of dictionnaries (one for each url)
all_statistics = self.get_all_statistics(
urls,
start_date,
end_date,
["Referrers.getReferrerType", "Referrers.getWebsites", "Referrers.getKeywords", "Actions.getPageUrl"],
)
except Exception as e:
all_statistics = {}
logger_method, msg = e.args
logger_method(f"Something failed with Matomo reporting system: {msg}")
messages.error(self.request, _("Impossible de récupérer les statistiques du site ({}).").format(msg))

if all_statistics != {}:
all_stats = all_statistics["Actions.getPageUrl"]
all_ref_websites = all_statistics["Referrers.getWebsites"]
all_ref_types = all_statistics["Referrers.getReferrerType"]
all_ref_keyword = all_statistics["Referrers.getKeywords"]

for index, url in enumerate(urls):
cumul_stats = self.get_cumulative(all_stats[index])
reports[url] = {}
cumulative_stats[url] = {}

for item, is_avg in report_field:
reports[url][item] = self.get_stat_metrics(all_stats[index], item)
if is_avg:
cumulative_stats[url][item] = 0
if cumul_stats.get("total") > 0:
cumulative_stats[url][item] = cumul_stats.get(item, 0) / cumul_stats.get("total")
else:
cumulative_stats[url][item] = cumul_stats.get(item, 0)

referrers = self.merge_ref_to_data(referrers, self.get_ref_metrics(all_ref_websites[index]))
type_referrers = self.merge_ref_to_data(type_referrers, self.get_ref_metrics(all_ref_types[index]))
keywords = self.merge_ref_to_data(keywords, self.get_ref_metrics(all_ref_keyword[index]))
referrers = self.merge_ref_to_data(referrers, self.get_ref_metrics(all_ref_websites[index]))
type_referrers = self.merge_ref_to_data(type_referrers, self.get_ref_metrics(all_ref_types[index]))
keywords = self.merge_ref_to_data(keywords, self.get_ref_metrics(all_ref_keyword[index]))

if display_mode.lower() == "global":
reports = {NamedUrl(display_mode, "", 0): self.merge_report_to_global(reports, report_field)}
if display_mode.lower() == "global":
reports = {NamedUrl(display_mode, "", 0): self.merge_report_to_global(reports, report_field)}

context.update(
{
Expand Down

0 comments on commit e8fd8ac

Please sign in to comment.