Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gather all the unit conversions into one file #2447

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e091acb
gather all the unit conversions into one file
ghazwarhili Dec 11, 2024
16ef55e
add licence
ghazwarhili Dec 11, 2024
302fe30
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 11, 2024
3e1df6c
Merge branch 'main' into mutualize-unit-conversion-into-one-file
thangqp Dec 16, 2024
013ee58
Merge two fieldType from commons-ui and study
thangqp Dec 16, 2024
537d481
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 17, 2024
1e7eb1f
fix fieldType
ghazwarhili Dec 17, 2024
23264ff
Merge remote-tracking branch 'origin/mutualize-unit-conversion-into-o…
ghazwarhili Dec 17, 2024
bdfed42
revert conversion unit for result table
ghazwarhili Dec 17, 2024
0865e4a
add conversion unit to modification by formula
ghazwarhili Dec 17, 2024
c1432f0
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 17, 2024
853950c
adapt the conversion unit to modifcation by tabular
ghazwarhili Dec 17, 2024
d841b7b
Merge remote-tracking branch 'origin/mutualize-unit-conversion-into-o…
ghazwarhili Dec 17, 2024
ebadd4a
fix converter unit input or output
ghazwarhili Dec 18, 2024
c38b717
enhance the name of the mapper function
ghazwarhili Dec 18, 2024
724af3d
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 18, 2024
2df7d90
rename MAXIMUM_SUSCEPTANCE to MAX_SUSCEPTANCE
thangqp Dec 18, 2024
f0fa241
revert modif in package.json
thangqp Dec 18, 2024
ba8a934
Merge branch 'main' into mutualize-unit-conversion-into-one-file
thangqp Dec 18, 2024
631011d
revert modif in package-lock.json
thangqp Dec 18, 2024
7b7d98b
Ignore maxSusceptance and maxQAtNominalV conversion in tabular
thangqp Dec 18, 2024
36f14ef
add convertCamelToSnake to modification by tabular
ghazwarhili Dec 20, 2024
8eaab7c
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 20, 2024
d811223
Merge branch 'mutualize-unit-conversion-into-one-file' of https://git…
ghazwarhili Dec 20, 2024
701bb43
get label plus unit
ghazwarhili Dec 23, 2024
fb9096f
Merge branch 'main' into mutualize-unit-conversion-into-one-file
ghazwarhili Dec 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/components/dialogs/converter-unit-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { kiloUnitToUnit, microUnitToUnit, unitToKiloUnit, unitToMicroUnit } from '@gridsuite/commons-ui';
ghazwarhili marked this conversation as resolved.
Show resolved Hide resolved

export enum FieldType {
ID = 'ID',
NAME = 'NAME',
NOMINAL_VOLTAGE = 'NOMINAL_VOLTAGE',
TARGET_V = 'TARGET_V',
TARGET_P = 'TARGET_P',
COUNTRY = 'COUNTRY',
PLANNED_ACTIVE_POWER_SET_POINT = 'PLANNED_ACTIVE_POWER_SET_POINT',
MARGINAL_COST = 'MARGINAL_COST',
PLANNED_OUTAGE_RATE = 'PLANNED_OUTAGE_RATE',
FORCED_OUTAGE_RATE = 'FORCED_OUTAGE_RATE',
P0 = 'P0',
Q0 = 'Q0',
LOW_VOLTAGE_LIMIT = 'LOW_VOLTAGE_LIMIT',
HIGH_VOLTAGE_LIMIT = 'HIGH_VOLTAGE_LIMIT',
SECTION_COUNT = 'SECTION_COUNT',
MAXIMUM_SECTION_COUNT = 'MAXIMUM_SECTION_COUNT',
CONNECTED = 'CONNECTED',
MAX_Q_AT_NOMINAL_V = 'MAX_Q_AT_NOMINAL_V',
SHUNT_CONDUCTANCE_1 = 'SHUNT_CONDUCTANCE_1',
SHUNT_CONDUCTANCE_2 = 'SHUNT_CONDUCTANCE_2',
SHUNT_SUSCEPTANCE = 'SHUNT_SUSCEPTANCE',
SHUNT_SUSCEPTANCE_1 = 'SHUNT_SUSCEPTANCE_1',
SHUNT_SUSCEPTANCE_2 = 'SHUNT_SUSCEPTANCE_2',
VOLTAGE_SET_POINT = 'VOLTAGE_SET_POINT',
ACTIVE_POWER_SET_POINT = 'ACTIVE_POWER_SET_POINT',
REACTIVE_POWER_SET_POINT = 'REACTIVE_POWER_SET_POINT',
LOW_SHORT_CIRCUIT_CURRENT_LIMIT = 'LOW_SHORT_CIRCUIT_CURRENT_LIMIT',
HIGH_SHORT_CIRCUIT_CURRENT_LIMIT = 'HIGH_SHORT_CIRCUIT_CURRENT_LIMIT',
MAXIMUM_SUSCEPTANCE = 'MAXIMUM_SUSCEPTANCE',
R = 'R',
X = 'X',
G = 'G',
B = 'B',
G1 = 'G1',
B1 = 'B1',
G2 = 'G2',
B2 = 'B2',
}

const microUnits = [
FieldType.SHUNT_CONDUCTANCE_1,
FieldType.SHUNT_CONDUCTANCE_2,
FieldType.SHUNT_SUSCEPTANCE_1,
FieldType.SHUNT_SUSCEPTANCE_2,
FieldType.G,
FieldType.B,
FieldType.G1,
FieldType.B1,
FieldType.G2,
FieldType.B2,
];

const kiloUnits = [FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, FieldType.LOW_SHORT_CIRCUIT_CURRENT_LIMIT];
export function convertInputValues(field: FieldType, value: any) {
if (microUnits.includes(field)) {
if (!Array.isArray(value)) {
return value ? unitToMicroUnit(value) : value;
}
return value.map((a: number) => unitToMicroUnit(a));
}
if (kiloUnits.includes(field)) {
if (!Array.isArray(value)) {
return value ? kiloUnitToUnit(value) : value;
}
return value.map((a: number) => kiloUnitToUnit(a));
}
return value;
}

export function convertOutputValues(field: FieldType, value: any) {
if (microUnits.includes(field)) {
if (!Array.isArray(value)) {
return value ? microUnitToUnit(value) : value;
}
return value.map((a: number) => microUnitToUnit(a));
}
if (kiloUnits.includes(field)) {
if (!Array.isArray(value)) {
return value ? unitToKiloUnit(value) : value;
}
return value.map((a: number) => unitToKiloUnit(a));
}
return value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { ReadonlyDeep } from 'type-fest';
import { DataType, FieldOptionType, FieldType } from './assignment.type';
import { LOAD_TYPES } from '../../../../../network/constants';
import { EquipmentType, kiloUnitToUnit, microUnitToUnit, unitToKiloUnit, unitToMicroUnit } from '@gridsuite/commons-ui';
import { EquipmentType } from '@gridsuite/commons-ui';
import { KILO_AMPERE, MICRO_SIEMENS } from '../../../../../utils/field-constants';

export const FIELD_OPTIONS = {
Expand Down Expand Up @@ -107,16 +107,12 @@ export const FIELD_OPTIONS = {
label: 'maxSusceptance',
unit: MICRO_SIEMENS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MICRO_SIEMENS to SIEMENS

Copy link
Contributor Author

@ghazwarhili ghazwarhili Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
MAXIMUM_Q_AT_NOMINAL_VOLTAGE: {
id: FieldType.MAXIMUM_Q_AT_NOMINAL_VOLTAGE,
label: 'maxQAtNominalV',
unit: MICRO_SIEMENS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVar and not Micro S

Copy link
Contributor Author

@ghazwarhili ghazwarhili Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
NOMINAL_VOLTAGE: {
id: FieldType.NOMINAL_VOLTAGE,
Expand All @@ -138,16 +134,12 @@ export const FIELD_OPTIONS = {
label: 'LowShortCircuitCurrentLimit',
unit: KILO_AMPERE,
dataType: DataType.DOUBLE,
outputConverter: (value) => kiloUnitToUnit(value),
inputConverter: (value) => unitToKiloUnit(value),
},
HIGH_SHORT_CIRCUIT_CURRENT_LIMIT: {
id: FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT,
label: 'HighShortCircuitCurrentLimit',
unit: KILO_AMPERE,
dataType: DataType.DOUBLE,
outputConverter: (value) => kiloUnitToUnit(value),
inputConverter: (value) => unitToKiloUnit(value),
},
ACTIVE_POWER: {
id: FieldType.ACTIVE_POWER,
Expand All @@ -174,16 +166,12 @@ export const FIELD_OPTIONS = {
label: 'G',
unit: MICRO_SIEMENS,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
B: {
id: FieldType.B,
label: 'B',
unit: MICRO_SIEMENS,
dataType: DataType.DOUBLE,
outputConverter: (value) => microUnitToUnit(value),
inputConverter: (value) => unitToMicroUnit(value),
},
RATED_U1: {
id: FieldType.RATED_U1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ export const getFieldOption = (fieldName?: string | null) => {
return Object.values(FIELD_OPTIONS).find((fieldOption) => fieldOption.id === fieldName);
};

export const convertOutputValue = (fieldName?: string | null, fieldValue?: FieldValue) => {
const fieldOption = getFieldOption(fieldName);
// @ts-expect-error TODO TS2339: Property outputConverter does not exist on typeof FIELD_OPTIONS[*]
return fieldOption?.outputConverter ? fieldOption.outputConverter(Number(fieldValue)) : fieldValue;
};

export const convertInputValue = (fieldName?: string | null, fieldValue?: FieldValue) => {
const fieldOption = getFieldOption(fieldName);
// @ts-expect-error TODO TS2339: Property inputConverter does not exist on typeof FIELD_OPTIONS[*]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { ASSIGNMENTS, EDITED_FIELD, EQUIPMENT_TYPE_FIELD, VALUE_FIELD } from '..
import { modifyByAssignment } from '../../../../../services/study/network-modifications';
import {
convertInputValue,
convertOutputValue,
getAssignmentFromEditData,
getAssignmentInitialValue,
getAssignmentsSchema,
getDataType,
} from './assignment/assignment-utils';
import { Assignment, ModificationByAssignment } from './assignment/assignment.type';
import { DeepNullable } from '../../../../utils/ts-utils';
import { convertOutputValues, FieldType } from '../../../converter-unit-utils';

const formSchema = yup
.object()
Expand Down Expand Up @@ -93,7 +93,11 @@ const ModificationByAssignmentDialog: FC<any> = ({
(formData: ModificationByAssignment) => {
const assignmentsList = formData[ASSIGNMENTS].map((assignment) => {
const dataType = getDataType(assignment[EDITED_FIELD]);
const valueConverted = convertOutputValue(assignment[EDITED_FIELD], assignment[VALUE_FIELD]);
console.log('=================assignment[EDITED_FIELD]', assignment[EDITED_FIELD]);
const fieldKey = assignment[EDITED_FIELD] as keyof typeof FieldType;
const field = FieldType[fieldKey];
const value = assignment[VALUE_FIELD];
const valueConverted = convertOutputValues(field, value);
return {
...assignment,
dataType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
R,
X,
} from 'components/utils/field-constants';
import { unitToMicroUnit } from 'utils/unit-converter';
import PropertiesForm from '../../common/properties/properties-form';
import useVoltageLevelsListInfos from '../../../../../hooks/use-voltage-levels-list-infos';
import GridSection from '../../../commons/grid-section';
import GridItem from '../../../commons/grid-item';
import { convertInputValues, FieldType } from '../../../converter-unit-utils';

const styles = {
h3: {
Expand Down Expand Up @@ -70,7 +70,7 @@ const LineCharacteristicsPane = ({
name={`${id}.${G1}`}
label="ShuntConductanceText"
adornment={MicroSusceptanceAdornment}
previousValue={unitToMicroUnit(lineToModify?.g1)}
previousValue={convertInputValues(FieldType.G1, lineToModify?.g1)}
clearable={clearableFields}
/>
);
Expand All @@ -80,7 +80,7 @@ const LineCharacteristicsPane = ({
name={`${id}.${B1}`}
label="ShuntSusceptanceText"
adornment={MicroSusceptanceAdornment}
previousValue={unitToMicroUnit(lineToModify?.b1)}
previousValue={convertInputValues(FieldType.B1, lineToModify?.b1)}
clearable={clearableFields}
/>
);
Expand All @@ -90,7 +90,7 @@ const LineCharacteristicsPane = ({
name={`${id}.${G2}`}
label="ShuntConductanceText"
adornment={MicroSusceptanceAdornment}
previousValue={unitToMicroUnit(lineToModify?.g2)}
previousValue={convertInputValues(FieldType.G2, lineToModify?.g2)}
clearable={clearableFields}
/>
);
Expand All @@ -100,7 +100,7 @@ const LineCharacteristicsPane = ({
name={`${id}.${B2}`}
label="ShuntSusceptanceText"
adornment={MicroSusceptanceAdornment}
previousValue={unitToMicroUnit(lineToModify?.b2)}
previousValue={convertInputValues(FieldType.B2, lineToModify?.b2)}
clearable={clearableFields}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import PropTypes from 'prop-types';
import { useCallback, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { FetchStatus } from '../../../../../services/utils';
import { microUnitToUnit, unitToMicroUnit } from 'utils/unit-converter';
import { FORM_LOADING_DELAY, UNDEFINED_CONNECTION_DIRECTION } from 'components/network/constants';
import yup from 'components/utils/yup-config';
import ModificationDialog from '../../../commons/modificationDialog';
Expand Down Expand Up @@ -76,6 +75,7 @@ import {
toModificationProperties,
} from '../../common/properties/property-utils';
import GridItem from '../../../commons/grid-item';
import { convertInputValues, convertOutputValues, FieldType } from '../../../converter-unit-utils';

const emptyFormData = {
...getHeaderEmptyFormData(),
Expand Down Expand Up @@ -149,10 +149,10 @@ const LineCreationDialog = ({
...getCharacteristicsFormData({
r: line.r,
x: line.x,
g1: unitToMicroUnit(line.g1), // this form uses and displays microSiemens
b1: unitToMicroUnit(line.b1),
g2: unitToMicroUnit(line.g2),
b2: unitToMicroUnit(line.b2),
g1: convertInputValues(FieldType.G1, line.g1), // this form uses and displays microSiemens
b1: convertInputValues(FieldType.B1, line.b1),
g2: convertInputValues(FieldType.G2, line.g2),
b2: convertInputValues(FieldType.B2, line.b2),
...(displayConnectivity &&
getConnectivityFormData(
{
Expand Down Expand Up @@ -200,10 +200,10 @@ const LineCreationDialog = ({
...getCharacteristicsFormData({
r: line.r,
x: line.x,
g1: unitToMicroUnit(line.g1),
b1: unitToMicroUnit(line.b1),
g2: unitToMicroUnit(line.g2),
b2: unitToMicroUnit(line.b2),
g1: convertInputValues(FieldType.G1, line.g1),
b1: convertInputValues(FieldType.B1, line.b1),
g2: convertInputValues(FieldType.G2, line.g2),
b2: convertInputValues(FieldType.B2, line.b2),
...getConnectivityFormData(
{
busbarSectionId: line.busOrBusbarSectionId1,
Expand Down Expand Up @@ -290,10 +290,10 @@ const LineCreationDialog = ({
sanitizeString(header[EQUIPMENT_NAME]),
characteristics[R],
characteristics[X],
microUnitToUnit(characteristics[G1]),
microUnitToUnit(characteristics[B1]),
microUnitToUnit(characteristics[G2]),
microUnitToUnit(characteristics[B2]),
convertOutputValues(FieldType.G1, characteristics[G1]),
convertOutputValues(FieldType.B1, characteristics[B1]),
convertOutputValues(FieldType.G2, characteristics[G2]),
convertOutputValues(FieldType.B2, characteristics[B2]),
characteristics[CONNECTIVITY_1]?.[VOLTAGE_LEVEL]?.id,
characteristics[CONNECTIVITY_1]?.[BUS_OR_BUSBAR_SECTION]?.id,
characteristics[CONNECTIVITY_2]?.[VOLTAGE_LEVEL]?.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
} from 'components/utils/field-constants';
import { useForm } from 'react-hook-form';
import { sanitizeString } from 'components/dialogs/dialog-utils';
import { microUnitToUnit, unitToMicroUnit } from 'utils/unit-converter';
import yup from 'components/utils/yup-config';
import ModificationDialog from '../../../commons/modificationDialog';

Expand Down Expand Up @@ -81,6 +80,7 @@ import {
getConnectivityFormData,
getCont1Cont2WithPositionEmptyFormData,
} from '../../../connectivity/connectivity-form-utils';
import { convertInputValues, convertOutputValues, FieldType } from '../../../converter-unit-utils';

export const LineModificationDialogTab = {
CONNECTIVITY_TAB: 0,
Expand Down Expand Up @@ -161,10 +161,10 @@ const LineModificationDialog = ({
...getCharacteristicsWithOutConnectivityFormData({
r: line.r?.value ?? null,
x: line.x?.value ?? null,
g1: unitToMicroUnit(line.g1?.value ?? null),
b1: unitToMicroUnit(line.b1?.value ?? null),
g2: unitToMicroUnit(line.g2?.value ?? null),
b2: unitToMicroUnit(line.b2?.value ?? null),
g1: convertInputValues(FieldType.G1, line.g1?.value ?? null),
b1: convertInputValues(FieldType.B1, line.b1?.value ?? null),
g2: convertInputValues(FieldType.G2, line.g2?.value ?? null),
b2: convertInputValues(FieldType.B2, line.b2?.value ?? null),
}),
...getLimitsFormData({
permanentLimit1: line.currentLimits1?.permanentLimit,
Expand Down Expand Up @@ -242,10 +242,10 @@ const LineModificationDialog = ({
sanitizeString(line[EQUIPMENT_NAME]),
characteristics[R],
characteristics[X],
microUnitToUnit(characteristics[G1]),
microUnitToUnit(characteristics[B1]),
microUnitToUnit(characteristics[G2]),
microUnitToUnit(characteristics[B2]),
convertOutputValues(FieldType.G1, characteristics[G1]),
convertOutputValues(FieldType.B1, characteristics[B1]),
convertOutputValues(FieldType.G2, characteristics[G2]),
convertOutputValues(FieldType.B2, characteristics[B2]),
currentLimits1,
currentLimits2,
connectivity1[VOLTAGE_LEVEL]?.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import { createTabulareModification } from 'services/study/network-modifications
import { FetchStatus } from 'services/utils';
import TabularModificationForm from './tabular-modification-form';
import {
convertValueFromBackToFront,
convertValueFromFrontToBack,
formatModification,
getEquipmentTypeFromModificationType,
TABULAR_MODIFICATION_TYPES,
} from './tabular-modification-utils';
import { useIntl } from 'react-intl';
import { convertInputValues, convertOutputValues, FieldType } from '../../converter-unit-utils';
import { toModificationOperation } from '../../../utils/utils.js';

const formSchema = yup
.object()
Expand Down Expand Up @@ -76,7 +76,7 @@ const TabularModificationDialog = ({
const modifications = editData?.modifications.map((modif) => {
const modification = {};
Object.keys(formatModification(modif)).forEach((key) => {
modification[key] = convertValueFromBackToFront(key, modif[key]);
modification[key] = convertInputValues(FieldType[key], modif[key]);
});
return modification;
});
Expand All @@ -95,7 +95,7 @@ const TabularModificationDialog = ({
type: modificationType,
};
Object.keys(row).forEach((key) => {
modification[key] = convertValueFromFrontToBack(key, row[key]);
modification[key] = toModificationOperation(convertOutputValues(FieldType[key], row[key]));
});
return modification;
});
Expand Down
Loading
Loading