Пример #1
0
    def test_stats(self):
        outdated = Stats.insert((id, point.dt, point.sum) for point in points)
        self.assertEqual(outdated, [])
        with assertQueries():
            point = Stats.latest(id)
        sample = id, point.dt, point.sum
        self.assertEqual(Stats.insert([sample]), [sample])
        self.assertEqual(point.timestamp % 10, 0)
        self.assertGreater(point, points[-2])
        self.assertLessEqual(point, points[-1])
        count = len(points) + 1
        for stat in Stats[:-1]:
            timestamps = map(
                operator.attrgetter('timestamp'),
                Stats.select(
                    id, point.dt -
                    (stat.expiration_time - timedelta(seconds=stat.step)),
                    point.dt))
            self.assertLess(len(timestamps), count)
            count = len(timestamps)
            steps = set(y - x for x, y in zip(timestamps, timestamps[1:]))
            self.assertLessEqual(len(steps), 1)
        timestamps = map(
            operator.attrgetter('timestamp'),
            Stats.select(id,
                         point.dt - timedelta(hours=1),
                         point.dt,
                         maxlen=100))
        self.assertLessEqual(len(timestamps), 100)
        self.assertFalse(any(timestamp % 60 for timestamp in timestamps))
        self.assertTrue(any(timestamp % 300 for timestamp in timestamps))
        for point in Stats.select(id,
                                  point.dt - timedelta(hours=1),
                                  point.dt,
                                  rate=True):
            self.assertEqual(point[1:], (1.0, 10))
        selection = list(
            Stats.select(id,
                         now - timedelta(seconds=30),
                         now + timedelta(seconds=30),
                         fixed=3))
        self.assertEqual(len(selection), 3)

        # This result of this can vary depending on how settings is configured, if we have only 1 day of 10 seconds then we get a different
        # answer to if we have more than a day. So cope with both configurations. If Stats[0] is now then it using the 60 second roll up and
        # so len = 6 (6 times 10) otherwise we get 3 (the 3 after 'now' because all dates are in the future)
        if Stats[0].start(id) < now:
            self.assertEqual(sum(point.len for point in selection), 3)
        else:
            # See HYD-3960 - This value does not always come out as 6 and so this code will fail, because there are 100
            # points this case is never tested (see comments above - look for HYD-3660) but when it is run the value ends up as 4, 5 or 6
            # I (Chris) don't know why it varies and when I've looked for patterns and not found any.
            self.assertEqual(sum(point.len for point in selection), 6)

        self.assertEqual(selection[0].len, 0)
        point, = Stats.select(id, now, now + timedelta(seconds=5), fixed=1)
        with assertQueries(*['DELETE'] * 5):
            Stats.delete(id)
        for model in Stats:
            self.assertListEqual(list(model.select(id)), [])
 def fetch_last(self, fetch_metrics):
     "Return latest datetime and dict of field names and values."
     latest, data = datetime.fromtimestamp(0, utc), {}
     for series in Series.filter(self.measured_object, name__in=fetch_metrics):
         point = Stats.latest(series.id)
         data[series.name] = point.mean
         latest = max(latest, point.dt)
     return latest, data