Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPascoli committed Oct 7, 2024
1 parent d81eda0 commit ece5f0e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
26 changes: 21 additions & 5 deletions antarest/study/business/link_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from antarest.study.storage.variantstudy.model.command.remove_link import RemoveLink
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig
from antarest.study.storage.variantstudy.model.command.update_link import UpdateLink

_ALL_LINKS_PATH = "input/links"

Expand Down Expand Up @@ -114,19 +115,34 @@ def create_link(self, study: RawStudy, link_creation_info: LinkInfoDTOType) -> L
return link_data

def update_link(self, study: RawStudy, link_creation_info: LinkInfoDTOType) -> LinkInfoDTOType:
self.check_attributes_coherence(int(study.version), link_creation_info)

file_study = self.storage_service.get_storage(study).get_raw(study)
existing_link = self.get_one_link(study, link_creation_info.area1, link_creation_info.area2)
existing_link = self.get_one_link(study, link_creation_info)

existing_link = existing_link.model_copy(update=link_creation_info.model_dump(exclude={"area1", "area2"}, exclude_none=True))
# create new object

command = UpdateLink(
area1=link_creation_info.area1,
area2=link_creation_info.area2,
parameters=existing_link.model_dump(exclude={"area1", "area2"}, exclude_none=True, by_alias=True),
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)

execute_or_add_commands(study, file_study, [command], self.storage_service)

return existing_link

def get_one_link(self, study: RawStudy, area1: str, area2: str) -> LinkInfoDTOType:
def check_attributes_coherence(self, study_version: int, link_creation_info: LinkInfoDTOType) -> None:
if study_version < 820:
if link_creation_info.filter_synthesis is not None or link_creation_info.filter_year_by_year is not None:
raise LinkValidationError("Cannot specify a filter value for study's version earlier than v8.2")

def get_one_link(self, study: RawStudy, link_creation_info: LinkInfoDTOType) -> LinkInfoDTOType:
file_study = self.storage_service.get_storage(study).get_raw(study)

area_from, area_to = sorted([area1, area2])
area_from, area_to = sorted([link_creation_info.area1, link_creation_info.area2])
try:
link_config = file_study.tree.get(["input", "links", area_from, "properties", area_to])
except KeyError:
Expand All @@ -137,8 +153,8 @@ def get_one_link(self, study: RawStudy, area1: str, area2: str) -> LinkInfoDTOTy

link_data: LinkInfoDTOType
if int(study.version) < 820:
return LinkInfoDTOBase(**link_config)
return LinkInfoDTO820(**link_config)
return LinkInfoDTOBase.model_validate(link_config)
return LinkInfoDTO820.model_validate(link_config)

def delete_link(self, study: RawStudy, area1_id: str, area2_id: str) -> None:
file_study = self.storage_service.get_storage(study).get_raw(study)
Expand Down
2 changes: 2 additions & 0 deletions antarest/study/storage/variantstudy/command_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from antarest.study.storage.variantstudy.model.command.update_comments import UpdateComments
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig
from antarest.study.storage.variantstudy.model.command.update_district import UpdateDistrict
from antarest.study.storage.variantstudy.model.command.update_link import UpdateLink
from antarest.study.storage.variantstudy.model.command.update_playlist import UpdatePlaylist
from antarest.study.storage.variantstudy.model.command.update_raw_file import UpdateRawFile
from antarest.study.storage.variantstudy.model.command.update_scenario_builder import UpdateScenarioBuilder
Expand All @@ -53,6 +54,7 @@
CommandName.CREATE_DISTRICT.value: CreateDistrict,
CommandName.REMOVE_DISTRICT.value: RemoveDistrict,
CommandName.CREATE_LINK.value: CreateLink,
CommandName.UPDATE_LINK.value: UpdateLink,
CommandName.REMOVE_LINK.value: RemoveLink,
CommandName.CREATE_BINDING_CONSTRAINT.value: CreateBindingConstraint,
CommandName.UPDATE_BINDING_CONSTRAINT.value: UpdateBindingConstraint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CommandName(Enum):
CREATE_DISTRICT = "create_district"
REMOVE_DISTRICT = "remove_district"
CREATE_LINK = "create_link"
UPDATE_LINK = "update_link"
REMOVE_LINK = "remove_link"
CREATE_BINDING_CONSTRAINT = "create_binding_constraint"
UPDATE_BINDING_CONSTRAINT = "update_binding_constraint"
Expand Down
74 changes: 74 additions & 0 deletions antarest/study/storage/variantstudy/model/command/update_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.
import typing as t

from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.variantstudy.model.command.common import CommandOutput, CommandName

from antarest.study.storage.variantstudy.model.command.icommand import ICommand, OutputTuple, MATCH_SIGNATURE_SEPARATOR
from antarest.study.storage.variantstudy.model.model import CommandDTO


class UpdateLink(ICommand):
"""
Command used to create a link between two areas.
"""

# Overloaded metadata
# ===================

command_name: CommandName = CommandName.UPDATE_LINK
version: int = 1

# Command parameters
# ==================
area1: str
area2: str
parameters: t.Optional[t.Dict[str, t.Any]] = None

def _apply_config(self, study_data: FileStudyTreeConfig) -> OutputTuple:
area_from, area_to = sorted([self.area1, self.area2])

return (
CommandOutput(
status=True,
message=f"Link between '{self.area1}' and '{self.area2}' updated",
),
{"area_from": area_from, "area_to": area_to},
)

def _apply(self, study_data: FileStudy) -> CommandOutput:
area_from, area_to = sorted([self.area1, self.area2])
study_data.tree.save(self.parameters, ["input", "links", area_from, "properties", area_to])
output, _ = self._apply_config(study_data.config)
return output

def to_dto(self) -> CommandDTO:
args = {
"area1": self.area1,
"area2": self.area2,
"parameters": self.parameters,
}
return CommandDTO(
action=CommandName.UPDATE_LINK.value,
args=args,
)

def match_signature(self) -> str:
return str(self.command_name.value + MATCH_SIGNATURE_SEPARATOR + self.area1 + MATCH_SIGNATURE_SEPARATOR + self.area2)

def _create_diff(self, other: "ICommand") -> t.List["ICommand"]:
pass

def get_inner_matrices(self) -> t.List[str]:
pass
19 changes: 19 additions & 0 deletions tests/integration/study_data_blueprint/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@

@pytest.mark.unit_test
class TestLink:

@pytest.mark.parametrize("study_type", ["raw", "variant"])
def test_link_update(self, client: TestClient, user_access_token: str, study_type: str) -> None:
client.headers = {"Authorization": f"Bearer {user_access_token}"} # type: ignore

preparer = PreparerProxy(client, user_access_token)
study_id = preparer.create_study("foo", version=820)
if study_type == "variant":
study_id = preparer.create_variant(study_id, name="Variant 1")

area1_id = preparer.create_area(study_id, name="Area 1")["id"]
area2_id = preparer.create_area(study_id, name="Area 2")["id"]
client.post(f"/v1/studies/{study_id}/links", json={"area1": area1_id, "area2": area2_id, "hurdles-cost": True})
res = client.put(f"/v1/studies/{study_id}/links", json={"area1": area1_id, "area2": area2_id, "hurdles-cost": False, "colorr": 150, "filter-synthesis": "hourly"})

assert res.status_code == 200
expected = {'area1': 'area 1', 'area2': 'area 2', 'asset-type': 'ac', 'colorb': 112, 'colorg': 112, 'colorr': 150, 'display-comments': True, 'filter-synthesis': 'hourly', 'filter-year-by-year': 'hourly, daily, weekly, monthly, annual', 'hurdles-cost': False, 'link-style': 'plain', 'link-width': 1.0, 'loop-flow': False, 'transmission-capacities': 'enabled', 'use-phase-shifter': False}
assert expected == res.json()

@pytest.mark.parametrize("study_type", ["raw", "variant"])
def test_link_820(self, client: TestClient, user_access_token: str, study_type: str) -> None:
client.headers = {"Authorization": f"Bearer {user_access_token}"} # type: ignore
Expand Down

0 comments on commit ece5f0e

Please sign in to comment.