Wrap event material tree in PiwikReport

This commit is contained in:
Alejandro Avilés 2014-10-15 11:29:22 +02:00
parent 860c6c857d
commit 4ae1f8bc76
2 changed files with 54 additions and 81 deletions

View File

@ -2,11 +2,10 @@ from flask import jsonify
from indico.core.config import Config
from indico.util.i18n import _
from MaKaC.conference import ConferenceHolder
from MaKaC.webinterface.rh.conferenceModif import RHConferenceModifBase
from .views import WPStatistics
from .reports import ReportCountries, ReportDevices, ReportDownloads, ReportGeneral, ReportVisitsPerDay
from .reports import ReportCountries, ReportDevices, ReportDownloads, ReportGeneral, ReportMaterial, ReportVisitsPerDay
class RHStatistics(RHConferenceModifBase):
@ -62,76 +61,6 @@ class RHApiEventGraphDevices(RHApiEventBase):
return jsonify(ReportDevices.get(**self._report_params))
class RHApiMaterial(RHApiBase):
"""
HTTP method for getting the Material dictionary of a Conference in
the format jqTree expects.
"""
TREE_STR_LIMIT = 24
# @BaseStatisticsImplementation.memoizeReport
class RHApiMaterial(RHApiEventBase):
def _process(self):
confId = self._conf.getId()
conference = ConferenceHolder().getById(confId)
material = conference.getAllMaterialDict()
return self.formatForJQTree(material, returnAsList=True)
def formatForJQTree(self, child, returnAsList=False):
"""
Wraps around the JSON output in Conference to the specific
format required by jqTree.
"""
node = {}
node['label'] = child['title']
node['children'] = []
for key, value in child.iteritems():
if key not in ['children', 'material']: # Performance only
continue
children = []
if key == 'children' and len(value) > 0:
for child in value:
newNode = self.formatForJQTree(child)
if newNode:
children.append(newNode)
if key == 'material' and len(value) > 0:
for material in value:
newNode = {}
newNode['label'] = material['title']
newNode['id'] = material['url']
children.append(newNode)
if children:
node['children'].extend(children)
# If the node has no children (i.e. Sessions, Contributions & Material,
# then there is no point in displaying it in the tree.
if len(node['children']) > 0:
self.shortenNodeLabel(node)
for child in node['children']:
self.shortenNodeLabel(child)
return [node] if returnAsList else node
else:
return None
def shortenNodeLabel(self, node):
"""
We don't want the strings to be massive in the labels, truncate them
to make the tree more manageable.
"""
if len(node['label']) > self.TREE_STR_LIMIT:
node['label'] = node['label'][:self.TREE_STR_LIMIT] + '...'
return jsonify(ReportMaterial.get(**self._report_params))

View File

@ -20,8 +20,6 @@ class ReportBase(Serializer):
default_report_interval = 14
def __init__(self, event_id, contrib_id=None, start_date=None, end_date=None, **report_params):
self.graphs = {}
self.metrics = {}
self.event_id = event_id
self.contrib_id = contrib_id
self._init_date_range(start_date, end_date)
@ -74,21 +72,21 @@ class ReportCountries(ReportBase):
__public__ = ['graphs']
def _build_report(self):
self.graphs['countries'] = PiwikQueryReportEventGraphCountries(**self.params).get_result()
self.graphs = {'countries': PiwikQueryReportEventGraphCountries(**self.params).get_result()}
class ReportDevices(ReportBase):
__public__ = ['graphs']
def _build_report(self):
self.graphs['devices'] = PiwikQueryReportEventGraphDevices(**self.params).get_result()
self.graphs = {'devices': PiwikQueryReportEventGraphDevices(**self.params).get_result()}
class ReportDownloads(ReportBase):
__public__ = ['metrics']
def _build_report(self, download_url):
self.metrics['downloads'] = PiwikQueryReportEventMetricDownloads(**self.params).get_result(download_url)
self.metrics = {'downloads': PiwikQueryReportEventMetricDownloads(**self.params).get_result(download_url)}
class ReportGeneral(ReportBase):
@ -96,6 +94,7 @@ class ReportGeneral(ReportBase):
def _build_report(self):
"""Build the report by performing queries to Piwik"""
self.metrics = {}
queries = {'visits': PiwikQueryReportEventMetricVisits(**self.params),
'unique_visits': PiwikQueryReportEventMetricUniqueVisits(**self.params),
'visit_duration': PiwikQueryReportEventMetricVisitDuration(**self.params),
@ -121,12 +120,57 @@ class ReportGeneral(ReportBase):
self.contributions[contrib_id] = info
class ReportMaterial(ReportBase):
__public__ = ['material']
tree_string_length = 24
def _build_report(self):
event = ConferenceHolder().getById(self.params['event_id'])
material = event.getAllMaterialDict()
self.material = {'tree': self._format_data(material, as_list=True)}
def _format_data(self, child, as_list=False):
node = {'label': child['title'],
'children': []}
for key, value in child.iteritems():
if key not in ['children', 'material']:
continue
children = []
if key == 'children' and value:
for child in value:
new_node = self._format_data(child)
if new_node:
children.append(new_node)
if key == 'material' and value:
for material in value:
new_node = {'label': material['title'],
'id': material['url']}
children.append(new_node)
if children:
node['children'].extend(children)
if not node['children']:
return None
self._truncate_node_label(node)
for child in node['children']:
self._truncate_node_label(child)
return [node] if as_list else node
def _truncate_node_label(self, node):
if len(node['label']) > self.tree_string_length:
node['label'] = node['label'][:self.tree_string_length] + '...'
class ReportVisitsPerDay(ReportBase):
__public__ = ['metrics']
def _build_report(self):
self.metrics['unique'] = PiwikQueryReportEventMetricUniqueVisits(**self.params).get_result(reduced=False)
self.metrics['total'] = PiwikQueryReportEventMetricVisits(**self.params).get_result(reduced=False)
self.metrics = {'unique': PiwikQueryReportEventMetricUniqueVisits(**self.params).get_result(reduced=False),
'total': PiwikQueryReportEventMetricVisits(**self.params).get_result(reduced=False)}
self._reduce_metrics()
def _reduce_metrics(self):