Commit 6c9d1fdb authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Refactor Metric2D.

parent 32a8e2be
# -*- coding: utf-8 -*-
{
'': '',
'%s between %s and %s': '%s du %s au %s',
'%s entries modified in the history table.': "%s entrés ont été modifié dans l'historique.",
'%s in %s': '%s en %s',
......
......@@ -501,18 +501,71 @@ class Metric2D(BaseReport):
"""
def __init__(self, config, selector):
BaseReport.__init__(self, config, selector, select=False)
self._do_metric()
# replace undefined value by 0
self.df = self.df.fillna(0)
def _do_data(self, map_x, map_y, map_z):
"""Build a temporarily list with the raw data for each metric.
This method handle the year along the x or y axis..
@type map_x: tuple
@param map_x: the field name map for the field along the x-axis
@type map_y: tuple
@param map_y: the field name map for the field along the y-axis
@type map_z: tuple
@param map_z: the field name map for the field along the z-axis
@rtype: list
"""
data = []
if config.field_horizontal == 'year' or config.field_vertical == 'year':
db = self.db
config = self.config
selector = self.selector
BaseReport.__init__(self, config, selector, select=False)
self._do_metric_time()
# year can be on the horizontal or vertical axis
# rotate to have year always along the horizontal axis
# reverse operation will be performed at the end
if map_y[0] == "year":
map_y = map_x
map_x = ("year", '', '')
else:
BaseReport.__init__(self, config, selector, select=True)
self._do_metric()
if map_x[0] == "year":
# replace undefined value by 0
self.df = self.df.fillna(0)
start = selector.period_start.year
end = selector.period_end.year
years = range(start, end + 1)
if config.conditions:
q_conditions = selector._extra_queries[0]
for year in years:
selector.reset_extra_queries()
selector.period_start = date(year, 1, 1)
selector.period_end = date(year, 12, 31)
if config.conditions:
selector.append_query(q_conditions)
for row in selector.select(db.history):
li = [year, get_value(row, *map_y), get_value(row, *map_z)]
data.append(li)
else:
for row in selector.select(db.history):
li = [get_value(row, *map_x),
get_value(row, *map_y),
get_value(row, *map_z)]
data.append(li)
return data
def _do_metric(self):
......@@ -531,15 +584,9 @@ class Metric2D(BaseReport):
map_z = split_dbfield(address_z)
# build the data frame
data = []
for row in self.rows:
li = [get_value(row, *map_x),
get_value(row, *map_y),
get_value(row, *map_z)]
data.append(li)
data = self._do_data(map_x, map_y, map_z)
df = pd.DataFrame(data, columns=[address_x, address_y, address_z])
# remove duplicate entries
if config.metric in ('count', 'size'):
df = df.drop_duplicates()
......@@ -554,83 +601,15 @@ class Metric2D(BaseReport):
# move to a multi index structure to a 2D table
df = df.unstack(level=0)
# rotate the data frame when year is along the y-axis
if address_y == 'year':
df = df.T
# data frame is persistent
self.df = df
def _do_metric_time(self):
"""Compute the metric involving one database field and time axis.
"""
db = self.db
config = self.config
selector = self.selector
# year can be on the horizontal or vertical axis
# rotate to have year always along the horizontal axis
# reverse operation will be performed at the end
address_x = 'year'
if config.field_horizontal == 'year':
address_y = config.field_vertical
else:
address_y = config.field_horizontal
address_z = config.field_z
# split the database field in tablename, fieldname, keyname
map_y = split_dbfield(address_y)
map_z = split_dbfield(address_z)
# build the data range
start = selector.period_start.year
end = selector.period_end.year
years = range(start, end + 1)
# build the data frame
# for each year interrogate the database
# therefore virtual fields are compute properly
# for each period of time
data = []
if config.conditions:
q_conditions = selector._extra_queries[0]
for year in years:
selector.reset_extra_queries()
selector.period_start = date(year, 1, 1)
selector.period_end = date(year, 12, 31)
if config.conditions:
selector.append_query(q_conditions)
for row in selector.select(db.history):
li = [year, get_value(row, *map_y), get_value(row, *map_z)]
data.append(li)
df = pd.DataFrame(data, columns=['year', address_y, address_z])
# remove duplicate entries
if config.metric in ('count', 'size'):
df = df.drop_duplicates()
# metric
metric = {}
metric[address_z] = config.metric
# group the data
df = df.groupby(['year', address_y])
df = df.agg(metric)
# move to a multi index structure to a 2D table
df = df.unstack(level=0)
# move to year along the vertical axis
if config.field_vertical == 'year':
df = df.T
# data frame is persistent
self.df = df
def _set_store_data(self):
""" Generate the C{Ext.data.Store.data} property.
......@@ -679,11 +658,13 @@ class Metric2D(BaseReport):
"""
config = self.config
address_y = self.config.field_vertical
address_y = config.field_vertical
map_y = split_dbfield(address_y)
text_y = (map_y[2] if map_y[2] else map_y[1])
grid = Storage(columns=[], features=[], title=config.title)
grid.columns.append({'text': address_y.title(),
grid.columns.append({'text': current.T(text_y.title()),
'dataIndex': address_y.replace('.', ''),
'flex': 0.8})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment