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 Aug 21, 2021
1 parent 935ceab commit 3cf6881
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions zds/tutorialv2/views/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,30 @@ 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."))
self.request, data.get("message", _("Impossible de récupérer les statistiques du site."))
)

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

return data_structured
return {}
else:
for index, method_url in enumerate(itertools.product(methods, urls)):
if isinstance(data[index], dict) and data[index].get("result", "") == "error":
self.logger.error(data[index].get("message", "Something failed with Matomo reporting system."))
messages.error(
self.request,
data[index].get("message", _("Impossible de récupérer les statistiques du site.")),
)
return {}

method = method_url[0]
data_structured[method].append(data[index])

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."))
messages.error(self.request, _("Impossible de récupérer les statistiques du site."))

return data
return {}

@staticmethod
def get_stat_metrics(data, metric_name):
Expand Down Expand Up @@ -223,31 +230,32 @@ def get_context_data(self, **kwargs):
["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)
if all != {}:
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)

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 3cf6881

Please sign in to comment.