def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ assert event_object topic = self._topic(event_object) to_name = (self._send_name.exchange, topic) log.trace("Publishing event message to %s", to_name) current_time = int(get_ion_ts()) #Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) > ( current_time + VALID_EVENT_TIME_PERIOD ): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD) : raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) #Validate this object #TODO - enable this once the resource agent issue sending a dict is figured out #event_object._validate() #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest("The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise try: # store published event but only if we specified an event_repo if PERSIST_ON_PUBLISH and self.event_repo: self.event_repo.put_event(event_object) except Exception as ex: log.exception("Failed to store published event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ assert event_object topic = self._topic(event_object) to_name = (self._send_name.exchange, topic) log.trace("Publishing event message to %s", to_name) current_time = int(get_ion_ts()) #Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest( "The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) > (current_time + VALID_EVENT_TIME_PERIOD): raise BadRequest( "This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) #Validate this object #TODO - enable this once the resource agent issue sending a dict is figured out #event_object._validate() #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest( "The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise try: # store published event but only if we specified an event_repo if PERSIST_ON_PUBLISH and self.event_repo: self.event_repo.put_event(event_object) except Exception as ex: log.exception("Failed to store published event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ if not event_object: raise BadRequest("Must provide event_object") event_object.base_types = event_object._get_extends() topic = self._topic( event_object ) # Routing key generated using type_, base_types, origin, origin_type, sub_type container = (hasattr(self, '_process') and hasattr( self._process, 'container') and self._process.container ) or BaseEndpoint._get_container_instance() if container and container.has_capability( container.CCAP.EXCHANGE_MANAGER): # make sure we are an xp, if not, upgrade if not isinstance(self._send_name, XOTransport): default_nt = NameTrio(self.get_events_exchange_point()) if isinstance(self._send_name, NameTrio) \ and self._send_name.exchange == default_nt.exchange \ and self._send_name.queue == default_nt.queue \ and self._send_name.binding == default_nt.binding: self._send_name = container.create_xp(self._events_xp) else: self._send_name = container.create_xp(self._send_name) xp = self._send_name to_name = xp.create_route(topic) else: to_name = (self._send_name.exchange, topic) current_time = get_ion_ts_millis() # Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest( "The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) > (current_time + VALID_EVENT_TIME_PERIOD): raise BadRequest( "This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) # Set the actor id based on if not event_object.actor_id: event_object.actor_id = self._get_actor_id() #Validate this object - ideally the validator should pass on problems, but for now just log #any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception as e: log.exception(e) #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest( "The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ if not event_object: raise BadRequest("Must provide event_object") event_object.base_types = event_object._get_extends() topic = self._topic(event_object) # Routing key generated using type_, base_types, origin, origin_type, sub_type container = (hasattr(self, '_process') and hasattr(self._process, 'container') and self._process.container) or BaseEndpoint._get_container_instance() if container and container.has_capability(container.CCAP.EXCHANGE_MANAGER): # make sure we are an xp, if not, upgrade if not isinstance(self._send_name, XOTransport): default_nt = NameTrio(self.get_events_exchange_point()) if isinstance(self._send_name, NameTrio) \ and self._send_name.exchange == default_nt.exchange \ and self._send_name.queue == default_nt.queue \ and self._send_name.binding == default_nt.binding: self._send_name = container.create_xp(self._events_xp) else: self._send_name = container.create_xp(self._send_name) xp = self._send_name to_name = xp.create_route(topic) else: to_name = (self._send_name.exchange, topic) current_time = get_ion_ts_millis() # Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) > ( current_time + VALID_EVENT_TIME_PERIOD ): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD) : raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) # Set the actor id based on if not event_object.actor_id: event_object.actor_id = self._get_actor_id() #Validate this object - ideally the validator should pass on problems, but for now just log #any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception as e: log.exception(e) #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest("The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def test_event_persist(self): events = [{'_id': '778dcc0811bd4b518ffd1ef873f3f457', 'base_types': ['Event'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'status': 1, 'sub_type': 'input_voltage', 'time_stamps': [2.0, 2.0], 'ts_created': '1364121284585', 'type_': 'DeviceStatusEvent', 'valid_values': [-100, 100], 'values': [110.0, 111.0]}, {'_id': 'b40731684e41418082e1727f3cf61026', 'base_types': ['Event'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'status': 1, 'sub_type': 'input_voltage', 'time_stamps': [2.0, 2.0], 'ts_created': '1364121284609', 'type_': 'DeviceStatusEvent', 'valid_values': [-100, 100], 'values': [110.0, 111.0]}] dsm = DatastoreManager() ds = dsm.get_datastore(DataStore.DS_EVENTS, DataStore.DS_PROFILE.EVENTS) ds.delete_datastore() ds.create_datastore() event_repo = EventRepository(dsm) # Store one event without ID event1_dict = events[0].copy() event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1 = IonObject(event1_type, **event1_dict) event_repo.put_event(event1) events_r = event_repo.find_events(origin=event1_dict["origin"]) self.assertEquals(len(events_r), 1) event1_read = events_r[0][2] self.assertEquals(event1_read.time_stamps, event1_dict["time_stamps"]) # Store one event with ID event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event2_obj._id = event2_id event_repo.put_event(event2_obj) # Store multiple new events with ID set and unset, non-existing event1_dict = events[0].copy() event1_id = event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1_obj = IonObject(event1_type, **event1_dict) event1_obj._id = create_unique_event_id() event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event_repo.put_events([event1_obj, event2_obj]) events_r = event_repo.find_events(event_type='DeviceStatusEvent') self.assertEquals(len(events_r), 4)
def test_event_persist(self): events = [{ '_id': '778dcc0811bd4b518ffd1ef873f3f457', 'base_types': ['Event', 'ResourceEvent'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'sub_type': 'input_voltage', 'ts_created': '1364121284585', 'type_': 'ResourceLifecycleEvent' }, { '_id': 'b40731684e41418082e1727f3cf61026', 'base_types': ['Event', 'ResourceEvent'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'sub_type': 'input_voltage', 'ts_created': '1364121284609', 'type_': 'ResourceModifiedEvent' }] dsm = DatastoreManager() ds = dsm.get_datastore(DataStore.DS_EVENTS, DataStore.DS_PROFILE.EVENTS) ds.delete_datastore() ds.create_datastore() event_repo = EventRepository(dsm) # Store one event without ID event1_dict = events[0].copy() event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1 = IonObject(event1_type, **event1_dict) event_repo.put_event(event1) events_r = event_repo.find_events(origin=event1_dict["origin"]) self.assertEquals(len(events_r), 1) event1_read = events_r[0][2] # Store one event with ID event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event2_obj._id = event2_id event_repo.put_event(event2_obj) # Store multiple new events with ID set and unset, non-existing event1_dict = events[0].copy() event1_id = event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1_obj = IonObject(event1_type, **event1_dict) event1_obj._id = create_unique_event_id() event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event_repo.put_events([event1_obj, event2_obj]) events_r = event_repo.find_events(event_type='ResourceModifiedEvent') self.assertEquals(len(events_r), 2)