コード例 #1
0
ファイル: test_core.py プロジェクト: merouaneagar/canopsis
    def test_intervals(self):
        """Test calculate on different intervals."""

        now = time()

        # let a period of 1 day
        period = Period(day=1)
        oneday = period.total_seconds()

        rnow = period.round_timestamp(now)

        # let a timewindow of 10+1/4 days
        timewindow = TimeWindow(start=now - oneday, stop=now + 45/4 * oneday)

        nan = float('nan')

        points = [
            # the first interval is empty
            (rnow, nan),  # the second interval contains nan at start
            (rnow + oneday + 1, nan),  # the third interval contains nan at start + 1
            (rnow + 2 * oneday, 1),  # the fourth interval contains 1 at start
            (rnow + 3 * oneday + 1, 1),  # the fourth interval contains 1 at start + 1
            (rnow + 4 * oneday, nan), (rnow + 4 * oneday + 1, 1),  # the fith interval contains 1 and nan
            (rnow + 5 * oneday, 1), (rnow + 5 * oneday + 1, 1),  # the sixth interval contains 1 and 1
            (rnow + 6 * oneday, 1), (rnow + 6 * oneday, 1),  # the sixth interval contains 1 and 1 at the same time
            (rnow + 7 * oneday, nan), (rnow + 7 * oneday, nan),  # the sixth interval contains nan and nan at the same time
        ]

        timeserie = TimeSerie(
            config=self.conf,
            aggregation='sum',
            period=period,
            round_time=True
        )

        _points = timeserie.calculate(points, timewindow)

        for i in [0, 1, 2, 5, 8, 9, 10, 11, 12]:
            self.assertEqual(_points[i][0], rnow + (i - 1) * oneday)
            self.assertTrue(isnan(_points[i][1]))

        for i in [3, 4]:
            self.assertEqual(_points[i][0], rnow + (i - 1) * oneday)
            self.assertEqual(_points[i][1], 1)

        for i in [6, 7]:
            self.assertEqual(_points[i][0], rnow + (i - 1) * oneday)
            self.assertEqual(_points[i][1], 2)

        self.assertEqual(len(_points), len(points) + 1)
コード例 #2
0
    def test_total_seconds_mix(self):
        """
        Test total seconds with all units
        """

        kwargs = {
            Period.MICROSECOND: 1,
            Period.SECOND: 1,
            Period.MINUTE: 1,
            Period.HOUR: 1,
            Period.DAY: 1,
            Period.WEEK: 1,
            Period.MONTH: 1,
            Period.YEAR: 1
        }

        period = Period(**kwargs)

        self.assertEqual(
            period.total_seconds(), 10**-9 + 1 + 60 + 3600 + 86400 +
            86400 * 7 + 86400 * 7 * 4 + 86400 * 7 * 4 * 12)
コード例 #3
0
ファイル: test_timewindow.py プロジェクト: capensis/canopsis
    def test_total_seconds_mix(self):
        """
        Test total seconds with all units
        """

        kwargs = {
            Period.MICROSECOND: 1,
            Period.SECOND: 1,
            Period.MINUTE: 1,
            Period.HOUR: 1,
            Period.DAY: 1,
            Period.WEEK: 1,
            Period.MONTH: 1,
            Period.YEAR: 1
        }

        period = Period(**kwargs)

        self.assertEqual(
            period.total_seconds(),
            10**-9 + 1 + 60 + 3600 + 86400 + 86400*7 + 86400*7*4 + 86400*7*4*12
        )