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

migration: reclaim the space previously wasted for hand-crafted and unsupervised series


closes #2
parent e7718f7a61a3
......@@ -13,7 +13,7 @@ setup(name='tshistory_supervision',
'tshistory'
],
entry_points={'tshistory.subcommands': [
'migrate-supervision=tshistory_supervision.cli:migrate_supervision',
'migrate-supervision-0.5-to-0.6=tshistory_supervision.cli:migrate_supervision_dot_5_to_dot_6',
'shell=tshistory_supervision.cli:shell'
]},
classifiers=[
......
import click
import tqdm
import pandas as pd
from sqlalchemy import create_engine
from tshistory.util import find_dburi
from collections import defaultdict
from tshistory_supervision.tsio import timeseries
@click.command(name='migrate-supervision')
@click.command(name='migrate-supervision-0.5-to-0.6')
@click.argument('dburi')
@click.option('--name')
@click.option('--namespace', default='tsh')
def migrate_supervision(dburi, skip_schema=False, namespace='tsh'):
def migrate_supervision_dot_5_to_dot_6(dburi, name=None, namespace='tsh'):
engine = create_engine(find_dburi(dburi))
tsh = timeseries(namespace)
if name:
series = [name]
else:
series = tsh.list_series(engine)
with engine.begin() as cn:
cn.execute(
f'alter schema "{namespace}-automatic" '
f'rename to "{namespace}-upstream"'
)
cn.execute(
f'alter schema "{namespace}-automatic.timeserie" '
f'rename to "{namespace}-upstream.timeserie"'
)
cn.execute(
f'alter schema "{namespace}-automatic.snapshot" '
f'rename to "{namespace}-upstream.snapshot"'
categories = defaultdict(list)
bar = tqdm.tqdm(range(len(series)))
for name in series:
synth_idates = tsh.insertion_dates(engine, name)
has_upstream = tsh.upstream.exists(engine, name)
upstream_idates = (
has_upstream and
tsh.upstream.insertion_dates(engine, name) or
[]
)
if synth_idates and not upstream_idates:
status = 'handcrafted'
elif len(synth_idates) == len(upstream_idates):
status = 'unsupervised'
else:
status = 'supervised'
categories[status].append(name)
meta = tsh.metadata(engine, name)
meta['supervision_status'] = status
with engine.begin() as cn:
tsh.update_metadata(cn, name, meta, internal=True)
# reclaim space
if status in ('handcrafted', 'unsupervised'):
if tsh.upstream.exists(cn, name):
tsh.upstream.delete(cn, name)
bar.update()
bar.close()
print('unsupervised', len(categories['unsupervised']))
print('handcrafted', len(categories['handcrafted']))
print('supervised', len(categories['supervised']))
@click.command(name='shell')
......
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