Exemple #1
0
 def setUp(self):
     self.period_begin = tzutils.local_to_utc(
         tzutils.get_month_start()).isoformat()
     self.period_end = tzutils.local_to_utc(
         tzutils.get_next_month()).isoformat()
     self.client = influx.InfluxClient()
     self._storage = influx.InfluxStorage()
Exemple #2
0
    def get_all(self, begin=None, end=None, tenant_id=None,
                resource_type=None):
        """Return a list of rated resources for a time period and a tenant.

        :param begin: Start of the period
        :param end: End of the period
        :param tenant_id: UUID of the tenant to filter on.
        :param resource_type: Type of the resource to filter on.
        :return: Collection of DataFrame objects.
        """

        project_id = tenant_id or pecan.request.context.project_id
        policy.authorize(pecan.request.context, 'storage:list_data_frames', {
            'tenant_id': project_id,
        })

        scope_key = CONF.collect.scope_key
        backend = pecan.request.storage_backend
        dataframes = []
        filters = {scope_key: tenant_id} if tenant_id else None

        try:
            resp = backend.retrieve(
                begin, end,
                filters=filters,
                metric_types=resource_type,
                paginate=False)
        except storage.NoTimeFrame:
            return storage_models.DataFrameCollection(dataframes=[])
        for frame in resp['dataframes']:
            frame_tenant = None
            for type_, points in frame.itertypes():
                resources = []
                for point in points:
                    resource = storage_models.RatedResource(
                        service=type_,
                        desc=point.desc,
                        volume=point.qty,
                        rating=point.price)
                    if frame_tenant is None:
                        # NOTE(jferrieu): Since DataFrame/DataPoint
                        # implementation patch we cannot guarantee
                        # anymore that a DataFrame does contain a scope_id
                        # therefore the __UNDEF__ default value has been
                        # retained to maintain backward compatibility
                        # if it would occur being absent
                        frame_tenant = point.desc.get(scope_key, '__UNDEF__')
                    resources.append(resource)
                dataframe = storage_models.DataFrame(
                    begin=tzutils.local_to_utc(frame.start, naive=True),
                    end=tzutils.local_to_utc(frame.end, naive=True),
                    tenant_id=frame_tenant,
                    resources=resources)
                dataframes.append(dataframe)
        return storage_models.DataFrameCollection(dataframes=dataframes)
Exemple #3
0
    def summary(self, begin=None, end=None, tenant_id=None,
                service=None, groupby=None, all_tenants=False):
        """Return the summary to pay for a given period.

        """
        if not begin:
            begin = ck_utils.get_month_start()
        if not end:
            end = ck_utils.get_next_month()

        if all_tenants:
            tenant_id = None
        else:
            tenant_context = pecan.request.context.project_id
            tenant_id = tenant_context if not tenant_id else tenant_id
        policy.authorize(pecan.request.context, 'report:get_summary',
                         {"tenant_id": tenant_id})
        storage = pecan.request.storage_backend

        scope_key = CONF.collect.scope_key
        storage_groupby = []
        if groupby is not None and 'tenant_id' in groupby:
            storage_groupby.append(scope_key)
        if groupby is not None and 'res_type' in groupby:
            storage_groupby.append('type')
        filters = {scope_key: tenant_id} if tenant_id else None
        result = storage.total(
            groupby=storage_groupby,
            begin=begin, end=end,
            metric_types=service,
            filters=filters)

        summarymodels = []
        for res in result['results']:
            kwargs = {
                'res_type': res.get('type') or res.get('res_type'),
                'tenant_id': res.get(scope_key) or res.get('tenant_id'),
                'begin': tzutils.local_to_utc(res['begin'], naive=True),
                'end': tzutils.local_to_utc(res['end'], naive=True),
                'rate': res['rate'],
            }
            summarymodel = report_models.SummaryModel(**kwargs)
            summarymodels.append(summarymodel)

        return report_models.SummaryCollectionModel(summary=summarymodels)
Exemple #4
0
    def total(self,
              groupby=None,
              begin=None,
              end=None,
              metric_types=None,
              filters=None,
              offset=0,
              limit=100,
              paginate=True):
        tenant_id = filters.get('project_id') if filters else None

        storage_gby = []
        if groupby:
            for elem in set(groupby):
                if elem == 'type':
                    storage_gby.append('res_type')
                elif elem == 'project_id':
                    storage_gby.append('tenant_id')
        storage_gby = ','.join(storage_gby) if storage_gby else None
        metric_types = self._check_metric_types(metric_types)
        total = self.storage.get_total(tzutils.local_to_utc(begin, naive=True),
                                       tzutils.local_to_utc(end, naive=True),
                                       tenant_id=tenant_id,
                                       service=metric_types,
                                       groupby=storage_gby)

        for t in total:
            if t.get('tenant_id') is None:
                t['tenant_id'] = tenant_id
            if t.get('rate') is None:
                t['rate'] = float(0)
            if groupby and 'type' in groupby:
                t['type'] = t.get('res_type')
            else:
                t['type'] = None
        self._localize_total(total)
        return {
            'total': len(total),
            'results': total,
        }
Exemple #5
0
 def retrieve(self,
              begin=None,
              end=None,
              filters=None,
              metric_types=None,
              offset=0,
              limit=100,
              paginate=True):
     tenant_id = filters.get('project_id') if filters else None
     metric_types = self._check_metric_types(metric_types)
     frames = self.storage.get_time_frame(
         tzutils.local_to_utc(begin, naive=True) if begin else None,
         tzutils.local_to_utc(end, naive=True) if end else None,
         res_type=metric_types,
         tenant_id=tenant_id)
     frames = [
         dataframe.DataFrame.from_dict(frame, legacy=True)
         for frame in frames
     ]
     self._localize_dataframes(frames)
     return {
         'total': len(frames),
         'dataframes': frames,
     }
Exemple #6
0
    def set_state(self,
                  identifier,
                  state,
                  fetcher=None,
                  collector=None,
                  scope_key=None):
        """Set the state of a scope.

        :param identifier: Identifier of the scope
        :type identifier: str
        :param state: state of the scope
        :type state: datetime.datetime
        :param fetcher: Fetcher associated to the scope
        :type fetcher: str
        :param collector: Collector associated to the scope
        :type collector: str
        :param scope_key: scope_key associated to the scope
        :type scope_key: str
        """
        state = tzutils.local_to_utc(state, naive=True)
        session = db.get_session()
        session.begin()
        r = self._get_db_item(session, identifier, fetcher, collector,
                              scope_key)

        if r:
            if r.state != state:
                r.state = state
                session.commit()
        else:
            state_object = self.model(
                identifier=identifier,
                state=state,
                fetcher=fetcher,
                collector=collector,
                scope_key=scope_key,
            )
            session.add(state_object)
            session.commit()

        session.close()
Exemple #7
0
 def test_local_to_utc_not_naive(self):
     local = tzutils.local_to_utc(self.local_now)
     naive = tzutils.local_to_utc(self.naive_now)
     self.assertIsNotNone(local.tzinfo)
     self.assertIsNotNone(naive.tzinfo)
     self.assertEqual(local, naive)
Exemple #8
0
 def test_local_to_utc_naive(self):
     naive_local = tzutils.local_to_utc(self.local_now, naive=True)
     naive_naive = tzutils.local_to_utc(self.naive_now, naive=True)
     self.assertIsNone(naive_local.tzinfo)
     self.assertIsNone(naive_naive.tzinfo)
     self.assertEqual(naive_local, naive_naive)
Exemple #9
0
 def _check_begin_end(begin, end):
     if not begin:
         begin = tzutils.get_month_start()
     if not end:
         end = tzutils.get_next_month()
     return tzutils.local_to_utc(begin), tzutils.local_to_utc(end)