Skip to content

Commit

Permalink
Add commands apply for raw studies (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
pl-buiquang authored Jun 21, 2022
1 parent ef8cad9 commit 213dbc7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
4 changes: 3 additions & 1 deletion antarest/study/business/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from antarest.study.model import Study, RawStudy
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.utils import remove_from_cache
from antarest.study.storage.utils import is_managed
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
from antarest.study.storage.variantstudy.model.model import CommandDTO

Expand All @@ -25,6 +25,8 @@ def execute_or_add_commands(
raise CommandApplicationError(result.message)
executed_commands.append(command)
storage_service.variant_study_service.invalidate_cache(study)
if not is_managed(study):
file_study.tree.async_denormalize()
else:
storage_service.variant_study_service.append_commands(
study.id,
Expand Down
3 changes: 2 additions & 1 deletion antarest/study/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from antarest.study.storage.rawstudy.raw_study_service import (
RawStudyService,
)
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.business.matrix_constants_generator import (
GeneratorMatrixConstants,
)
Expand Down Expand Up @@ -143,7 +144,7 @@ def build_study_service(
)
application.include_router(
create_study_variant_routes(
variant_study_service=variant_study_service,
study_service=study_service,
config=config,
)
)
Expand Down
40 changes: 40 additions & 0 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from antarest.study.business.config_management import ConfigManager
from antarest.study.business.link_management import LinkManager, LinkInfoDTO
from antarest.study.business.matrix_management import MatrixManager
from antarest.study.business.utils import execute_or_add_commands
from antarest.study.business.xpansion_management import (
XpansionManager,
XpansionSettingsDTO,
Expand Down Expand Up @@ -139,6 +140,7 @@
UpdateRawFile,
)
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy
from antarest.study.storage.variantstudy.model.model import CommandDTO
from antarest.study.storage.variantstudy.variant_study_service import (
VariantStudyService,
)
Expand Down Expand Up @@ -1415,6 +1417,44 @@ def _edit_study_using_command(
raise NotImplementedError()
return command # for testing purpose

def apply_commands(
self, uuid: str, commands: List[CommandDTO], params: RequestParameters
) -> None:
study = self.get_study(uuid)
if isinstance(study, VariantStudy):
self.storage_service.variant_study_service.append_commands(
uuid, commands, params
)
else:
file_study = self.storage_service.raw_study_service.get_raw(study)
assert_permission(params.user, study, StudyPermissionType.WRITE)
self._assert_study_unarchived(study)
parsed_commands: List[ICommand] = []
for command in commands:
parsed_commands.extend(
self.storage_service.variant_study_service.command_factory.to_icommand(
command
)
)
execute_or_add_commands(
study,
file_study,
parsed_commands,
self.storage_service,
)
self.event_bus.push(
Event(
type=EventType.STUDY_DATA_EDITED,
payload=study.to_json_summary(),
permissions=create_permission_from_study(study),
)
)
logger.info(
"Study %s updated by user %s",
uuid,
params.get_user_id(),
)

def edit_study(
self,
uuid: str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def denormalize(self) -> None:
if self.config.path.exists() or not self.get_link_path().exists():
return

logger.info(f"Denormalizing matrix {self.config.path}")
uuid = self.get_link_path().read_text()
matrix = self.context.resolver.resolve(uuid)
if not matrix or not isinstance(matrix, dict):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
from threading import Thread

from antarest.study.storage.rawstudy.model.filesystem.folder_node import (
FolderNode,
)
Expand Down Expand Up @@ -26,6 +29,9 @@
)


logger = logging.getLogger(__name__)


class FileStudyTree(FolderNode):
"""
Top level node of antares tree structure
Expand Down Expand Up @@ -54,3 +60,11 @@ def build(self) -> TREE:
children["output"] = Output(self.context, output_config)

return children

def async_denormalize(self) -> Thread:
logger.info(
f"Denormalizing (async) study data for study {self.config.study_id}"
)
thread = Thread(target=self.denormalize)
thread.start()
return thread
3 changes: 1 addition & 2 deletions antarest/study/storage/variantstudy/variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def append_commands(
study_id: str,
commands: List[CommandDTO],
params: RequestParameters,
) -> str:
) -> None:
"""
Add command to list of commands (at the end)
Args:
Expand All @@ -245,7 +245,6 @@ def append_commands(
]
)
self.invalidate_cache(study)
return str(study.id)

def replace_commands(
self,
Expand Down
13 changes: 7 additions & 6 deletions antarest/study/web/variant_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from antarest.core.utils.web import APITag
from antarest.login.auth import Auth
from antarest.study.model import StudyMetadataDTO
from antarest.study.service import StudyService
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.model.model import (
CommandDTO,
VariantTreeDTO,
Expand All @@ -25,20 +27,21 @@


def create_study_variant_routes(
variant_study_service: VariantStudyService,
study_service: StudyService,
config: Config,
) -> APIRouter:
"""
Endpoint implementation for studies area management
Args:
variant_study_service: study service facade to handle request
study_service: study service facade to handle request
config: main server configuration
Returns:
"""
bp = APIRouter(prefix="/v1")
auth = Auth(config)
variant_study_service = study_service.storage_service.variant_study_service

@bp.post(
"/studies/{uuid}/variants",
Expand Down Expand Up @@ -159,16 +162,14 @@ def append_commands(
uuid: str,
commands: List[CommandDTO] = Body(...),
current_user: JWTUser = Depends(auth.get_current_user),
) -> str:
) -> None:
logger.info(
f"Appending new command to variant study {uuid}",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
sanitized_uuid = sanitize_uuid(uuid)
return variant_study_service.append_commands(
sanitized_uuid, commands, params
)
study_service.apply_commands(sanitized_uuid, commands, params)

@bp.put(
"/studies/{uuid}/commands",
Expand Down

0 comments on commit 213dbc7

Please sign in to comment.