Commit e029a38c authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

tsio: add a `diff mode` for `get_history`

parent 2510ff6f65ef
......@@ -787,6 +787,14 @@ insertion_date value_date
2017-01-03 2.0
""", histts)
diffs = tsh.get_history(engine, 'smallserie', diffmode=True)
assert_df("""
insertion_date value_date
2017-02-01 2017-01-01 0.0
2017-02-02 2017-01-02 1.0
2017-02-03 2017-01-03 2.0
""", diffs)
for idx, idate in enumerate(histts.groupby('insertion_date').groups):
with engine.connect() as cn:
with tsh.newchangeset(cn, 'aurelien.campeas@pythonian.f',
......@@ -860,6 +868,16 @@ insertion_date value_date
2017-01-02 1.0
""", tsc)
diffs = tsh.get_history(engine, 'smallserie',
diffmode=True,
from_value_date=datetime(2017, 1, 1),
to_value_date=datetime(2017, 1, 2))
assert_df("""
insertion_date value_date
2017-02-01 2017-01-01 0.0
2017-02-02 2017-01-02 1.0
""", diffs)
tsc = tsh.get_history(engine, 'smallserie',
from_value_date=datetime(2017, 1, 2))
assert_df("""
......
......@@ -98,11 +98,18 @@ def get(db_uri, seriename, json):
@click.argument('db-uri')
@click.argument('seriename')
@click.option('--json', is_flag=True, default=False)
def history(db_uri, seriename, json):
@click.option('--from-insertion-date', type=temporal)
@click.option('--to-insertion-date', type=temporal)
@click.option('--diff', is_flag=True, default=True)
def history(db_uri, seriename,
from_insertion_date, to_insertion_date,
diff, json):
"""show a serie full history """
engine = create_engine(db_uri)
ts = TSH.get_history(engine, seriename)
ts = TSH.get_history(engine, seriename,
from_insertion_date, to_insertion_date,
diffmode=diff)
if json:
print(ts.to_json())
else:
......
......@@ -200,7 +200,8 @@ class TimeSerie(object):
from_insertion_date=None,
to_insertion_date=None,
from_value_date=None,
to_value_date=None):
to_value_date=None,
diffmode=False):
table = self._get_ts_table(cn, name)
if table is None:
return
......@@ -222,6 +223,22 @@ class TimeSerie(object):
# where noting did happen, but you get nothing
return
if diffmode:
series = []
for csid, revdate, diff in diffs:
if diff is None: # we must fetch the initial snapshot
sql = select([table.c.snapshot]).where(table.c.csid == csid)
diff = cn.execute(sql).scalar()
serie = subset(self._deserialize(diff, name), from_value_date, to_value_date)
mindex = [(revdate, valuestamp) for valuestamp in serie.index]
serie.index = pd.MultiIndex.from_tuples(mindex, names=[
'insertion_date', 'value_date']
)
series.append(serie)
series = pd.concat(series)
series.name = name
return series
csid, revdate, diff_ = diffs[0]
snapshot = self._build_snapshot_upto(cn, table, [
lambda cset, _: cset.c.id == csid
......@@ -231,6 +248,7 @@ class TimeSerie(object):
for csid_, revdate, diff in diffs[1:]:
diff = subset(self._deserialize(diff, table.name),
from_value_date, to_value_date)
serie = self._apply_diff(series[-1][1], diff)
series.append((revdate, serie))
......
Supports Markdown
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