179 lines
6.4 KiB
Python
179 lines
6.4 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2014-2015 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import json
|
|
from collections import OrderedDict
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.db import models
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.conf import settings
|
|
|
|
from combo.data.models import CellBase
|
|
from combo.data.library import register_cell_class
|
|
|
|
|
|
@register_cell_class
|
|
class Gauge(CellBase):
|
|
title = models.CharField(_('Title'), max_length=150, blank=True, null=True)
|
|
url = models.URLField(_('URL'), max_length=150, blank=True, null=True)
|
|
data_source = models.CharField(_('Data Source'), max_length=150, blank=True, null=True)
|
|
jsonp_data_source = models.BooleanField(_('Use JSONP to get data'), default=True)
|
|
max_value = models.PositiveIntegerField(_('Max Value'), blank=True, null=True)
|
|
|
|
template_name = 'combo/gauge-cell.html'
|
|
|
|
class Media:
|
|
js = ('js/gauge.min.js', 'js/combo.gauge.js')
|
|
|
|
class Meta:
|
|
verbose_name = _('Gauge')
|
|
|
|
def get_additional_label(self):
|
|
return self.title
|
|
|
|
def get_cell_extra_context(self):
|
|
if self.jsonp_data_source:
|
|
data_source_url = self.data_source
|
|
else:
|
|
data_source_url = reverse('combo-ajax-gauge-count', kwargs={'cell': self.id})
|
|
return {'cell': self,
|
|
'title': self.title,
|
|
'url': self.url,
|
|
'max_value': self.max_value,
|
|
'data_source_url': data_source_url,
|
|
'jsonp': self.jsonp_data_source,
|
|
}
|
|
|
|
|
|
class BaseCubesChart(CellBase):
|
|
title = models.CharField(_('Title'), max_length=150, blank=True, null=True)
|
|
url = models.URLField(_('URL'), max_length=150, blank=True, null=True)
|
|
cube = models.CharField(verbose_name=_('Cube'), max_length=256, blank=True, null=True)
|
|
aggregate1 = models.CharField(verbose_name=_('Aggregate'), max_length=64, blank=True, null=True)
|
|
drilldown1 = models.CharField(verbose_name=_('Drilldown 1'), max_length=64, blank=True,
|
|
null=True)
|
|
drilldown2 = models.CharField(verbose_name=_('Drilldown 2'), max_length=64, blank=True,
|
|
null=True)
|
|
other_parameters = models.TextField(verbose_name=_('Other parameters'), blank=True, null=True)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
@classmethod
|
|
def is_enabled(self):
|
|
return bool(getattr(settings, 'CUBES_URL', None))
|
|
|
|
def get_additional_label(self):
|
|
return self.title
|
|
|
|
def get_default_form_class(self):
|
|
from .forms import CubesBarChartForm
|
|
return CubesBarChartForm
|
|
|
|
def get_cell_extra_context(self):
|
|
return {
|
|
'cell': self,
|
|
'title': self.title,
|
|
'url': self.url,
|
|
'aggregate': self.get_aggregate(),
|
|
}
|
|
|
|
def get_aggregate(self):
|
|
'''Get aggregate defined by chosen cube and the two drildown paths, request ordering of the
|
|
data by natural order of each axis.'''
|
|
from .utils import get_aggregate, get_cube, compute_levels
|
|
aggregate = get_aggregate(name=self.cube,
|
|
aggregate1=self.aggregate1,
|
|
drilldown1=self.drilldown1,
|
|
drilldown2=self.drilldown2,
|
|
other_parameters=(json.loads(self.other_parameters) if
|
|
self.other_parameters else None))
|
|
cube = get_cube(self.cube)
|
|
if not aggregate or not cube:
|
|
return
|
|
|
|
label_refs1 = []
|
|
key_refs1 = []
|
|
if self.drilldown1:
|
|
compute_levels(cube, self.drilldown1, label_refs=label_refs1, key_refs=key_refs1)
|
|
key_refs2 = []
|
|
label_refs2 = []
|
|
if self.drilldown2:
|
|
compute_levels(cube, self.drilldown2, label_refs=label_refs2, key_refs=key_refs2)
|
|
for ag in cube['aggregates']:
|
|
if ag['name'] != self.aggregate1:
|
|
continue
|
|
break
|
|
|
|
def cell_ref(cell, refs):
|
|
return tuple(cell[ref] for ref in refs)
|
|
|
|
keys1 = OrderedDict()
|
|
labels = OrderedDict()
|
|
datasets = OrderedDict()
|
|
|
|
for cell in aggregate['cells']:
|
|
label1 = u' / '.join(map(unicode, cell_ref(cell, label_refs1)))
|
|
key1 = cell_ref(cell, key_refs1)
|
|
labels[key1] = label1
|
|
keys1[key1] = 1
|
|
if key_refs2:
|
|
label2 = u' / '.join(map(unicode, cell_ref(cell, label_refs2)))
|
|
key2 = cell_ref(cell, key_refs2)
|
|
else:
|
|
label2 = ''
|
|
key2 = 1
|
|
dataset = datasets.setdefault(key2, {'label': label2,
|
|
'data': OrderedDict()})
|
|
value = cell[self.aggregate1]
|
|
dataset['data'][key1] = value
|
|
for dataset in datasets.itervalues():
|
|
dataset['data'] = [dataset['data'].get(key, 0) for key in keys1]
|
|
|
|
return {
|
|
'labels': labels.values(),
|
|
'datasets': [{
|
|
'label': dataset['label'],
|
|
'data': dataset['data'],
|
|
} for dataset in datasets.itervalues()]
|
|
}
|
|
|
|
|
|
@register_cell_class
|
|
class CubesBarChart(BaseCubesChart):
|
|
template_name = 'combo/cubes-barchart.html'
|
|
|
|
class Media:
|
|
js = ('xstatic/Chart.min.js', 'js/combo.cubes-barchart.js')
|
|
|
|
class Meta:
|
|
verbose_name = _('Cubes Barchart')
|
|
|
|
def get_cell_extra_context(self):
|
|
ctx = super(CubesBarChart, self).get_cell_extra_context()
|
|
# Need JSON serialization to pass data to Chart.js
|
|
ctx['json_aggregate'] = json.dumps(ctx['aggregate'])
|
|
return ctx
|
|
|
|
|
|
@register_cell_class
|
|
class CubesTable(BaseCubesChart):
|
|
template_name = 'combo/cubes-table.html'
|
|
|
|
class Meta:
|
|
verbose_name = _('Cubes Table')
|