Skip to content

Commit

Permalink
feat(api-study): add new endpoint to aggregate raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte committed Mar 19, 2024
1 parent e757801 commit a7c4ec0
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 20 deletions.
13 changes: 13 additions & 0 deletions antarest/study/common/default_values.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from enum import Enum


class FilteringOptions:
FILTER_SYNTHESIS: str = "hourly, daily, weekly, monthly, annual"
FILTER_YEAR_BY_YEAR: str = "hourly, daily, weekly, monthly, annual"
Expand Down Expand Up @@ -25,3 +28,13 @@ class LinkProperties:
COLORR: int = 112
COLORG: int = 112
COLORB: int = 112


class QueryFile(str, Enum):
LINKS_VALUES = "links/values"
AREAS_VALUES = "areas/values"
LINKS_DETAILS = "links/details"
AREAS_DETAILS = "areas/details"
AREAS_DETAILS_ST_STORAGE = "areas/details-ST-storage"
AREAS_DETAILS_RES = "areas/details-res"
BINDING_CONSTRAINTS = "binding_constraints/binding-constraints"
52 changes: 41 additions & 11 deletions antarest/study/common/studystorage.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import typing as t
from abc import ABC, abstractmethod
from pathlib import Path
from typing import BinaryIO, Generic, List, Optional, Sequence, TypeVar, Union

from antarest.core.exceptions import StudyNotFoundError
from antarest.core.model import JSON
from antarest.core.requests import RequestParameters
from antarest.study.common.default_values import QueryFile
from antarest.study.model import Study, StudyMetadataDTO, StudyMetadataPatchDTO, StudySimResultDTO
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfigDTO
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.rawstudy.model.filesystem.matrix.matrix import MatrixFrequency

T = TypeVar("T", bound=Study)
T = t.TypeVar("T", bound=Study)


class IStudyStorageService(ABC, Generic[T]):
class IStudyStorageService(ABC, t.Generic[T]):
@abstractmethod
def create(self, metadata: T) -> T:
"""
Expand Down Expand Up @@ -46,6 +48,34 @@ def get(
"""
raise NotImplementedError()

@abstractmethod
def aggregate_data(
self,
metadata: T,
output_name: str,
query_file: QueryFile,
frequency: MatrixFrequency,
mc_years: t.Sequence[str],
areas_names: t.Sequence[str],
columns_names: t.Sequence[str],
) -> t.Any:
"""
Entry point to fetch data inside study.
Args:
metadata:
output_name:
query_file: QueryFile,
frequency: MatrixFrequency,
mc_years: t.Sequence[str],
areas_names: t.Sequence[str],
columns_names: t.Sequence[str],
Returns: study data formatted in json
"""
raise NotImplementedError()

@abstractmethod
def exists(self, metadata: T) -> bool:
"""
Expand All @@ -59,7 +89,7 @@ def exists(self, metadata: T) -> bool:
raise NotImplementedError()

@abstractmethod
def copy(self, src_meta: T, dest_name: str, groups: Sequence[str], with_outputs: bool = False) -> T:
def copy(self, src_meta: T, dest_name: str, groups: t.Sequence[str], with_outputs: bool = False) -> T:
"""
Create a new study by copying a reference study.
Expand Down Expand Up @@ -91,9 +121,9 @@ def patch_update_study_metadata(self, study: T, metadata: StudyMetadataPatchDTO)
def import_output(
self,
study: T,
output: Union[BinaryIO, Path],
output_name: Optional[str] = None,
) -> Optional[str]:
output: t.Union[t.BinaryIO, Path],
output_name: t.Optional[str] = None,
) -> t.Optional[str]:
"""
Import an output
Args:
Expand All @@ -113,7 +143,7 @@ def get_raw(
self,
metadata: T,
use_cache: bool = True,
output_dir: Optional[Path] = None,
output_dir: t.Optional[Path] = None,
) -> FileStudy:
"""
Fetch a study raw tree object and its config
Expand All @@ -127,7 +157,7 @@ def get_raw(
raise NotImplementedError()

@abstractmethod
def get_study_sim_result(self, metadata: T) -> List[StudySimResultDTO]:
def get_study_sim_result(self, metadata: T) -> t.List[StudySimResultDTO]:
"""
Get global result information
Expand Down Expand Up @@ -236,7 +266,7 @@ def export_study_flat(
metadata: T,
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:
"""
Expand All @@ -252,7 +282,7 @@ def export_study_flat(
raise NotImplementedError()

@abstractmethod
def get_synthesis(self, metadata: T, params: Optional[RequestParameters] = None) -> FileStudyTreeConfigDTO:
def get_synthesis(self, metadata: T, params: t.Optional[RequestParameters] = None) -> FileStudyTreeConfigDTO:
"""
Return study synthesis
Args:
Expand Down
36 changes: 36 additions & 0 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
XpansionCandidateDTO,
XpansionManager,
)
from antarest.study.common.default_values import QueryFile
from antarest.study.model import (
DEFAULT_WORKSPACE_NAME,
NEW_DEFAULT_STUDY_VERSION,
Expand Down Expand Up @@ -108,6 +109,7 @@
from antarest.study.storage.rawstudy.model.filesystem.ini_file_node import IniFileNode
from antarest.study.storage.rawstudy.model.filesystem.inode import INode
from antarest.study.storage.rawstudy.model.filesystem.matrix.input_series_matrix import InputSeriesMatrix
from antarest.study.storage.rawstudy.model.filesystem.matrix.matrix import MatrixFrequency
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.raw_study_service import RawStudyService
Expand Down Expand Up @@ -317,6 +319,40 @@ def get(

return self.storage_service.get_storage(study).get(study, url, depth, formatted)

def aggregate(
self,
uuid: str,
output_name: str,
query_file: QueryFile,
frequency: MatrixFrequency,
mc_years: t.Sequence[str],
areas_names: t.Sequence[str],
columns_names: t.Sequence[str],
params: RequestParameters,
) -> t.Any:
"""
Get study data inside filesystem
Args:
uuid: study uuid
output_name: simulation output id
query_file: which types of data to retrieve
frequency: yearly, monthly, weekly, daily or hourly.
mc_years: list of monte-carlo years, if empty, all years are selected
areas_names: list of areas names, if empty, all areas are selected
columns_names: columns to be selected, if empty, all columns are selected
params: request parameters
Returns: data study formatted in json
"""
study = self.get_study(uuid)
assert_permission(params.user, study, StudyPermissionType.READ)
output = self.storage_service.get_storage(study).aggregate_data(
study, output_name, query_file, frequency, mc_years, areas_names, columns_names
)

return output

def get_logs(
self,
study_id: str,
Expand Down
Loading

0 comments on commit a7c4ec0

Please sign in to comment.