示例#1
0
 def get_content_urls(self):
     content = self.versioned_object
     urls = [NamedUrl(content.title, content.get_absolute_url_online(), 0)]
     if content.has_extracts():
         return urls
     for child in content.children:
         urls.append(NamedUrl(child.title, child.get_absolute_url_online(), 1))
         if not child.has_extracts():
             for subchild in child.children:
                 urls.append(NamedUrl(subchild.title, subchild.get_absolute_url_online(), 2))
     return urls
示例#2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if not (self.is_author or self.is_staff):
            raise PermissionDenied

        urls = self.get_urls_to_render()
        start_date, end_date = self.get_start_and_end_dates()
        display_mode = self.get_display_mode(urls)
        reports = {}
        cumulative_stats = {}
        referrers = {}
        type_referrers = {}
        keywords = {}
        report_field = [("nb_uniq_visitors", False), ("nb_hits", False), ("avg_time_on_page", True)]

        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 StatisticsException 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))
        except Exception as e:
            all_statistics = {}
            self.logger.error(f"Something failed with Matomo reporting system: {e}")
            messages.error(self.request, _("Impossible de récupérer les statistiques du site ({}).").format(e))

        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]))

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

        context.update(
            {
                "display": display_mode,
                "urls": urls,
                "reports": reports,
                "cumulative_stats": cumulative_stats,
                "referrers": referrers,
                "type_referrers": type_referrers,
                "keywords": keywords,
            }
        )
        return context
示例#3
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if not (self.is_author or self.is_staff):
            raise PermissionDenied

        urls = self.get_urls_to_render()
        start_date, end_date = self.get_start_and_end_dates()
        display_mode = self.get_display_mode(urls)
        reports = {}
        cumulative_stats = {}
        referrers = {}
        type_referrers = {}
        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)

            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)
            }

        context.update({
            "display": display_mode,
            "urls": urls,
            "reports": reports,
            "cumulative_stats": cumulative_stats,
            "referrers": referrers,
            "type_referrers": type_referrers,
            "keywords": keywords,
        })
        return context