Exemple #1
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.
        """

        policy.authorize(pecan.request.context, 'storage:list_data_frames', {})

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

        if begin:
            begin = ck_utils.dt2ts(begin)
        if end:
            end = ck_utils.dt2ts(end)
        try:
            resp = backend.retrieve(begin,
                                    end,
                                    group_filters=group_filters,
                                    metric_types=resource_type,
                                    paginate=False)
        except storage.NoTimeFrame:
            return storage_models.DataFrameCollection(dataframes=[])
        for frame in resp['dataframes']:
            for service, data_list in frame['usage'].items():
                frame_tenant = None
                resources = []
                for data in data_list:
                    # This means we use a v1 storage backend
                    if 'desc' in data.keys():
                        desc = data['desc']
                    else:
                        desc = data['metadata'].copy()
                        desc.update(data.get('groupby', {}))
                    price = decimal.Decimal(str(data['rating']['price']))
                    resource = storage_models.RatedResource(
                        service=service,
                        desc=desc,
                        volume=data['vol']['qty'],
                        rating=price)
                    if frame_tenant is None:
                        frame_tenant = desc[scope_key]
                    resources.append(resource)
                dataframe = storage_models.DataFrame(
                    begin=ck_utils.iso2dt(frame['period']['begin']),
                    end=ck_utils.iso2dt(frame['period']['end']),
                    tenant_id=frame_tenant,
                    resources=resources)
                dataframes.append(dataframe)
        return storage_models.DataFrameCollection(dataframes=dataframes)
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 post(self, begin, end, unit, qty, rate, desc, tenant_id, res_type):
        """Adding rated data frames.

        :param begin: Start of the period
        :param end: End of the period
        :param unit: unit
        :param qty: quantity
        :param rate: rate
        :param desc: description
        :param tenant_id: UUID of the tenant
        :param res_type: Type of the resource
        :return: Collection of DataFrame objects.
        """

        kwargs = {
            "begin": begin,
            "end": end,
            "tenant_id": tenant_id,
            "unit": unit,
            "qty": qty,
            "res_type": res_type,
            "rate": rate,
            "desc": desc,
        }
        backend = pecan.request.storage_backend
        frames = backend.add_time_frame_custom(**kwargs)
        return storage_models.DataFrameCollection()
Exemple #4
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.
        """

        policy.authorize(pecan.request.context, 'storage:list_data_frames', {})

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

        begin_ts = ck_utils.dt2ts(begin)
        end_ts = ck_utils.dt2ts(end)
        backend = pecan.request.storage_backend
        dataframes = []
        try:
            frames = backend.get_time_frame(begin_ts,
                                            end_ts,
                                            tenant_id=tenant_id,
                                            res_type=resource_type)
            for frame in frames:
                for service, data_list in frame['usage'].items():
                    frame_tenant = None
                    resources = []
                    for data in data_list:
                        desc = data['desc'] if data['desc'] else {}
                        price = decimal.Decimal(str(data['rating']['price']))
                        resource = storage_models.RatedResource(
                            service=service,
                            desc=desc,
                            volume=data['vol']['qty'],
                            rating=price)
                        frame_tenant = data['tenant_id']
                        resources.append(resource)
                    dataframe = storage_models.DataFrame(
                        begin=ck_utils.iso2dt(frame['period']['begin']),
                        end=ck_utils.iso2dt(frame['period']['end']),
                        tenant_id=frame_tenant,
                        resources=resources)
                    dataframes.append(dataframe)
        except ck_storage.NoTimeFrame:
            pass
        return storage_models.DataFrameCollection(dataframes=dataframes)