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

tsio: initial log functionality

parent 17a36cde3208
......@@ -6,6 +6,7 @@ from dateutil import parser
import pandas as pd
import numpy as np
import pytest
from mock import patch
from tshistory.tsio import TimeSerie
......@@ -42,24 +43,26 @@ def test_changeset(engine):
index = pd.date_range(start=datetime(2017, 1, 1), freq='D', periods=3)
data = [1., 2., 3.]
with engine.connect() as cnx:
with tso.newchangeset(cnx, 'babar'):
tso.insert(cnx, pd.Series(data, index=index), 'ts_values')
tso.insert(cnx, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
with patch('tshistory.tsio.datetime') as mock_date:
mock_date.now.return_value = datetime(2020, 1, 1)
with engine.connect() as cnx:
with tso.newchangeset(cnx, 'babar'):
tso.insert(cnx, pd.Series(data, index=index), 'ts_values')
tso.insert(cnx, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
g = tso.get_group(engine, 'ts_values')
g2 = tso.get_group(engine, 'ts_othervalues')
assert_group_equals(g, g2)
g = tso.get_group(engine, 'ts_values')
g2 = tso.get_group(engine, 'ts_othervalues')
assert_group_equals(g, g2)
with pytest.raises(AssertionError):
tso.insert(engine, pd.Series([2,3,4], index=index), 'ts_values')
with pytest.raises(AssertionError):
tso.insert(engine, pd.Series([2,3,4], index=index), 'ts_values')
with engine.connect() as cnx:
data.append(data.pop(0))
with tso.newchangeset(cnx, 'celeste'):
tso.insert(cnx, pd.Series(data, index=index), 'ts_values')
# below should be a noop
tso.insert(cnx, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
with engine.connect() as cnx:
data.append(data.pop(0))
with tso.newchangeset(cnx, 'celeste'):
tso.insert(cnx, pd.Series(data, index=index), 'ts_values')
# below should be a noop
tso.insert(cnx, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
g = tso.get_group(engine, 'ts_values')
assert ['ts_values'] == list(g.keys())
......@@ -76,6 +79,18 @@ def test_changeset(engine):
2017-01-03 c
""", tso.get(engine, 'ts_othervalues'))
log = tso.log(engine)
assert [
{'author': 'babar',
'rev': 1,
'date': datetime(2020, 1, 1, 0, 0),
'names': ['ts_values', 'ts_othervalues']},
{'author': 'celeste',
'rev': 2,
'date': datetime(2020, 1, 1, 0, 0),
'names': ['ts_values']}
] == log
def test_tstamp_roundtrip(engine):
tso = TimeSerie()
......
......@@ -6,7 +6,7 @@ import pandas as pd
import numpy as np
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.sql.expression import select, func, desc
from sqlalchemy.sql.expression import select, func, asc, desc
from sqlalchemy.dialects.postgresql import JSONB
from tshistory import schema
......@@ -183,6 +183,26 @@ class TimeSerie(object):
).where(tstable.c.csid == cset.c.id)
return cnx.execute(sql).scalar()
def log(self, cnx):
"""Build a structure showing the history of all the series in the db,
per changeset, in chronological order.
"""
log = []
cset, cset_series, reg = schema.changeset, schema.changeset_series, schema.registry
sql = select([cset.c.id, cset.c.author, cset.c.insertion_date, reg.c.name]
).order_by(asc(cset.c.id)
).where(cset.c.id == cset_series.c.csid
).where(cset_series.c.serie == reg.c.name)
rset = cnx.execute(sql)
for csetid, author, revdate, tsname in rset.fetchall():
if log and csetid == log[-1]['rev']:
log[-1]['names'].append(tsname)
continue
log.append({'rev': csetid, 'author': author,
'date': revdate, 'names': [tsname]})
return log
# /API
# Helpers
......
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