Skip to content

Commit

Permalink
v2.16.5
Browse files Browse the repository at this point in the history
Merge pull request #1948 from AntaresSimulatorTeam/hotfix/v2.16.5
  • Loading branch information
skamril authored Feb 28, 2024
2 parents 7e69104 + 3db8583 commit b2ebae8
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 97 deletions.
4 changes: 2 additions & 2 deletions antarest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

# Standard project metadata

__version__ = "2.16.4"
__version__ = "2.16.5"
__author__ = "RTE, Antares Web Team"
__date__ = "2024-02-14"
__date__ = "2024-02-29"
# noinspection SpellCheckingInspection
__credits__ = "(c) Réseau de Transport de l’Électricité (RTE)"

Expand Down
3 changes: 2 additions & 1 deletion antarest/study/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ class Study(Base): # type: ignore
__mapper_args__ = {"polymorphic_identity": "study", "polymorphic_on": type}

def __str__(self) -> str:
cls = self.__class__.__name__
return (
f"[Study]"
f"[{cls}]"
f" id={self.id},"
f" type={self.type},"
f" name={self.name},"
Expand Down
31 changes: 18 additions & 13 deletions antarest/study/storage/patch_service.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import logging
import json
import typing as t
from pathlib import Path
from typing import Optional, Union

from antarest.study.model import Patch, PatchOutputs, RawStudy, StudyAdditionalData
from antarest.study.repository import StudyMetadataRepository
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy

logger = logging.getLogger(__name__)
PATCH_JSON = "patch.json"


class PatchService:
def __init__(self, repository: Optional[StudyMetadataRepository] = None):
"""
Handle patch file ("patch.json") for a RawStudy or VariantStudy
"""

def __init__(self, repository: t.Optional[StudyMetadataRepository] = None):
self.repository = repository

def get(self, study: Union[RawStudy, VariantStudy], get_from_file: bool = False) -> Patch:
if not get_from_file:
def get(self, study: t.Union[RawStudy, VariantStudy], get_from_file: bool = False) -> Patch:
if not get_from_file and study.additional_data is not None:
# the `study.additional_data.patch` field is optional
if patch_data := study.additional_data.patch:
return Patch.parse_raw(patch_data)
if study.additional_data.patch:
patch_obj = json.loads(study.additional_data.patch or "{}")
return Patch.parse_obj(patch_obj)

patch = Patch()
patch_path = Path(study.path) / "patch.json"
patch_path = Path(study.path) / PATCH_JSON
if patch_path.exists():
patch = Patch.parse_file(patch_path)

return patch

def get_from_filestudy(self, file_study: FileStudy) -> Patch:
patch = Patch()
patch_path = (Path(file_study.config.study_path)) / "patch.json"
patch_path = (Path(file_study.config.study_path)) / PATCH_JSON
if patch_path.exists():
patch = Patch.parse_file(patch_path)
return patch

def set_reference_output(
self,
study: Union[RawStudy, VariantStudy],
study: t.Union[RawStudy, VariantStudy],
output_id: str,
status: bool = True,
) -> None:
Expand All @@ -47,12 +52,12 @@ def set_reference_output(
patch.outputs = PatchOutputs(reference=output_id)
self.save(study, patch)

def save(self, study: Union[RawStudy, VariantStudy], patch: Patch) -> None:
def save(self, study: t.Union[RawStudy, VariantStudy], patch: Patch) -> None:
if self.repository:
study.additional_data = study.additional_data or StudyAdditionalData()
study.additional_data.patch = patch.json()
self.repository.save(study)

patch_path = (Path(study.path)) / "patch.json"
patch_path = (Path(study.path)) / PATCH_JSON
patch_path.parent.mkdir(parents=True, exist_ok=True)
patch_path.write_text(patch.json())
20 changes: 10 additions & 10 deletions antarest/study/storage/rawstudy/raw_study_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import shutil
import time
import typing as t
from datetime import datetime
from pathlib import Path
from threading import Thread
from typing import BinaryIO, List, Optional, Sequence
from uuid import uuid4
from zipfile import ZipFile

Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
)
self.cleanup_thread.start()

def update_from_raw_meta(self, metadata: RawStudy, fallback_on_default: Optional[bool] = False) -> None:
def update_from_raw_meta(self, metadata: RawStudy, fallback_on_default: t.Optional[bool] = False) -> None:
"""
Update metadata from study raw metadata
Args:
Expand Down Expand Up @@ -90,7 +90,7 @@ def update_from_raw_meta(self, metadata: RawStudy, fallback_on_default: Optional
metadata.version = metadata.version or 0
metadata.created_at = metadata.created_at or datetime.utcnow()
metadata.updated_at = metadata.updated_at or datetime.utcnow()
if not metadata.additional_data:
if metadata.additional_data is None:
metadata.additional_data = StudyAdditionalData()
metadata.additional_data.patch = metadata.additional_data.patch or Patch().json()
metadata.additional_data.author = metadata.additional_data.author or "Unknown"
Expand Down Expand Up @@ -148,7 +148,7 @@ def get_raw(
self,
metadata: RawStudy,
use_cache: bool = True,
output_dir: Optional[Path] = None,
output_dir: t.Optional[Path] = None,
) -> FileStudy:
"""
Fetch a study object and its config
Expand All @@ -163,7 +163,7 @@ def get_raw(
study_path = self.get_study_path(metadata)
return self.study_factory.create_from_fs(study_path, metadata.id, output_dir, use_cache=use_cache)

def get_synthesis(self, metadata: RawStudy, params: Optional[RequestParameters] = None) -> FileStudyTreeConfigDTO:
def get_synthesis(self, metadata: RawStudy, params: t.Optional[RequestParameters] = None) -> FileStudyTreeConfigDTO:
self._check_study_exists(metadata)
study_path = self.get_study_path(metadata)
study = self.study_factory.create_from_fs(study_path, metadata.id)
Expand Down Expand Up @@ -206,7 +206,7 @@ def copy(
self,
src_meta: RawStudy,
dest_name: str,
groups: Sequence[str],
groups: t.Sequence[str],
with_outputs: bool = False,
) -> RawStudy:
"""
Expand All @@ -223,7 +223,7 @@ def copy(
"""
self._check_study_exists(src_meta)

if not src_meta.additional_data:
if src_meta.additional_data is None:
additional_data = StudyAdditionalData()
else:
additional_data = StudyAdditionalData(
Expand Down Expand Up @@ -295,7 +295,7 @@ def delete_output(self, metadata: RawStudy, output_name: str) -> None:
output_path.unlink(missing_ok=True)
remove_from_cache(self.cache, metadata.id)

def import_study(self, metadata: RawStudy, stream: BinaryIO) -> Study:
def import_study(self, metadata: RawStudy, stream: t.BinaryIO) -> Study:
"""
Import study in the directory of the study.
Expand Down Expand Up @@ -329,7 +329,7 @@ def export_study_flat(
metadata: RawStudy,
dst_path: Path,
outputs: bool = True,
output_list_filter: Optional[List[str]] = None,
output_list_filter: t.Optional[t.List[str]] = None,
denormalize: bool = True,
) -> None:
try:
Expand All @@ -352,7 +352,7 @@ def export_study_flat(
def check_errors(
self,
metadata: RawStudy,
) -> List[str]:
) -> t.List[str]:
"""
Check study antares data integrity
Args:
Expand Down
2 changes: 1 addition & 1 deletion antarest/study/storage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def update_antares_info(metadata: Study, study_tree: FileStudyTree, *, update_au
study_data_info["antares"]["created"] = metadata.created_at.timestamp()
study_data_info["antares"]["lastsave"] = metadata.updated_at.timestamp()
study_data_info["antares"]["version"] = metadata.version
if update_author:
if update_author and metadata.additional_data:
study_data_info["antares"]["author"] = metadata.additional_data.author
study_tree.save(study_data_info, ["study"])

Expand Down
4 changes: 4 additions & 0 deletions antarest/study/storage/variantstudy/model/dbmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ def is_snapshot_up_to_date(self) -> bool:
and (self.snapshot.created_at >= self.updated_at)
and (self.snapshot_dir / "study.antares").is_file()
)

def has_snapshot(self) -> bool:
"""Check if the snapshot exists."""
return (self.snapshot is not None) and (self.snapshot_dir / "study.antares").is_file()
2 changes: 1 addition & 1 deletion antarest/study/storage/variantstudy/snapshot_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def search_ref_study(
# To reuse the snapshot of the current variant, the last executed command
# must be one of the commands of the current variant.
curr_variant = descendants[-1]
if curr_variant.snapshot:
if curr_variant.has_snapshot():
last_exec_cmd = curr_variant.snapshot.last_executed_command
command_ids = [c.id for c in curr_variant.commands]
# If the variant has no command, we can reuse the snapshot if it is recent
Expand Down
Loading

0 comments on commit b2ebae8

Please sign in to comment.