Skip to content

Commit

Permalink
make the endpoint front-end oriented
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Nov 26, 2024
1 parent 44d0ff8 commit 757054d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
6 changes: 3 additions & 3 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
from antarest.study.storage.rawstudy.model.filesystem.matrix.output_series_matrix import OutputSeriesMatrix
from antarest.study.storage.rawstudy.model.filesystem.raw_file_node import RawFileNode
from antarest.study.storage.rawstudy.model.filesystem.root.output.simulation.mode.mcall.digest import (
DigestDTO,
DigestSynthesis,
DigestUI,
)
from antarest.study.storage.rawstudy.model.filesystem.root.user.user import User
from antarest.study.storage.rawstudy.raw_study_service import RawStudyService
Expand Down Expand Up @@ -2752,7 +2752,7 @@ def delete_file_or_folder(self, study_id: str, path: str, current_user: JWTUser)
updated_tree = study_tree.get()
self.storage_service.get_storage(study).cache.put(cache_id, updated_tree) # type: ignore

def get_digest_file(self, study_id: str, output_id: str, params: RequestParameters) -> DigestDTO:
def get_digest_file(self, study_id: str, output_id: str, params: RequestParameters) -> DigestUI:
"""
Returns the digest file as 4 separated intelligible matrices.
Raises ChildNotFoundError if the output_id doesn't exist or if the digest file wasn't generated
Expand All @@ -2762,4 +2762,4 @@ def get_digest_file(self, study_id: str, output_id: str, params: RequestParamete
file_study = self.storage_service.get_storage(study).get_raw(study)
digest_node = file_study.tree.get_node(url=["output", output_id, "economy", "mc-all", "grid", "digest"])
assert isinstance(digest_node, DigestSynthesis)
return digest_node.get_dto()
return digest_node.get_ui()
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import typing as t

import pandas as pd
from pydantic import Field

from antarest.core.model import JSON
from antarest.core.serialization import AntaresBaseModel
Expand All @@ -21,61 +22,61 @@
from antarest.study.storage.rawstudy.model.filesystem.root.output.simulation.mode.mcall.synthesis import OutputSynthesis


class DigestMatrixDTO(AntaresBaseModel):
class DigestMatrixUI(AntaresBaseModel):
columns: t.List[t.Union[str, t.List[str]]]
data: t.List[t.List[t.Any]]
groupedColumns: bool
grouped_columns: bool = Field(alias="groupedColumns")


class DigestDTO(AntaresBaseModel):
area: DigestMatrixDTO
districts: DigestMatrixDTO
flowLinear: DigestMatrixDTO
flowQuadratic: DigestMatrixDTO
class DigestUI(AntaresBaseModel):
area: DigestMatrixUI
districts: DigestMatrixUI
flow_linear: DigestMatrixUI = Field(alias="flowLinear")
flow_quadratic: DigestMatrixUI = Field(alias="flowQuadratic")


def _get_flow_linear(df: pd.DataFrame) -> DigestMatrixDTO:
def _get_flow_linear(df: pd.DataFrame) -> DigestMatrixUI:
return _get_flow(df, "Links (FLOW LIN.)")


def _get_flow_quadratic(df: pd.DataFrame) -> DigestMatrixDTO:
def _get_flow_quadratic(df: pd.DataFrame) -> DigestMatrixUI:
return _get_flow(df, "Links (FLOW QUAD.)")


def _get_flow(df: pd.DataFrame, keyword: str) -> DigestMatrixDTO:
def _get_flow(df: pd.DataFrame, keyword: str) -> DigestMatrixUI:
first_column = df["1"].tolist()
index = next((k for k, v in enumerate(first_column) if v == keyword), None)
if not index:
return DigestMatrixDTO(columns=[], data=[], groupedColumns=False)
return DigestMatrixUI(columns=[], data=[], groupedColumns=False)
index_start = index + 2
df_col_start = 1
df_size = next((k for k, v in enumerate(first_column[index_start:]) if v == ""), len(first_column) - index_start)
flow_df = df.iloc[index_start : index_start + df_size, df_col_start : df_col_start + df_size]
data = flow_df.iloc[:, 1:].to_numpy().tolist()
cols = [""] + flow_df.iloc[0, 1:].tolist()
return DigestMatrixDTO(columns=cols, data=data, groupedColumns=False)
return DigestMatrixUI(columns=cols, data=data, groupedColumns=False)


def _build_areas_and_districts(df: pd.DataFrame, first_row: int) -> DigestMatrixDTO:
def _build_areas_and_districts(df: pd.DataFrame, first_row: int) -> DigestMatrixUI:
first_column = df["1"].tolist()
first_area_row = df.iloc[first_row, 2:].tolist()
col_number = next((k for k, v in enumerate(first_area_row) if v == ""), df.shape[1])
final_index = first_column[first_row:].index("") + first_row
data = df.iloc[first_row:final_index, 1 : col_number + 1].to_numpy().tolist()
cols_raw = df.iloc[first_row - 3 : first_row, 2 : col_number + 1].to_numpy().tolist()
columns = [[""]] + [[a, b, c] for a, b, c in zip(cols_raw[0], cols_raw[1], cols_raw[2])]
return DigestMatrixDTO(columns=columns, data=data, groupedColumns=True)
return DigestMatrixUI(columns=columns, data=data, groupedColumns=True)


def _get_area(df: pd.DataFrame) -> DigestMatrixDTO:
def _get_area(df: pd.DataFrame) -> DigestMatrixUI:
return _build_areas_and_districts(df, 7)


def _get_district(df: pd.DataFrame) -> DigestMatrixDTO:
def _get_district(df: pd.DataFrame) -> DigestMatrixUI:
first_column = df["1"].tolist()
first_row = next((k for k, v in enumerate(first_column) if "@" in v), None)
if not first_row:
return DigestMatrixDTO(columns=[], data=[], groupedColumns=False)
return DigestMatrixUI(columns=[], data=[], groupedColumns=False)
return _build_areas_and_districts(df, first_row)


Expand All @@ -96,7 +97,7 @@ def load(
del output["index"]
return t.cast(JSON, output)

def get_dto(self) -> DigestDTO:
def get_ui(self) -> DigestUI:
"""
Parse a digest file and returns it as 4 separated matrices.
One for areas, one for the districts, one for linear flow and the last one for quadratic flow.
Expand All @@ -106,7 +107,7 @@ def get_dto(self) -> DigestDTO:
flow_quadratic = _get_flow_quadratic(df)
area = _get_area(df)
districts = _get_district(df)
return DigestDTO(area=area, districts=districts, flowLinear=flow_linear, flowQuadratic=flow_quadratic)
return DigestUI(area=area, districts=districts, flowLinear=flow_linear, flowQuadratic=flow_quadratic)

def _parse_digest_file(self) -> pd.DataFrame:
"""
Expand Down
10 changes: 5 additions & 5 deletions antarest/study/web/studies_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from antarest.study.repository import AccessPermissions, StudyFilter, StudyPagination, StudySortBy
from antarest.study.service import StudyService
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfigDTO
from antarest.study.storage.rawstudy.model.filesystem.root.output.simulation.mode.mcall.digest import DigestDTO
from antarest.study.storage.rawstudy.model.filesystem.root.output.simulation.mode.mcall.digest import DigestUI

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -817,16 +817,16 @@ def unarchive_output(
return content

@bp.get(
"/private/studies/{study_id}/outputs/{output_id}/digest",
"/private/studies/{study_id}/outputs/{output_id}/digest-ui",
tags=[APITag.study_outputs],
summary="Get an output digest file",
response_model=DigestDTO,
summary="Display an output digest file for the front-end",
response_model=DigestUI,
)
def get_digest_file(
study_id: str,
output_id: str,
current_user: JWTUser = Depends(auth.get_current_user),
) -> DigestDTO:
) -> DigestUI:
study_id = sanitize_uuid(study_id)
output_id = sanitize_string(output_id)
logger.info(
Expand Down

0 comments on commit 757054d

Please sign in to comment.