blob: bed3c56fc69b2c9815e8e3ccd1e8dfc6289e3811 [file] [log] [blame]
kdanylov aka koderb0833332017-05-13 20:39:17 +03001import os
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +03002import shutil
3import tempfile
4import contextlib
5from typing import Tuple, Union, Dict, Any
6
7import numpy
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +03008
9from wally.result_classes import DataSource, TimeSeries, SuiteConfig
10from wally.suits.job import JobConfig, JobParams
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030011from wally.hlstorage import ResultStorage
12
kdanylov aka koderb0833332017-05-13 20:39:17 +030013from cephlib.storage import make_storage
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030014
15@contextlib.contextmanager
16def in_temp_dir():
17 dname = tempfile.mkdtemp()
18 try:
19 yield dname
20 finally:
21 shutil.rmtree(dname)
22
23
kdanylov aka koderb0833332017-05-13 20:39:17 +030024SUITE_ID = "suite_1"
25JOB_ID = "job_11"
26NODE_ID = "11.22.33.44:223"
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030027SENSOR = "sensor"
28DEV = "dev"
29METRIC = "metric"
30TAG = "csv"
31DATA_UNITS = "x"
32TIME_UNITS = "us"
33
34
kdanylov aka koderb0833332017-05-13 20:39:17 +030035class TJobParams(JobParams):
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030036 def __init__(self) -> None:
37 JobParams.__init__(self)
38
39 @property
40 def summary(self) -> str:
41 return "UT_Job_CFG"
42
43 @property
44 def long_summary(self) -> str:
45 return "UT_Job_Config"
46
kdanylov aka koderb0833332017-05-13 20:39:17 +030047 def copy(self, **updated) -> 'TJobParams':
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030048 return self.__class__()
49
50 @property
51 def char_tpl(self) -> Tuple[Union[str, int, float, bool], ...]:
52 return (1, 2, 3)
53
54
kdanylov aka koderb0833332017-05-13 20:39:17 +030055class TJobConfig(JobConfig):
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030056 @property
57 def storage_id(self) -> str:
58 return JOB_ID
59
60 @property
61 def params(self) -> JobParams:
kdanylov aka koderb0833332017-05-13 20:39:17 +030062 return TJobParams()
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030063
64 def raw(self) -> Dict[str, Any]:
65 return {}
66
67 @classmethod
kdanylov aka koderb0833332017-05-13 20:39:17 +030068 def fromraw(cls, data: Dict[str, Any]) -> 'TJobConfig':
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030069 return cls()
70
71
kdanylov aka koderb0833332017-05-13 20:39:17 +030072class TSuiteConfig(SuiteConfig):
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030073 def __init__(self):
74 SuiteConfig.__init__(self, "UT", {}, "run_uuid", [], "/tmp", 0, False)
75 self.storage_id = SUITE_ID
76
77
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030078def test_sensor_ts():
79 with in_temp_dir() as root:
kdanylov aka koderb0833332017-05-13 20:39:17 +030080 size = 5
81 sensor_data = numpy.arange(size)
82 collected_at = numpy.arange(size * 2) + 100
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030083
kdanylov aka koder45183182017-04-30 23:55:40 +030084 ds = DataSource(node_id=NODE_ID, sensor=SENSOR, dev=DEV, metric=METRIC, tag='csv')
85 cds = DataSource(node_id=NODE_ID, metric='collected_at', tag='csv')
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030086
87 with make_storage(root, existing=False) as storage:
88 rstorage = ResultStorage(storage)
89
kdanylov aka koderb0833332017-05-13 20:39:17 +030090 rstorage.append_sensor(sensor_data, ds, units=DATA_UNITS)
91 rstorage.append_sensor(sensor_data, ds, units=DATA_UNITS)
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030092
kdanylov aka koderb0833332017-05-13 20:39:17 +030093 rstorage.append_sensor(collected_at, cds, units=TIME_UNITS)
94 rstorage.append_sensor(collected_at + size * 2, cds, units=TIME_UNITS)
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +030095
96 with make_storage(root, existing=True) as storage2:
97 rstorage2 = ResultStorage(storage2)
kdanylov aka koderb0833332017-05-13 20:39:17 +030098 ts = rstorage2.get_sensor(ds)
99 assert numpy.array_equal(ts.data, numpy.concatenate((sensor_data, sensor_data)))
100 assert numpy.array_equal(ts.times, numpy.concatenate((collected_at, collected_at + size * 2))[::2])
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +0300101
102
103def test_result_ts():
104 with in_temp_dir() as root:
105 sensor_data = numpy.arange(5, dtype=numpy.uint32)
106 collected_at = numpy.arange(5, dtype=numpy.uint32) + 100
107 ds = DataSource(suite_id=SUITE_ID, job_id=JOB_ID,
108 node_id=NODE_ID, sensor=SENSOR, dev=DEV, metric=METRIC, tag=TAG)
kdanylov aka koderb0833332017-05-13 20:39:17 +0300109 ds.verify()
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +0300110
kdanylov aka koderb0833332017-05-13 20:39:17 +0300111 ts = TimeSeries(sensor_data, times=collected_at, units=DATA_UNITS, source=ds, time_units=TIME_UNITS)
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +0300112
kdanylov aka koderb0833332017-05-13 20:39:17 +0300113 suite = TSuiteConfig()
114 job = TJobConfig(1)
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +0300115
116 with make_storage(root, existing=False) as storage:
117 rstorage = ResultStorage(storage)
118 rstorage.put_or_check_suite(suite)
119 rstorage.put_job(suite, job)
120 rstorage.put_ts(ts)
121
122 with make_storage(root, existing=True) as storage2:
123 rstorage2 = ResultStorage(storage2)
124 suits = list(rstorage2.iter_suite('UT'))
125 suits2 = list(rstorage2.iter_suite())
kdanylov aka koderb0833332017-05-13 20:39:17 +0300126 assert len(suits) == 1
127 assert len(suits2) == 1