This file is indexed.

/usr/lib/python2.7/dist-packages/gnocchiclient/v1/metric.py is in python-gnocchiclient 7.0.1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import datetime
import uuid

from debtcollector import removals
import iso8601
import json

from gnocchiclient import utils
from gnocchiclient.v1 import base


class MetricManager(base.Manager):
    metric_url = "v1/metric/"
    resource_url = "v1/resource/generic/%s/metric/"
    metric_batch_url = "v1/batch/metrics/measures"
    resources_batch_url = "v1/batch/resources/metrics/measures"

    def list(self, limit=None, marker=None, sorts=None):
        """List metrics

        :param limit: maximum number of resources to return
        :type limit: int
        :param marker: the last item of the previous page; we return the next
                       results after this value.
        :type marker: str
        :param sorts: list of resource attributes to order by. (example
                      ["user_id:desc-nullslast", "project_id:asc"]
        :type sorts: list of str
        """
        params = utils.build_pagination_options(False, False, limit, marker,
                                                sorts)
        metrics = []
        page_url = "%s?%s" % (self.metric_url[:-1],
                              utils.dict_to_querystring(params))
        while page_url:
            page = self._get(page_url)
            metrics.extend(page.json())
            if limit is None or len(metrics) < limit:
                page_url = page.links.get("next", {'url': None})['url']
            else:
                break
        return metrics

    @staticmethod
    def _ensure_metric_is_uuid(metric, attribute="resource_id"):
        try:
            uuid.UUID(metric)
        except ValueError:
            raise TypeError("%s is required to get a metric by name" %
                            attribute)

    def get(self, metric, resource_id=None):
        """Get an metric

        :param metric: ID or Name of the metric
        :type metric: str
        :param resource_id: ID of the resource (required
                            to get a metric by name)
        :type resource_id: str
        """
        if resource_id is None:
            self._ensure_metric_is_uuid(metric)
            url = self.metric_url + metric
        else:
            url = (self.resource_url % resource_id) + metric
        return self._get(url).json()

    # FIXME(jd): This is what create will be after debtcollector warnings have
    # been removed. We provide it right now for the benchmark code, that can't
    # pickle a debtcollector-ed method.
    def _create_new(self, name=None, archive_policy_name=None,
                    resource_id=None, unit=None):
        """Create an metric

        :param name: Metric name.
        :type name: str
        :param archive_policy_name: Archive policy name.
        :type archive_policy_name: str
        :param resource_id: The resource ID to attach the metric to.
        :type resource_id: str
        :param unit: The unit of the metric.
        :type unit: str
        """

        metric = {}
        if name is not None:
            metric["name"] = name
        if archive_policy_name is not None:
            metric["archive_policy_name"] = archive_policy_name
        if unit is not None:
            metric["unit"] = unit

        if resource_id is None:
            return self._post(
                self.metric_url, headers={'Content-Type': "application/json"},
                data=ujson.dumps(metric)).json()

        if name is None:
            raise TypeError(
                "Metric name is required if resource_id is set")

        return self._post(
            self.resource_url % resource_id,
            headers={'Content-Type': "application/json"},
            data=ujson.dumps({name: metric})).json()[0]

    # FIXME(jd): remove refetch_metric when LP#1497171 is fixed
    @removals.removed_kwarg("refetch_metric")
    @removals.removed_kwarg("metric")
    def create(self, metric=None, refetch_metric=True,
               name=None,
               archive_policy_name=None,
               resource_id=None,
               unit=None):
        """Create an metric

        :param name: Metric name.
        :type name: str
        :param archive_policy_name: Archive policy name.
        :type archive_policy_name: str
        :param resource_id: The resource ID to attach the metric to.
        :type resource_id: str
        :param unit: The unit of the metric.
        :type unit: str
        """

        if metric is None:
            metric = {}
            if name is not None:
                metric["name"] = name
            if archive_policy_name is not None:
                metric["archive_policy_name"] = archive_policy_name
            if resource_id is not None:
                metric["resource_id"] = resource_id
            if unit is not None:
                metric["unit"] = unit

        resource_id = metric.get('resource_id')

        if resource_id is None:
            metric = self._post(
                self.metric_url, headers={'Content-Type': "application/json"},
                data=json.dumps(metric)).json()
            # FIXME(sileht): create and get have a
            # different output: LP#1497171
            if refetch_metric:
                return self.get(metric["id"])
            return metric

        metric_name = metric.get('name')

        if metric_name is None:
            raise TypeError("metric_name is required if resource_id is set")

        del metric['resource_id']
        metric = {metric_name: metric}
        metric = self._post(
            self.resource_url % resource_id,
            headers={'Content-Type': "application/json"},
            data=json.dumps(metric))
        return self.get(metric_name, resource_id)

    def delete(self, metric, resource_id=None):
        """Delete an metric

        :param metric: ID or Name of the metric
        :type metric: str
        :param resource_id: ID of the resource (required
                            to get a metric by name)
        :type resource_id: str
        """
        if resource_id is None:
            self._ensure_metric_is_uuid(metric)
            url = self.metric_url + metric
        else:
            url = self.resource_url % resource_id + metric
        self._delete(url)

    def add_measures(self, metric, measures, resource_id=None):
        """Add measurements to a metric

        :param metric: ID or Name of the metric
        :type metric: str
        :param resource_id: ID of the resource (required
                            to get a metric by name)
        :type resource_id: str
        :param measures: measurements
        :type measures: list of dict(timestamp=timestamp, value=float)
        """
        if resource_id is None:
            self._ensure_metric_is_uuid(metric)
            url = self.metric_url + metric + "/measures"
        else:
            url = self.resource_url % resource_id + metric + "/measures"
        return self._post(
            url, headers={'Content-Type': "application/json"},
            data=json.dumps(measures))

    def batch_metrics_measures(self, measures):
        """Add measurements to metrics

        :param measures: measurements
        :type dict(metric_id: list of dict(timestamp=timestamp, value=float))
        """

        return self._post(
            self.metric_batch_url,
            headers={'Content-Type': "application/json"},
            data=json.dumps(measures))

    def batch_resources_metrics_measures(self, measures, create_metrics=False):
        """Add measurements to named metrics if resources

        :param measures: measurements
        :type dict(resource_id: dict(metric_name:
            list of dict(timestamp=timestamp, value=float)))
        """

        return self._post(
            self.resources_batch_url,
            headers={'Content-Type': "application/json"},
            data=json.dumps(measures),
            params=dict(create_metrics=create_metrics))

    def get_measures(self, metric, start=None, stop=None, aggregation=None,
                     granularity=None, resource_id=None, refresh=False,
                     resample=None, **kwargs):
        """Get measurements of a metric

        :param metric: ID or Name of the metric
        :type metric: str
        :param start: beginning of the period
        :type start: timestamp
        :param stop: end of the period
        :type stop: timestamp
        :param aggregation: aggregation to retrieve
        :type aggregation: str
        :param granularity: granularity to retrieve (in seconds)
        :type granularity: int
        :param resource_id: ID of the resource (required
                            to get a metric by name)
        :type resource_id: str
        :param refresh: force aggregation of all known measures
        :type refresh: bool
        :param resample: resample measures to new granularity
        :type resample: float

        All other arguments are arguments are dedicated to custom aggregation
        method passed as-is to the Gnocchi.
        """

        if isinstance(start, datetime.datetime):
            start = start.isoformat()
        if isinstance(stop, datetime.datetime):
            stop = stop.isoformat()

        params = dict(start=start, stop=stop, aggregation=aggregation,
                      granularity=granularity, refresh=refresh,
                      resample=resample)
        params.update(kwargs)
        if resource_id is None:
            self._ensure_metric_is_uuid(metric)
            url = self.metric_url + metric + "/measures"
        else:
            url = self.resource_url % resource_id + metric + "/measures"
        measures = self._get(url, params=params).json()
        return [(iso8601.parse_date(ts), g, value)
                for ts, g, value in measures]

    def aggregation(self, metrics, query=None,
                    start=None, stop=None, aggregation=None,
                    reaggregation=None, granularity=None,
                    needed_overlap=None, resource_type="generic",
                    groupby=None, refresh=False, resample=None, fill=None):
        """Get measurements of an aggregated metrics

        :param metrics: IDs of metric or metric name
        :type metric: list or str
        :param query: The query dictionary
        :type query: dict
        :param start: beginning of the period
        :type start: timestamp
        :param stop: end of the period
        :type stop: timestamp
        :param aggregation: granularity aggregation function to retrieve
        :type aggregation: str
        :param reaggregation: groupby aggregation function to retrieve
        :type reaggregation: str
        :param granularity: granularity to retrieve (in seconds)
        :type granularity: int
        :param needed_overlap: percent of datapoints in each metrics required
        :type needed_overlap: float
        :param resource_type: type of resource for the query
        :type resource_type: str
        :param groupby: list of attribute to group by
        :type groupby: list
        :param refresh: force aggregation of all known measures
        :type refresh: bool
        :param resample: resample measures to new granularity
        :type resample: float
        :param fill: value to use when backfilling missing datapoints
        :type fill: float or 'null'

        See Gnocchi REST API documentation for the format
        of *query dictionary*
        http://docs.openstack.org/developer/gnocchi/rest.html#searching-for-resources
        """

        if isinstance(start, datetime.datetime):
            start = start.isoformat()
        if isinstance(stop, datetime.datetime):
            stop = stop.isoformat()

        params = dict(start=start, stop=stop, aggregation=aggregation,
                      reaggregation=reaggregation, granularity=granularity,
                      needed_overlap=needed_overlap, groupby=groupby,
                      refresh=refresh, resample=resample, fill=fill)
        if query is None:
            for metric in metrics:
                self._ensure_metric_is_uuid(metric)
            params['metric'] = metrics
            measures = self._get("v1/aggregation/metric",
                                 params=params).json()
        else:
            if isinstance(query, dict):
                measures = self._post(
                    "v1/aggregation/resource/%s/metric/%s?%s" % (
                        resource_type, metrics,
                        utils.dict_to_querystring(params)),
                    headers={'Content-Type': "application/json"},
                    data=json.dumps(query)).json()
            else:
                params['filter'] = query
                measures = self._post(
                    "v1/aggregation/resource/%s/metric/%s?%s" % (
                        resource_type, metrics,
                        utils.dict_to_querystring(params)),
                    headers={'Content-Type': "application/json"}).json()
        if groupby is None:
            return [(iso8601.parse_date(ts), g, value)
                    for ts, g, value in measures]

        for group in measures:
            group["measures"] = [
                (iso8601.parse_date(ts), g, value)
                for ts, g, value in group["measures"]
            ]

        return measures