Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(area): move area command creation #2322

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 10 additions & 65 deletions antarest/study/business/area_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.model.command.create_area import CreateArea
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
from antarest.study.storage.variantstudy.model.command.move_area import MoveArea
from antarest.study.storage.variantstudy.model.command.remove_area import RemoveArea
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig

Expand Down Expand Up @@ -680,73 +681,17 @@ def update_area_metadata(
)

def update_area_ui(self, study: Study, area_id: str, area_ui: UpdateAreaUi, layer: str = "0") -> None:
obj = {
"x": area_ui.x,
"y": area_ui.y,
"color_r": area_ui.color_rgb[0],
"color_g": area_ui.color_rgb[1],
"color_b": area_ui.color_rgb[2],
}
file_study = self.storage_service.get_storage(study).get_raw(study)
commands = (
[
UpdateConfig(
target=f"input/areas/{area_id}/ui/ui/x",
data=obj["x"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/ui/y",
data=obj["y"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/ui/color_r",
data=obj["color_r"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/ui/color_g",
data=obj["color_g"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/ui/color_b",
data=obj["color_b"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
]
if layer == "0"
else []
)
commands.extend(
[
UpdateConfig(
target=f"input/areas/{area_id}/ui/layerX/{layer}",
data=obj["x"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/layerY/{layer}",
data=obj["y"],
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
UpdateConfig(
target=f"input/areas/{area_id}/ui/layerColor/{layer}",
data=f"{obj['color_r']},{obj['color_g']},{obj['color_b']}",
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
),
]

command = MoveArea(
area_id=area_id,
new_area_parameters=area_ui.model_dump(),
layer=layer,
command_context=self.storage_service.variant_study_service.command_factory.command_context,
study_version=file_study.config.version,
)
execute_or_add_commands(study, file_study, commands, self.storage_service)

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

def update_thermal_cluster_metadata(
self,
Expand Down
18 changes: 10 additions & 8 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def __init__(
self.event_bus = event_bus
self.file_transfer_manager = file_transfer_manager
self.task_service = task_service
self.areas = AreaManager(self.storage_service, self.repository)
self.area_manager = AreaManager(self.storage_service, self.repository)
self.district_manager = DistrictManager(self.storage_service)
self.links_manager = LinkManager(self.storage_service)
self.config_manager = ConfigManager(self.storage_service)
Expand All @@ -426,7 +426,7 @@ def __init__(
self.binding_constraint_manager = BindingConstraintManager(self.storage_service)
self.correlation_manager = CorrelationManager(self.storage_service)
self.table_mode_manager = TableModeManager(
self.areas,
self.area_manager,
self.links_manager,
self.thermal_manager,
self.renewable_manager,
Expand Down Expand Up @@ -1899,7 +1899,9 @@ def get_all_areas(
) -> t.Union[t.List[AreaInfoDTO], t.Dict[str, t.Any]]:
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.READ)
return self.areas.get_all_areas_ui_info(study) if ui else self.areas.get_all_areas(study, area_type)
return (
self.area_manager.get_all_areas_ui_info(study) if ui else self.area_manager.get_all_areas(study, area_type)
)

def get_all_links(
self,
Expand All @@ -1919,7 +1921,7 @@ def create_area(
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
new_area = self.areas.create_area(study, area_creation_dto)
new_area = self.area_manager.create_area(study, area_creation_dto)
self.event_bus.push(
Event(
type=EventType.STUDY_DATA_EDITED,
Expand Down Expand Up @@ -1979,7 +1981,7 @@ def update_area(
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
updated_area = self.areas.update_area_metadata(study, area_id, area_patch_dto)
updated_area = self.area_manager.update_area_metadata(study, area_id, area_patch_dto)
self.event_bus.push(
Event(
type=EventType.STUDY_DATA_EDITED,
Expand All @@ -2000,7 +2002,7 @@ def update_area_ui(
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
return self.areas.update_area_ui(study, area_id, area_ui, layer)
return self.area_manager.update_area_ui(study, area_id, area_ui, layer)

def update_thermal_cluster_metadata(
self,
Expand All @@ -2012,7 +2014,7 @@ def update_thermal_cluster_metadata(
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
return self.areas.update_thermal_cluster_metadata(study, area_id, clusters_metadata)
return self.area_manager.update_thermal_cluster_metadata(study, area_id, clusters_metadata)

def delete_area(self, uuid: str, area_id: str, params: RequestParameters) -> None:
"""
Expand All @@ -2036,7 +2038,7 @@ def delete_area(self, uuid: str, area_id: str, params: RequestParameters) -> Non
if referencing_binding_constraints:
binding_ids = [bc.id for bc in referencing_binding_constraints]
raise ReferencedObjectDeletionNotAllowed(area_id, binding_ids, object_type="Area")
self.areas.delete_area(study, area_id)
self.area_manager.delete_area(study, area_id)
self.event_bus.push(
Event(
type=EventType.STUDY_DATA_EDITED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
GenerateThermalClusterTimeSeries,
)
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
from antarest.study.storage.variantstudy.model.command.move_area import MoveArea
from antarest.study.storage.variantstudy.model.command.remove_area import RemoveArea
from antarest.study.storage.variantstudy.model.command.remove_binding_constraint import RemoveBindingConstraint
from antarest.study.storage.variantstudy.model.command.remove_cluster import RemoveCluster
Expand Down Expand Up @@ -72,6 +73,10 @@ def _revert_create_area(base_command: CreateArea, history: t.List["ICommand"], b
def _revert_remove_area(base_command: RemoveArea, history: t.List["ICommand"], base: FileStudy) -> t.List[ICommand]:
raise NotImplementedError("The revert function for RemoveArea is not available")

@staticmethod
def _revert_move_area(base_command: MoveArea, history: t.List["ICommand"], base: FileStudy) -> t.List[ICommand]:
raise NotImplementedError("The revert function for MoveArea is not available")

@staticmethod
def _revert_create_district(
base_command: CreateDistrict,
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 @@ -32,6 +32,7 @@
GenerateThermalClusterTimeSeries,
)
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
from antarest.study.storage.variantstudy.model.command.move_area import MoveArea
from antarest.study.storage.variantstudy.model.command.remove_area import RemoveArea
from antarest.study.storage.variantstudy.model.command.remove_binding_constraint import RemoveBindingConstraint
from antarest.study.storage.variantstudy.model.command.remove_cluster import RemoveCluster
Expand All @@ -57,6 +58,7 @@

COMMAND_MAPPING = {
CommandName.CREATE_AREA.value: CreateArea,
CommandName.MOVE_AREA.value: MoveArea,
CommandName.REMOVE_AREA.value: RemoveArea,
CommandName.CREATE_DISTRICT.value: CreateDistrict,
CommandName.REMOVE_DISTRICT.value: RemoveDistrict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class FilteringOptions:

class CommandName(Enum):
CREATE_AREA = "create_area"
MOVE_AREA = "move_area"
REMOVE_AREA = "remove_area"
CREATE_DISTRICT = "create_district"
REMOVE_DISTRICT = "remove_district"
Expand Down
96 changes: 96 additions & 0 deletions antarest/study/storage/variantstudy/model/command/move_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright (c) 2025, 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 typing_extensions import override

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 CommandName, CommandOutput
from antarest.study.storage.variantstudy.model.command.icommand import MATCH_SIGNATURE_SEPARATOR, ICommand
from antarest.study.storage.variantstudy.model.command_listener.command_listener import ICommandListener
from antarest.study.storage.variantstudy.model.model import CommandDTO


class MoveArea(ICommand):
"""
Command used to move an area in the study.
TheoPascoli marked this conversation as resolved.
Show resolved Hide resolved
"""

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

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

# Command parameters
# ==================

area_id: str
new_area_parameters: t.Dict[str, t.Any]
TheoPascoli marked this conversation as resolved.
Show resolved Hide resolved
layer: str

@override
def _apply_config(self, study_data: FileStudyTreeConfig) -> t.Tuple[CommandOutput, t.Dict[str, t.Any]]:
return (
TheoPascoli marked this conversation as resolved.
Show resolved Hide resolved
CommandOutput(
status=True,
message=f"area '{self.area_id}' updated",
),
{},
)

@override
def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] = None) -> CommandOutput:
current_area = study_data.tree.get(["input", "areas", self.area_id, "ui"])

if self.layer == "0":
ui = current_area["ui"]
ui["x"] = self.new_area_parameters["x"]
ui["y"] = self.new_area_parameters["y"]
ui["color_r"], ui["color_g"], ui["color_b"] = self.new_area_parameters["color_rgb"]
current_area["layerX"][self.layer] = self.new_area_parameters["x"]
current_area["layerY"][self.layer] = self.new_area_parameters["y"]
current_area["layerColor"][self.layer] = ",".join(map(str, self.new_area_parameters["color_rgb"]))

study_data.tree.save(current_area, ["input", "areas", self.area_id, "ui"])

output, _ = self._apply_config(study_data.config)

return output

@override
def to_dto(self) -> CommandDTO:
return CommandDTO(
action=CommandName.MOVE_AREA.value,
args={"area_id": self.area_id, "new_area_parameters": self.new_area_parameters, "layer": self.layer},
study_version=self.study_version,
)

@override
def match_signature(self) -> str:
return str(self.command_name.value + MATCH_SIGNATURE_SEPARATOR + self.area_id)

@override
def match(self, other: ICommand, equal: bool = False) -> bool:
if not isinstance(other, MoveArea):
return False
return self.area_id == other.area_id

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

@override
def get_inner_matrices(self) -> t.List[str]:
return []
10 changes: 5 additions & 5 deletions antarest/study/web/study_data_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def get_layers(
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(uuid, StudyPermissionType.READ, params)
return study_service.areas.get_layers(study)
return study_service.area_manager.get_layers(study)

@bp.post(
"/studies/{uuid}/layers",
Expand All @@ -342,7 +342,7 @@ def create_layer(
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(uuid, StudyPermissionType.WRITE, params)
return study_service.areas.create_layer(study, name)
return study_service.area_manager.create_layer(study, name)

@bp.put(
"/studies/{uuid}/layers/{layer_id}",
Expand All @@ -363,9 +363,9 @@ def update_layer(
params = RequestParameters(user=current_user)
study = study_service.check_study_access(uuid, StudyPermissionType.WRITE, params)
if name:
study_service.areas.update_layer_name(study, layer_id, name)
study_service.area_manager.update_layer_name(study, layer_id, name)
if areas:
study_service.areas.update_layer_areas(study, layer_id, areas)
study_service.area_manager.update_layer_areas(study, layer_id, areas)

@bp.delete(
"/studies/{uuid}/layers/{layer_id}",
Expand All @@ -385,7 +385,7 @@ def remove_layer(
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(uuid, StudyPermissionType.READ, params)
study_service.areas.remove_layer(study, layer_id)
study_service.area_manager.remove_layer(study, layer_id)

@bp.get(
"/studies/{uuid}/districts",
Expand Down
46 changes: 12 additions & 34 deletions tests/storage/business/test_arealink_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,41 +183,19 @@ def test_area_crud(empty_study: FileStudy, matrix_service: SimpleMatrixService):
[
CommandDTO(
id=None,
action=CommandName.UPDATE_CONFIG.value,
args=[
{
"target": "input/areas/test/ui/ui/x",
"data": 100,
},
{
"target": "input/areas/test/ui/ui/y",
"data": 200,
},
{
"target": "input/areas/test/ui/ui/color_r",
"data": 255,
},
{
"target": "input/areas/test/ui/ui/color_g",
"data": 0,
},
{
"target": "input/areas/test/ui/ui/color_b",
"data": 100,
},
{
"target": "input/areas/test/ui/layerX/0",
"data": 100,
},
{
"target": "input/areas/test/ui/layerY/0",
"data": 200,
},
{
"target": "input/areas/test/ui/layerColor/0",
"data": "255,0,100",
action=CommandName.MOVE_AREA.value,
args={
"area_id": "test",
"new_area_parameters": {
"x": 100,
"y": 200,
"color_rgb": (255, 0, 100),
"layer_x": {},
"layer_y": {},
"layer_color": {},
},
],
"layer": "0",
},
study_version=study_version,
),
],
Expand Down
Loading
Loading