Skip to content

Commit

Permalink
fix ui
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Jan 22, 2025
1 parent 2a3f28d commit ca4502d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 66 deletions.
6 changes: 3 additions & 3 deletions antarest/study/business/timeseries_config_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig


class TSConfigFields(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True):
class TSConfig(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True):
number: int


@all_optional_model
class TSConfigDTO(AntaresBaseModel, extra="forbid", validate_assignment=True, populate_by_name=True):
thermal: TSConfigFields
thermal: TSConfig


class TimeSeriesConfigManager:
Expand All @@ -40,7 +40,7 @@ def get_values(self, study: Study) -> TSConfigDTO:
url.extend(["general", "nbtimeseriesthermal"])
nb_ts_gen_thermal = file_study.tree.get(url)

args = {"thermal": TSConfigFields(number=nb_ts_gen_thermal)}
args = {"thermal": TSConfig(number=nb_ts_gen_thermal)}
return TSConfigDTO.model_validate(args)

def set_values(self, study: Study, field_values: TSConfigDTO) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/storage/business/test_timeseries_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pytest

from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSConfigDTO, TSConfigFields
from antarest.study.business.timeseries_config_management import TimeSeriesConfigManager, TSConfigDTO, TSConfig
from antarest.study.model import RawStudy
from antarest.study.storage.rawstudy.model.filesystem.config.files import build
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
Expand Down Expand Up @@ -53,9 +53,9 @@ def test_nominal_case(
study = RawStudy(id="test", path=str(file_study_820.config.path))

# Asserts the get method returns the right value
assert config_manager.get_values(study) == TSConfigDTO(thermal=TSConfigFields(number=1))
assert config_manager.get_values(study) == TSConfigDTO(thermal=TSConfig(number=1))

# Modifies the value and asserts the get takes the modification into account
new_value = TSConfigDTO(thermal=TSConfigFields(number=2))
new_value = TSConfigDTO(thermal=TSConfig(number=2))
config_manager.set_values(study, new_value)
assert config_manager.get_values(study) == new_value
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from
import capitalize from "lodash/capitalize";
import NumberFE from "../../../../../common/fieldEditors/NumberFE";
import { useFormContextPlus } from "../../../../../common/Form";
import type { TSConfigDTO, TSType } from "@/services/api/studies/timeseries/types.ts";
import BooleanFE from "../../../../../common/fieldEditors/BooleanFE";
import { useTranslation } from "react-i18next";
import { validateNumber } from "@/utils/validation/number";
import type { TSConfigValues } from "./utils";
import { TSType } from "@/services/api/studies/timeseries/constants";

const borderStyle = "1px solid rgba(255, 255, 255, 0.12)";

function Fields() {
const { control, watch } = useFormContextPlus<TSConfigDTO>();
const isReadyMade = watch("thermal.stochasticTsStatus") === false;
const { control, watch } = useFormContextPlus<TSConfigValues>();
const formValues = watch();
const { t } = useTranslation();

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -59,29 +60,31 @@ function Fields() {
},
}}
>
<TableRow>
<TableCell sx={{ fontWeight: "bold" }}>{capitalize(TSType.Thermal)}</TableCell>
<TableCell align="center">
<BooleanFE
name={"thermal.stochasticTsStatus"}
control={control}
trueText={t("study.configuration.tsManagement.status.toBeGenerated")}
falseText={t("study.configuration.tsManagement.status.readyMade")}
variant="outlined"
size="small"
/>
</TableCell>
<TableCell align="center">
<NumberFE
name={"thermal.number"}
control={control}
size="small"
disabled={isReadyMade}
rules={{ validate: validateNumber({ min: 1 }) }}
sx={{ width: 110 }}
/>
</TableCell>
</TableRow>
{Object.values(TSType).map((type) => (
<TableRow key={type}>
<TableCell sx={{ fontWeight: "bold" }}>{capitalize(type)}</TableCell>
<TableCell align="center">
<BooleanFE
name={"thermal.stochasticTsStatus"}
control={control}
trueText={t("study.configuration.tsManagement.status.toBeGenerated")}
falseText={t("study.configuration.tsManagement.status.readyMade")}
variant="outlined"
size="small"
/>
</TableCell>
<TableCell align="center">
<NumberFE
name={"thermal.number"}
control={control}
size="small"
disabled={formValues[type].stochasticTsStatus === false}
rules={{ validate: validateNumber({ min: 1 }) }}
sx={{ width: 110 }}
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@ import { useTranslation } from "react-i18next";
import usePromiseHandler from "../../../../../../hooks/usePromiseHandler";
import BuildIcon from "@mui/icons-material/Build";
import { useRef, useState } from "react";
import {setTimeSeriesConfig, generateTimeSeries} from "@/services/api/studies/timeseries";
import type {TSConfigDTO} from "@/services/api/studies/timeseries/types.ts";
import {DeepPartial} from "react-hook-form";

export const DEFAULT_VALUES: DeepPartial<TSConfigDTO> = {
thermal: {
number: 1,
},
};

import { setTimeSeriesConfig, generateTimeSeries } from "@/services/api/studies/timeseries";
import { DEFAULT_VALUES, toConfigDTO, type TSConfigValues } from "./utils";

function TimeSeriesManagement() {
const { study } = useOutletContext<{ study: StudyMetadata }>();
const { t } = useTranslation();
const [launchTaskInProgress, setLaunchTaskInProgress] = useState(false);
const apiRef = useRef<UseFormReturnPlus<TSConfigDTO>>(null);
const apiRef = useRef<UseFormReturnPlus<TSConfigValues>>(null);

const handleGenerateTs = usePromiseHandler({
fn: generateTimeSeries,
Expand All @@ -48,8 +40,8 @@ function TimeSeriesManagement() {
// Event Handlers
////////////////////////////////////////////////////////////////

const handleSubmit = (data: SubmitHandlerPlus<TSConfigDTO>) => {
return setTimeSeriesConfig({studyId: study.id, values: data.values});
const handleSubmit = (data: SubmitHandlerPlus<TSConfigValues>) => {
return setTimeSeriesConfig({ studyId: study.id, values: toConfigDTO(data.values) });
};

const handleSubmitSuccessful = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2025, RTE (https://www.rte-france.com)
*
* See AUTHORS.txt
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*
* This file is part of the Antares project.
*/

import { TSType } from "@/services/api/studies/timeseries/constants";
import type { TSConfig, TSConfigDTO, TTSType } from "@/services/api/studies/timeseries/types";

export type TSConfigValues = Record<TTSType, TSConfig & { stochasticTsStatus: boolean }>;

export const DEFAULT_VALUES = Object.values(TSType).reduce((acc, type) => {
acc[type] = { number: 1, stochasticTsStatus: false };
return acc;
}, {} as TSConfigValues);

export function toConfigDTO(data: TSConfigValues) {
return Object.entries(data).reduce((acc, [key, { stochasticTsStatus, ...config }]) => {
acc[key as TTSType] = config;
return acc;
}, {} as TSConfigDTO);
}
17 changes: 17 additions & 0 deletions webapp/src/services/api/studies/timeseries/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2025, RTE (https://www.rte-france.com)
*
* See AUTHORS.txt
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*
* This file is part of the Antares project.
*/

export const TSType = {
Thermal: "thermal",
} as const;
9 changes: 3 additions & 6 deletions webapp/src/services/api/studies/timeseries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* This file is part of the Antares project.
*/

import { StudyMetadata } from "../../../../common/types.ts";
import type { StudyMetadata } from "../../../../common/types.ts";
import client from "../../client.ts";
import type {SetTimeSeriesConfigParams} from "@/services/api/studies/timeseries/types.ts";
import type { SetTimeSeriesConfigParams } from "@/services/api/studies/timeseries/types.ts";

/**
* Launches time series generation task for the specified study.
Expand All @@ -28,9 +28,6 @@ export async function generateTimeSeries(params: { studyId: StudyMetadata["id"]
return data;
}


export async function setTimeSeriesConfig(
{studyId, values}: SetTimeSeriesConfigParams
) {
export async function setTimeSeriesConfig<T>({ studyId, values }: SetTimeSeriesConfigParams<T>) {
await client.put(`v1/studies/${studyId}/timeseries/config`, values);
}
29 changes: 14 additions & 15 deletions webapp/src/services/api/studies/timeseries/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2024, RTE (https://www.rte-france.com)
* Copyright (c) 2025, RTE (https://www.rte-france.com)
*
* See AUTHORS.txt
*
Expand All @@ -11,23 +11,22 @@
*
* This file is part of the Antares project.
*/
import {StudyMetadata} from "@/common/types.ts";
import {DeepPartial} from "react-hook-form";

export enum TSType {
Thermal = "thermal"
}
import type { StudyMetadata } from "@/common/types.ts";
import type { DeepPartial } from "react-hook-form";
import type { F, O } from "ts-toolbelt";
import type { TSType } from "./constants";

export type TTSType = O.UnionOf<typeof TSType>;

interface TSConfigFields {
export interface TSConfig {
number: number;
}

export interface TSConfigDTO
{
[TSType.Thermal]: TSConfigFields;
}
export type TSConfigDTO = Record<TTSType, TSConfig>;

export interface SetTimeSeriesConfigParams {
studyId: StudyMetadata["id"],
values: DeepPartial<TSConfigDTO>,
}
export interface SetTimeSeriesConfigParams<T> {
studyId: StudyMetadata["id"];
// Extra fields not allowed by the API
values: DeepPartial<F.Exact<T, TSConfigDTO>>;
}

0 comments on commit ca4502d

Please sign in to comment.