From f07cd333d2c0ebb845007b7485bade2fb47064a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Avil=C3=A9s?= Date: Tue, 14 Oct 2014 19:35:24 +0200 Subject: [PATCH] Load downloads statistics --- piwik/indico_piwik/controllers.py | 4 +-- piwik/indico_piwik/queries/metrics.py | 18 ++++++------- piwik/indico_piwik/static/js/statistics.js | 31 ++++++++++------------ 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/piwik/indico_piwik/controllers.py b/piwik/indico_piwik/controllers.py index 0492c21..04b0601 100644 --- a/piwik/indico_piwik/controllers.py +++ b/piwik/indico_piwik/controllers.py @@ -38,9 +38,9 @@ class RHApiEventBase(RHApiBase): self._report_params['contrib_id'] = params.get('contrib_id') -class RHApiDownloads(RHApiBase): +class RHApiDownloads(RHApiEventBase): def _checkParams(self, params): - RHApiBase._checkParams(self, params) + RHApiEventBase._checkParams(self, params) self._report_params['download_url'] = params['download_url'] def _process(self): diff --git a/piwik/indico_piwik/queries/metrics.py b/piwik/indico_piwik/queries/metrics.py index bbaa2ae..e9fbcab 100644 --- a/piwik/indico_piwik/queries/metrics.py +++ b/piwik/indico_piwik/queries/metrics.py @@ -29,7 +29,7 @@ class PiwikQueryReportEventMetricVisitsBase(PiwikQueryReportEventMetricBase): class PiwikQueryReportEventMetricDownloads(PiwikQueryReportEventMetricBase): def call(self, download_url): - return super(PiwikQueryReportEventMetricReferrers, self).call(method='Actions.getDownload', + return super(PiwikQueryReportEventMetricDownloads, self).call(method='Actions.getDownload', downloadUrl=quote(download_url)) def get_result(self, download_url): @@ -42,28 +42,28 @@ class PiwikQueryReportEventMetricDownloads(PiwikQueryReportEventMetricBase): # Piwik returns hits as a list of hits per date for date, hits in results.iteritems(): - day_hits = {'total_hits': 0, 'unique_hits': 0} + day_hits = {'total': 0, 'unique': 0} if hits: for metrics in hits: - day_hits['total_hits'] += metrics['nb_hits'] - day_hits['unique_hits'] += metrics['nb_uniq_visitors'] + day_hits['total'] += metrics['nb_hits'] + day_hits['unique'] += metrics['nb_uniq_visitors'] hits_calendar[date] = day_hits return hits_calendar def _get_cumulative_results(self, results): """ - Returns a dictionary of {'total_hits': x, 'unique_hits': y} for the + Returns a dictionary of {'total': x, 'unique': y} for the date range. """ - total_hits = {'total_hits': 0, 'unique_hits': 0} + hits = {'total': 0, 'unique': 0} day_hits = list(hits[0] for hits in results.values() if hits) for metrics in day_hits: - total_hits['total_hits'] += metrics['nb_hits'] - total_hits['unique_hits'] += metrics['nb_uniq_visitors'] + hits['total'] += metrics['nb_hits'] + hits['unique'] += metrics['nb_uniq_visitors'] - return total_hits + return hits class PiwikQueryReportEventMetricReferrers(PiwikQueryReportEventMetricBase): diff --git a/piwik/indico_piwik/static/js/statistics.js b/piwik/indico_piwik/static/js/statistics.js index 9147169..d47f7ac 100644 --- a/piwik/indico_piwik/static/js/statistics.js +++ b/piwik/indico_piwik/static/js/statistics.js @@ -182,26 +182,23 @@ $(function() { replot = typeof replot !== 'undefined' ? replot : false; var DOMTarget = 'materialDownloadChart'; var graphParams = getIndicoBaseParams(); - graphParams['materialURL'] = uri; + graphParams.download_url = uri; - indicoRequest('piwik.getMaterialStatistics', graphParams, - function(result, error) { - if (!error) { - if (result !== null) { - var materialHits = [getArrayValues(result['individual'], - 'total_hits'), - getArrayValues(result['individual'], - 'unique_hits')]; - - drawGraph(materialHits, DOMTarget, replot); - // Write to the title the total material downloads - $('#materialTotalDownloads').html(result['cumulative']['total_hits']); - } - } else { - $('#dialogNoGraphData').dialog('open'); + $.ajax({ + url: build_url(PiwikPlugin.urls.data_downloads, {'confId': $('#confId').val(), 'download_url': uri}), + type: 'POST', + dataType: 'json', + success: function(data) { + if (handleAjaxError(data)) { + return; } + var materialHits = [getArrayValues(data.metrics.downloads.individual, 'total'), + getArrayValues(data.metrics.downloads.individual, 'unique')]; + drawGraph(materialHits, DOMTarget, replot); + // Write to the title the total material downloads + $('#materialTotalDownloads').html(data.metrics.downloads.cumulative.total); } - ); + }); }; /* jqTree Specifics */