Exemplo n.º 1
0
 def test_iter_period_bis(self):
     times = list(base.iter_period(
         datetime.datetime(2013, 01, 02, 13, 00),
         datetime.datetime(2013, 01, 02, 14, 00),
         55))
     self.assertEqual(len(times), math.ceil(3600 / 55.0))
     self.assertEqual(times[10],
                      (datetime.datetime(2013, 01, 02, 13, 9, 10),
                       datetime.datetime(2013, 01, 02, 13, 10, 5)))
     self.assertEqual(times[21],
                      (datetime.datetime(2013, 01, 02, 13, 19, 15),
                       datetime.datetime(2013, 01, 02, 13, 20, 10)))
Exemplo n.º 2
0
 def test_iter_period(self):
     times = list(base.iter_period(
         datetime.datetime(2013, 01, 01, 12, 00),
         datetime.datetime(2013, 01, 01, 13, 00),
         60))
     self.assertEqual(len(times), 60)
     self.assertEqual(times[10],
                      (datetime.datetime(2013, 01, 01, 12, 10),
                       datetime.datetime(2013, 01, 01, 12, 11)))
     self.assertEqual(times[21],
                      (datetime.datetime(2013, 01, 01, 12, 21),
                       datetime.datetime(2013, 01, 01, 12, 22)))
Exemplo n.º 3
0
    def get_meter_statistics(self, event_filter, period=None):
        """Return an iterable of api_models.Statistics instances containing
        meter statistics described by the query parameters.

        The filter must have a meter value set.

        """
        if not period or not event_filter.start or not event_filter.end:
            res = self._make_stats_query(event_filter).all()[0]

        if not period:
            yield self._stats_result_to_model(res, 0, res.tsmin, res.tsmax)
            return

        query = self._make_stats_query(event_filter)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
                event_filter.start or res.tsmin, event_filter.end or res.tsmax,
                period):
            q = query.filter(Meter.timestamp >= period_start)
            q = q.filter(Meter.timestamp < period_end)
            r = q.all()[0]
            # Don't return results that didn't have any event
            if r.count:
                yield self._stats_result_to_model(
                    result=r,
                    period=int(
                        timeutils.delta_seconds(period_start, period_end)),
                    period_start=period_start,
                    period_end=period_end,
                )
Exemplo n.º 4
0
    def get_meter_statistics(self, sample_filter, period=None):
        """Return an iterable of api_models.Statistics instances containing
        meter statistics described by the query parameters.

        The filter must have a meter value set.

        """
        if not period or not sample_filter.start or not sample_filter.end:
            res = self._make_stats_query(sample_filter).all()[0]

        if not period:
            yield self._stats_result_to_model(res, 0, res.tsmin, res.tsmax)
            return

        query = self._make_stats_query(sample_filter)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
            sample_filter.start or res.tsmin, sample_filter.end or res.tsmax, period
        ):
            q = query.filter(Meter.timestamp >= period_start)
            q = q.filter(Meter.timestamp < period_end)
            r = q.all()[0]
            # Don't return results that didn't have any data.
            if r.count:
                yield self._stats_result_to_model(
                    result=r,
                    period=int(timeutils.delta_seconds(period_start, period_end)),
                    period_start=period_start,
                    period_end=period_end,
                )
Exemplo n.º 5
0
    def get_meter_statistics(self,
                             sample_filter,
                             period=None,
                             groupby=None,
                             aggregate=None):
        """Return an iterable of api_models.Statistics instances.

        Items are containing meter statistics described by the query
        parameters. The filter must have a meter value set.
        """
        if groupby:
            for group in groupby:
                if group not in [
                        'user_id', 'project_id', 'resource_id',
                        'resource_metadata.instance_type'
                ]:
                    raise ceilometer.NotImplementedError('Unable to group by '
                                                         'these fields')

        if not period:
            for res in self._make_stats_query(sample_filter, groupby,
                                              aggregate):
                if res.count:
                    yield self._stats_result_to_model(res, 0, res.tsmin,
                                                      res.tsmax, groupby,
                                                      aggregate)
            return

        if not (sample_filter.start_timestamp and sample_filter.end_timestamp):
            res = self._make_stats_query(sample_filter, None,
                                         aggregate).first()
            if not res:
                # NOTE(liusheng):The 'res' may be NoneType, because no
                # sample has found with sample filter(s).
                return

        query = self._make_stats_query(sample_filter, groupby, aggregate)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
                sample_filter.start_timestamp or res.tsmin,
                sample_filter.end_timestamp or res.tsmax, period):
            q = query.filter(models.Sample.timestamp >= period_start)
            q = q.filter(models.Sample.timestamp < period_end)
            for r in q.all():
                if r.count:
                    yield self._stats_result_to_model(
                        result=r,
                        period=int(
                            timeutils.delta_seconds(period_start, period_end)),
                        period_start=period_start,
                        period_end=period_end,
                        groupby=groupby,
                        aggregate=aggregate)
Exemplo n.º 6
0
    def get_meter_statistics(self, sample_filter, period=None, groupby=None,
                             aggregate=None):
        """Return an iterable of api_models.Statistics instances.

        Items are containing meter statistics described by the query
        parameters. The filter must have a meter value set.
        """
        if groupby:
            for group in groupby:
                if group not in ['user_id', 'project_id', 'resource_id',
                                 'resource_metadata.instance_type']:
                    raise ceilometer.NotImplementedError('Unable to group by '
                                                         'these fields')

        if not period:
            for res in self._make_stats_query(sample_filter,
                                              groupby,
                                              aggregate):
                if res.count:
                    yield self._stats_result_to_model(res, 0,
                                                      res.tsmin, res.tsmax,
                                                      groupby,
                                                      aggregate)
            return

        if not (sample_filter.start_timestamp and sample_filter.end_timestamp):
            res = self._make_stats_query(sample_filter,
                                         None,
                                         aggregate).first()
            if not res:
                # NOTE(liusheng):The 'res' may be NoneType, because no
                # sample has found with sample filter(s).
                return

        query = self._make_stats_query(sample_filter, groupby, aggregate)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
                sample_filter.start_timestamp or res.tsmin,
                sample_filter.end_timestamp or res.tsmax,
                period):
            q = query.filter(models.Sample.timestamp >= period_start)
            q = q.filter(models.Sample.timestamp < period_end)
            for r in q.all():
                if r.count:
                    yield self._stats_result_to_model(
                        result=r,
                        period=int(timeutils.delta_seconds(period_start,
                                                           period_end)),
                        period_start=period_start,
                        period_end=period_end,
                        groupby=groupby,
                        aggregate=aggregate
                    )
Exemplo n.º 7
0
 def test_iter_period(self):
     times = list(
         base.iter_period(datetime.datetime(2013, 1, 1, 12, 0),
                          datetime.datetime(2013, 1, 1, 13, 0), 60))
     self.assertEqual(len(times), 60)
     self.assertEqual(times[10], (datetime.datetime(
         2013, 1, 1, 12, 10), datetime.datetime(2013, 1, 1, 12, 11)))
     self.assertEqual(times[21], (datetime.datetime(
         2013, 1, 1, 12, 21), datetime.datetime(2013, 1, 1, 12, 22)))
Exemplo n.º 8
0
 def test_iter_period_bis(self):
     times = list(
         base.iter_period(datetime.datetime(2013, 1, 2, 13, 0),
                          datetime.datetime(2013, 1, 2, 14, 0), 55))
     self.assertEqual(len(times), math.ceil(3600 / 55.0))
     self.assertEqual(times[10], (datetime.datetime(
         2013, 1, 2, 13, 9, 10), datetime.datetime(2013, 1, 2, 13, 10, 5)))
     self.assertEqual(times[21],
                      (datetime.datetime(2013, 1, 2, 13, 19, 15),
                       datetime.datetime(2013, 1, 2, 13, 20, 10)))
Exemplo n.º 9
0
 def test_iter_period(self):
     times = list(base.iter_period(
         datetime.datetime(2013, 1, 1, 12, 0),
         datetime.datetime(2013, 1, 1, 13, 0),
         60))
     self.assertEqual(60, len(times))
     self.assertEqual((datetime.datetime(2013, 1, 1, 12, 10),
                       datetime.datetime(2013, 1, 1, 12, 11)), times[10])
     self.assertEqual((datetime.datetime(2013, 1, 1, 12, 21),
                       datetime.datetime(2013, 1, 1, 12, 22)), times[21])
Exemplo n.º 10
0
 def test_iter_period_bis(self):
     times = list(base.iter_period(
         datetime.datetime(2013, 1, 2, 13, 0),
         datetime.datetime(2013, 1, 2, 14, 0),
         55))
     self.assertEqual(math.ceil(3600 / 55.0), len(times))
     self.assertEqual((datetime.datetime(2013, 1, 2, 13, 9, 10),
                       datetime.datetime(2013, 1, 2, 13, 10, 5)),
                      times[10])
     self.assertEqual((datetime.datetime(2013, 1, 2, 13, 19, 15),
                       datetime.datetime(2013, 1, 2, 13, 20, 10)),
                      times[21])
Exemplo n.º 11
0
    def get_meter_statistics(self, event_filter, period=None):
        """Return a dictionary containing meter statistics.
        described by the query parameters.

        The filter must have a meter value set.

        { 'min':
          'max':
          'avg':
          'sum':
          'count':
          'period':
          'period_start':
          'period_end':
          'duration':
          'duration_start':
          'duration_end':
          }
        """

        if not period or not event_filter.start or not event_filter.end:
            res = self._make_stats_query(event_filter).all()[0]

        if not period:
            return [self._stats_result_to_dict(res, 0, res.tsmin, res.tsmax)]

        query = self._make_stats_query(event_filter)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        results = []
        for period_start, period_end in base.iter_period(
                event_filter.start or res.tsmin,
                event_filter.end or res.tsmax,
                period):
            q = query.filter(Meter.timestamp >= period_start)
            q = q.filter(Meter.timestamp < period_end)
            r = q.all()[0]
            # Don't add results that didn't have any event
            if r.count:
                results.append(self._stats_result_to_dict(
                    result=r,
                    period=int(timeutils.delta_seconds(period_start,
                                                       period_end)),
                    period_start=period_start,
                    period_end=period_end,
                ))

        return results
Exemplo n.º 12
0
    def get_meter_statistics(self, sample_filter, period=None, groupby=None):
        """Return an iterable of api_models.Statistics instances containing
        meter statistics described by the query parameters.

        The filter must have a meter value set.

        """
        if groupby:
            for group in groupby:
                if group not in ['user_id', 'project_id', 'resource_id']:
                    raise NotImplementedError(
                        _("Unable to group by these fields"))

        if not period:
            for res in self._make_stats_query(sample_filter, groupby):
                if res.count:
                    yield self._stats_result_to_model(res, 0,
                                                      res.tsmin, res.tsmax,
                                                      groupby)
            return

        if not sample_filter.start or not sample_filter.end:
            res = self._make_stats_query(sample_filter, None).first()

        query = self._make_stats_query(sample_filter, groupby)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
                sample_filter.start or res.tsmin,
                sample_filter.end or res.tsmax,
                period):
            q = query.filter(Meter.timestamp >= period_start)
            q = q.filter(Meter.timestamp < period_end)
            for r in q.all():
                if r.count:
                    yield self._stats_result_to_model(
                        result=r,
                        period=int(timeutils.delta_seconds(period_start,
                                                           period_end)),
                        period_start=period_start,
                        period_end=period_end,
                        groupby=groupby
                    )
Exemplo n.º 13
0
    def get_meter_statistics(self, sample_filter, period=None, groupby=None):
        """Return an iterable of api_models.Statistics instances containing
        meter statistics described by the query parameters.

        The filter must have a meter value set.

        """
        if groupby:
            for group in groupby:
                if group not in ['user_id', 'project_id', 'resource_id']:
                    raise NotImplementedError(
                        "Unable to group by these fields")

        if not period:
            for res in self._make_stats_query(sample_filter, groupby):
                if res.count:
                    yield self._stats_result_to_model(res, 0,
                                                      res.tsmin, res.tsmax,
                                                      groupby)
            return

        if not sample_filter.start or not sample_filter.end:
            res = self._make_stats_query(sample_filter, None).first()

        query = self._make_stats_query(sample_filter, groupby)
        # HACK(jd) This is an awful method to compute stats by period, but
        # since we're trying to be SQL agnostic we have to write portable
        # code, so here it is, admire! We're going to do one request to get
        # stats by period. We would like to use GROUP BY, but there's no
        # portable way to manipulate timestamp in SQL, so we can't.
        for period_start, period_end in base.iter_period(
                sample_filter.start or res.tsmin,
                sample_filter.end or res.tsmax,
                period):
            q = query.filter(Meter.timestamp >= period_start)
            q = q.filter(Meter.timestamp < period_end)
            for r in q.all():
                if r.count:
                    yield self._stats_result_to_model(
                        result=r,
                        period=int(timeutils.delta_seconds(period_start,
                                                           period_end)),
                        period_start=period_start,
                        period_end=period_end,
                        groupby=groupby
                    )