def add_data_to_attribute(scenario_id, resource_attr_id, dataset, **kwargs): """ Add data to a resource scenario outside of a network update """ user_id = kwargs.get('user_id') _check_can_edit_scenario(scenario_id, user_id) scenario_i = _get_scenario(scenario_id, False, False) try: r_scen_i = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id == scenario_id, ResourceScenario.resource_attr_id == resource_attr_id).one() log.info("Existing resource scenario found for %s in scenario %s", resource_attr_id, scenario_id) except NoResultFound: log.info( "No existing resource scenarios found for %s in scenario %s. Adding a new one.", resource_attr_id, scenario_id) r_scen_i = ResourceScenario() r_scen_i.scenario_id = scenario_id r_scen_i.resource_attr_id = resource_attr_id scenario_i.resourcescenarios.append(r_scen_i) data_type = dataset.type.lower() start_time = None frequency = None value = dataset.parse_value() dataset_metadata = dataset.get_metadata_as_dict( user_id=kwargs.get('user_id'), source=kwargs.get('source')) if value is None: raise HydraError( "Cannot set value to attribute. " "No value was sent with dataset %s", dataset.id) data_hash = dataset.get_hash(value, dataset_metadata) assign_value(r_scen_i, data_type, value, dataset.unit, dataset.name, dataset.dimension, metadata=dataset_metadata, data_hash=data_hash, user_id=user_id) DBSession.flush() return r_scen_i
def add_data_to_attribute(scenario_id, resource_attr_id, dataset,**kwargs): """ Add data to a resource scenario outside of a network update """ user_id = kwargs.get('user_id') _check_can_edit_scenario(scenario_id, user_id) scenario_i = _get_scenario(scenario_id, False, False) try: r_scen_i = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id==scenario_id, ResourceScenario.resource_attr_id==resource_attr_id).one() log.info("Existing resource scenario found for %s in scenario %s", resource_attr_id, scenario_id) except NoResultFound: log.info("No existing resource scenarios found for %s in scenario %s. Adding a new one.", resource_attr_id, scenario_id) r_scen_i = ResourceScenario() r_scen_i.scenario_id = scenario_id r_scen_i.resource_attr_id = resource_attr_id scenario_i.resourcescenarios.append(r_scen_i) data_type = dataset.type.lower() start_time = None frequency = None value = dataset.parse_value() dataset_metadata = dataset.get_metadata_as_dict(user_id=kwargs.get('user_id'), source=kwargs.get('source')) if value is None: raise HydraError("Cannot set value to attribute. " "No value was sent with dataset %s", dataset.id) data_hash = dataset.get_hash(value, dataset_metadata) assign_value(r_scen_i, data_type, value, dataset.unit, dataset.name, dataset.dimension, metadata=dataset_metadata, data_hash=data_hash, user_id=user_id) DBSession.flush() return r_scen_i
def copy_data_from_scenario(resource_attrs, source_scenario_id, target_scenario_id, **kwargs): """ For a given list of resource attribute IDS copy the dataset_ids from the resource scenarios in the source scenario to those in the 'target' scenario. """ #Get all the resource scenarios we wish to update target_resourcescenarios = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id == target_scenario_id, ResourceScenario.resource_attr_id.in_(resource_attrs)).all() target_rs_dict = {} for target_rs in target_resourcescenarios: target_rs_dict[target_rs.resource_attr_id] = target_rs #get all the resource scenarios we are using to get our datsets source. source_resourcescenarios = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id == source_scenario_id, ResourceScenario.resource_attr_id.in_(resource_attrs)).all() #If there is an RS in scenario 'source' but not in 'target', then create #a new one in 'target' for source_rs in source_resourcescenarios: target_rs = target_rs_dict.get(source_rs.resource_attr_id) if target_rs is not None: target_rs.dataset_id = source_rs.dataset_id else: target_rs = ResourceScenario() target_rs.scenario_id = target_scenario_id target_rs.dataset_id = source_rs.dataset_id target_rs.resource_attr_id = source_rs.resource_attr_id DBSession.add(target_rs) DBSession.flush() return target_resourcescenarios
def copy_data_from_scenario(resource_attrs, source_scenario_id, target_scenario_id, **kwargs): """ For a given list of resource attribute IDS copy the dataset_ids from the resource scenarios in the source scenario to those in the 'target' scenario. """ #Get all the resource scenarios we wish to update target_resourcescenarios = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id==target_scenario_id, ResourceScenario.resource_attr_id.in_(resource_attrs)).all() target_rs_dict = {} for target_rs in target_resourcescenarios: target_rs_dict[target_rs.resource_attr_id] = target_rs #get all the resource scenarios we are using to get our datsets source. source_resourcescenarios = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id==source_scenario_id, ResourceScenario.resource_attr_id.in_(resource_attrs)).all() #If there is an RS in scenario 'source' but not in 'target', then create #a new one in 'target' for source_rs in source_resourcescenarios: target_rs = target_rs_dict.get(source_rs.resource_attr_id) if target_rs is not None: target_rs.dataset_id = source_rs.dataset_id else: target_rs = ResourceScenario() target_rs.scenario_id = target_scenario_id target_rs.dataset_id = source_rs.dataset_id target_rs.resource_attr_id = source_rs.resource_attr_id DBSession.add(target_rs) DBSession.flush() return target_resourcescenarios
def _update_resourcescenario(scenario, resource_scenario, dataset=None, new=False, user_id=None, source=None): """ Insert or Update the value of a resource's attribute by first getting the resource, then parsing the input data, then assigning the value. returns a ResourceScenario object. """ if scenario is None: scenario = DBSession.query(Scenario).filter( Scenario.scenario_id == 1).one() ra_id = resource_scenario.resource_attr_id log.debug("Assigning resource attribute: %s", ra_id) try: r_scen_i = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id == scenario.scenario_id, ResourceScenario.resource_attr_id == ra_id).one() except NoResultFound as e: r_scen_i = ResourceScenario() r_scen_i.resource_attr_id = resource_scenario.resource_attr_id r_scen_i.scenario_id = scenario.scenario_id DBSession.add(r_scen_i) if scenario.locked == 'Y': log.info("Scenario %s is locked", scenario.scenario_id) return r_scen_i if dataset is not None: r_scen_i.dataset = dataset return r_scen_i dataset = resource_scenario.value start_time = None frequency = None value = dataset.parse_value() log.info("Assigning %s to resource attribute: %s", value, ra_id) if value is None: log.info("Cannot set data on resource attribute %s", ra_id) return None metadata = dataset.get_metadata_as_dict(source=source, user_id=user_id) dimension = dataset.dimension data_unit = dataset.unit # Assign dimension if necessary # It happens that dimension is and empty string. We set it to # None to achieve consistency in the DB. if data_unit is not None and dimension is None or \ data_unit is not None and len(dimension) == 0: dimension = hydra_units.get_unit_dimension(data_unit) else: if dimension is None or len(dimension) == 0: dimension = None data_hash = dataset.get_hash(value, metadata) assign_value(r_scen_i, dataset.type.lower(), value, data_unit, dataset.name, dataset.dimension, metadata=metadata, data_hash=data_hash, user_id=user_id, source=source) return r_scen_i
def clone_scenario(scenario_id, **kwargs): scen_i = _get_scenario(scenario_id) log.info("cloning scenario %s", scen_i.scenario_name) cloned_name = "%s (clone)" % (scen_i.scenario_name) existing_scenarios = DBSession.query(Scenario).filter( Scenario.network_id == scen_i.network_id).all() num_cloned_scenarios = 0 for existing_sceanrio in existing_scenarios: if existing_sceanrio.scenario_name.find('clone') >= 0: num_cloned_scenarios = num_cloned_scenarios + 1 if num_cloned_scenarios > 0: cloned_name = cloned_name + " %s" % (num_cloned_scenarios) log.info("Cloned scenario name is %s", cloned_name) cloned_scen = Scenario() cloned_scen.network_id = scen_i.network_id cloned_scen.scenario_name = cloned_name cloned_scen.scenario_description = scen_i.scenario_description cloned_scen.created_by = kwargs['user_id'] cloned_scen.start_time = scen_i.start_time cloned_scen.end_time = scen_i.end_time cloned_scen.time_step = scen_i.time_step log.info("New scenario created") for rs in scen_i.resourcescenarios: new_rs = ResourceScenario() new_rs.resource_attr_id = rs.resource_attr_id new_rs.dataset_id = rs.dataset_id if kwargs.get('app_name') is None: new_rs.source = rs.source else: new_rs.source = kwargs['app_name'] cloned_scen.resourcescenarios.append(new_rs) log.info("ResourceScenarios cloned") for resourcegroupitem_i in scen_i.resourcegroupitems: new_resourcegroupitem_i = ResourceGroupItem() new_resourcegroupitem_i.ref_key = resourcegroupitem_i.ref_key new_resourcegroupitem_i.link_id = resourcegroupitem_i.link_id new_resourcegroupitem_i.node_id = resourcegroupitem_i.node_id new_resourcegroupitem_i.subgroup_id = resourcegroupitem_i.subgroup_id new_resourcegroupitem_i.group_id = resourcegroupitem_i.group_id cloned_scen.resourcegroupitems.append(new_resourcegroupitem_i) log.info("Resource group items cloned.") DBSession.add(cloned_scen) DBSession.flush() log.info("Cloning finished.") return cloned_scen
def add_scenario(network_id, scenario, **kwargs): """ Add a scenario to a specified network. """ user_id = int(kwargs.get('user_id')) log.info("Adding scenarios to network") _check_network_ownership(network_id, user_id) existing_scen = DBSession.query(Scenario).filter( Scenario.scenario_name == scenario.name, Scenario.network_id == network_id).first() if existing_scen is not None: raise HydraError("Scenario with name %s already exists in network %s" % (scenario.name, network_id)) scen = Scenario() scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.network_id = network_id scen.created_by = user_id scen.start_time = str(timestamp_to_ordinal( scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal( scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step #Just in case someone puts in a negative ID for the scenario. if scenario.id < 0: scenario.id = None if scenario.resourcescenarios is not None: #extract the data from each resourcescenario so it can all be #inserted in one go, rather than one at a time all_data = [r.value for r in scenario.resourcescenarios] datasets = data._bulk_insert_data(all_data, user_id=user_id) #record all the resource attribute ids resource_attr_ids = [ r.resource_attr_id for r in scenario.resourcescenarios ] #get all the resource scenarios into a list and bulk insert them for i, ra_id in enumerate(resource_attr_ids): rs_i = ResourceScenario() rs_i.resource_attr_id = ra_id rs_i.dataset_id = datasets[i].dataset_id rs_i.scenario_id = scen.scenario_id rs_i.dataset = datasets[i] scen.resourcescenarios.append(rs_i) if scenario.resourcegroupitems is not None: #Again doing bulk insert. for group_item in scenario.resourcegroupitems: group_item_i = ResourceGroupItem() group_item_i.scenario_id = scen.scenario_id group_item_i.group_id = group_item.group_id group_item_i.ref_key = group_item.ref_key if group_item.ref_key == 'NODE': group_item_i.node_id = group_item.ref_id elif group_item.ref_key == 'LINK': group_item_i.link_id = group_item.ref_id elif group_item.ref_key == 'GROUP': group_item_i.subgroup_id = group_item.ref_id scen.resourcegroupitems.append(group_item_i) DBSession.add(scen) DBSession.flush() return scen
def _update_resourcescenario(scenario, resource_scenario, dataset=None, new=False, user_id=None, source=None): """ Insert or Update the value of a resource's attribute by first getting the resource, then parsing the input data, then assigning the value. returns a ResourceScenario object. """ if scenario is None: scenario = DBSession.query(Scenario).filter(Scenario.scenario_id==1).one() ra_id = resource_scenario.resource_attr_id log.debug("Assigning resource attribute: %s",ra_id) try: r_scen_i = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id==scenario.scenario_id, ResourceScenario.resource_attr_id==ra_id).one() except NoResultFound as e: r_scen_i = ResourceScenario() r_scen_i.resource_attr_id = resource_scenario.resource_attr_id r_scen_i.scenario_id = scenario.scenario_id DBSession.add(r_scen_i) if scenario.locked == 'Y': log.info("Scenario %s is locked",scenario.scenario_id) return r_scen_i if dataset is not None: r_scen_i.dataset = dataset return r_scen_i dataset = resource_scenario.value start_time = None frequency = None value = dataset.parse_value() log.info("Assigning %s to resource attribute: %s", value, ra_id) if value is None: log.info("Cannot set data on resource attribute %s",ra_id) return None metadata = dataset.get_metadata_as_dict(source=source, user_id=user_id) dimension = dataset.dimension data_unit = dataset.unit # Assign dimension if necessary # It happens that dimension is and empty string. We set it to # None to achieve consistency in the DB. if data_unit is not None and dimension is None or \ data_unit is not None and len(dimension) == 0: dimension = hydra_units.get_unit_dimension(data_unit) else: if dimension is None or len(dimension) == 0: dimension = None data_hash = dataset.get_hash(value, metadata) assign_value(r_scen_i, dataset.type.lower(), value, data_unit, dataset.name, dataset.dimension, metadata=metadata, data_hash=data_hash, user_id=user_id, source=source) return r_scen_i
def clone_scenario(scenario_id,**kwargs): scen_i = _get_scenario(scenario_id) log.info("cloning scenario %s", scen_i.scenario_name) cloned_name = "%s (clone)"%(scen_i.scenario_name) existing_scenarios = DBSession.query(Scenario).filter(Scenario.network_id==scen_i.network_id).all() num_cloned_scenarios = 0 for existing_sceanrio in existing_scenarios: if existing_sceanrio.scenario_name.find('clone') >= 0: num_cloned_scenarios = num_cloned_scenarios + 1 if num_cloned_scenarios > 0: cloned_name = cloned_name + " %s"%(num_cloned_scenarios) log.info("Cloned scenario name is %s", cloned_name) cloned_scen = Scenario() cloned_scen.network_id = scen_i.network_id cloned_scen.scenario_name = cloned_name cloned_scen.scenario_description = scen_i.scenario_description cloned_scen.created_by = kwargs['user_id'] cloned_scen.start_time = scen_i.start_time cloned_scen.end_time = scen_i.end_time cloned_scen.time_step = scen_i.time_step log.info("New scenario created") for rs in scen_i.resourcescenarios: new_rs = ResourceScenario() new_rs.resource_attr_id = rs.resource_attr_id new_rs.dataset_id = rs.dataset_id if kwargs.get('app_name') is None: new_rs.source = rs.source else: new_rs.source = kwargs['app_name'] cloned_scen.resourcescenarios.append(new_rs) log.info("ResourceScenarios cloned") for resourcegroupitem_i in scen_i.resourcegroupitems: new_resourcegroupitem_i = ResourceGroupItem() new_resourcegroupitem_i.ref_key = resourcegroupitem_i.ref_key new_resourcegroupitem_i.link_id = resourcegroupitem_i.link_id new_resourcegroupitem_i.node_id = resourcegroupitem_i.node_id new_resourcegroupitem_i.subgroup_id = resourcegroupitem_i.subgroup_id new_resourcegroupitem_i.group_id = resourcegroupitem_i.group_id cloned_scen.resourcegroupitems.append(new_resourcegroupitem_i) log.info("Resource group items cloned.") DBSession.add(cloned_scen) DBSession.flush() log.info("Cloning finished.") return cloned_scen
def add_scenario(network_id, scenario,**kwargs): """ Add a scenario to a specified network. """ user_id = int(kwargs.get('user_id')) log.info("Adding scenarios to network") _check_network_ownership(network_id, user_id) existing_scen = DBSession.query(Scenario).filter(Scenario.scenario_name==scenario.name, Scenario.network_id==network_id).first() if existing_scen is not None: raise HydraError("Scenario with name %s already exists in network %s"%(scenario.name, network_id)) scen = Scenario() scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.network_id = network_id scen.created_by = user_id scen.start_time = str(timestamp_to_ordinal(scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal(scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step #Just in case someone puts in a negative ID for the scenario. if scenario.id < 0: scenario.id = None if scenario.resourcescenarios is not None: #extract the data from each resourcescenario so it can all be #inserted in one go, rather than one at a time all_data = [r.value for r in scenario.resourcescenarios] datasets = data._bulk_insert_data(all_data, user_id=user_id) #record all the resource attribute ids resource_attr_ids = [r.resource_attr_id for r in scenario.resourcescenarios] #get all the resource scenarios into a list and bulk insert them for i, ra_id in enumerate(resource_attr_ids): rs_i = ResourceScenario() rs_i.resource_attr_id = ra_id rs_i.dataset_id = datasets[i].dataset_id rs_i.scenario_id = scen.scenario_id rs_i.dataset = datasets[i] scen.resourcescenarios.append(rs_i) if scenario.resourcegroupitems is not None: #Again doing bulk insert. for group_item in scenario.resourcegroupitems: group_item_i = ResourceGroupItem() group_item_i.scenario_id = scen.scenario_id group_item_i.group_id = group_item.group_id group_item_i.ref_key = group_item.ref_key if group_item.ref_key == 'NODE': group_item_i.node_id = group_item.ref_id elif group_item.ref_key == 'LINK': group_item_i.link_id = group_item.ref_id elif group_item.ref_key == 'GROUP': group_item_i.subgroup_id = group_item.ref_id scen.resourcegroupitems.append(group_item_i) DBSession.add(scen) DBSession.flush() return scen