Commit 9a014435 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Coherent processing of datetime using ISO 8601.

parent 12b7bdeb
...@@ -501,10 +501,11 @@ class List(BaseReport): ...@@ -501,10 +501,11 @@ class List(BaseReport):
The type of the column determine by the pandas might be wrong. The type of the column determine by the pandas might be wrong.
This append when value are undefined or missing. This append when value are undefined or missing.
This is fine in most of the case but not wiht computed column. This is fine in most of the case but not with computed column.
In that scase the eval computation crashed. In that case the eval computation crashed.
This method avoid this problem. This method avoid this problem. It also convert properly
datetime column allowing computation with them.
@type column: str @type column: str
@param column: the index of the column in the DataFrame @param column: the index of the column in the DataFrame
...@@ -519,32 +520,19 @@ class List(BaseReport): ...@@ -519,32 +520,19 @@ class List(BaseReport):
if df[column].dtype == 'object' and xtype != None: if df[column].dtype == 'object' and xtype != None:
if xtype in ('datecolumn', 'gridcolumn'): if xtype == 'gridcolumn':
return return
elif xtype == 'booleancolumn': elif xtype == 'booleancolumn':
df[column] = df[column].astype('bool') df[column] = df[column].astype('bool')
elif xtype == 'datecolumn':
df[column] = pd.to_datetime(df[column])
elif xtype == 'numbercolumn': elif xtype == 'numbercolumn':
df[column] = df[column].astype('float64') df[column] = df[column].astype('float64')
def _convert(self, value):
"""Encode date and timedelta with a define format.
It will then exploit in the grid.
"""
# encode date
if isinstance(value, date):
value = value.strftime("%Y-%m-%d")
# encode time delta
elif isinstance(value, timedelta):
value = value.total_seconds()
return value
def _do_metric(self): def _do_metric(self):
"""Interface the database with the DataFrame structure. """Interface the database with the DataFrame structure.
This method handle the "year" database field. This method handle the "year" database field.
...@@ -571,7 +559,7 @@ class List(BaseReport): ...@@ -571,7 +559,7 @@ class List(BaseReport):
# fill the DataFrame # fill the DataFrame
df = pd.DataFrame(data, columns=index) df = pd.DataFrame(data, columns=index)
# make the data frame persistent # make the data frame persistent
self.df = df self.df = df
...@@ -596,10 +584,13 @@ class List(BaseReport): ...@@ -596,10 +584,13 @@ class List(BaseReport):
the key is the name of the C{Ext.data.Field}. the key is the name of the C{Ext.data.Field}.
""" """
store = self._store # extract the list of records as a JSON-string
for row in self.df.T.to_dict().itervalues(): # at this stage date/time are converted as an ISO8601 string
store.data.append(row) data = self.df.to_json(orient='records', date_format='iso')
# convert the JSON-string into a list
self._store.data = json.loads(data)
def _set_store_fields(self): def _set_store_fields(self):
""" Generate the C{Ext.data.Store.fields} property. """ Generate the C{Ext.data.Store.fields} property.
...@@ -641,24 +632,16 @@ class List(BaseReport): ...@@ -641,24 +632,16 @@ class List(BaseReport):
if dbfield.type in ('string', 'text', 'json'): if dbfield.type in ('string', 'text', 'json'):
cfg.type = 'string' cfg.type = 'string'
elif dbfield.type == 'date': elif dbfield.type in ('date', 'datetime', 'time'):
cfg.type = 'date' cfg.type = 'date'
cfg.dateFormat = 'Y-m-d' cfg.dateFormat = 'c'
elif dbfield.type == 'datetime':
cfg.type = 'date'
cfg.dateFormat = 'Y-m-d H:i:s'
elif dbfield.type == 'double': elif dbfield.type == 'double':
cfg.type = 'float' cfg.type = 'float'
elif dbfield.type == 'integer': elif dbfield.type == 'integer':
cfg.type = 'int' cfg.type = 'int'
elif dbfield.type == 'time':
cfg.type = 'date'
cfg.dateFormat = 'H:i:s'
store.fields.append(cfg) store.fields.append(cfg)
......
/** /**
* Convert a duration in second into a string. * Convert a duration in nanosecond into a string.
* *
* @param {Number} value * @param {Number} value
* The duration in seconds * The duration in nanoseconds
* *
* @return {String} * @return {String}
* The duration express as the number of year + the number of months e.g. "3y + 09m" * The duration express as the number of year + the number of months e.g. "3y + 09m"
...@@ -12,22 +12,36 @@ function reprDuration(value) { ...@@ -12,22 +12,36 @@ function reprDuration(value) {
"use strict"; "use strict";
var nmonths = 0, var duration = value / 1.E9,
nyears = 0, month,
year = 365 * 24 * 3600, nd,
month = year / 12; nm,
ny,
rep,
year;
nyears = Math.floor(value / year); // duration of year and month in seconds
year = 365 * 24 * 3600;
month = year / 12;
nmonths = Math.floor((value - nyears * year) / month); // number of years and months
nmonths = Ext.String.leftPad(nmonths.toString(), 2, '0'); ny = Math.floor(duration / year);
nm = Math.floor((duration - ny * year) / month);
if (nyears > 0) { // format the number of month
return Ext.String.format("{0}y + {1}m", nyears, nmonths); nm = Ext.String.leftPad(nm.toString(), 2, '0');
} else if (nmonths > 0) { // format the response
return Ext.String.format("{0}m", nmonths); if (ny > 0) {
rep = Ext.String.format("{0}y + {1}m", ny, nm);
} else if (nm > 0) {
rep = Ext.String.format("{0}m", nm);
} else {
nd = duration / (24 * 3600);
rep = Ext.String.leftPad(nd.toString(), 2, '0');
} }
return ""; return rep;
} }
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