kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 1 | import os |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 2 | import shutil |
| 3 | import tempfile |
| 4 | import contextlib |
| 5 | from typing import Tuple, Union, Dict, Any |
| 6 | |
| 7 | import numpy |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 8 | |
| 9 | from wally.result_classes import DataSource, TimeSeries, SuiteConfig |
| 10 | from wally.suits.job import JobConfig, JobParams |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 11 | from wally.hlstorage import ResultStorage |
| 12 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 13 | from cephlib.storage import make_storage |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 14 | |
| 15 | @contextlib.contextmanager |
| 16 | def in_temp_dir(): |
| 17 | dname = tempfile.mkdtemp() |
| 18 | try: |
| 19 | yield dname |
| 20 | finally: |
| 21 | shutil.rmtree(dname) |
| 22 | |
| 23 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 24 | SUITE_ID = "suite_1" |
| 25 | JOB_ID = "job_11" |
| 26 | NODE_ID = "11.22.33.44:223" |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 27 | SENSOR = "sensor" |
| 28 | DEV = "dev" |
| 29 | METRIC = "metric" |
| 30 | TAG = "csv" |
| 31 | DATA_UNITS = "x" |
| 32 | TIME_UNITS = "us" |
| 33 | |
| 34 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 35 | class TJobParams(JobParams): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 36 | 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 koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 47 | def copy(self, **updated) -> 'TJobParams': |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 48 | 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 koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 55 | class TJobConfig(JobConfig): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 56 | @property |
| 57 | def storage_id(self) -> str: |
| 58 | return JOB_ID |
| 59 | |
| 60 | @property |
| 61 | def params(self) -> JobParams: |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 62 | return TJobParams() |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 63 | |
| 64 | def raw(self) -> Dict[str, Any]: |
| 65 | return {} |
| 66 | |
| 67 | @classmethod |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 68 | def fromraw(cls, data: Dict[str, Any]) -> 'TJobConfig': |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 69 | return cls() |
| 70 | |
| 71 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 72 | class TSuiteConfig(SuiteConfig): |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 73 | def __init__(self): |
| 74 | SuiteConfig.__init__(self, "UT", {}, "run_uuid", [], "/tmp", 0, False) |
| 75 | self.storage_id = SUITE_ID |
| 76 | |
| 77 | |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 78 | def test_sensor_ts(): |
| 79 | with in_temp_dir() as root: |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 80 | size = 5 |
| 81 | sensor_data = numpy.arange(size) |
| 82 | collected_at = numpy.arange(size * 2) + 100 |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 83 | |
kdanylov aka koder | 4518318 | 2017-04-30 23:55:40 +0300 | [diff] [blame] | 84 | 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 koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 86 | |
| 87 | with make_storage(root, existing=False) as storage: |
| 88 | rstorage = ResultStorage(storage) |
| 89 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 90 | rstorage.append_sensor(sensor_data, ds, units=DATA_UNITS) |
| 91 | rstorage.append_sensor(sensor_data, ds, units=DATA_UNITS) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 92 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 93 | rstorage.append_sensor(collected_at, cds, units=TIME_UNITS) |
| 94 | rstorage.append_sensor(collected_at + size * 2, cds, units=TIME_UNITS) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 95 | |
| 96 | with make_storage(root, existing=True) as storage2: |
| 97 | rstorage2 = ResultStorage(storage2) |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 98 | 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 koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def 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 koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 109 | ds.verify() |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 110 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 111 | ts = TimeSeries(sensor_data, times=collected_at, units=DATA_UNITS, source=ds, time_units=TIME_UNITS) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 112 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 113 | suite = TSuiteConfig() |
| 114 | job = TJobConfig(1) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 115 | |
| 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 koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 126 | assert len(suits) == 1 |
| 127 | assert len(suits2) == 1 |