"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-10.0.0/zaqar/transport/wsgi/v2_0/topic_stats.py" (13 May 2020, 2536 Bytes) of package /linux/misc/openstack/zaqar-10.0.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 "topic_stats.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.0.0_vs_10.0.0.

    1 # Copyright (c) 2019 Rackspace, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 from oslo_log import log as logging
   17 import six
   18 
   19 from zaqar.common import decorators
   20 from zaqar.i18n import _
   21 from zaqar.storage import errors as storage_errors
   22 from zaqar.transport import acl
   23 from zaqar.transport import utils
   24 from zaqar.transport.wsgi import errors as wsgi_errors
   25 
   26 
   27 LOG = logging.getLogger(__name__)
   28 
   29 
   30 class Resource(object):
   31 
   32     __slots__ = '_topic_ctrl'
   33 
   34     def __init__(self, topic_controller):
   35         self._topic_ctrl = topic_controller
   36 
   37     @decorators.TransportLog("Topics stats item")
   38     @acl.enforce("topics:stats")
   39     def on_get(self, req, resp, project_id, topic_name):
   40         try:
   41             resp_dict = self._topic_ctrl.stats(topic_name,
   42                                                project=project_id)
   43 
   44             message_stats = resp_dict['messages']
   45 
   46             if message_stats['total'] != 0:
   47                 base_path = req.path[:req.path.rindex('/')] + '/messages/'
   48 
   49                 newest = message_stats['newest']
   50                 newest['href'] = base_path + newest['id']
   51                 del newest['id']
   52 
   53                 oldest = message_stats['oldest']
   54                 oldest['href'] = base_path + oldest['id']
   55                 del oldest['id']
   56 
   57             resp.body = utils.to_json(resp_dict)
   58             # status defaults to 200
   59 
   60         except (storage_errors.TopicDoesNotExist,
   61                 storage_errors.TopicIsEmpty) as ex:
   62             resp_dict = {
   63                 'messages': {
   64                     'claimed': 0,
   65                     'free': 0,
   66                     'total': 0
   67                 }
   68             }
   69             resp.body = utils.to_json(resp_dict)
   70 
   71         except storage_errors.DoesNotExist as ex:
   72             LOG.debug(ex)
   73             raise wsgi_errors.HTTPNotFound(six.text_type(ex))
   74 
   75         except Exception:
   76             description = _(u'Topic stats could not be read.')
   77             LOG.exception(description)
   78             raise wsgi_errors.HTTPServiceUnavailable(description)