Exemplo n.º 1
0
def _add_resourcegroupitem(group_item, scenario_id):
    """
        Add a single resource group item (no DB flush, as it's an internal function)
    """
    if group_item.id and group_item.id > 0:
        try:
            group_item_i = DBSession.query(ResourceGroupItem).filter(
                ResourceGroupItem.item_id == group_item.id).one()
        except NoResultFound:
            raise ResourceNotFoundError("ResourceGroupItem %s not found" %
                                        (group_item.id))

    else:
        group_item_i = ResourceGroupItem()
        group_item_i.group_id = group_item.group_id
        if scenario_id is not None:
            group_item_i.scenario_id = scenario_id

    ref_key = group_item.ref_key
    group_item_i.ref_key = ref_key
    if ref_key == 'NODE':
        group_item_i.node_id = group_item.ref_id
    elif ref_key == 'LINK':
        group_item_i.link_id = group_item.ref_id
    elif ref_key == 'GROUP':
        group_item_i.subgroup_id = group_item.ref_id
    DBSession.add(group_item_i)
    return group_item_i
Exemplo n.º 2
0
def _add_resourcegroupitem(group_item, scenario_id):
    """
        Add a single resource group item (no DB flush, as it's an internal function)
    """
    if group_item.id and group_item.id > 0:
        try:
            group_item_i = DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.item_id == group_item.id).one()
        except NoResultFound:
            raise ResourceNotFoundError("ResourceGroupItem %s not found" % (group_item.id))

    else:
        group_item_i = ResourceGroupItem()
        group_item_i.group_id = group_item.group_id
        if scenario_id is not None:
            group_item_i.scenario_id = scenario_id

    ref_key = group_item.ref_key
    group_item_i.ref_key = ref_key
    if ref_key == 'NODE':
        group_item_i.node_id =group_item.ref_id
    elif ref_key == 'LINK':
        group_item_i.link_id =group_item.ref_id
    elif ref_key == 'GROUP':
        group_item_i.subgroup_id =group_item.ref_id
    DBSession.add(group_item_i)
    return group_item_i
Exemplo n.º 3
0
def add_resourcegroupitem(group_item, scenario_id, **kwargs):

    scenario._check_can_edit_scenario(scenario_id, kwargs['user_id'])
    #Check whether the ref_id is correct.

    if group_item.ref_key == 'NODE':
        try:
            DBSession.query(Node).filter(
                Node.node_id == group_item.ref_id).one()

        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Node group item!" %
                             (group_item.ref_id))
    elif group_item.ref_key == 'LINK':
        try:
            DBSession.query(Link).filter(
                Link.link_id == group_item.ref_id).one()
        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Link group item!" %
                             (group_item.ref_id))
    elif group_item.ref_key == 'GROUP':
        try:
            DBSession.query(ResourceGroup).filter(
                ResourceGroup.group_id == group_item.ref_id).one()
        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Group group item!" %
                             (group_item.ref_id))
    else:
        raise HydraError("Invalid ref key: %s" % (group_item.ref_key))

    group_item_i = ResourceGroupItem()
    group_item_i.scenario_id = 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

    DBSession.add(group_item_i)
    DBSession.flush()

    return group_item_i
Exemplo n.º 4
0
def add_resourcegroupitem(group_item, scenario_id,**kwargs):

    scenario._check_can_edit_scenario(scenario_id, kwargs['user_id'])
    #Check whether the ref_id is correct.

    if group_item.ref_key == 'NODE':
        try:
            DBSession.query(Node).filter(Node.node_id==group_item.ref_id).one()

        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Node group item!"%(group_item.ref_id))
    elif group_item.ref_key == 'LINK':
        try:
            DBSession.query(Link).filter(Link.link_id==group_item.ref_id).one()
        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Link group item!"%(group_item.ref_id))
    elif group_item.ref_key == 'GROUP':
        try:
            DBSession.query(ResourceGroup).filter(ResourceGroup.group_id==group_item.ref_id).one()
        except NoResultFound:
            raise HydraError("Invalid ref ID %s for a Group group item!"%(group_item.ref_id))
    else:
        raise HydraError("Invalid ref key: %s"%(group_item.ref_key))

    group_item_i             = ResourceGroupItem()
    group_item_i.scenario_id = 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
        

    DBSession.add(group_item_i)
    DBSession.flush()

    return group_item_i
Exemplo n.º 5
0
def compare_scenarios(scenario_id_1, scenario_id_2, **kwargs):
    user_id = kwargs.get('user_id')

    scenario_1 = _get_scenario(scenario_id_1)
    scenario_2 = _get_scenario(scenario_id_2)

    if scenario_1.network_id != scenario_2.network_id:
        raise HydraError("Cannot compare scenarios that are not"
                         " in the same network!")

    scenariodiff = dict(object_type='ScenarioDiff')
    resource_diffs = []

    #Make a list of all the resource scenarios (aka data) that are unique
    #to scenario 1 and that are in both scenarios, but are not the same.

    #For efficiency, build a dictionary of the data in scenarios and refer
    #them rather than nesting for loops.
    r_scen_1_dict = dict()
    r_scen_2_dict = dict()
    for s1_rs in scenario_1.resourcescenarios:
        r_scen_1_dict[s1_rs.resource_attr_id] = s1_rs
    for s2_rs in scenario_2.resourcescenarios:
        r_scen_2_dict[s2_rs.resource_attr_id] = s2_rs

    rscen_1_dataset_ids = set(
        [r_scen.dataset_id for r_scen in scenario_1.resourcescenarios])
    rscen_2_dataset_ids = set(
        [r_scen.dataset_id for r_scen in scenario_2.resourcescenarios])

    log.info("Datasets In 1 not in 2: %s" %
             (rscen_1_dataset_ids - rscen_2_dataset_ids))
    log.info("Datasets In 2 not in 1: %s" %
             (rscen_2_dataset_ids - rscen_1_dataset_ids))

    for ra_id, s1_rs in r_scen_1_dict.items():
        s2_rs = r_scen_2_dict.get(ra_id)
        if s2_rs is not None:
            log.debug("Is %s == %s?" % (s1_rs.dataset_id, s2_rs.dataset_id))
            if s1_rs.dataset_id != s2_rs.dataset_id:
                resource_diff = dict(
                    resource_attr_id=s1_rs.resource_attr_id,
                    scenario_1_dataset=_get_as_obj(
                        _get_dataset_as_dict(s1_rs, user_id), 'Dataset'),
                    scenario_2_dataset=_get_as_obj(
                        _get_dataset_as_dict(s2_rs, user_id), 'Dataset'),
                )
                resource_diffs.append(resource_diff)

            continue
        else:
            resource_diff = dict(
                resource_attr_id=s1_rs.resource_attr_id,
                scenario_1_dataset=_get_as_obj(
                    _get_dataset_as_dict(s1_rs, user_id), 'Dataset'),
                scenario_2_dataset=None,
            )
            resource_diffs.append(resource_diff)

    #make a list of all the resource scenarios (aka data) that are unique
    #in scenario 2.
    for ra_id, s2_rs in r_scen_2_dict.items():
        s1_rs = r_scen_1_dict.get(ra_id)
        if s1_rs is None:
            resource_diff = dict(
                resource_attr_id=s1_rs.resource_attr_id,
                scenario_1_dataset=None,
                scenario_2_dataset=_get_as_obj(
                    _get_dataset_as_dict(s2_rs, user_id), 'Dataset'),
            )
            resource_diffs.append(resource_diff)

    scenariodiff['resourcescenarios'] = resource_diffs

    #Now compare groups.
    #Return list of group items in scenario 1 not in scenario 2 and vice versa
    s1_items = []
    for s1_item in scenario_1.resourcegroupitems:
        s1_items.append((s1_item.group_id, s1_item.ref_key, s1_item.node_id,
                         s1_item.link_id, s1_item.subgroup_id))
    s2_items = []
    for s2_item in scenario_2.resourcegroupitems:
        s2_items.append((s2_item.group_id, s2_item.ref_key, s2_item.node_id,
                         s2_item.link_id, s2_item.subgroup_id))

    groupdiff = dict()
    scenario_1_items = []
    scenario_2_items = []
    for s1_only_item in set(s1_items) - set(s2_items):

        item = ResourceGroupItem(
            group_id=s1_only_item[0],
            ref_key=s1_only_item[1],
            node_id=s1_only_item[2],
            link_id=s1_only_item[3],
            subgroup_id=s1_only_item[4],
        )
        scenario_1_items.append(item)
    for s2_only_item in set(s2_items) - set(s1_items):
        item = ResourceGroupItem(
            group_id=s2_only_item[0],
            ref_key=s2_only_item[1],
            node_id=s2_only_item[2],
            link_id=s2_only_item[3],
            subgroup_id=s2_only_item[4],
        )
        scenario_2_items.append(item)

    groupdiff['scenario_1_items'] = scenario_1_items
    groupdiff['scenario_2_items'] = scenario_2_items
    scenariodiff['groups'] = groupdiff

    return scenariodiff
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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