"Fossies" - the Fresh Open Source Software Archive

Member "panko-8.1.0/panko/storage/__init__.py" (4 Aug 2021, 4891 Bytes) of package /linux/misc/openstack/panko-8.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.

    1 #
    2 # Copyright 2012 New Dream Network, LLC (DreamHost)
    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 """Storage backend management
   16 """
   17 
   18 from oslo_config import cfg
   19 from oslo_log import log
   20 import six
   21 import six.moves.urllib.parse as urlparse
   22 from stevedore import driver
   23 import tenacity
   24 
   25 from panko import utils
   26 
   27 LOG = log.getLogger(__name__)
   28 
   29 
   30 OPTS = [
   31     cfg.IntOpt('event_time_to_live',
   32                default=-1,
   33                help=("Number of seconds that events are kept "
   34                      "in the database for (<= 0 means forever).")),
   35     cfg.IntOpt('events_delete_batch_size',
   36                default=0,
   37                min=0,
   38                help=("Number of events to be deleted in one iteration "
   39                      "from the database for (0 means all).")),
   40     cfg.StrOpt('event_connection',
   41                secret=True,
   42                deprecated_for_removal=True,
   43                help='The connection string used to connect '
   44                'to the event database - rather use ${database.connection}'),
   45     cfg.BoolOpt('es_ssl_enabled',
   46                 default=False,
   47                 help="Enable HTTPS connection in the Elasticsearch "
   48                      "connection"),
   49     cfg.StrOpt('es_index_name',
   50                default='events',
   51                help='The name of the index in Elasticsearch')
   52 ]
   53 
   54 
   55 class StorageUnknownWriteError(Exception):
   56     """Error raised when an unknown error occurs while recording."""
   57 
   58 
   59 class StorageBadVersion(Exception):
   60     """Error raised when the storage backend version is not good enough."""
   61 
   62 
   63 class StorageBadAggregate(Exception):
   64     """Error raised when an aggregate is unacceptable to storage backend."""
   65     code = 400
   66 
   67 
   68 class InvalidMarker(Exception):
   69     """Invalid pagination marker parameters"""
   70 
   71 
   72 def get_connection_from_config(conf):
   73     retries = conf.database.max_retries
   74 
   75     @tenacity.retry(
   76         reraise=True,
   77         wait=tenacity.wait_fixed(conf.database.retry_interval),
   78         stop=(tenacity.stop_after_attempt(retries) if retries >= 0
   79               else tenacity.stop_never)
   80     )
   81     def _inner():
   82         url = (conf.database.connection or
   83                getattr(conf.database, 'event_connection', None))
   84         return get_connection(url, conf)
   85 
   86     return _inner()
   87 
   88 
   89 def get_connection(url, conf):
   90     """Return an open connection to the database."""
   91     connection_scheme = urlparse.urlparse(url).scheme
   92     # SqlAlchemy connections specify may specify a 'dialect' or
   93     # 'dialect+driver'. Handle the case where driver is specified.
   94     engine_name = connection_scheme.split('+')[0]
   95     # NOTE: translation not applied bug #1446983
   96     LOG.debug('looking for %(name)r driver in panko.storage',
   97               {'name': engine_name})
   98     mgr = driver.DriverManager('panko.storage', engine_name)
   99     return mgr.driver(url, conf)
  100 
  101 
  102 class EventFilter(object):
  103     """Properties for building an Event query.
  104 
  105     :param start_timestamp: UTC start datetime (mandatory)
  106     :param end_timestamp: UTC end datetime (mandatory)
  107     :param event_type: the name of the event. None for all.
  108     :param message_id: the message_id of the event. None for all.
  109     :param admin_proj: the project_id of admin role. None if non-admin user.
  110     :param traits_filter: the trait filter dicts, all of which are optional.
  111       This parameter is a list of dictionaries that specify trait values:
  112 
  113     .. code-block:: python
  114 
  115         {'key': <key>,
  116         'string': <value>,
  117         'integer': <value>,
  118         'datetime': <value>,
  119         'float': <value>,
  120         'op': <eq, lt, le, ne, gt or ge> }
  121     """
  122 
  123     def __init__(self, start_timestamp=None, end_timestamp=None,
  124                  event_type=None, message_id=None, traits_filter=None,
  125                  admin_proj=None):
  126         self.start_timestamp = utils.sanitize_timestamp(start_timestamp)
  127         self.end_timestamp = utils.sanitize_timestamp(end_timestamp)
  128         self.message_id = message_id
  129         self.event_type = event_type
  130         self.traits_filter = traits_filter or []
  131         self.admin_proj = admin_proj
  132 
  133     def __repr__(self):
  134         return ("<EventFilter(start_timestamp: %s,"
  135                 " end_timestamp: %s,"
  136                 " event_type: %s,"
  137                 " traits: %s)>" %
  138                 (self.start_timestamp,
  139                  self.end_timestamp,
  140                  self.event_type,
  141                  six.text_type(self.traits_filter)))