13"""Common functions for MongoDB backend
15from oslo_log
import log
21from panko
import utils
23LOG = log.getLogger(__name__)
26COMMON_AVAILABLE_CAPABILITIES = {
27 'events': {
'query': {
'simple':
True}},
31AVAILABLE_STORAGE_CAPABILITIES = {
32 'storage': {
'production_ready':
True},
37 """Base event Connection class for MongoDB driver."""
38 CAPABILITIES = utils.update_nested(base.Connection.CAPABILITIES,
39 COMMON_AVAILABLE_CAPABILITIES)
41 STORAGE_CAPABILITIES = utils.update_nested(
42 base.Connection.STORAGE_CAPABILITIES,
43 AVAILABLE_STORAGE_CAPABILITIES,
47 """Write the events to database.
52 for event_model
in event_models:
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})
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)
74 """Return an iter of models.Event objects.
77 for events that are stored
in database.
78 :param pagination: Pagination parameters.
82 if pagination.get(
'sort'):
83 LOG.warning(
'Driver does not support sort functionality')
84 limit = pagination.get(
'limit')
87 q = pymongo_utils.make_events_query_from_filter(event_filter)
89 results = self.db.event.find(q, limit=limit)
91 results = self.db.event.find(q)
94 for trait
in event[
'traits']:
96 dtype=int(trait[
'trait_type']),
97 value=trait[
'trait_value']))
99 event_type=event[
'event_type'],
100 generated=event[
'timestamp'],
101 traits=traits, raw=event.get(
'raw'))
104 """Return all event types as an iter of strings."""
105 return self.db.event.distinct(
'event_type')
108 """Return a dictionary containing the name and data type of the trait.
110 Only trait types for the provided event_type are returned.
112 :param event_type: the type of the Event.
115 events = self.db.event.find({'event_type': event_type})
118 for trait
in event[
'traits']:
119 trait_name = trait[
'trait_name']
120 if trait_name
not in trait_names:
125 trait_names.add(trait_name)
126 yield {
'name': trait_name,
127 'data_type': trait[
'trait_type']}
130 """Return all trait instances associated with an event_type.
132 If trait_type is specified, only
return instances of that trait type.
134 :param event_type: the type of the Event to filter by
135 :param trait_name: the name of the Trait to filter by
138 events = self.db.event.find({
'event_type': event_type})
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}}
148 for trait
in event[
'traits']:
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)
def get_traits(self, event_type, trait_name=None)
def record_events(self, event_models)