Example #1
0
class PerfDataTest(TestCase):

    def setUp(self):
        self.perfdata = PerfData(data_scope='test')

    def test_put_get_data(self):

        timewindow = TimeWindow()

        metric_id = 'test_manager'

        self.perfdata.remove(metric_id=metric_id, with_meta=True)

        count = self.perfdata.count(metric_id=metric_id)
        self.assertEqual(count, 0)

        tv0 = (int(timewindow.start()), None)
        tv1 = (int(timewindow.start() + 1), 0)
        tv2 = (int(timewindow.stop()), 2)
        tv3 = (int(timewindow.stop() + 1000000), 3)

        # set values with timestamp without order
        points = [tv0, tv2, tv1, tv3]

        meta = {'plop': None}

        self.perfdata.put(
            metric_id=metric_id,
            points=points,
            meta=meta
        )

        data, _meta = self.perfdata.get(
            metric_id=metric_id,
            timewindow=timewindow,
            with_meta=True
        )

        self.assertEqual(meta, _meta[0][PerfData.META_VALUE])

        self.assertEqual([tv0, tv1, tv2], data)

        # remove 1 data at stop point
        _timewindow = get_offset_timewindow(timewindow.stop())

        self.perfdata.remove(
            metric_id=metric_id,
            timewindow=_timewindow
        )

        data, _meta = self.perfdata.get(
            metric_id=metric_id,
            timewindow=timewindow,
            with_meta=True
        )

        self.assertEqual(meta, _meta[0][PerfData.META_VALUE])

        self.assertEqual(data, [tv0, tv1])

        # get data on timewindow
        data, _meta = self.perfdata.get(
            metric_id=metric_id,
            timewindow=timewindow,
            with_meta=True
        )

        self.assertEqual(meta, _meta[0][PerfData.META_VALUE])

        # get all data
        data, _meta = self.perfdata.get(
            metric_id=metric_id,
            with_meta=True
        )

        self.assertEqual(meta, _meta[0][PerfData.META_VALUE])

        self.assertEqual(len(data), 3)

        # remove all data
        self.perfdata.remove(
            metric_id=metric_id,
            with_meta=True
        )

        data, _meta = self.perfdata.get(
            metric_id=metric_id,
            with_meta=True
        )

        self.assertIsNone(_meta)

        self.assertEqual(len(data), 0)
Example #2
0
class PerfDataUtils(object):
    """docstring for PerfDataUtils"""

    def __init__(self):
        self.manager = PerfData()

    def perfdata_count(self, metric_id, timewindow=None):
        if timewindow is not None:
            timewindow = TimeWindow(**timewindow)

        result = self.manager.count(
            metric_id=metric_id, timewindow=timewindow
        )

        return result

    def perfdata(
        self, metric_id, timewindow=None, period=None, with_meta=True,
        limit=0, skip=0, timeserie=None
    ):
        if timewindow is not None:
            timewindow = TimeWindow(**timewindow)

        if timeserie is not None:
            if period is None:
                period = timeserie.pop('period', None)

            timeserie = TimeSerie(**timeserie)

            if period is not None:
                timeserie.period = Period(**period)

        if not isinstance(metric_id, list):
            metrics = [metric_id]

        else:
            metrics = metric_id

        result = []

        for metric_id in metrics:
            pts, meta = self.manager.get(
                metric_id=metric_id, with_meta=True,
                timewindow=timewindow, limit=limit, skip=skip
            )

            meta = meta[0]

            if timeserie is not None:
                pts = timeserie.calculate(pts, timewindow, meta=meta)

            if with_meta:
                result.append({
                    "points": pts,
                    "meta": meta
                })

            else:
                result.append({
                    "points": pts
                })

        return (result, len(result))

    def perfdata_meta(self, metric_id, timewindow=None, limit=0, sort=None):
        if timewindow is not None:
            timewindow = TimeWindow(**timewindow)

        result = self.manager.get_meta(
            metric_id=metric_id, timewindow=timewindow, limit=limit, sort=sort
        )

        return result