187 lines
7.0 KiB
Python
187 lines
7.0 KiB
Python
# bijoe - BI dashboard
|
|
# Copyright (C) 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 hashlib
|
|
import datetime
|
|
import decimal
|
|
from collections import OrderedDict
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.core.cache import cache
|
|
|
|
from ..utils import get_warehouses, human_join
|
|
from ..engine import Engine
|
|
from .ods import Workbook
|
|
|
|
|
|
class Visualization(object):
|
|
def __init__(self, cube, representation, measures, drilldown=None, filters=None):
|
|
self.cube = cube
|
|
self.representation = representation
|
|
|
|
self.measures = measures
|
|
self.drilldown = drilldown or []
|
|
self.filters = filters or {}
|
|
|
|
def to_json(self):
|
|
return {
|
|
'warehouse': self.cube.engine.warehouse.name,
|
|
'cube': self.cube.name,
|
|
'representation': self.representation,
|
|
'measures': [measure.name for measure in self.measures],
|
|
'drilldown': [drilldown.name for drilldown in self.drilldown],
|
|
'filters': self.filters,
|
|
}
|
|
|
|
@classmethod
|
|
def from_json(cls, d, request=None):
|
|
for warehouse in get_warehouses(request=request):
|
|
if d['warehouse'] == warehouse.name:
|
|
break
|
|
else:
|
|
raise LookupError('warehouse %s not found' % d['warehouse'])
|
|
cube = Engine(warehouse)[d['cube']]
|
|
representation = d['representation']
|
|
measures = [cube.measures[name] for name in d['measures']]
|
|
drilldown = [cube.dimensions[name] for name in d.get('drilldown', [])]
|
|
filters = d.get('filters', {})
|
|
return cls(cube, representation, measures, drilldown=drilldown, filters=filters)
|
|
|
|
@classmethod
|
|
def from_form(cls, cube, form):
|
|
cleaned_data = form.cleaned_data
|
|
filters = {}
|
|
for kw, values in cleaned_data.iteritems():
|
|
if values and kw.startswith('filter__'):
|
|
dimension_name = kw[8:]
|
|
filters[dimension_name] = values
|
|
measures = cleaned_data.get('measures', [])
|
|
measures = [cube.measures[name] for name in measures]
|
|
drilldown = cleaned_data.get('drilldown', [])
|
|
drilldown = [cube.dimensions[name] for name in drilldown]
|
|
return cls(cube, cleaned_data['representation'], measures, drilldown=drilldown,
|
|
filters=filters)
|
|
|
|
@property
|
|
def key(self):
|
|
l = []
|
|
for kw, values in self.filters.iteritems():
|
|
if values:
|
|
if isinstance(values, dict):
|
|
values = values.items()
|
|
l.append('$'.join([kw] + sorted(map(unicode, values))))
|
|
l += [dim.name for dim in self.drilldown]
|
|
l += [measure.name for measure in self.measures]
|
|
key = '$'.join(v.encode('utf8') for v in l)
|
|
return hashlib.md5(key).hexdigest()
|
|
|
|
def stringified(self):
|
|
data = self.cached()
|
|
for row in data:
|
|
for cell in row:
|
|
value = cell['value']
|
|
if cell['type'] == 'percent':
|
|
try:
|
|
value = ('%05.2f' % float(value)).replace('.', ',') + u' %'
|
|
except:
|
|
value = _('Not applicable')
|
|
elif value is not None and cell['type'] == 'duration':
|
|
s = ''
|
|
if value.days:
|
|
s += '%d jour(s)' % value.days
|
|
if value.seconds / 3600:
|
|
s += ' %d heure(s)' % (value.seconds / 3600)
|
|
if not s:
|
|
s = 'moins d\'1 heure'
|
|
value = s
|
|
cell['value'] = value
|
|
return data
|
|
|
|
def data(self):
|
|
return self.cube.query(self.filters.items(),
|
|
[dim.name for dim in self.drilldown],
|
|
[measure.name for measure in self.measures])
|
|
|
|
def cached(self):
|
|
key = self.key
|
|
data = cache.get(key)
|
|
if data is None:
|
|
data = list(self.data())
|
|
cache.set(key, data)
|
|
return data
|
|
|
|
def grouped_data(self):
|
|
data = self.stringified()
|
|
dims = len(self.drilldown)
|
|
if not dims:
|
|
return ([(v, 0) for v in row] for row in data)
|
|
grouped = OrderedDict()
|
|
for row in data:
|
|
d = grouped
|
|
for cell in row[:dims - 1]:
|
|
dim = cell['value']
|
|
if dim not in d:
|
|
d[dim] = OrderedDict(), 1
|
|
else:
|
|
d[dim] = d[dim][0], d[dim][1] + 1
|
|
d, count = d[dim]
|
|
d[row[dims - 1]['value']] = row[dims:], 0
|
|
|
|
def helper(d, prefix=[]):
|
|
for key in d:
|
|
row, count = d[key]
|
|
if count == 0:
|
|
yield prefix + [(key, 1)] + [(v, 0) for v in row]
|
|
else:
|
|
for r in helper(row, prefix + [(key, count)]):
|
|
yield r
|
|
prefix = []
|
|
return helper(grouped)
|
|
|
|
def json_data(self):
|
|
json_data = []
|
|
for row in self.data():
|
|
coords = []
|
|
for cell in row[:len(self.drilldown)]:
|
|
coords.append(cell)
|
|
measures = []
|
|
for cell in row[len(self.drilldown):]:
|
|
if isinstance(cell['value'], decimal.Decimal):
|
|
cell['value'] = float(cell['value'])
|
|
if isinstance(cell['value'], datetime.timedelta):
|
|
cell['value'] = cell['value'].days + cell['value'].seconds / 86400.
|
|
measures.append(cell)
|
|
json_data.append({'coords': coords, 'measures': measures})
|
|
return json_data
|
|
|
|
def ods(self):
|
|
workbook = Workbook()
|
|
sheet = workbook.add_sheet(self.cube.label)
|
|
for j, m in enumerate(self.drilldown + self.measures):
|
|
sheet.write(0, j, m.label)
|
|
for i, row in enumerate(self.stringified()):
|
|
for j, cell in enumerate(row):
|
|
sheet.write(i + 1, j, unicode(cell['value']))
|
|
return workbook
|
|
|
|
def title(self):
|
|
if self.drilldown:
|
|
return _('{0} by {1}').format(human_join([measure.label for measure in self.measures]),
|
|
human_join([dimension.label for dimension in
|
|
self.drilldown]))
|
|
else:
|
|
return human_join([measure.label for measure in self.measures])
|