Skip to content

Commit

Permalink
Hot fixes (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
pl-buiquang authored May 10, 2022
1 parent 4f79b4e commit e2a8a5b
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 32 deletions.
12 changes: 7 additions & 5 deletions antarest/login/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from antarest.core.config import Config
from antarest.core.interfaces.eventbus import IEventBus, DummyEventBusService
from antarest.core.utils.fastapi_sqlalchemy import db
from antarest.login.ldap import LdapService
from antarest.login.repository import (
UserRepository,
Expand Down Expand Up @@ -77,11 +78,12 @@ def check_if_token_is_revoked(decrypted_token: Any) -> bool:
subject = json.loads(decrypted_token["sub"])
user_id = subject["id"]
token_type = subject["type"]
return (
token_type == "bots"
and service is not None
and not service.exists_bot(user_id)
)
with db():
return (
token_type == "bots"
and service is not None
and not service.exists_bot(user_id)
)

if application:
application.include_router(create_login_api(service, config))
Expand Down
4 changes: 2 additions & 2 deletions antarest/study/business/area_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class AreaCreationDTO(BaseModel):
class ClusterInfoDTO(PatchCluster):
id: str
name: str
unitcount: int
nominalcapacity: int
unitcount: int = 0
nominalcapacity: int = 0
group: Optional[str] = None
min_stable_power: Optional[int] = None
min_up_time: Optional[int] = None
Expand Down
1 change: 1 addition & 0 deletions antarest/study/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class StudySimSettingsDTO(BaseModel):
otherPreferences: Dict[str, Any]
advancedParameters: Dict[str, Any]
seedsMersenneTwister: Dict[str, Any]
playlist: Optional[List[int]] = None


class StudySimResultDTO(BaseModel):
Expand Down
13 changes: 10 additions & 3 deletions antarest/study/storage/abstract_storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tempfile
from abc import ABC
from pathlib import Path
from typing import List, Union, Optional, IO
from typing import List, Union, Optional, IO, cast
from uuid import uuid4

from pydantic import ValidationError
Expand All @@ -29,6 +29,9 @@
StudyAdditionalData,
)
from antarest.study.storage.patch_service import PatchService
from antarest.study.storage.rawstudy.model.filesystem.config.files import (
ConfigPathBuilder,
)
from antarest.study.storage.rawstudy.model.filesystem.config.model import (
Simulation,
)
Expand Down Expand Up @@ -188,7 +191,10 @@ def get_study_sim_result(
if study_data.config.outputs is not None:
reference = (patch_metadata.outputs or PatchOutputs()).reference
for output in study_data.config.outputs:
file_metadata = FileStudyHelpers.get_config(study_data)
output_data: Simulation = study_data.config.outputs[output]
file_metadata = FileStudyHelpers.get_config(
study_data, output_data.get_file()
)
settings = StudySimSettingsDTO(
general=file_metadata["general"],
input=file_metadata["input"],
Expand All @@ -199,8 +205,9 @@ def get_study_sim_result(
seedsMersenneTwister=file_metadata[
"seeds - Mersenne Twister"
],
playlist=ConfigPathBuilder.get_playlist(file_metadata),
)
output_data: Simulation = study_data.config.outputs[output]

results.append(
StudySimResultDTO(
name=output_data.get_file(),
Expand Down
12 changes: 7 additions & 5 deletions antarest/study/storage/rawstudy/model/filesystem/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,28 @@ def parse_simulation(path: Path) -> Optional["Simulation"]:
return None

@staticmethod
def get_playlist(config: JSON) -> List[int]:
def get_playlist(config: JSON) -> Optional[List[int]]:
general_config = config.get("general", {})
nb_years = cast(int, general_config.get("nbyears"))
playlist_activated = cast(
bool, general_config.get("user-playlist", False)
)
if not playlist_activated:
return list(range(0, nb_years))
return None
playlist_config = config.get("playlist", {})
playlist_reset = playlist_config.get("playlist_reset", True)
added = playlist_config.get("playlist_year +", [])
removed = playlist_config.get("playlist_year -", [])
if playlist_reset:
return [year for year in range(0, nb_years) if year not in removed]
return [year for year in added if year not in removed]
return [
year + 1 for year in range(0, nb_years) if year not in removed
]
return [year + 1 for year in added if year not in removed]

@staticmethod
def _parse_outputs_parameters(
path: Path,
) -> Tuple[int, bool, bool, List[int]]:
) -> Tuple[int, bool, bool, Optional[List[int]]]:
par: JSON = MultipleSameKeysIniReader(DUPLICATE_KEYS).read(
path / "about-the-study/parameters.ini"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Simulation(BaseModel):
synthesis: bool
by_year: bool
error: bool
playlist: List[int]
playlist: Optional[List[int]]

def get_file(self) -> str:
modes = {"economy": "eco", "adequacy": "adq", "draft": "dft"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def __init__(

def build(self) -> TREE:
children: TREE = {
str("{:05d}".format(scn + 1)): OutputSimulationModeMcIndScn(
self.context, self.config.next_file("{:05d}".format(scn + 1))
str("{:05d}".format(scn)): OutputSimulationModeMcIndScn(
self.context, self.config.next_file("{:05d}".format(scn))
)
for scn in self.simulation.playlist
or range(1, self.simulation.nbyears + 1)
}
return children
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


DUPLICATE_KEYS = [
"playlist_reset",
"playlist_year_weight",
"playlist_year +",
"playlist_year -",
Expand Down
9 changes: 6 additions & 3 deletions antarest/study/storage/rawstudy/model/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def save_config(study: FileStudy, config: JSON) -> None:
@staticmethod
def get_playlist(
study: FileStudy, output_id: Optional[str] = None
) -> List[int]:
) -> Optional[List[int]]:
config = FileStudyHelpers.get_config(study, output_id)
return ConfigPathBuilder.get_playlist(config)

@staticmethod
def set_playlist(study: FileStudy, playlist: List[int]) -> None:
playlist_without_offset = [year - 1 for year in playlist]
config = FileStudyHelpers.get_config(study)
general_config: Optional[JSON] = config.get("general", None)
assert_this(general_config is not None)
Expand All @@ -50,12 +51,14 @@ def set_playlist(study: FileStudy, playlist: List[int]) -> None:
if "playlist_year +" in playlist_config:
del playlist_config["playlist_year +"]
playlist_config["playlist_year -"] = [
year for year in range(0, nb_years) if year not in playlist
year
for year in range(0, nb_years)
if year not in playlist_without_offset
]
else:
playlist_config["playlist_reset"] = False
if "playlist_year -" in playlist_config:
del playlist_config["playlist_year -"]
playlist_config["playlist_year +"] = playlist
playlist_config["playlist_year +"] = playlist_without_offset
config["playlist"] = playlist_config
FileStudyHelpers.save_config(study, config)
9 changes: 9 additions & 0 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def test_main(app: FastAPI):
study_id = next(iter(res.json()))
comments = "<text>Hello</text>"

res = client.get(
f"/v1/studies/{study_id}/outputs",
headers={
"Authorization": f'Bearer {george_credentials["access_token"]}'
},
)
res_output = res.json()
assert len(res_output) == 4

# Set new comments
res = client.put(
f"/v1/studies/{study_id}/comments",
Expand Down
16 changes: 8 additions & 8 deletions tests/storage/rawstudies/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
[
(
{"general": {"nbyears": 10, "user-playlist": False}},
list(range(0, 10)),
None,
),
(
{"general": {"nbyears": 10, "user-playlist": True}},
list(range(0, 10)),
list(range(1, 11)),
),
(
{
Expand All @@ -24,7 +24,7 @@
"playlist_year +": [1, 2],
},
},
list(range(0, 10)),
None,
),
(
{
Expand All @@ -34,7 +34,7 @@
"playlist_year +": [1, 2],
},
},
[1, 2],
[2, 3],
),
(
{
Expand All @@ -44,7 +44,7 @@
"playlist_year -": [1, 2],
},
},
[0, 3, 4, 5, 6, 7, 8, 9],
[1, 4, 5, 6, 7, 8, 9, 10],
),
],
)
Expand All @@ -67,9 +67,9 @@ def test_set_playlist():
},
{"general": {"nbyears": 10, "user-playlist": False}},
]
FileStudyHelpers.set_playlist(study, [1, 3])
FileStudyHelpers.set_playlist(study, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
FileStudyHelpers.set_playlist(study, [1, 2, 3, 6, 7, 8])
FileStudyHelpers.set_playlist(study, [2, 4])
FileStudyHelpers.set_playlist(study, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
FileStudyHelpers.set_playlist(study, [2, 3, 4, 7, 8, 9])
study.tree.save.assert_has_calls(
[
call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_parse_outputs(tmp_path: Path) -> None:
synthesis=True,
by_year=True,
error=False,
playlist=[0],
playlist=[1],
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function CreateTokenDialog(props: Props) {
) as BotCreateDTO["roles"];

const tokenValue = await mounted(
createBot({ name, is_author: true, roles })
createBot({ name, is_author: false, roles })
);

setTokenValueToDisplay(tokenValue);
Expand Down

0 comments on commit e2a8a5b

Please sign in to comment.