"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/v2/reference/metrics.py" (27 Sep 2019, 15881 Bytes) of package /linux/misc/openstack/monasca-api-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "metrics.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0.0_vs_3.1.0.

    1 # (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
    2 # Copyright 2018 OP5 AB
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 # not use this file except in compliance with the License. You may obtain
    6 # a copy of the License at
    7 #
    8 # http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 # License for the specific language governing permissions and limitations
   14 # under the License.
   15 
   16 import falcon
   17 from monasca_common.simport import simport
   18 from monasca_common.validation import metrics as metric_validation
   19 from oslo_config import cfg
   20 from oslo_log import log
   21 
   22 from monasca_api.api import metrics_api_v2
   23 from monasca_api.common.messaging import (
   24     exceptions as message_queue_exceptions)
   25 from monasca_api.common.messaging.message_formats import (
   26     metrics as metrics_message)
   27 from monasca_api.v2.common.exceptions import HTTPUnprocessableEntityError
   28 from monasca_api.v2.reference import helpers
   29 from monasca_api.v2.reference import resource
   30 
   31 LOG = log.getLogger(__name__)
   32 
   33 
   34 def get_merge_metrics_flag(req):
   35     '''Return the value of the optional metrics_flag
   36 
   37     Returns False if merge_metrics parameter is not supplied or is not a
   38     string that evaluates to True, otherwise True
   39     '''
   40 
   41     merge_metrics_flag = helpers.get_query_param(req,
   42                                                  'merge_metrics',
   43                                                  False,
   44                                                  False)
   45     if merge_metrics_flag is not False:
   46         return helpers.str_2_bool(merge_metrics_flag)
   47     else:
   48         return False
   49 
   50 
   51 class Metrics(metrics_api_v2.MetricsV2API):
   52     def __init__(self):
   53         try:
   54             super(Metrics, self).__init__()
   55             self._region = cfg.CONF.region
   56             self._message_queue = simport.load(cfg.CONF.messaging.driver)(
   57                 'metrics')
   58             self._metrics_repo = simport.load(
   59                 cfg.CONF.repositories.metrics_driver)()
   60 
   61         except Exception as ex:
   62             LOG.exception(ex)
   63             raise falcon.HTTPInternalServerError('Service unavailable',
   64                                                  str(ex))
   65 
   66     def _send_metrics(self, metrics):
   67         try:
   68             self._message_queue.send_message(metrics)
   69         except message_queue_exceptions.MessageQueueException as ex:
   70             LOG.exception(ex)
   71             raise falcon.HTTPServiceUnavailable('Service unavailable',
   72                                                 str(ex), 60)
   73 
   74     def _list_metrics(self, tenant_id, name, dimensions, req_uri, offset,
   75                       limit, start_timestamp, end_timestamp):
   76 
   77         result = self._metrics_repo.list_metrics(tenant_id,
   78                                                  self._region,
   79                                                  name,
   80                                                  dimensions,
   81                                                  offset, limit,
   82                                                  start_timestamp,
   83                                                  end_timestamp)
   84 
   85         return helpers.paginate(result, req_uri, limit)
   86 
   87     @resource.resource_try_catch_block
   88     def on_post(self, req, res):
   89         helpers.validate_json_content_type(req)
   90         helpers.validate_authorization(req, ['api:metrics:post'])
   91         metrics = helpers.from_json(req)
   92         try:
   93             metric_validation.validate(metrics)
   94         except Exception as ex:
   95             LOG.exception(ex)
   96             raise HTTPUnprocessableEntityError("Unprocessable Entity", str(ex))
   97 
   98         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
   99         transformed_metrics = metrics_message.transform(
  100             metrics, tenant_id, self._region)
  101         self._send_metrics(transformed_metrics)
  102         res.status = falcon.HTTP_204
  103 
  104     @resource.resource_try_catch_block
  105     def on_get(self, req, res):
  106         helpers.validate_authorization(req, ['api:metrics:get'])
  107         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  108         name = helpers.get_query_name(req)
  109         helpers.validate_query_name(name)
  110         dimensions = helpers.get_query_dimensions(req)
  111         helpers.validate_query_dimensions(dimensions)
  112         offset = helpers.get_query_param(req, 'offset')
  113         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  114         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  115         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  116         result = self._list_metrics(tenant_id, name,
  117                                     dimensions, req.uri,
  118                                     offset, req.limit,
  119                                     start_timestamp, end_timestamp)
  120         res.body = helpers.to_json(result)
  121         res.status = falcon.HTTP_200
  122 
  123 
  124 class MetricsMeasurements(metrics_api_v2.MetricsMeasurementsV2API):
  125     def __init__(self):
  126         try:
  127             super(MetricsMeasurements, self).__init__()
  128             self._region = cfg.CONF.region
  129             self._metrics_repo = simport.load(
  130                 cfg.CONF.repositories.metrics_driver)()
  131 
  132         except Exception as ex:
  133             LOG.exception(ex)
  134             raise falcon.HTTPInternalServerError('Service unavailable',
  135                                                  str(ex))
  136 
  137     @resource.resource_try_catch_block
  138     def on_get(self, req, res):
  139         helpers.validate_authorization(req, ['api:metrics:get'])
  140         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  141         name = helpers.get_query_name(req, True)
  142         helpers.validate_query_name(name)
  143         dimensions = helpers.get_query_dimensions(req)
  144         helpers.validate_query_dimensions(dimensions)
  145         start_timestamp = helpers.get_query_starttime_timestamp(req)
  146         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  147         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  148         offset = helpers.get_query_param(req, 'offset')
  149         merge_metrics_flag = get_merge_metrics_flag(req)
  150         group_by = helpers.get_query_group_by(req)
  151 
  152         result = self._measurement_list(tenant_id, name, dimensions,
  153                                         start_timestamp, end_timestamp,
  154                                         req.uri, offset,
  155                                         req.limit, merge_metrics_flag,
  156                                         group_by)
  157 
  158         res.body = helpers.to_json(result)
  159         res.status = falcon.HTTP_200
  160 
  161     def _measurement_list(self, tenant_id, name, dimensions, start_timestamp,
  162                           end_timestamp, req_uri, offset,
  163                           limit, merge_metrics_flag, group_by):
  164 
  165         result = self._metrics_repo.measurement_list(tenant_id,
  166                                                      self._region,
  167                                                      name,
  168                                                      dimensions,
  169                                                      start_timestamp,
  170                                                      end_timestamp,
  171                                                      offset,
  172                                                      limit,
  173                                                      merge_metrics_flag,
  174                                                      group_by)
  175 
  176         return helpers.paginate_measurements(result, req_uri, limit)
  177 
  178 
  179 class MetricsStatistics(metrics_api_v2.MetricsStatisticsV2API):
  180     def __init__(self):
  181         try:
  182             super(MetricsStatistics, self).__init__()
  183             self._region = cfg.CONF.region
  184             self._metrics_repo = simport.load(
  185                 cfg.CONF.repositories.metrics_driver)()
  186 
  187         except Exception as ex:
  188             LOG.exception(ex)
  189             raise falcon.HTTPInternalServerError('Service unavailable',
  190                                                  str(ex))
  191 
  192     @resource.resource_try_catch_block
  193     def on_get(self, req, res):
  194         helpers.validate_authorization(req, ['api:metrics:get'])
  195         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  196         name = helpers.get_query_name(req, True)
  197         helpers.validate_query_name(name)
  198         dimensions = helpers.get_query_dimensions(req)
  199         helpers.validate_query_dimensions(dimensions)
  200         start_timestamp = helpers.get_query_starttime_timestamp(req)
  201         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  202         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  203         statistics = helpers.get_query_statistics(req)
  204         period = helpers.get_query_period(req)
  205         offset = helpers.get_query_param(req, 'offset')
  206         merge_metrics_flag = get_merge_metrics_flag(req)
  207         group_by = helpers.get_query_group_by(req)
  208 
  209         result = self._metric_statistics(tenant_id, name, dimensions,
  210                                          start_timestamp, end_timestamp,
  211                                          statistics, period, req.uri,
  212                                          offset, req.limit, merge_metrics_flag,
  213                                          group_by)
  214 
  215         res.body = helpers.to_json(result)
  216         res.status = falcon.HTTP_200
  217 
  218     def _metric_statistics(self, tenant_id, name, dimensions, start_timestamp,
  219                            end_timestamp, statistics, period, req_uri,
  220                            offset, limit, merge_metrics_flag, group_by):
  221 
  222         result = self._metrics_repo.metrics_statistics(tenant_id,
  223                                                        self._region,
  224                                                        name,
  225                                                        dimensions,
  226                                                        start_timestamp,
  227                                                        end_timestamp,
  228                                                        statistics, period,
  229                                                        offset,
  230                                                        limit,
  231                                                        merge_metrics_flag,
  232                                                        group_by)
  233 
  234         return helpers.paginate_statistics(result, req_uri, limit)
  235 
  236 
  237 class MetricsNames(metrics_api_v2.MetricsNamesV2API):
  238     def __init__(self):
  239         try:
  240             super(MetricsNames, self).__init__()
  241             self._region = cfg.CONF.region
  242             self._metrics_repo = simport.load(
  243                 cfg.CONF.repositories.metrics_driver)()
  244 
  245         except Exception as ex:
  246             LOG.exception(ex)
  247             raise falcon.HTTPInternalServerError('Service unavailable',
  248                                                  str(ex))
  249 
  250     @resource.resource_try_catch_block
  251     def on_get(self, req, res):
  252         helpers.validate_authorization(req, ['api:metrics:get'])
  253         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  254         dimensions = helpers.get_query_dimensions(req)
  255         helpers.validate_query_dimensions(dimensions)
  256         offset = helpers.get_query_param(req, 'offset')
  257         result = self._list_metric_names(tenant_id, dimensions,
  258                                          req.uri, offset, req.limit)
  259         res.body = helpers.to_json(result)
  260         res.status = falcon.HTTP_200
  261 
  262     def _list_metric_names(self, tenant_id, dimensions, req_uri, offset,
  263                            limit):
  264 
  265         result = self._metrics_repo.list_metric_names(tenant_id,
  266                                                       self._region,
  267                                                       dimensions)
  268 
  269         return helpers.paginate_with_no_id(result, req_uri, offset, limit)
  270 
  271 
  272 class DimensionValues(metrics_api_v2.DimensionValuesV2API):
  273     def __init__(self):
  274         try:
  275             super(DimensionValues, self).__init__()
  276             self._region = cfg.CONF.region
  277             self._metrics_repo = simport.load(
  278                 cfg.CONF.repositories.metrics_driver)()
  279 
  280         except Exception as ex:
  281             LOG.exception(ex)
  282             raise falcon.HTTPInternalServerError('Service unavailable', str(ex))
  283 
  284     @resource.resource_try_catch_block
  285     def on_get(self, req, res):
  286         helpers.validate_authorization(req, ['api:metrics:dimension:values'])
  287         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  288         metric_name = helpers.get_query_param(req, 'metric_name')
  289         dimension_name = helpers.get_query_param(req, 'dimension_name',
  290                                                  required=True)
  291         offset = helpers.get_query_param(req, 'offset')
  292         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  293         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  294         result = self._dimension_values(tenant_id, req.uri, metric_name,
  295                                         dimension_name, offset, req.limit,
  296                                         start_timestamp, end_timestamp)
  297         res.body = helpers.to_json(result)
  298         res.status = falcon.HTTP_200
  299 
  300     def _dimension_values(self, tenant_id, req_uri, metric_name,
  301                           dimension_name, offset, limit, start_timestamp,
  302                           end_timestamp):
  303 
  304         result = self._metrics_repo.list_dimension_values(tenant_id,
  305                                                           self._region,
  306                                                           metric_name,
  307                                                           dimension_name,
  308                                                           start_timestamp,
  309                                                           end_timestamp)
  310 
  311         return helpers.paginate_with_no_id(result, req_uri, offset, limit)
  312 
  313 
  314 class DimensionNames(metrics_api_v2.DimensionNamesV2API):
  315     def __init__(self):
  316         try:
  317             super(DimensionNames, self).__init__()
  318             self._region = cfg.CONF.region
  319             self._metrics_repo = simport.load(
  320                 cfg.CONF.repositories.metrics_driver)()
  321 
  322         except Exception as ex:
  323             LOG.exception(ex)
  324             raise falcon.HTTPInternalServerError('Service unavailable',
  325                                                  str(ex))
  326 
  327     @resource.resource_try_catch_block
  328     def on_get(self, req, res):
  329         helpers.validate_authorization(req, ['api:metrics:dimension:names'])
  330         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  331         metric_name = helpers.get_query_param(req, 'metric_name')
  332         offset = helpers.get_query_param(req, 'offset')
  333         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  334         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  335         result = self._dimension_names(tenant_id, req.uri, metric_name,
  336                                        offset, req.limit,
  337                                        start_timestamp, end_timestamp)
  338         res.body = helpers.to_json(result)
  339         res.status = falcon.HTTP_200
  340 
  341     def _dimension_names(self, tenant_id, req_uri, metric_name, offset, limit,
  342                          start_timestamp, end_timestamp):
  343 
  344         result = self._metrics_repo.list_dimension_names(tenant_id,
  345                                                          self._region,
  346                                                          metric_name,
  347                                                          start_timestamp,
  348                                                          end_timestamp)
  349 
  350         return helpers.paginate_with_no_id(result, req_uri, offset, limit)