def set_temporal_constraint(self, deployment_obj, start_time=None, end_time=None): tc = self.get_temporal_constraint(deployment_obj) if tc is None: tc = TemporalBounds() deployment_obj.constraint_list.append(tc) if start_time == "NOW": start_time = str(int(time.time())) if end_time == "NOW": end_time = str(int(time.time())) if start_time is not None: tc.start_datetime = start_time if end_time is not None: tc.end_datetime = end_time
def calc_temp_bounds_for_temp_bounds_list(temp_bounds_list): startval_list = [ float(tb.start_datetime) for tb in temp_bounds_list if tb and tb.start_datetime ] start_min = str(int(min(startval_list))) if startval_list else "" endval_list = [ float(tb.end_datetime) for tb in temp_bounds_list if tb and tb.end_datetime ] end_max = str(int(max(endval_list))) if endval_list else "" temp_bounds = TemporalBounds(start_datetime=start_min, end_datetime=end_max) return temp_bounds
def test_multiple_deployments(self): from interface.objects import DataProductTypeEnum, TemporalBounds, Deployment, RemotePlatformDeploymentContext # First deployment starting = str(calendar.timegm(datetime(2014, 1, 1, 0).timetuple())) site_1_id, device_1_id, dataset_1_id, deployment_1_id, param_dict_a, data_product_1_id = self.create_device_site_deployment( dep_name="Site 1 - Device 1", starting=starting) site = self.resource_registry.read(site_1_id) # Create SDPs # This logis is also in preload, but testing preload is painful. # Testing it manually here for now. for i, scfg in enumerate(site.stream_configurations): pdict = self.container.resource_registry.find_resources( name=scfg.parameter_dictionary_name, restype=RT.ParameterDictionary, id_only=False)[0][0] # Clone/Create the new ParameterDictionary del pdict._id del pdict._rev sdp_pdict_id, _ = self.container.resource_registry.create(pdict) stream_def_id = self.create_stream_definition( name='CTD 1 - SDP', parameter_dictionary_id=sdp_pdict_id) sdp_id = self.create_data_product(name="SDP_%d" % i, stream_def_id=stream_def_id, stream_configuration=scfg) self.activate_data_product(sdp_id) self.container.resource_registry.create_association( subject=site_1_id, predicate=PRED.hasOutputProduct, object=sdp_id, assoc_type=RT.DataProduct) sdp = self.resource_registry.read(sdp_id) sdp.category = DataProductTypeEnum.SITE self.resource_registry.update(sdp) self.observatory_management.activate_deployment( deployment_id=deployment_1_id) self.assertEquals( deployment_1_id, self.resource_registry.find_objects(device_1_id, PRED.hasPrimaryDeployment, id_only=True)[0][0]) self.assertEquals( deployment_1_id, self.resource_registry.find_objects(site_1_id, PRED.hasPrimaryDeployment, id_only=True)[0][0]) self.assertEquals( deployment_1_id, self.resource_registry.find_objects(device_1_id, PRED.withinDeployment, id_only=True)[0][0]) self.observatory_management.deactivate_deployment( deployment_id=deployment_1_id) self.assertEquals([], self.resource_registry.find_objects( device_1_id, PRED.hasPrimaryDeployment, id_only=True)[0]) self.assertEquals([], self.resource_registry.find_objects( site_1_id, PRED.hasPrimaryDeployment, id_only=True)[0]) self.assertEquals( deployment_1_id, self.resource_registry.find_objects(device_1_id, PRED.withinDeployment, id_only=True)[0][0]) sdps, _ = self.resource_registry.find_objects(site_1_id, PRED.hasOutputProduct) for sdp in sdps: self.assertEquals(sdp.category, DataProductTypeEnum.SITE) self.assertEquals(len(sdp.dataset_windows), 1) assert sdp.dataset_windows[0].dataset_id == dataset_1_id assert sdp.dataset_windows[0].bounds.start_datetime == starting assert int( sdp.dataset_windows[0].bounds.end_datetime) - calendar.timegm( datetime.utcnow().timetuple()) < 10 # Second deployment (same site and device) starting2 = str(calendar.timegm(datetime(2014, 1, 5, 0).timetuple())) temp_bounds2 = TemporalBounds(start_datetime=starting2, end_datetime='') deployment_2 = Deployment(name="Site 1 - Device 1 - v2", type="RemotePlatform", context=RemotePlatformDeploymentContext(), constraint_list=[temp_bounds2]) deployment_2_id = self.observatory_management.create_deployment( deployment=deployment_2, site_id=site_1_id, device_id=device_1_id) self.observatory_management.activate_deployment( deployment_id=deployment_2_id) self.assertEquals( deployment_2_id, self.resource_registry.find_objects(device_1_id, PRED.hasPrimaryDeployment, id_only=True)[0][0]) self.assertEquals( deployment_2_id, self.resource_registry.find_objects(site_1_id, PRED.hasPrimaryDeployment, id_only=True)[0][0]) self.assertItemsEqual([deployment_1_id, deployment_2_id], self.resource_registry.find_objects( device_1_id, PRED.withinDeployment, id_only=True)[0]) self.observatory_management.deactivate_deployment( deployment_id=deployment_1_id) self.assertEquals([], self.resource_registry.find_objects( device_1_id, PRED.hasPrimaryDeployment, id_only=True)[0]) self.assertEquals([], self.resource_registry.find_objects( site_1_id, PRED.hasPrimaryDeployment, id_only=True)[0]) self.assertItemsEqual([deployment_1_id, deployment_2_id], self.resource_registry.find_objects( device_1_id, PRED.withinDeployment, id_only=True)[0]) sdps, _ = self.resource_registry.find_objects(site_1_id, PRED.hasOutputProduct) for sdp in sdps: self.assertEquals(sdp.category, DataProductTypeEnum.SITE) self.assertEquals(len(sdp.dataset_windows), 2) assert sdp.dataset_windows[0].dataset_id == dataset_1_id assert sdp.dataset_windows[0].bounds.start_datetime == starting assert int( sdp.dataset_windows[0].bounds.end_datetime) - calendar.timegm( datetime.utcnow().timetuple()) < 10
def create_device_site_deployment(self, dep_name="Deployment", starting=''): from interface.objects import StreamConfiguration, StreamConfigurationType, InstrumentDevice from interface.objects import InstrumentModel, PlatformAgent, InstrumentSite, TemporalBounds, Deployment from interface.objects import RemotePlatformDeploymentContext stream_conf = StreamConfiguration( stream_name="CTD 1 Parsed Stream", parameter_dictionary_name='ctd_parsed_param_dict', stream_type=StreamConfigurationType.PARSED) pdict_id = self.dataset_management.read_parameter_dictionary_by_name( name='ctd_parsed_param_dict') stream_def_id = self.create_stream_definition( name='CTD 1', parameter_dictionary_id=pdict_id) data_product_id = self.create_data_product( name="DDP_1", stream_def_id=stream_def_id, stream_configuration=stream_conf) self.activate_data_product(data_product_id) dataset_id = self.RR2.find_dataset_id_of_data_product_using_has_dataset( data_product_id) stream_def = self.resource_registry.find_objects( data_product_id, PRED.hasStreamDefinition)[0][0] param_dict = self.resource_registry.find_objects( stream_def._id, PRED.hasParameterDictionary)[0][0] # Add data to the DataProduct dataset_monitor = DatasetMonitor(dataset_id) self.addCleanup(dataset_monitor.stop) rdt = self.ph.get_rdt(stream_def._id) rdt_ = self.ph.rdt_for_data_product(data_product_id) self.assertEquals(rdt.fields, rdt_.fields) rdt['time'] = [0, 1, 2, 3] rdt['temp'] = [10, 11, 12, 13] self.ph.publish_rdt_to_data_product(data_product_id, rdt) self.assertTrue(dataset_monitor.wait()) # Create Device device = InstrumentDevice(name='Device 1') device_id = self.instrument_management.create_instrument_device(device) self.data_acquisition_management.register_instrument(device_id) self.data_acquisition_management.assign_data_product( device_id, data_product_id) # Create Model model = InstrumentModel(name='Model 1') model_id = self.instrument_management.create_instrument_model(model) self.instrument_management.assign_instrument_model_to_instrument_device( model_id, device_id) # Create AgentDefinition ad = PlatformAgent(stream_configurations=[stream_conf]) ad_id, _ = self.resource_registry.create(ad) # Create Site site = InstrumentSite(name='Site 1', stream_configurations=[stream_conf]) site_id, _ = self.resource_registry.create(site) self.resource_registry.create_association(site_id, PRED.hasModel, model_id) self.resource_registry.create_association(site_id, PRED.hasAgentDefinition, ad_id) # TemporalBounds of the Deployment temp_bounds = TemporalBounds(start_datetime=starting, end_datetime='') # Create Deployment deployment = Deployment(name=dep_name, type="RemotePlatform", context=RemotePlatformDeploymentContext(), constraint_list=[temp_bounds]) deployment_id = self.observatory_management.create_deployment( deployment=deployment, site_id=site_id, device_id=device_id) return site_id, device_id, dataset_id, deployment_id, param_dict, data_product_id
def create_notification(self, notification=None, user_id=''): """ Persists the provided NotificationRequest object for the specified Origin id. Associate the Notification resource with the user_id string. returned id is the internal id by which NotificationRequest will be identified in the data store. @param notification NotificationRequest @param user_id str @retval notification_id str @throws BadRequest if object passed has _id or _rev attribute """ if not user_id: raise BadRequest("User id not provided.") log.debug("Create notification called for user_id: %s, and notification: %s", user_id, notification) #--------------------------------------------------------------------------------------------------- # Persist Notification object as a resource if it has already not been persisted #--------------------------------------------------------------------------------------------------- # if the notification has already been registered, simply use the old id notification_id = self._notification_in_notifications(notification, self.notifications) # since the notification has not been registered yet, register it and get the id temporal_bounds = TemporalBounds() temporal_bounds.start_datetime = get_ion_ts() temporal_bounds.end_datetime = '' if not notification_id: notification.temporal_bounds = temporal_bounds notification_id, _ = self.clients.resource_registry.create(notification) self.notifications[notification_id] = notification else: log.debug("Notification object has already been created in resource registry before. No new id to be generated. notification_id: %s", notification_id) # Read the old notification already in the resource registry notification = self.clients.resource_registry.read(notification_id) # Update the temporal bounds of the old notification resource notification.temporal_bounds = temporal_bounds # Update the notification in the resource registry self.clients.resource_registry.update(notification) log.debug("The temporal bounds for this resubscribed notification object with id: %s, is: %s", notification_id,notification.temporal_bounds) # Link the user and the notification with a hasNotification association assocs= self.clients.resource_registry.find_associations(subject=user_id, predicate=PRED.hasNotification, object=notification_id, id_only=True) if assocs: log.debug("Got an already existing association: %s, between user_id: %s, and notification_id: %s", assocs,user_id,notification_id) return notification_id else: log.debug("Creating association between user_id: %s, and notification_id: %s", user_id, notification_id ) self.clients.resource_registry.create_association(user_id, PRED.hasNotification, notification_id) # read the registered notification request object because this has an _id and is more useful notification = self.clients.resource_registry.read(notification_id) # Update the user info object with the notification self.event_processor.add_notification_for_user(new_notification=notification, user_id=user_id) #------------------------------------------------------------------------------------------------------------------- # Generate an event that can be picked by a notification worker so that it can update its user_info dictionary #------------------------------------------------------------------------------------------------------------------- log.debug("(create notification) Publishing ReloadUserInfoEvent for notification_id: %s", notification_id) self.event_publisher.publish_event( event_type= "ReloadUserInfoEvent", origin="UserNotificationService", description= "A notification has been created.", notification_id = notification_id) return notification_id
def create_notification(self, notification=None, user_id=''): """ Persists the provided NotificationRequest object for the specified Origin id. Associate the Notification resource with the user_id string. returned id is the internal id by which NotificationRequest will be identified in the data store. @param notification NotificationRequest @param user_id str @retval notification_id str @throws BadRequest if object passed has _id or _rev attribute """ if not user_id: raise BadRequest("User id not provided.") log.debug( "Create notification called for user_id: %s, and notification: %s", user_id, notification) #--------------------------------------------------------------------------------------------------- # Persist Notification object as a resource if it has already not been persisted #--------------------------------------------------------------------------------------------------- notification_id = None # if the notification has already been registered, simply use the old id existing_user_notifications = self.get_user_notifications( user_info_id=user_id) if existing_user_notifications: notification_id = self._notification_in_notifications( notification, existing_user_notifications) # since the notification has not been registered yet, register it and get the id temporal_bounds = TemporalBounds() temporal_bounds.start_datetime = get_ion_ts() temporal_bounds.end_datetime = '' if not notification_id: notification.temporal_bounds = temporal_bounds notification_id, rev = self.clients.resource_registry.create( notification) else: log.debug( "Notification object has already been created in resource registry before. No new id to be generated. notification_id: %s", notification_id) # Read the old notification already in the resource registry notification = self.clients.resource_registry.read(notification_id) # Update the temporal bounds of the old notification resource notification.temporal_bounds = temporal_bounds # Update the notification in the resource registry self.clients.resource_registry.update(notification) log.debug( "The temporal bounds for this resubscribed notification object with id: %s, is: %s", notification._id, notification.temporal_bounds) # Link the user and the notification with a hasNotification association assocs = self.clients.resource_registry.find_associations( subject=user_id, predicate=PRED.hasNotification, object=notification_id, id_only=True) if assocs: log.debug( "Got an already existing association: %s, between user_id: %s, and notification_id: %s", assocs, user_id, notification_id) return notification_id else: log.debug( "Creating association between user_id: %s, and notification_id: %s", user_id, notification_id) self.clients.resource_registry.create_association( user_id, PRED.hasNotification, notification_id) # read the registered notification request object because this has an _id and is more useful notification = self.clients.resource_registry.read(notification_id) #------------------------------------------------------------------------------------------------------------------- # Generate an event that can be picked by a notification worker so that it can update its user_info dictionary #------------------------------------------------------------------------------------------------------------------- log.debug( "(create notification) Publishing ReloadUserInfoEvent for notification_id: %s", notification_id) self.event_publisher.publish_event( event_type=OT.ReloadUserInfoEvent, origin="UserNotificationService", description="A notification has been created.", notification_id=notification_id) return notification_id
def _temprng(self, t1="", t2=None): if t2 is None: t2 = t1 return TemporalBounds(start_datetime=str(t1), end_datetime=str(t2))