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

tsio: retire the `newchangeset` and `get_group` apis

upstream use is disappearing and it was clunky

closes #40.
parent 5edbbb47afd8
......@@ -47,9 +47,8 @@ def tsh(request, engine):
if 'big' in name:
continue
allnames.add(name)
with tsh.newchangeset(engine, rev['author'],
_insertion_date=rev['date']):
tsh.insert(engine, ts, 'new_' + name)
tsh.insert(engine, ts, 'new_' + name,
rev['author'], _insertion_date=rev['date'])
# NOTE: the name set varies depending on the amount of tests
# so we don't capture that exact set for regression purpposes
......
# coding: utf-8
import calendar
from datetime import datetime, timedelta
from pathlib import Path
from time import time
from functools import partial
import pytz
from dateutil import parser
......@@ -26,86 +22,11 @@ def utcdt(*dt):
return pd.Timestamp(datetime(*dt), tz='UTC')
def test_changeset(engine, tsh):
index = pd.date_range(start=datetime(2017, 1, 1), freq='D', periods=3)
data = [1., 2., 3.]
with engine.connect() as cn:
with tsh.newchangeset(cn, 'babar', _insertion_date=utcdt(2020, 1, 1)):
tsh.insert(cn, pd.Series(data, index=index), 'ts_values', author='WONTBEUSED')
tsh.insert(cn, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
# bogus author won't show up
assert tsh.log(engine)[0]['author'] == 'babar'
g = tsh.get_group(engine, 'ts_values')
g2 = tsh.get_group(engine, 'ts_othervalues')
assert_group_equals(g, g2)
with pytest.raises(AssertionError):
tsh.insert(engine, pd.Series([2, 3, 4], index=index), 'ts_values')
with engine.connect() as cn:
data.append(data.pop(0))
with tsh.newchangeset(cn, 'celeste', _insertion_date=utcdt(2020, 1, 1)):
tsh.insert(cn, pd.Series(data, index=index), 'ts_values')
# below should be a noop
tsh.insert(cn, pd.Series(['a', 'b', 'c'], index=index), 'ts_othervalues')
g = tsh.get_group(engine, 'ts_values')
assert ['ts_values'] == list(g.keys())
assert_df("""
2017-01-01 2.0
2017-01-02 3.0
2017-01-03 1.0
""", tsh.get(engine, 'ts_values'))
assert_df("""
2017-01-01 a
2017-01-02 b
2017-01-03 c
""", tsh.get(engine, 'ts_othervalues'))
log = tsh.log(engine, names=['ts_values', 'ts_othervalues'])
assert [
{'author': 'babar',
'rev': 1,
'date': pd.Timestamp('2020-01-01 00:00:00+0000', tz='UTC'),
'meta': {},
'names': ['ts_values', 'ts_othervalues']},
{'author': 'celeste',
'rev': 2,
'meta': {},
'date': pd.Timestamp('2020-01-01 00:00:00+0000', tz='UTC'),
'names': ['ts_values']}
] == log
log = tsh.log(engine, names=['ts_othervalues'])
assert len(log) == 1
assert log[0]['rev'] == 1
assert log[0]['names'] == ['ts_values', 'ts_othervalues']
log = tsh.log(engine, fromrev=2)
assert len(log) == 1
log = tsh.log(engine, torev=1)
assert len(log) == 1
info = tsh.info(engine)
assert {
'changeset count': 2,
'serie names': ['ts_othervalues', 'ts_values'],
'series count': 2
} == info
def test_strip(engine, tsh):
for i in range(1, 5):
pubdate = utcdt(2017, 1, i)
ts = genserie(datetime(2017, 1, 10), 'H', 1 + i)
with tsh.newchangeset(engine, 'babar', _insertion_date=pubdate):
tsh.insert(engine, ts, 'xserie')
tsh.insert(engine, ts, 'xserie', 'babar', _insertion_date=pubdate)
# also insert something completely unrelated
tsh.insert(engine, genserie(datetime(2018, 1, 1), 'D', 1 + i), 'yserie', 'celeste')
......@@ -679,34 +600,33 @@ def test_revision_date(engine, tsh):
for i in range(1, 5):
with engine.connect() as cn:
with tsh.newchangeset(cn, 'test',
_insertion_date=utcdt(2016, 1, i)):
tsh.insert(cn, genserie(datetime(2017, 1, i), 'D', 3, [i]), 'revdate')
tsh.insert(cn, genserie(datetime(2017, 1, i), 'D', 3, [i]), 'revdate',
'test', _insertion_date=utcdt(2016, 1, i))
# end of prologue, now some real meat
idate0 = pd.Timestamp('2015-1-1 00:00:00', tz='UTC')
with tsh.newchangeset(engine, 'test', _insertion_date=idate0):
ts = genserie(datetime(2010, 1, 4), 'D', 4, [0], name='truc')
tsh.insert(engine, ts, 'ts_through_time')
assert idate0 == tsh.latest_insertion_date(engine, 'ts_through_time')
ts = genserie(datetime(2010, 1, 4), 'D', 4, [0], name='truc')
tsh.insert(engine, ts, 'ts_through_time',
'test', _insertion_date=idate0)
assert idate0 == tsh.latest_insertion_date(engine, 'ts_through_time')
idate1 = pd.Timestamp('2015-1-1 15:45:23', tz='UTC')
with tsh.newchangeset(engine, 'test', _insertion_date=idate1):
ts = genserie(datetime(2010, 1, 4), 'D', 4, [1], name='truc')
tsh.insert(engine, ts, 'ts_through_time')
assert idate1 == tsh.latest_insertion_date(engine, 'ts_through_time')
ts = genserie(datetime(2010, 1, 4), 'D', 4, [1], name='truc')
tsh.insert(engine, ts, 'ts_through_time',
'test', _insertion_date=idate1)
assert idate1 == tsh.latest_insertion_date(engine, 'ts_through_time')
idate2 = pd.Timestamp('2015-1-2 15:43:23', tz='UTC')
with tsh.newchangeset(engine, 'test', _insertion_date=idate2):
ts = genserie(datetime(2010, 1, 4), 'D', 4, [2], name='truc')
tsh.insert(engine, ts, 'ts_through_time')
assert idate2 == tsh.latest_insertion_date(engine, 'ts_through_time')
ts = genserie(datetime(2010, 1, 4), 'D', 4, [2], name='truc')
tsh.insert(engine, ts, 'ts_through_time',
'test', _insertion_date=idate2)
assert idate2 == tsh.latest_insertion_date(engine, 'ts_through_time')
idate3 = pd.Timestamp('2015-1-3', tz='UTC')
with tsh.newchangeset(engine, 'test', _insertion_date=idate3):
ts = genserie(datetime(2010, 1, 4), 'D', 4, [3], name='truc')
tsh.insert(engine, ts, 'ts_through_time')
assert idate3 == tsh.latest_insertion_date(engine, 'ts_through_time')
ts = genserie(datetime(2010, 1, 4), 'D', 4, [3], name='truc')
tsh.insert(engine, ts, 'ts_through_time',
'test', _insertion_date=idate3)
assert idate3 == tsh.latest_insertion_date(engine, 'ts_through_time')
ts = tsh.get(engine, 'ts_through_time')
......@@ -1194,9 +1114,9 @@ insertion_date app_date fc_date
def test_get_history(engine, tsh):
for numserie in (1, 2, 3):
with engine.connect() as cn:
with tsh.newchangeset(cn, 'aurelien.campeas@pythonian.fr',
_insertion_date=utcdt(2017, 2, numserie)):
tsh.insert(cn, genserie(datetime(2017, 1, 1), 'D', numserie), 'smallserie')
tsh.insert(cn, genserie(datetime(2017, 1, 1), 'D', numserie), 'smallserie',
'aurelien.campeas@pythonian.fr',
_insertion_date=utcdt(2017, 2, numserie))
ts = tsh.get(engine, 'smallserie')
assert_df("""
......@@ -1248,9 +1168,8 @@ insertion_date value_date
for idate in histts.index.get_level_values('insertion_date').unique():
with engine.connect() as cn:
idate = idate.replace(tzinfo=pytz.timezone('UTC'))
with tsh.newchangeset(cn, 'aurelien.campeas@pythonian.f',
_insertion_date=idate):
tsh.insert(cn, histts[idate], 'smallserie2')
tsh.insert(cn, histts[idate], 'smallserie2',
'aurelien.campeas@pythonian.f', _insertion_date=idate)
# this is perfectly round-tripable
assert (tsh.get(engine, 'smallserie2') == ts).all()
......@@ -1364,9 +1283,9 @@ def test_nr_gethistory(engine, tsh):
idate = utcdt(2016, 1, 1)
for i in range(5):
with engine.connect() as cn:
with tsh.newchangeset(cn, 'aurelien.campeas@pythonian.f',
_insertion_date=idate + timedelta(days=i)):
tsh.insert(cn, s1 * i, 'foo')
tsh.insert(cn, s1 * i, 'foo',
'aurelien.campeas@pythonian.f',
_insertion_date=idate + timedelta(days=i))
df = tsh.get_history(engine, 'foo',
datetime(2016, 1, 3),
......
......@@ -25,7 +25,6 @@ TABLES = {}
class TimeSerie(SeriesServices):
_csid = None
namespace = 'tsh'
schema = None
......@@ -35,40 +34,13 @@ class TimeSerie(SeriesServices):
self.schema.define()
self.metadatacache = {}
# API : changeset, insert, get, delete
@contextmanager
def newchangeset(self, cn, author, _insertion_date=None):
"""A context manager to allow insertion of several series within the
same changeset identifier
This allows to group changes to several series, hence
producing a macro-change.
_insertion_date is *only* provided for migration purposes and
not part of the API.
"""
assert self._csid is None
self._csid = self._newchangeset(cn, author, _insertion_date)
self._author = author
yield
del self._csid
del self._author
def insert(self, cn, newts, name, author=None, _insertion_date=None):
def insert(self, cn, newts, name, author, _insertion_date=None):
"""Create a new revision of a given time series
newts: pandas.Series with date index
name: str unique identifier of the serie
author: str free-form author name (mandatory, unless provided
to the newchangeset context manager).
author: str free-form author name
"""
assert self._csid or author, 'author is mandatory'
if self._csid and author:
L.info('author r{} will not be used when in a changeset'.format(author))
author = None
assert isinstance(newts, pd.Series)
assert not newts.index.duplicated().any()
......@@ -131,16 +103,6 @@ class TimeSerie(SeriesServices):
self.metadatacache[tsname] = meta
return meta
def get_group(self, cn, name, revision_date=None):
csid = self._latest_csid_for(cn, name)
group = {}
for seriename in self._changeset_series(cn, csid):
serie = self.get(cn, seriename, revision_date)
if serie is not None:
group[seriename] = serie
return group
def get_history(self, cn, name,
from_insertion_date=None,
to_insertion_date=None,
......@@ -339,7 +301,7 @@ class TimeSerie(SeriesServices):
if len(newts) == 0:
return None
snapshot = Snapshot(cn, self, name)
csid = self._csid or self._newchangeset(cn, author, insertion_date)
csid = self._newchangeset(cn, author, insertion_date)
head = snapshot.create(newts)
value = {
'cset': csid,
......@@ -361,7 +323,7 @@ class TimeSerie(SeriesServices):
name, author or self._author, len(newts))
return
csid = self._csid or self._newchangeset(cn, author, insertion_date)
csid = self._newchangeset(cn, author, insertion_date)
head = snapshot.update(diff)
value = {
'cset': csid,
......@@ -459,11 +421,6 @@ class TimeSerie(SeriesServices):
insertion_date=idate)
return cn.execute(sql).inserted_primary_key[0]
def _latest_csid_for(self, cn, name):
table = self._get_ts_table(cn, name)
sql = select([func.max(table.c.cset)])
return cn.execute(sql).scalar()
def _changeset_series(self, cn, csid):
cset_serie = self.schema.changeset_series
sql = select([cset_serie.c.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