Exemplo n.º 1
0
 def test_intdata(self):
     i = TimeSeries("int", "ph")
     for j in range(10):
         i.insert_point(j, int(j * 2.1))
     self.assertEqual(len(i), 10)
     self.assertEqual(i[3].ts, 3)
     self.assertEqual(i[3].value, 6)
Exemplo n.º 2
0
    def test_dictitem(self):
        i1 = EventList("test", "ph")
        self.assertTrue(i1.empty())
        i1.insert_point(1, dict(hey=1.0, ho=2.0))
        self.assertFalse(i1.empty())

        i2 = TimeSeries("test1", "ph", [(1, 1.0)])
        self.assertEqual(i2[0].value, 1.0)
        i2.insert_point(1, 2.0)
        self.assertEqual(i2[0].value, 2.0)

        i3 = TimeSeries("test2", "ph", [(1, 1.0)])
        self.assertNotEqual(i2, i3)

        i4 = TimeSeries("test1", "ph", [(1, 1.0)])
        self.assertEqual(i2, i4)

        self.assertEqual(i2.to_hash(), i4.to_hash())
Exemplo n.º 3
0
    def test_delete(self):
        conf = get_unit_test_config()
        db = Connection(engine=conf.ENGINE,
                        engine_options=conf.ENGINE_OPTIONS,
                        metric_definitions=get_test_metrics())
        db.database_init(silent=True)
        db.timeseries._create_metric("ph", silent=True)

        base = datetime.datetime.now()
        data_list = [(base - datetime.timedelta(minutes=10 * x),
                      random.random() * 5) for x in range(0, 144 * 5)]
        ts = TimeSeries("device", "ph", values=data_list)
        from_pd = pendulum.instance(data_list[-1][0])
        from_ts = from_pd.int_timestamp
        to_pd = pendulum.instance(data_list[0][0])
        to_ts = to_pd.int_timestamp

        #delete all data just in case
        r = db.timeseries.delete_timeseries("device", ["ph"],
                                            from_ts - 24 * 60 * 60,
                                            to_ts + 24 * 60 * 60)

        #insert
        db.timeseries.insert_timeseries(ts)

        # get
        r = db.timeseries.get_single_timeseries("device", "ph", from_ts, to_ts)
        a = list(r.all())
        self.assertEqual(len(a), 144 * 5)

        # perform delete
        r = db.timeseries.delete_timeseries("device", ["ph"], from_ts, from_ts)
        self.assertEqual(r, 1)

        # get
        r = db.timeseries.get_single_timeseries("device", "ph",
                                                from_ts + 24 * 60 * 60,
                                                to_ts + 24 * 60 * 60)
        a = list(r.all())
        self.assertEqual(len(a), 144 * 4)

        # delete all
        r = db.timeseries.delete_timeseries("device", ["ph"], from_ts, to_ts)
        self.assertGreaterEqual(r, 5)
Exemplo n.º 4
0
    def test_proto(self):
        res = TimeSeries("ddd", "ph")
        ts = to_ts(datetime.datetime(2000, 1, 1, 0, 0))
        for j in range(500):
            res.insert_point(j * 600, float(j % 6))
        p = FloatTimeSeries()
        p.ParseFromString(res.to_proto_bytes())
        self.assertEqual(len(p.timestamps), 500)
        self.assertEqual(len(p.timestamp_offsets), 500)
        self.assertEqual(len(p.values), 500)
        self.assertEqual(p.key, "ddd")
        self.assertEqual(p.metric, "ph")

        ts2 = TimeSeries.from_proto(p)
        self.assertEqual(len(ts2), 500)

        ts3 = TimeSeries.from_proto_bytes(ts2.to_proto_bytes())
        self.assertEqual(len(ts3), 500)
Exemplo n.º 5
0
    def test_rawitem(self):
        d = []
        for i in range(100):
            d.append((i, i * 2.5))
        self.assertEqual(len(d), 100)

        d1 = list(d[:50])
        d2 = list(d[50:])
        random.shuffle(d1)
        random.shuffle(d2)

        i = TimeSeries("ph_sensor", "ph")
        for t, v in d1:
            i.insert_point(t, v)
        i.insert(d2)

        l = list([x for x in i.all()])
        self.assertEqual(len(l), 100)
        logging.warning(l)
        for i in range(100):
            self.assertEqual(l[i][0], i)
            self.assertEqual(l[i][1], i * 2.5)
Exemplo n.º 6
0
    def test_selective_delete(self):
        conf = get_unit_test_config()
        db = Connection(engine=conf.ENGINE,
                        engine_options=conf.ENGINE_OPTIONS,
                        metric_definitions=get_test_metrics())
        db.database_init(silent=True)

        base = datetime.datetime(2019,
                                 2,
                                 1,
                                 23,
                                 50,
                                 tzinfo=datetime.timezone.utc)
        ph_data = [(base - datetime.timedelta(minutes=10 * x), ((x % 3) + 4))
                   for x in range(0, 144 * 5)]
        act_data = [(base - datetime.timedelta(minutes=10 * x), ((x % 3) + 20))
                    for x in range(0, 144 * 5)]
        ph = TimeSeries("dev1", "ph", values=ph_data)
        act = TimeSeries("dev1", "act", values=act_data)

        from_pd = pendulum.instance(ph_data[-1][0])
        from_ts = from_pd.int_timestamp
        to_pd = pendulum.instance(ph_data[0][0])
        to_ts = to_pd.int_timestamp

        #delete all data just in case
        r = db.timeseries.delete_timeseries("dev1", ["act", "ph"],
                                            from_ts - 24 * 60 * 60,
                                            to_ts + 24 * 60 * 60)
        db.timeseries.insert_timeseries(act)
        db.timeseries.insert_timeseries(ph)

        get_timeseries = db.timeseries.get_single_timeseries
        self.assertEqual(len(get_timeseries("dev1", "ph", from_ts, to_ts)),
                         144 * 5)
        self.assertEqual(len(get_timeseries("dev1", "act", from_ts, to_ts)),
                         144 * 5)
        # perform delete
        r = db.timeseries.delete_timeseries("dev1", ["ph"], from_ts, from_ts)
        self.assertEqual(r, 1)

        self.assertEqual(
            len(
                get_timeseries("dev1", "ph", from_ts,
                               from_ts + 24 * 60 * 60 - 1)), 0)
        self.assertEqual(len(get_timeseries("dev1", "ph", from_ts, to_ts)),
                         144 * 4)
        self.assertEqual(len(get_timeseries("dev1", "act", from_ts, to_ts)),
                         144 * 5)

        delete_start = from_ts + 24 * 60 * 60
        delete_end = from_ts + 24 * 60 * 60 * 3

        r = db.timeseries.delete_timeseries("dev1", ["act"],
                                            delete_start + 12 * 60 * 60,
                                            delete_end - 12 * 60 * 60)
        self.assertEqual(r, 2)
        self.assertEqual(
            len(
                get_timeseries("dev1", "ph", from_ts,
                               from_ts + 24 * 60 * 60 - 1)), 0)
        self.assertEqual(len(get_timeseries("dev1", "ph", from_ts, to_ts)),
                         144 * 4)
        self.assertEqual(len(get_timeseries("dev1", "act", from_ts, to_ts)),
                         144 * 3)
        self.assertEqual(
            len(get_timeseries("dev1", "act", from_ts, delete_start)), 144)
        self.assertEqual(
            len(get_timeseries("dev1", "act", delete_start, delete_end - 1)),
            0)
        self.assertEqual(len(get_timeseries("dev1", "act", delete_end, to_ts)),
                         144 * 2)
Exemplo n.º 7
0
    def test_aggregations(self):
        res = TimeSeries("ddd", "temp")
        ts = to_ts(datetime.datetime(2000, 1, 1, 0, 0))
        for _ in range(10):
            for j in range(144):
                res.insert_point(ts + j * 600, float(j % 6))
            ts += 144 * 600

        # All
        self.assertEqual(len(list(res.all())), 144 * 10)

        # Daily
        daily = list(res.daily())
        self.assertEqual(len(daily), 10)
        self.assertEqual(len(list(daily[0])), 144)

        # Daily Aggr
        g = res.aggregation("daily", "sum")
        for x in g:
            self.assertEqual(x[1], 360.0)

        g = res.aggregation("daily", "count")
        for x in g:
            self.assertEqual(x[1], 144)

        g = res.aggregation("daily", "mean")
        for x in g:
            self.assertEqual(x[1], 2.5)

        g = res.aggregation("daily", "min")
        for x in g:
            self.assertEqual(x[1], 0.0)

        g = res.aggregation("daily", "max")
        for x in g:
            self.assertEqual(x[1], 5.0)

        g = res.aggregation("daily", "amp")
        for x in g:
            self.assertEqual(x[1], 5.0)

        # Hourly
        daily = list(res.daily())
        self.assertEqual(len(daily), 10)
        self.assertEqual(len(list(daily[0])), 144)

        # Hourly Aggr
        g = res.aggregation("hourly", "sum")
        for x in g:
            self.assertEqual(x[1], 15.0)

        g = res.aggregation("hourly", "count")
        for x in g:
            self.assertEqual(x[1], 6)

        g = res.aggregation("hourly", "mean")
        for x in g:
            self.assertEqual(x[1], 2.5)

        g = res.aggregation("hourly", "min")
        for x in g:
            self.assertEqual(x[1], 0.0)

        g = res.aggregation("hourly", "max")
        for x in g:
            self.assertEqual(x[1], 5.0)

        g = res.aggregation("hourly", "amp")
        for x in g:
            self.assertEqual(x[1], 5.0)
Exemplo n.º 8
0
    def test_local_aggregation(self):
        ts1 = TimeSeries("ddd", "temp")
        ts2 = TimeSeries("fff", "temp")

        # daily, timeshift to winter
        start = pendulum.datetime(2018, 10, 25, 0, 0, tz='Europe/Vienna')
        cur = start
        for i in range(10):
            for j in range(24 * 6):
                ts1.insert_point(cur, float(i+1))
                cur = cur.add(minutes=10)
        end = cur
        self.assertEqual(start.add(days=10).subtract(hours=1), end)

        l = [list(x) for x in ts1.daily_local()]
        self.assertEqual(len(l), 10)
        self.assertEqual(len(l[0]), 144)
        self.assertEqual(l[0][0].dt, start)
        for i in range(144):
            self.assertEqual(l[0][i].value, 1.0)
        self.assertEqual(l[1][0].dt, start.add(days=1))
        for i in range(144):
            self.assertEqual(l[1][i].value, 2.0)

        l = list(ts1.aggregation("daily", "mean", raw=False, tz_mode="local"))
        self.assertEqual(len(l), 10)
        self.assertEqual(l[0].dt, start)
        self.assertEqual(l[-1].dt, start.add(days=9))

        l = list(ts1.aggregation("daily", "mean", raw=True, tz_mode="local"))
        self.assertEqual(len(l), 10)
        self.assertEqual(l[0].ts, start.int_timestamp)
        self.assertEqual(l[0].ts_offset, 7200)
        self.assertEqual(l[0].value, 1.0)
        self.assertEqual(l[-1].ts, end.start_of("day").int_timestamp)
        self.assertEqual(l[-1].ts_offset, 3600)
        self.assertEqual(l[-1].value, 10.0)

        # daily, timeshift to summer
        start = pendulum.datetime(2018, 3, 20, 0, 0, tz='Europe/Vienna')
        cur = start
        for i in range(10):
            for j in range(24 * 6):
                ts2.insert_point(cur, float(i+1))
                cur = cur.add(minutes=10)
        end = cur
        self.assertEqual(start.add(days=10).add(hours=1), end)

        l = list(ts2.aggregation("daily", "all", raw=False, tz_mode="local"))
        print(l)
        self.assertEqual(len(l), 11)
        self.assertEqual(l[0].dt, start)
        self.assertEqual(l[-1].dt, start.add(days=10))

        l = list(ts2.aggregation("daily", "all", raw=True, tz_mode="local"))
        self.assertEqual(len(l), 11)
        self.assertEqual(l[0].ts, start.int_timestamp)
        self.assertEqual(l[0].ts_offset, 3600)
        self.assertEqual(l[0].value.count, 144)
        self.assertEqual(l[0].value.mean, 1.0)
        self.assertEqual(l[0].value.stdev, 0.0)
        self.assertEqual(l[5].value.count, 144 - 6)
        self.assertEqual(l[-1].ts, end.start_of("day").int_timestamp)
        self.assertEqual(l[-1].ts_offset, 7200)
        self.assertEqual(l[-1].value.count, 6)
        self.assertEqual(l[-1].value.mean, 10.0)
        self.assertEqual(l[-1].value.stdev, 0.0)