Commit 318f06f0 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Simplify report objects by moving the method do_data to the base class.

parent 7c2e08b7
......@@ -159,6 +159,70 @@ class BaseReport(object):
selector.append_query(q_conditions)
def _do_data(self, maps):
"""Build a temporarily list with the raw data for each series.
This method handle the "year" database field.
@type maps: list
@param maps: the database field map (tablename, fieldname, keyname).
One per series.
@rtype: list
"""
data = []
config = self.config
db = self.db
selector = self.selector
convert = self._convert
# the year axis is on
# scan the database and compute virtual field on the year basis
if self._is_year(maps):
# get the year range
for year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
values = [get_value(row, *map, year=year) for map in maps]
values = [convert(value) for value in values]
data.append(values)
# standard scan
else:
for row in selector.select(db.history):
values = [get_value(row, *map) for map in maps]
values = [convert(value) for value in values]
data.append(values)
return data
def _convert(self, value):
"""Helper function to convert value before entering
it in the data list.
"""
return value
def _is_year(self, maps):
"""
@type maps: list
@param maps: the database field map (tablename, fieldname, keyname).
One per series.
@rtype: boolean
@return: C{True} is the pseudo field C{year} is in maps
"""
li = [True for el in maps if el[0] == 'year']
return (True if li else False)
def to_df(self):
"""Return the pandas DataFrame.
......@@ -398,47 +462,7 @@ class List(BaseReport):
"""
if not (column.dataIndex or column.xtype == "rownumberer"):
column.dataIndex = column.dbfield.replace('.', '')
def _do_data(self, maps):
"""Build a temporarily list with the raw data for each series.
This method handle the "year" database field.
@type maps: list
@param maps: the database field map (tablename, fieldname, keyname)
@rtype: list
"""
data = []
config = self.config
db = self.db
selector = self.selector
# the year axis is on
# scan the database and compute virtual field on the year basis
is_year = [True for el in maps if el[0] == 'year']
if is_year:
# get the year range
for year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
values = [get_value(row, *map, year=year) for map in maps]
values = [self._encode(value) for value in values]
data.append(values)
# standard scan
else:
for row in selector.select(db.history):
values = [get_value(row, *map) for map in maps]
values = [self._encode(value) for value in values]
data.append(values)
return data
def _do_metric(self):
"""Interface the database with the DataFrame structure.
......@@ -477,7 +501,7 @@ class List(BaseReport):
self.df = df
def _encode(self, value):
def _convert(self, value):
"""Encode date and timedelta with a define format.
It will then exploit in the grid.
......@@ -699,41 +723,6 @@ class Metric1D(BaseReport):
column.dataIndex = column.text
def _do_data(self, maps):
"""Build a temporarily list with the raw data for each metric.
This method handle the year group_field.
@type maps: list
@param maps: the database field map (tablename, fieldname, keyname)
@rtype: list
"""
data = []
db = self.db
config = self.config
selector = self.selector
# the year axis is on
# scan the database and compute virtual field on the year basis
if self.config.group_field == 'year':
for year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
values = [get_value(row, *map, year=year) for map in maps]
data.append(values)
# standard scanning
else:
for row in selector.select(db.history):
values = [get_value(row, *map) for map in maps]
data.append(values)
return data
def _do_metric(self):
"""Compute the metric according to user specifications.
......@@ -790,6 +779,15 @@ class Metric1D(BaseReport):
self.df = df
def _is_year(self, maps):
"""Supersede the method of the base class.
@retrun: true if the group_field is year.
"""
return self.config.group_field == 'year'
def _set_store_data(self):
"""Generate the C{Ext.data.Store.data} property.
It is a list of dictionaries. Each of them contains the data
......@@ -907,82 +905,46 @@ class Metric2D(BaseReport):
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 = []
db = self.db
config = self.config
selector = self.selector
# the year axis is on
# scan the database and compute virtual field on the year basis
is_year = [True for el in (map_x, map_y, map_z) if el[0] == 'year']
if is_year:
for year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
values = [year, get_value(row, *map_y), get_value(row, *map_z)]
data.append(values)
# standard scan of the database
else:
for row in selector.select(db.history):
values = [get_value(row, *map) for map in (map_x, map_y, map_z)]
data.append(values)
return data
def _do_metric(self):
"""Compute the metric involving two database field.
"""
config = self.config
# database field addresses
#
# the 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 = config.group_field_x
address_y = config.group_field_y
address_z = config.metric_field_z
aggregate = config.aggregation_z
# metric
metric = {}
metric[address_z] = aggregate
# the 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 address_y == 'year':
address_y, address_x = address_x, 'year'
addresses = [address_x, address_y, address_z]
# split the database field in (tablename, fieldname, keyname)
# the database field maps (tablename, fieldname, keyname)
map_x = split_dbfield(address_x)
map_y = split_dbfield(address_y)
map_z = split_dbfield(address_z)
maps = [map_x, map_y, map_z]
# metric
aggregate = config.aggregation_z
metric = {}
metric[address_z] = aggregate
# build the data frame
data = self._do_data(map_x, map_y, map_z)
data = self._do_data(maps)
if not data:
self.df = pd.DataFrame(columns=[address_x, address_y, address_z])
self.df = pd.DataFrame(columns=addresses)
return
df = pd.DataFrame(data, columns=[address_x, address_y, address_z])
df = pd.DataFrame(data, columns=addresses)
# remove duplicate entries
if aggregate in ('count', 'size'):
......
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