"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-10.0.0/zaqar/transport/wsgi/v1_1/stats.py" (13 May 2020, 2385 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. See also the latest Fossies "Diffs" side-by-side code changes report for "stats.py": 9.0.0_vs_10.0.0.

    1 # Copyright (c) 2013 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.i18n import _
   20 from zaqar.storage import errors as storage_errors
   21 from zaqar.transport import utils
   22 from zaqar.transport.wsgi import errors as wsgi_errors
   23 
   24 
   25 LOG = logging.getLogger(__name__)
   26 
   27 
   28 class Resource(object):
   29 
   30     __slots__ = '_queue_ctrl'
   31 
   32     def __init__(self, queue_controller):
   33         self._queue_ctrl = queue_controller
   34 
   35     def on_get(self, req, resp, project_id, queue_name):
   36         try:
   37             resp_dict = self._queue_ctrl.stats(queue_name,
   38                                                project=project_id)
   39 
   40             message_stats = resp_dict['messages']
   41 
   42             if message_stats['total'] != 0:
   43                 base_path = req.path[:req.path.rindex('/')] + '/messages/'
   44 
   45                 newest = message_stats['newest']
   46                 newest['href'] = base_path + newest['id']
   47                 del newest['id']
   48 
   49                 oldest = message_stats['oldest']
   50                 oldest['href'] = base_path + oldest['id']
   51                 del oldest['id']
   52 
   53             resp.body = utils.to_json(resp_dict)
   54             # status defaults to 200
   55 
   56         except (storage_errors.QueueDoesNotExist,
   57                 storage_errors.QueueIsEmpty) as ex:
   58             resp_dict = {
   59                 'messages': {
   60                     'claimed': 0,
   61                     'free': 0,
   62                     'total': 0
   63                 }
   64             }
   65             resp.body = utils.to_json(resp_dict)
   66 
   67         except storage_errors.DoesNotExist as ex:
   68             LOG.debug(ex)
   69             raise wsgi_errors.HTTPNotFound(six.text_type(ex))
   70 
   71         except Exception:
   72             description = _(u'Queue stats could not be read.')
   73             LOG.exception(description)
   74             raise wsgi_errors.HTTPServiceUnavailable(description)