Exemple #1
0
def query_data(request,
               date_from,
               date_to,
               group_by,
               meter,
               period=None,
               query=None,
               additional_query=None):

    if not period:
        period = metering.calc_period(date_from, date_to, 50)
    if additional_query is None:
        additional_query = []
    if date_from:
        additional_query += [{
            'field': 'timestamp',
            'op': 'ge',
            'value': date_from
        }]
    if date_to:
        additional_query += [{
            'field': 'timestamp',
            'op': 'le',
            'value': date_to
        }]

    # TODO(lsmola) replace this by logic implemented in I1 in bugs
    # 1226479 and 1226482, this is just a quick fix for RC1
    try:
        meter_list = [
            m for m in ceilometer.meter_list(request) if m.name == meter
        ]
        unit = meter_list[0].unit
    except Exception:
        unit = ""

    ceilometer_usage = ceilometer.CeilometerUsage(request)
    try:
        if group_by:
            resources = ceilometer_usage.resource_aggregates_with_statistics(
                query, [meter],
                period=period,
                stats_attr=None,
                additional_query=additional_query)
        else:
            resources = ceilometer_usage.resources_with_statistics(
                query, [meter],
                period=period,
                stats_attr=None,
                additional_query=additional_query)
    except Exception:
        resources = []
        exceptions.handle(request, _('Unable to retrieve statistics.'))
    return resources, unit
Exemple #2
0
def list_of_resource_aggregates(request, meters, stats_attr="avg"):
    queries, additional_query = make_tenant_queries(request)

    ceilometer_usage = ceilometer.CeilometerUsage(request)
    try:
        resource_aggregates = ceilometer_usage.\
            resource_aggregates_with_statistics(
                queries, meters, stats_attr="avg",
                additional_query=additional_query)
    except Exception:
        resource_aggregates = []
        exceptions.handle(request, _('Unable to retrieve statistics.'))

    return resource_aggregates
Exemple #3
0
    def get_global_object_store_usage_data(self):
        request = self.tab_group.request
        ceilometer_usage = ceilometer.CeilometerUsage(request)

        date_from = datetime.now() - timedelta(30)
        date_to = datetime.now()
        additional_query = [{
            'field': 'timestamp',
            'op': 'ge',
            'value': date_from
        }, {
            'field': 'timestamp',
            'op': 'le',
            'value': date_to
        }]
        try:
            result = ceilometer_usage.global_object_store_usage(
                with_statistics=True, additional_query=additional_query)
        except Exception:
            result = []
            exceptions.handle(request, _('Unable to retrieve statistics.'))
        return result
Exemple #4
0
def query_data(request,
               date_from,
               date_to,
               date_options,
               group_by,
               meter,
               period=None,
               additional_query=None):
    date_from, date_to = _calc_date_args(date_from,
                                         date_to,
                                         date_options)
    if not period:
        period = _calc_period(date_from, date_to)
    if additional_query is None:
        additional_query = []
    if date_from:
        additional_query += [{'field': 'timestamp',
                              'op': 'ge',
                              'value': date_from}]
    if date_to:
        additional_query += [{'field': 'timestamp',
                              'op': 'le',
                              'value': date_to}]

    # TODO(lsmola) replace this by logic implemented in I1 in bugs
    # 1226479 and 1226482, this is just a quick fix for RC1
    try:
        meter_list = [m for m in ceilometer.meter_list(request)
                      if m.name == meter]
        unit = meter_list[0].unit
    except Exception:
        unit = ""
    if group_by == "project":
        try:
            tenants, more = api.keystone.tenant_list(
                request,
                domain=None,
                paginate=False)
        except Exception:
            tenants = []
            exceptions.handle(request,
                              _('Unable to retrieve tenant list.'))
        queries = {}
        for tenant in tenants:
            tenant_query = [{
                            "field": "project_id",
                            "op": "eq",
                            "value": tenant.id}]

            queries[tenant.name] = tenant_query

        ceilometer_usage = ceilometer.CeilometerUsage(request)
        resources = ceilometer_usage.resource_aggregates_with_statistics(
            queries, [meter], period=period, stats_attr=None,
            additional_query=additional_query)

    else:
        query = []

        def filter_by_meter_name(resource):
            """Function for filtering of the list of resources.

            Will pick the right resources according to currently selected
            meter.
            """
            for link in resource.links:
                if link['rel'] == meter:
                    # If resource has the currently chosen meter.
                    return True
            return False

        ceilometer_usage = ceilometer.CeilometerUsage(request)
        try:
            resources = ceilometer_usage.resources_with_statistics(
                query, [meter], period=period, stats_attr=None,
                additional_query=additional_query,
                filter_func=filter_by_meter_name)
        except Exception:
            resources = []
            exceptions.handle(request,
                              _('Unable to retrieve statistics.'))
    return resources, unit
Exemple #5
0
    def get(self, request, *args, **kwargs):
        meter = request.GET.get('meter', None)
        meter_name = meter.replace(".", "_")
        date_options = request.GET.get('date_options', None)
        date_from = request.GET.get('date_from', None)
        date_to = request.GET.get('date_to', None)
        stats_attr = request.GET.get('stats_attr', 'avg')

        # TODO(lsmola) all timestamps should probably work with
        # current timezone. And also show the current timezone in chart.
        if (date_options == "other"):
            try:
                if date_from:
                    date_from = datetime.strptime(date_from,
                                                  "%Y-%m-%d")
                else:
                    # TODO(lsmola) there should be probably the date
                    # of the first sample as default, so it correctly
                    # counts the time window. Though I need ordering
                    # and limit of samples to obtain that.
                    pass
                if date_to:
                    date_to = datetime.strptime(date_to,
                                                "%Y-%m-%d")
                    # It return beginning of the day, I want the and of
                    # the day, so i will add one day without a second.
                    date_to = (date_to + timedelta(days=1) -
                               timedelta(seconds=1))
                else:
                    date_to = datetime.now()
            except Exception:
                raise ValueError("The dates haven't been "
                                 "recognized")
        else:
            try:
                date_from = datetime.now() - timedelta(days=int(date_options))
                date_to = datetime.now()
            except Exception:
                raise ValueError("The time delta must be an "
                                 "integer representing days.")

        if date_from and date_to:
            if date_to < date_from:
                # TODO(lsmola) propagate the Value error through Horizon
                # handler to the client with verbose message.
                raise ValueError("Date to must be bigger than date "
                                 "from.")
            # get the time delta in seconds
            delta = date_to - date_from
            if delta.days <= 0:
                # it's one day
                delta_in_seconds = 3600 * 24
            else:
                delta_in_seconds = delta.days * 24 * 3600 + delta.seconds
            # Lets always show 400 samples in the chart. Know that it is
            # maximum amount of samples and it can be lower.
            number_of_samples = 400
            period = delta_in_seconds / number_of_samples
        else:
            # If some date is missing, just set static window to one day.
            period = 3600 * 24

        additional_query = []
        if date_from:
            additional_query += [{'field': 'timestamp',
                                  'op': 'ge',
                                  'value': date_from}]
        if date_to:
            additional_query += [{'field': 'timestamp',
                                  'op': 'le',
                                  'value': date_to}]

        # TODO(lsmola) replace this by logic implemented in I1 in bugs
        # 1226479 and 1226482, this is just a quick fix for RC1
        try:
            meter_list = [m for m in ceilometer.meter_list(request)
                            if m.name == meter]
            unit = meter_list[0].unit
        except Exception:
            unit = ""

        if request.GET.get('group_by', None) == "project":
            try:
                tenants, more = api.keystone.tenant_list(
                    request,
                    domain=None,
                    paginate=True,
                    marker="tenant_marker")
            except Exception:
                tenants = []
                exceptions.handle(request,
                                  _('Unable to retrieve tenant list.'))
            queries = {}
            for tenant in tenants:
                tenant_query = [{
                    "field": "project_id",
                    "op": "eq",
                    "value": tenant.id}]

                queries[tenant.name] = tenant_query

            ceilometer_usage = ceilometer.CeilometerUsage(request)
            resources = ceilometer_usage.resource_aggregates_with_statistics(
                queries, [meter], period=period, stats_attr=None,
                additional_query=additional_query)

            series = self._series_for_meter(resources,
                                            'id',
                                            meter_name,
                                            stats_attr,
                                            unit)
        else:
            query = []

            def filter_by_meter_name(resource):
                """Function for filtering of the list of resources.

                Will pick the right resources according to currently selected
                meter.
                """
                for link in resource.links:
                    if link['rel'] == meter:
                        # If resource has the currently chosen meter.
                        return True
                return False

            ceilometer_usage = ceilometer.CeilometerUsage(request)
            try:
                resources = ceilometer_usage.resources_with_statistics(
                    query, [meter], period=period, stats_attr=None,
                    additional_query=additional_query,
                    filter_func=filter_by_meter_name)
            except Exception:
                resources = []
                exceptions.handle(request,
                                  _('Unable to retrieve statistics.'))

            series = self._series_for_meter(resources,
                                            'resource_id',
                                            meter_name,
                                            stats_attr,
                                            unit)

        ret = {}
        ret['series'] = series
        ret['settings'] = {}

        return HttpResponse(json.dumps(ret),
            mimetype='application/json')
Exemple #6
0
    def get(self, request, *args, **kwargs):
        meter = request.GET.get('meter', None)
        meter_name = meter.replace(".", "_")
        date_options = request.GET.get('date_options', None)
        date_from = request.GET.get('date_from', None)
        date_to = request.GET.get('date_to', None)
        resource = request.GET.get('resource', None)
        stats_attr = request.GET.get('stats_attr', 'avg')

        if (date_options == "other"):
            try:
                if date_from:
                    date_from = datetime.strptime(date_from, "%Y-%m-%d")
                if date_to:
                    date_to = datetime.strptime(date_to, "%Y-%m-%d")
            except ValueError:
                raise exceptions.NotFound
        else:
            date_from = datetime.now() - timedelta(days=int(date_options))
            date_to = datetime.now()

        query = [{
            "field": "metadata.OS-EXT-AZ:availability_zone",
            "op": "eq",
            "value": "nova"
        }]

        additional_query = []
        if date_from:
            additional_query += [{
                'field': 'timestamp',
                'op': 'ge',
                'value': date_from
            }]
        if date_to:
            additional_query += [{
                'field': 'timestamp',
                'op': 'le',
                'value': date_to
            }]

        if request.GET.get('group_by', None) == "project":
            try:
                tenants, more = api.keystone.tenant_list(
                    request,
                    domain=None,
                    paginate=True,
                    marker="tenant_marker")
            except Exception:
                tenants = []
                exceptions.handle(request,
                                  _('Unable to retrieve tenant list.'))
            queries = {}
            for tenant in tenants:
                tenant_query = [{
                    "field": "project_id",
                    "op": "eq",
                    "value": tenant.id
                }]

                queries[tenant.name] = tenant_query

            ceilometer_usage = ceilometer.CeilometerUsage(request)
            resources = ceilometer_usage.resource_aggregates_with_statistics(
                queries, [meter],
                period=1000,
                stats_attr=None,
                additional_query=additional_query)

            series = []
            for resource in resources:
                name = resource.id
                if getattr(resource, meter_name):
                    serie = {
                        'unit':
                        getattr(getattr(resource, meter_name)[0], 'unit', ""),
                        'name':
                        name,
                        'data': []
                    }

                    for statistic in getattr(resource, meter_name):
                        date = statistic.duration_end[:19]
                        value = int(getattr(statistic, stats_attr))
                        serie['data'].append({'x': date, 'y': value})

                    series.append(serie)
        else:
            ceilometer_usage = ceilometer.CeilometerUsage(request)
            try:
                resources = ceilometer_usage.resources_with_statistics(
                    query, [meter],
                    period=1000,
                    stats_attr=None,
                    additional_query=additional_query)
            except Exception:
                resources = []
                exceptions.handle(request, _('Unable to retrieve statistics.'))

            series = []
            for resource in resources:
                if getattr(resource, meter_name):
                    serie = {
                        'unit':
                        getattr(getattr(resource, meter_name)[0], 'unit', ""),
                        'name':
                        resource.resource_id,
                        'data': []
                    }
                    for statistic in getattr(resource, meter_name):
                        date = statistic.duration_end[:19]
                        value = int(getattr(statistic, stats_attr))
                        serie['data'].append({'x': date, 'y': value})

                    series.append(serie)

        ret = {}
        ret['series'] = series
        ret['settings'] = {}

        return HttpResponse(json.dumps(ret), mimetype='application/json')