Commit 30cda435 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Refactor method to handle the year axis.

parent 4cd5de89
......@@ -119,17 +119,6 @@ class BaseReport(object):
selector.append_query(q_conditions)
def _get_years(self):
"""build the range of years selected by the user.
"""
start = self.selector.period_start.year
end = self.selector.period_end.year
years = range(start, end + 1)
return years
def to_df(self):
"""Return as pandas DataFrame.
......@@ -184,44 +173,27 @@ class List(BaseReport):
db = self.db
maps = self._maps
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:
# build the year range
years = self._get_years()
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)
# get the year range
for year in selector.get_years():
selector.set_year(year)
if config.conditions:
selector.append_query(q_conditions)
for row in selector.select(db.history):
li = []
for map in maps:
value = get_value(row, *map, year=year)
value = self._encode(value)
li.append(value)
data.append(li)
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):
li = []
for map in maps:
value = get_value(row, *map)
value = self._encode(value)
li.append(value)
data.append(li)
values = [get_value(row, *map) for map in maps]
values = [self._encode(value) for value in values]
data.append(values)
return data
......@@ -232,6 +204,7 @@ class List(BaseReport):
"""
# dataIndex can not contains dot !
dataIndex = [''.join(el) for el in self._maps]
data = self._do_data()
......@@ -241,7 +214,8 @@ class List(BaseReport):
def _encode(self, value):
"""Encode properly date and timedelta.
"""Encode date and timedelta with a define format.
It will then exploit in the grid.
"""
# encode date
......@@ -287,13 +261,14 @@ class List(BaseReport):
cfg = Storage()
tablename, fieldname, keyname = map
# special case the year
# the special case: the year
if tablename == 'year':
cfg.name = 'year'
cfg.type ='int'
store.fields.append(cfg)
continue
# standard processing extract type from the database field
dbfield = db[tablename][fieldname]
# the store index is derived from the dbfield name
......@@ -343,7 +318,7 @@ class List(BaseReport):
for cfg in self._columns:
# encode the dataIndex
# not needed for rownumber column
# not needed for rownumberercolumn
if cfg.dbfield:
cfg.dataIndex = cfg.dbfield.replace('.', '')
del cfg.dbfield
......@@ -444,32 +419,21 @@ class Metric1D(BaseReport):
maps = [split_dbfield(cfg.dbfield) for cfg in columns]
# the year axis is on
# scan the database and compute virtual field on the year basis
if self.config.group_field == 'year':
# build the year range
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 year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
li = [year]
li.extend([get_value(row, *map) for map in maps[1:]])
data.append(li)
values = [get_value(row, *map, year=year) for map in maps]
data.append(values)
# standard scanning
else:
for row in selector.select(db.history):
data.append([get_value(row, *map) for map in maps])
values = [get_value(row, *map) for map in maps]
data.append(values)
return data
......@@ -646,7 +610,7 @@ class Metric2D(BaseReport):
config = self.config
selector = self.selector
# year can be on the horizontal or vertical axis
# 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 map_y[0] == "year":
......@@ -655,31 +619,17 @@ class Metric2D(BaseReport):
if map_x[0] == "year":
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 year in selector.get_years():
selector.set_year(year)
for row in selector.select(db.history):
li = [year, get_value(row, *map_y), get_value(row, *map_z)]
data.append(li)
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):
li = [get_value(row, *map_x),
get_value(row, *map_y),
get_value(row, *map_z)]
data.append(li)
values = [get_value(row, *map) for map in (map_x, map_y, map_z)]
data.append(values)
return data
......
......@@ -28,6 +28,8 @@ class SelectorActiveItems(Selector):
excluded in the query.
"""
self._cache_period = None
self.periode_end = None
self.period_start = None
self.year = None
......@@ -41,6 +43,22 @@ class SelectorActiveItems(Selector):
self.period_end = date(self.year, 12, 31)
def get_years(self):
"""
@rtype: list
@return: the list of year between period start and end
"""
if self._cache_period:
start, end = self._cache_period
else:
start = self.period_start.year
end = self.period_end.year
return range(start, end + 1)
def query(self, table):
"""Build the database query for the database table
including inner join for foreign keys, selector constraints and
......@@ -66,6 +84,31 @@ class SelectorActiveItems(Selector):
return query
def reset_period(self):
"""Reset the period to the initial values.
"""
if self._cahche_period:
self.period_start, self.period_end = self._cahche_period
self._cache_period = None
def set_year(self, year):
"""Modify the period attributes in order to query records active
during the given year.
@type year: integer
@param year:
"""
# keep original period
if self._cache_period == None:
self._cache_period = (self.period_start, self.period_end)
self.period_start = date(year, 1, 1)
self.period_end = date(year, 12, 31)
class MySelector(SelectorActiveItems):
"""Main selector to build list, metric or graph according to
the user criteria:
......
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