panko  8.1.0
About: OpenStack Panko is an event storage service and REST API for Ceilometer.
The "Ussuri" series (maintained release).
  Fossies Dox: panko-8.1.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pymongo_base.py
Go to the documentation of this file.
2# Licensed under the Apache License, Version 2.0 (the "License"); you may
3# not use this file except in compliance with the License. You may obtain
4# a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11# License for the specific language governing permissions and limitations
12# under the License.
13"""Common functions for MongoDB backend
14"""
15from oslo_log import log
16import pymongo
17
18from panko.storage import base
19from panko.storage import models
20from panko.storage.mongo import utils as pymongo_utils
21from panko import utils
22
23LOG = log.getLogger(__name__)
24
25
26COMMON_AVAILABLE_CAPABILITIES = {
27 'events': {'query': {'simple': True}},
28}
29
30
31AVAILABLE_STORAGE_CAPABILITIES = {
32 'storage': {'production_ready': True},
33}
34
35
37 """Base event Connection class for MongoDB driver."""
38 CAPABILITIES = utils.update_nested(base.Connection.CAPABILITIES,
39 COMMON_AVAILABLE_CAPABILITIES)
40
41 STORAGE_CAPABILITIES = utils.update_nested(
42 base.Connection.STORAGE_CAPABILITIES,
43 AVAILABLE_STORAGE_CAPABILITIES,
44 )
45
46 def record_events(self, event_models):
47 """Write the events to database.
48
49 :param event_models: a list of models.Event objects.
50 """
51 error = None
52 for event_model in event_models:
53 traits = []
54 if event_model.traits:
55 for trait in event_model.traits:
56 traits.append({'trait_name': trait.name,
57 'trait_type': trait.dtype,
58 'trait_value': trait.value})
59 try:
60 self.db.event.insert_one(
61 {'_id': event_model.message_id,
62 'event_type': event_model.event_type,
63 'timestamp': event_model.generated,
64 'traits': traits, 'raw': event_model.raw})
65 except pymongo.errors.DuplicateKeyError as ex:
66 LOG.debug("Duplicate event detected, skipping it: %s", ex)
67 except Exception as ex:
68 LOG.exception("Failed to record event: %s", ex)
69 error = ex
70 if error:
71 raise error
72
73 def get_events(self, event_filter, pagination=None):
74 """Return an iter of models.Event objects.
75
76 :param event_filter: storage.EventFilter object, consists of filters
77 for events that are stored in database.
78 :param pagination: Pagination parameters.
79 """
80 limit = None
81 if pagination:
82 if pagination.get('sort'):
83 LOG.warning('Driver does not support sort functionality')
84 limit = pagination.get('limit')
85 if limit == 0:
86 return
87 q = pymongo_utils.make_events_query_from_filter(event_filter)
88 if limit is not None:
89 results = self.db.event.find(q, limit=limit)
90 else:
91 results = self.db.event.find(q)
92 for event in results:
93 traits = []
94 for trait in event['traits']:
95 traits.append(models.Trait(name=trait['trait_name'],
96 dtype=int(trait['trait_type']),
97 value=trait['trait_value']))
98 yield models.Event(message_id=event['_id'],
99 event_type=event['event_type'],
100 generated=event['timestamp'],
101 traits=traits, raw=event.get('raw'))
102
104 """Return all event types as an iter of strings."""
105 return self.db.event.distinct('event_type')
106
107 def get_trait_types(self, event_type):
108 """Return a dictionary containing the name and data type of the trait.
109
110 Only trait types for the provided event_type are returned.
111
112 :param event_type: the type of the Event.
113 """
114 trait_names = set()
115 events = self.db.event.find({'event_type': event_type})
116
117 for event in events:
118 for trait in event['traits']:
119 trait_name = trait['trait_name']
120 if trait_name not in trait_names:
121 # Here we check that our method return only unique
122 # trait types. Method will return only one trait type. It
123 # is proposed that certain trait name could have only one
124 # trait type.
125 trait_names.add(trait_name)
126 yield {'name': trait_name,
127 'data_type': trait['trait_type']}
128
129 def get_traits(self, event_type, trait_name=None):
130 """Return all trait instances associated with an event_type.
131
132 If trait_type is specified, only return instances of that trait type.
133
134 :param event_type: the type of the Event to filter by
135 :param trait_name: the name of the Trait to filter by
136 """
137 if not trait_name:
138 events = self.db.event.find({'event_type': event_type})
139 else:
140 # We choose events that simultaneously have event_type and certain
141 # trait_name, and retrieve events contains only mentioned traits.
142 events = self.db.event.find({'$and': [{'event_type': event_type},
143 {'traits.trait_name': trait_name}]},
144 {'traits': {'$elemMatch':
145 {'trait_name': trait_name}}
146 })
147 for event in events:
148 for trait in event['traits']:
149 yield models.Trait(name=trait['trait_name'],
150 dtype=trait['trait_type'],
151 value=trait['trait_value'])
def get_trait_types(self, event_type)
def get_events(self, event_filter, pagination=None)
Definition: pymongo_base.py:73
def get_traits(self, event_type, trait_name=None)
def record_events(self, event_models)
Definition: pymongo_base.py:46