Beispiel #1
0
    def test_empty(self):
        """
        Test to reduce empty interval.
        """

        # start with empty intervals
        interval = Interval()
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), 0)
    def test_empty(self):
        """
        Test to reduce empty interval.
        """

        # start with empty intervals
        interval = Interval()
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), 0)
    def test_copy(self):

        sub_intervals = list()
        for i in range(randint(1, 99)):
            sub_intervals += (i - random(), i + random())

        interval = Interval(*sub_intervals)

        copy = Interval(interval)

        self.assertEqual(copy, interval)
    def test_intervals(self):
        """
        Test intervals.
        """

        # use five intervals where union & reduced interval = reduced interval
        interval = Interval(
            (self.lower - 5, self.lower - 2), (self.lower - 1, self.lower + 1),
            (self.lower + 2, self.upper - 2), (self.upper - 1, self.upper + 1),
            (self.upper + 2, self.upper + 5))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length - 2)
Beispiel #5
0
    def test_intervals(self):
        """
        Test intervals.
        """

        # use five intervals where union & reduced interval = reduced interval
        interval = Interval(
            (self.lower - 5, self.lower - 2),
            (self.lower - 1, self.lower + 1),
            (self.lower + 2, self.upper - 2),
            (self.upper - 1, self.upper + 1),
            (self.upper + 2, self.upper + 5)
        )
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length - 2)
    def test_is_empty(self):
        interval = Interval()

        self.assertTrue(interval.is_empty())

        interval = Interval(10**-99)

        self.assertFalse(interval.is_empty())

        interval = Interval(0)

        self.assertFalse(interval.is_empty())
    def test_contains_empty(self):

        interval = Interval()

        self.assertFalse(2 in interval)

        self.assertFalse((0, 2) in interval)
    def test_no_startstop(self):

        start, stop = 5, 10
        intervals = Interval((start, stop))
        timewindow = TimeWindow(intervals=intervals)
        self.assertEqual(timewindow.start(), start)
        self.assertEqual(timewindow.stop(), stop)
Beispiel #9
0
    def count_alarms_by_period(
            self,
            start,
            stop,
            subperiod=None,
            limit=100,
            query=None,
    ):
        """
        Count alarms that have been opened during (stop - start) period.

        :param start: Beginning timestamp of period
        :type start: int

        :param stop: End timestamp of period
        :type stop: int

        :param subperiod: Cut (stop - start) in ``subperiod`` subperiods.
        :type subperiod: dict

        :param limit: Counts cannot exceed this value
        :type limit: int

        :param query: Custom mongodb filter for alarms
        :type query: dict

        :return: List in which each item contains an interval and the
                 related count
        :rtype: list
        """

        if subperiod is None:
            subperiod = {'day': 1}

        if query is None:
            query = {}

        intervals = Interval.get_intervals_by_period(start, stop, subperiod)

        results = []
        for date in intervals:
            count = self.alarm_storage.count(
                data_ids=None,
                timewindow=TimeWindow(start=date['begin'], stop=date['end']),
                window_start_bind=True,
                _filter=query,
            )

            results.append(
                {
                    'date': date,
                    'count': limit if count > limit else count,
                }
            )

        return results
Beispiel #10
0
    def count_alarms_by_period(
            self,
            start,
            stop,
            subperiod=None,
            limit=100,
            query=None,
    ):
        """
        Count alarms that have been opened during (stop - start) period.

        :param start: Beginning timestamp of period
        :type start: int

        :param stop: End timestamp of period
        :type stop: int

        :param subperiod: Cut (stop - start) in ``subperiod`` subperiods.
        :type subperiod: dict

        :param limit: Counts cannot exceed this value
        :type limit: int

        :param query: Custom mongodb filter for alarms
        :type query: dict

        :return: List in which each item contains an interval and the
                 related count
        :rtype: list
        """

        if subperiod is None:
            subperiod = {'day': 1}

        if query is None:
            query = {}

        intervals = Interval.get_intervals_by_period(start, stop, subperiod)

        results = []
        for date in intervals:
            count = self.alarm_storage.count(
                data_ids=None,
                timewindow=TimeWindow(start=date['begin'], stop=date['end']),
                window_start_bind=True,
                _filter=query,
            )

            results.append(
                {
                    'date': date,
                    'count': limit if count > limit else count,
                }
            )

        return results
    def test_contains_multi_interval(self):

        interval = Interval((1, 2), (6, 8))

        self.assertTrue(2 in interval)

        self.assertTrue(7 in interval)

        self.assertFalse(3 in interval)

        self.assertTrue((1, 2, 7) in interval)

        self.assertFalse((0, 2, 7) in interval)
    def test_contains_interval(self):

        interval = Interval((1, 2))

        self.assertTrue(2 in interval)

        self.assertTrue(1.5 in interval)

        self.assertFalse(-1 in interval)

        self.assertTrue((1, 2) in interval)

        self.assertFalse((0, 2) in interval)
    def test_contains_multi_simple_point(self):

        interval = Interval(1, 2)

        self.assertTrue(2 in interval)

        self.assertTrue(1.5 in interval)

        self.assertFalse(-1 in interval)

        self.assertTrue((1, 2) in interval)

        self.assertFalse((-1, 2) in interval)
    def test_contains_simple_negative_interval(self):

        interval = Interval(-2)

        self.assertTrue(-2 in interval)

        self.assertTrue(-1.5 in interval)

        self.assertFalse(1 in interval)

        self.assertTrue((0, -2) in interval)

        self.assertFalse((-1, 2) in interval)
    def test_one(self):
        """
        Test to reduce one interval.
        """

        # test same interval
        interval = Interval((self.lower, self.upper))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length)

        # remove 2 points
        interval = Interval((self.lower + 1, self.upper - 1))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length - 2)

        # add 2 points
        interval = Interval((self.lower - 1, self.upper + 1))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length)
Beispiel #16
0
    def test_is_empty(self):
        interval = Interval()

        self.assertTrue(interval.is_empty())

        interval = Interval(10 ** -99)

        self.assertFalse(interval.is_empty())

        interval = Interval(0)

        self.assertFalse(interval.is_empty())
Beispiel #17
0
    def get_eventlog_count_by_period(
        self, tstart, tstop, limit=100, query={}
    ):
        """Get an eventlog count for each interval found in the given period and
           with a given filter.
           This period is given by tstart and tstop.
           Counts can be limited thanks to the 'limit' parameter.

           :param start: begin interval timestamp
           :param stop: end interval timestamp
           :param limit: number that limits the max count returned
           :param query: filter for events_log collection
           :return: list in which each item contains an interval and the
           related count
           :rtype: list
        """
        period = {'day': 1}
        intervals = Interval.get_intervals_by_period(tstart, tstop, period)
        results = []

        for date in intervals:
            eventfilter = {
                '$and': [
                    query,
                    {
                        'timestamp': {
                            '$gte': date['begin'],
                            '$lte': date['end']
                        }
                    }
                ]
            }

            elements, count = self[EventsLog.EVENTSLOG_STORAGE].find_elements(
                query=eventfilter,
                limit=limit,
                with_count=True
            )

            results.append({
                'date': date,
                'count': count
            })

        return results
    def get_eventlog_count_by_period(
            self, tstart, tstop, limit=100, query={}.copy()):
        """Get an eventlog count for each interval found in the given period and
           with a given filter.
           This period is given by tstart and tstop.
           Counts can be limited thanks to the 'limit' parameter.

           :param start: begin interval timestamp
           :param stop: end interval timestamp
           :param limit: number that limits the max count returned
           :param query: filter for events_log collection
           :return: list in which each item contains an interval and the
           related count
           :rtype: list
        """
        period = {'day': 1}
        intervals = Interval.get_intervals_by_period(tstart, tstop, period)
        results = []

        for date in intervals:
            eventfilter = {
                '$and': [
                    query,
                    {
                        'timestamp': {
                            '$gte': date['begin'],
                            '$lte': date['end']
                        }
                    }
                ]
            }

            _, count = self.el_storage.find_elements(
                query=eventfilter,
                limit=limit,
                with_count=True
            )

            results.append({
                'date': date,
                'count': count
            })

        return results
Beispiel #19
0
    def test_one(self):
        """
        Test to reduce one interval.
        """

        # test same interval
        interval = Interval((self.lower, self.upper))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length)

        # remove 2 points
        interval = Interval((self.lower + 1, self.upper - 1))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length - 2)

        # add 2 points
        interval = Interval((self.lower - 1, self.upper + 1))
        reduced = interval.reduce(self.lower, self.upper)
        self.assertEqual(len(reduced), self.length)
Beispiel #20
0
    def test_min_max_points(self):

        interval = Interval(2, 3)

        self.assertEqual(0, interval.min())
        self.assertEqual(3, interval.max())
    def test_min_max_intervals(self):

        interval = Interval((2, 3), (4, 6))

        self.assertEqual(2, interval.min())
        self.assertEqual(6, interval.max())
Beispiel #22
0
    def test_min_max_empty(self):

        interval = Interval()

        self.assertEqual(None, interval.min())
        self.assertEqual(None, interval.max())
    def test_sub_interval_simple_point(self):

        interval = Interval(1)

        self.assertEqual(len(interval.sub_intervals), 1)
    def test_empty_sub_interval(self):

        interval = Interval()

        self.assertEqual(len(interval.sub_intervals), 0)
    def test_min_max_interval(self):

        interval = Interval((2, 3))

        self.assertEqual(2, interval.min())
        self.assertEqual(3, interval.max())
    def test_len_empty(self):

        interval = Interval()

        self.assertEqual(len(interval), 0)
    def test_sub_interval_interval(self):

        interval = Interval((2, 3))

        self.assertEqual(len(interval.sub_intervals), 1)
    def test_sub_interval_multi_points(self):

        interval = Interval(2, 3)

        self.assertEqual(len(interval.sub_intervals), 1)
    def test_sub_interval_multi_interval_with_intersection(self):

        interval = Interval((2, 5), (4, 6))

        self.assertEqual(len(interval.sub_intervals), 1)
    def test_sub_interval_multi_interval(self):

        interval = Interval((2, 3), (4, 5))

        self.assertEqual(len(interval.sub_intervals), 2)
    def test_multi_interval_with_intersection(self):

        interval = Interval((2, 5), (5, 6))

        self.assertEqual(len(interval), 4)
    def test_multi_interval(self):

        interval = Interval((2, 4), (5, 6))

        self.assertEqual(len(interval), 3)
    def test_negative_interval_len(self):

        interval = Interval((-2, 4))

        self.assertEqual(len(interval), 6)
Beispiel #34
0
    def test_min_max_interval(self):

        interval = Interval((2, 3))

        self.assertEqual(2, interval.min())
        self.assertEqual(3, interval.max())
    def test_min_max_points(self):

        interval = Interval(2, 3)

        self.assertEqual(0, interval.min())
        self.assertEqual(3, interval.max())
Beispiel #36
0
    def test_min_max_intervals(self):

        interval = Interval((2, 3), (4, 6))

        self.assertEqual(2, interval.min())
        self.assertEqual(6, interval.max())
    def test_multi_simple_len(self):

        interval = Interval(2, 4)

        self.assertEqual(len(interval), 4)
Beispiel #38
0
    def consolidation(
            self, serieconf, perfdatas, timewindow,
            period=None, usenan=True, fixed=True
    ):
        """
        Get consolidated point from serie.

        :param serieconf: Serie used for consolidation
        :type serieconf: dict

        :param perfdatas: Aggregated perfdatas from ``aggregation()`` method
        :type perfdatas: dict

        :param timewindow: Time window used for consolidation
        :type timewindow: canopsis.timeserie.timewindow.TimeWindow

        :returns: Consolidated points
        """

        # configure consolidation period (same as aggregation period)
        tw, period, usenan, fixed = self.get_timewindow_period_usenan_fixed(
            serieconf, timewindow, period, usenan, fixed
        )

        intervals = Interval.get_intervals_by_period(
            tw.start(),
            tw.stop(),
            period
        )

        points = []

        # generator consolidation operators
        operatorset = get_task('serie.operatorset')

        # generate one point per aggregation interval in timewindow
        for interval in intervals:
            tw = TimeWindow(
                start=interval['begin'],
                stop=interval['end'] - 1
            )

            # operators are acting on a specific timewindow
            operators = operatorset(self, period, perfdatas, tw, usenan)

            # execute formula in sand-boxed environment
            restricted_globals = {
                '__builtins__': safe_builtins,
            }

            restricted_globals.update(operators)

            formula = serieconf['formula']
            code = compile_restricted(formula, '<string>', 'eval')

            try:
                val = eval(code, restricted_globals)

            except Exception as ex:
                self.logger.warning(
                    'Wrong serie formula: {0}/{1} ({2})'.format(
                        serieconf['crecord_name'], formula, ex
                    )
                )
                val = float('nan')

            else:
                if isnan(val):
                    self.logger.warning(
                        'Formula result is nan: {0}/{1}.'.format(
                            serieconf['crecord_name'], formula
                        )
                    )

            # result contains consolidated value
            # point is computed at the start of interval
            points.append((interval['begin'], val))

        return points
    def test_multi_simple_negative_len(self):

        interval = Interval(-2, -4)

        self.assertEqual(len(interval), 4)
    def test_interval_len(self):

        interval = Interval((2, 4))

        self.assertEqual(len(interval), 2)
    def test_simple_negative_len(self):

        interval = Interval(-10)

        self.assertEqual(len(interval), 10)