Skip to content

Commit

Permalink
feat(backend): support v8.7's upgrader, clusters and binding constrai…
Browse files Browse the repository at this point in the history
…nts (#1818)

ANT-902
  • Loading branch information
MartinBelthle authored Mar 14, 2024
1 parent 8b00715 commit a36b236
Show file tree
Hide file tree
Showing 49 changed files with 2,029 additions and 802 deletions.
10 changes: 10 additions & 0 deletions antarest/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.BAD_REQUEST, message)


class InvalidFieldForVersionError(HTTPException):
def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.UNPROCESSABLE_ENTITY, message)


class IncoherenceBetweenMatricesLength(HTTPException):
def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.UNPROCESSABLE_ENTITY, message)


class MissingDataError(HTTPException):
def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.NOT_FOUND, message)
Expand Down
50 changes: 35 additions & 15 deletions antarest/matrixstore/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
import logging
import tempfile
import typing as t
import zipfile
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Sequence, Tuple, Union

import numpy as np
import py7zr
Expand Down Expand Up @@ -58,11 +58,11 @@ def __init__(self, matrix_content_repository: MatrixContentRepository) -> None:
self.matrix_content_repository = matrix_content_repository

@abstractmethod
def create(self, data: Union[List[List[MatrixData]], npt.NDArray[np.float64]]) -> str:
def create(self, data: t.Union[t.List[t.List[MatrixData]], npt.NDArray[np.float64]]) -> str:
raise NotImplementedError()

@abstractmethod
def get(self, matrix_id: str) -> Optional[MatrixDTO]:
def get(self, matrix_id: str) -> t.Optional[MatrixDTO]:
raise NotImplementedError()

@abstractmethod
Expand All @@ -73,12 +73,32 @@ def exists(self, matrix_id: str) -> bool:
def delete(self, matrix_id: str) -> None:
raise NotImplementedError()

def get_matrix_id(self, matrix: t.Union[t.List[t.List[float]], str]) -> str:
"""
Get the matrix ID from a matrix or a matrix link.
Args:
matrix: The matrix or matrix link to get the ID from.
Returns:
The matrix ID.
Raises:
TypeError: If the provided matrix is neither a matrix nor a link to a matrix.
"""
if isinstance(matrix, str):
return matrix.lstrip("matrix://")
elif isinstance(matrix, list):
return self.create(matrix)
else:
raise TypeError(f"Invalid type for matrix: {type(matrix)}")


class SimpleMatrixService(ISimpleMatrixService):
def __init__(self, matrix_content_repository: MatrixContentRepository):
super().__init__(matrix_content_repository=matrix_content_repository)

def create(self, data: Union[List[List[MatrixData]], npt.NDArray[np.float64]]) -> str:
def create(self, data: t.Union[t.List[t.List[MatrixData]], npt.NDArray[np.float64]]) -> str:
return self.matrix_content_repository.save(data)

def get(self, matrix_id: str) -> MatrixDTO:
Expand Down Expand Up @@ -119,7 +139,7 @@ def __init__(
self.config = config

@staticmethod
def _from_dto(dto: MatrixDTO) -> Tuple[Matrix, MatrixContent]:
def _from_dto(dto: MatrixDTO) -> t.Tuple[Matrix, MatrixContent]:
matrix = Matrix(
id=dto.id,
width=dto.width,
Expand All @@ -131,7 +151,7 @@ def _from_dto(dto: MatrixDTO) -> Tuple[Matrix, MatrixContent]:

return matrix, content

def create(self, data: Union[List[List[MatrixData]], npt.NDArray[np.float64]]) -> str:
def create(self, data: t.Union[t.List[t.List[MatrixData]], npt.NDArray[np.float64]]) -> str:
"""
Creates a new matrix object with the specified data.
Expand Down Expand Up @@ -168,7 +188,7 @@ def create(self, data: Union[List[List[MatrixData]], npt.NDArray[np.float64]]) -
self.repo.save(matrix)
return matrix_id

def create_by_importation(self, file: UploadFile, is_json: bool = False) -> List[MatrixInfoDTO]:
def create_by_importation(self, file: UploadFile, is_json: bool = False) -> t.List[MatrixInfoDTO]:
"""
Imports a matrix from a TSV or JSON file or a collection of matrices from a ZIP file.
Expand All @@ -191,7 +211,7 @@ def create_by_importation(self, file: UploadFile, is_json: bool = False) -> List
if file.content_type == "application/zip":
with contextlib.closing(f):
buffer = io.BytesIO(f.read())
matrix_info: List[MatrixInfoDTO] = []
matrix_info: t.List[MatrixInfoDTO] = []
if file.filename.endswith("zip"):
with zipfile.ZipFile(buffer) as zf:
for info in zf.infolist():
Expand Down Expand Up @@ -237,7 +257,7 @@ def get_dataset(
self,
id: str,
params: RequestParameters,
) -> Optional[MatrixDataSet]:
) -> t.Optional[MatrixDataSet]:
if not params.user:
raise UserHasNotPermissionError()
dataset = self.repo_dataset.get(id)
Expand All @@ -250,7 +270,7 @@ def get_dataset(
def create_dataset(
self,
dataset_info: MatrixDataSetUpdateDTO,
matrices: List[MatrixInfoDTO],
matrices: t.List[MatrixInfoDTO],
params: RequestParameters,
) -> MatrixDataSet:
if not params.user:
Expand Down Expand Up @@ -296,10 +316,10 @@ def update_dataset(

def list(
self,
dataset_name: Optional[str],
dataset_name: t.Optional[str],
filter_own: bool,
params: RequestParameters,
) -> List[MatrixDataSetDTO]:
) -> t.List[MatrixDataSetDTO]:
"""
List matrix user metadata
Expand Down Expand Up @@ -337,7 +357,7 @@ def delete_dataset(self, id: str, params: RequestParameters) -> str:
self.repo_dataset.delete(id)
return id

def get(self, matrix_id: str) -> Optional[MatrixDTO]:
def get(self, matrix_id: str) -> t.Optional[MatrixDTO]:
"""
Get a matrix object from the database and the matrix content repository.
Expand Down Expand Up @@ -414,7 +434,7 @@ def check_access_permission(
raise UserHasNotPermissionError()
return access

def create_matrix_files(self, matrix_ids: Sequence[str], export_path: Path) -> str:
def create_matrix_files(self, matrix_ids: t.Sequence[str], export_path: Path) -> str:
with tempfile.TemporaryDirectory(dir=self.config.storage.tmp_dir) as tmpdir:
stopwatch = StopWatch()
for mid in matrix_ids:
Expand Down Expand Up @@ -461,7 +481,7 @@ def download_dataset(

def download_matrix_list(
self,
matrix_list: Sequence[str],
matrix_list: t.Sequence[str],
dataset_name: str,
params: RequestParameters,
) -> FileDownloadTaskDTO:
Expand Down
8 changes: 4 additions & 4 deletions antarest/study/business/areas/thermal_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from antarest.study.model import Study
from antarest.study.storage.rawstudy.model.filesystem.config.model import transform_name_to_id
from antarest.study.storage.rawstudy.model.filesystem.config.thermal import (
Thermal860Config,
Thermal860Properties,
Thermal870Config,
Thermal870Properties,
ThermalConfigType,
create_thermal_config,
)
Expand All @@ -32,7 +32,7 @@


@camel_case_model
class ThermalClusterInput(Thermal860Properties, metaclass=AllOptionalMetaclass, use_none=True):
class ThermalClusterInput(Thermal870Properties, metaclass=AllOptionalMetaclass, use_none=True):
"""
Model representing the data structure required to edit an existing thermal cluster within a study.
"""
Expand Down Expand Up @@ -72,7 +72,7 @@ def to_config(self, study_version: t.Union[str, int]) -> ThermalConfigType:


@camel_case_model
class ThermalClusterOutput(Thermal860Config, metaclass=AllOptionalMetaclass, use_none=True):
class ThermalClusterOutput(Thermal870Config, metaclass=AllOptionalMetaclass, use_none=True):
"""
Model representing the output data structure to display the details of a thermal cluster within a study.
"""
Expand Down
Loading

0 comments on commit a36b236

Please sign in to comment.