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

tsio/log: provide the diffs if asked


This should be sufficient if we want to reconstruct a serie history.
parent 37852a99f0f0
......@@ -6,7 +6,7 @@ import pytest
from pytest_sa_pg.fixture import db
from tshistory import schema
from tshistory import schema, tsio
DATADIR = Path(__file__).parent / 'test' / 'data'
......@@ -28,4 +28,23 @@ def engine(request):
# /cleanup
schema.init(engine)
return create_engine(uri)
e = create_engine(uri)
yield e
# build a ts using the logs from another
tsh = tsio.TimeSerie()
log = tsh.log(engine, diff=True)
allnames = set()
for rev in log:
for name, ts in rev['diff'].items():
allnames.add(name)
with tsh.newchangeset(engine, rev['author'],
_insertion_date=rev['date']):
tsh.insert(engine, ts, 'new_' + name)
# NOTE: the name set varies depending on the amount of tests
# so we don't capture that exact set for regression purpposes
# We only want to prove the manipulated series can be reconstructed
# using the logger.
for name in allnames:
assert (tsh.get(engine, name) == tsh.get(engine, 'new_' + name)).all()
......@@ -33,6 +33,10 @@ def tojson(ts):
def fromjson(jsonb, tsname):
return _fromjson(jsonb, tsname).fillna(value=np.nan)
def _fromjson(jsonb, tsname):
if jsonb == '{}':
return pd.Series(name=tsname)
......@@ -183,7 +187,7 @@ class TimeSerie(object):
).where(tstable.c.csid == cset.c.id)
return cnx.execute(sql).scalar()
def log(self, cnx):
def log(self, cnx, diff=False):
"""Build a structure showing the history of all the series in the db,
per changeset, in chronological order.
"""
......@@ -201,6 +205,12 @@ class TimeSerie(object):
continue
log.append({'rev': csetid, 'author': author,
'date': revdate, 'names': [tsname]})
if diff:
for rev in log:
rev['diff'] = {name: self._diff(cnx, rev['rev'], name)
for name in rev['names']}
return log
# /API
......@@ -359,6 +369,25 @@ class TimeSerie(object):
# diff handling
def _diff(self, cnx, csetid, name):
table = self._get_ts_table(cnx, name)
cset = schema.changeset
def filtercset(sql):
return sql.where(table.c.csid == cset.c.id
).where(cset.c.id == csetid)
sql = filtercset(select([table.c.id]))
tsid = cnx.execute(sql).scalar()
if tsid == 1:
sql = select([table.c.snapshot])
else:
sql = select([table.c.diff])
sql = filtercset(sql)
return fromjson(cnx.execute(sql).scalar(), name)
def _compute_diff(self, fromts, tots):
"""Compute the difference between fromts and tots
(like in tots - fromts).
......
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