def get_all_group_data(ctx, network_id, scenario_id, group_ids, include_metadata): """ Return all the attributes for all the groups in a given network and a given scenario. Returns a list of ResourceAttr objects, each with a resourcescenario attribute, containing the actual value for the scenario specified. Args: network_id (int): The network to search in scenario_id (int): The scenario to search group_ids (List(int)) (optional): The specific resource groups to search for data in. If not specified, all the groups in the network will be searched. include_metadata: (string) ('Y' or 'N'): Default 'N'. Set to 'Y' to return metadata. This may vause a performance hit as metadata is BIG! Returns: List(ResourceAttr), each with a resourcescenario attribute, containing the actual value for the scenario specified. """ group_resourcescenarios = network.get_attributes_for_resource( network_id, scenario_id, 'GROUP', group_ids, include_metadata) return_ras = [] for grouprs in group_resourcescenarios: ra = ResourceAttr(grouprs.resourceattr) ra.resourcescenario = ResourceScenario(grouprs, ra.attr_id) return_ras.append(ra) return return_ras
def get_all_node_attributes(ctx, network_id, template_id): """ Get all the resource attributes for all the nodes in the network. Args: network_id (int): The ID of the network that you want the node attributes from template_id (int) (optional): If this is specified, then it will only return the attributes in this template. Returns: List(hydra_complexmodels.ResourceAttr): The resource attributes of all the nodes in the network. """ resource_attrs = attributes.get_all_resource_attributes( 'NODE', network_id, template_id) return [ResourceAttr(ra) for ra in resource_attrs]
def add_group_attrs_from_type(ctx, type_id, group_id): """ Adds all the attributes defined by a type to a group. Args: type_id (int): ID of the type used to get the resource attributes from group_id (int): ID of the group Returns: List(hydra_complexmodels.ResourceAttr): All the newly created group attributes Raises: ResourceNotFoundError if the type_id or group_id are not in the DB """ new_resource_attrs = attributes.add_resource_attrs_from_type( type_id, 'GROUP', group_id, **ctx.in_header.__dict__) return [ResourceAttr(ra) for ra in new_resource_attrs]
def add_network_attrs_from_type(ctx, type_id, network_id): """ Adds all the attributes defined by a type to a network. Args: type_id (int): ID of the type used to get the resource attributes from network_id (int): ID of the network Returns: List(hydra_complexmodels.ResourceAttr): All the newly created network attributes Raises: ResourceNotFoundError if the type_id or network_id are not in the DB """ new_resource_attrs = attributes.add_resource_attrs_from_type( type_id, 'NETWORK', network_id, **ctx.in_header.__dict__) return [ResourceAttr(ra) for ra in new_resource_attrs]
def update_resource_attribute(ctx, resource_attr_id, is_var): """ Update a resource attribute (which means update the is_var flag as this is the only thing you can update on a resource attr) Args: resource_attr_id (int): ID of the complex model to be updated is_var (unicode): 'Y' or 'N' Returns: List(hydra_complexmodels.ResourceAttr): Updated ResourceAttr Raises: ResourceNotFoundError if the resource_attr_id is not in the DB """ updated_ra = attributes.update_resource_attribute( resource_attr_id, is_var, **ctx.in_header.__dict__) return ResourceAttr(updated_ra)
def get_group_attributes(ctx, group_id, type_id): """ Get all a group's attributes. Args: group_id (int): ID of the group type_id (int) (optional): ID of the type. If specified will only return the resource attributes relative to that type Returns: List(hydra_complexmodels.ResourceAttr): All the group's attributes Raises: ResourceNotFoundError if the type_id or group_id do not exist. """ resource_attrs = attributes.get_resource_attributes( 'GROUP', group_id, type_id, **ctx.in_header.__dict__) return [ResourceAttr(ra) for ra in resource_attrs]
def get_link_attributes(ctx, link_id, type_id): """ Get all a link's attributes. Args: link_id (int): ID of the link type_id (int) (optional): ID of the type. If specified will only return the resource attributes relative to that type Returns: List(hydra_complexmodels.ResourceAttr): All the link's attributes Raises: ResourceNotFoundError if the type_id or link_id do not exist. """ resource_attrs = attributes.get_resource_attributes( 'LINK', link_id, type_id) return [ResourceAttr(ra) for ra in resource_attrs]
def get_network_attributes(ctx, network_id, type_id): """ Get all a network's attributes (not the attributes of the nodes and links. just the network itself). Args: network_id (int): ID of the network type_id (int) (optional): ID of the type. If specified will only return the resource attributes relative to that type Returns: List(hydra_complexmodels.ResourceAttr): All the network attributes Raises: ResourceNotFoundError if the type_id or network_id are not in the DB """ resource_attrs = attributes.get_resource_attributes( 'NETWORK', network_id, type_id) return [ResourceAttr(ra) for ra in resource_attrs]
def get_attribute_datasets(ctx, attr_id, scenario_id): """ Get all the datasets from resource attributes with the given attribute ID in the given scenario. Return a list of resource attributes with their associated resource scenarios (and values). """ resource_attrs = scenario.get_attribute_datasests( attr_id, scenario_id, **ctx.in_header.__dict__) ra_cms = [] for ra in resource_attrs: res_attr_cm = ResourceAttr(ra) for rs in ra.resourcescenarios: if rs.scenario_id == scenario_id: res_attr_cm.resourcescenario = ResourceScenario(rs) ra_cms.append(res_attr_cm) return ra_cms
def get_node_attribute_data(ctx, node_ids, attr_ids): """ Get the data for multiple attributes on multiple nodes across multiple scenarios. @returns a list of AttributeData objects, which consist of a list of ResourceAttribute objects and a list of corresponding ResourceScenario objects. """ node_attrs, resource_scenarios = scenario.get_attribute_data( attr_ids, node_ids, **ctx.in_header.__dict__) node_ras = [ResourceAttr(na) for na in node_attrs] node_rs = [ResourceScenario(rs) for rs in resource_scenarios] ret_obj = AttributeData() ret_obj.resourceattrs = node_ras ret_obj.resourcescenarios = node_rs return ret_obj
def get_all_link_data(ctx, network_id, scenario_id, link_ids, include_metadata): """ Return all the attributes for all the links in a given network and a given scenario. Returns a list of ResourceAttr objects, each with a resourcescenario attribute, containing the actual value for the scenario specified. Args: network_id (int): The network to search in scenario_id (int): The scenario to search link_ids (List(int)) (optional): The specific links to search for data in. If not specified, all the links in the network will be searched. include_metadata: (string) ('Y' or 'N'): Default 'N'. Set to 'Y' to return metadata. This may vause a performance hit as metadata is BIG! Returns: List(ResourceAttr), each with a resourcescenario attribute, containing the actual value for the scenario specified. Raises: ResourceNotFoundError: If the network or scenario are not found """ start = datetime.datetime.now() link_resourcescenarios = network.get_attributes_for_resource( network_id, scenario_id, 'LINK', link_ids, include_metadata) log.info("Qry done in %s", (datetime.datetime.now() - start)) start = datetime.datetime.now() return_ras = [] for linkrs in link_resourcescenarios: ra = ResourceAttr(linkrs.resourceattr) ra.resourcescenario = ResourceScenario(linkrs, ra.attr_id) return_ras.append(ra) log.info("Return vals built in %s", (datetime.datetime.now() - start)) return return_ras
def add_link_attribute(ctx, link_id, attr_id, is_var): """ Add a resource attribute to a link. Args: link_id (int): The ID of the Link attr_id (int): THe ID if the attribute being added. is_var (char): Y or N. Indicates whether the attribute is a variable or not. Returns: hydra_complexmodels.ResourceAttr: The newly created link attribute Raises: ResourceNotFoundError: If the link or attribute do not exist HydraError: If this addition causes a duplicate attribute on the link. """ new_ra = attributes.add_resource_attribute('LINK', link_id, attr_id, is_var, **ctx.in_header.__dict__) return ResourceAttr(new_ra)
def add_network_attribute(ctx, network_id, attr_id, is_var): """ Add a resource attribute to a network. Args: network_id (int): ID of the network attr_id (int): ID of the attribute to assign to the network is_var (string) 'Y' or 'N'. Indicates whether this attribute is a variable or not. (a variable is typically the result of a model run, so therefore doesn't need data assigned to it initially) Returns: hydra_complexmodels.ResourceAttr: A complex model of the newly created resource attribute. Raises: ResourceNotFoundError: If the network or attribute are not in the DB. HydraError : If the attribute is already on the network. """ new_ra = attributes.add_resource_attribute('NETWORK', network_id, attr_id, is_var, **ctx.in_header.__dict__) return ResourceAttr(new_ra)
def get_resource_attribute_datasets(ctx, resource_attr_id, scenario_id): """ Get all the datasets from resource attributes with the given resource attribute IDs in the given scenarios. """ if not isinstance(resource_attr_id, list): resource_attr_id = [resource_attr_id] if not isinstance(scenario_id, list): scenario_id = [scenario_id] ra_cms = [] resource_attrs = scenario.get_resource_attribute_datasets( resource_attr_id, scenario_id, **ctx.in_header.__dict__) for ra in resource_attrs: res_attr_cm = ResourceAttr(ra) for rs in ra.resourcescenarios: if rs.scenario_id in scenario_id: res_attr_cm.resourcescenario = ResourceScenario(rs) ra_cms.append(res_attr_cm) return ra_cms