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 conversion into one file #651

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/components/filter/expert/ExpertFilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import {
RULES,
} from './expertFilterConstants';

import { FieldType } from './expertFilter.type';
import { FieldConstants } from '../../../utils/constants/fieldConstants';
import { InputWithPopupConfirmation } from '../../inputs/reactHookForm/selectInputs/InputWithPopupConfirmation';
import { SelectInput } from '../../inputs/reactHookForm/selectInputs/SelectInput';
import { FilterType } from '../constants/FilterConstants';
import { CustomReactQueryBuilder } from '../../inputs/reactQueryBuilder/CustomReactQueryBuilder';
import { unscrollableDialogStyles } from '../../dialogs';
import { FieldType } from '../../../utils';

yup.setLocale({
mixed: {
Expand Down
138 changes: 1 addition & 137 deletions src/components/filter/expert/expertFilter.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { FullField } from 'react-querybuilder';
import { UUID } from 'crypto';
import { FieldType } from '../../../utils';

export enum OperatorType {
EQUALS = 'EQUALS',
Expand All @@ -31,143 +32,6 @@ export enum CombinatorType {
OR = 'OR',
}

export enum FieldType {
ID = 'ID',
NAME = 'NAME',
NOMINAL_VOLTAGE = 'NOMINAL_VOLTAGE',
MIN_P = 'MIN_P',
MAX_P = 'MAX_P',
TARGET_V = 'TARGET_V',
TARGET_P = 'TARGET_P',
TARGET_Q = 'TARGET_Q',
ENERGY_SOURCE = 'ENERGY_SOURCE',
COUNTRY = 'COUNTRY',
VOLTAGE_REGULATOR_ON = 'VOLTAGE_REGULATOR_ON',
PLANNED_ACTIVE_POWER_SET_POINT = 'PLANNED_ACTIVE_POWER_SET_POINT',
RATED_S = 'RATED_S',
RATED_S1 = 'RATED_S1',
RATED_S2 = 'RATED_S2',
RATED_S3 = 'RATED_S3',
MARGINAL_COST = 'MARGINAL_COST',
PLANNED_OUTAGE_RATE = 'PLANNED_OUTAGE_RATE',
FORCED_OUTAGE_RATE = 'FORCED_OUTAGE_RATE',
VOLTAGE_LEVEL_ID = 'VOLTAGE_LEVEL_ID',
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',
SHUNT_COMPENSATOR_TYPE = 'SHUNT_COMPENSATOR_TYPE',
CONNECTED = 'CONNECTED',
MAX_Q_AT_NOMINAL_V = 'MAX_Q_AT_NOMINAL_V',
MIN_Q_AT_NOMINAL_V = 'MIN_Q_AT_NOMINAL_V',
FIX_Q_AT_NOMINAL_V = 'FIX_Q_AT_NOMINAL_V',
SWITCHED_ON_Q_AT_NOMINAL_V = 'SWITCHED_ON_Q_AT_NOMINAL_V',
MAX_SUSCEPTANCE = 'MAX_SUSCEPTANCE',
MIN_SUSCEPTANCE = 'MIN_SUSCEPTANCE',
SWITCHED_ON_SUSCEPTANCE = 'SWITCHED_ON_SUSCEPTANCE',
CONNECTED_1 = 'CONNECTED_1',
CONNECTED_2 = 'CONNECTED_2',
CONNECTED_3 = 'CONNECTED_3',
VOLTAGE_LEVEL_ID_1 = 'VOLTAGE_LEVEL_ID_1',
VOLTAGE_LEVEL_ID_2 = 'VOLTAGE_LEVEL_ID_2',
VOLTAGE_LEVEL_ID_3 = 'VOLTAGE_LEVEL_ID_3',
NOMINAL_VOLTAGE_1 = 'NOMINAL_VOLTAGE_1',
NOMINAL_VOLTAGE_2 = 'NOMINAL_VOLTAGE_2',
NOMINAL_VOLTAGE_3 = 'NOMINAL_VOLTAGE_3',
COUNTRY_1 = 'COUNTRY_1',
COUNTRY_2 = 'COUNTRY_2',
SERIE_RESISTANCE = 'SERIE_RESISTANCE',
SERIE_RESISTANCE_1 = 'SERIE_RESISTANCE_1',
SERIE_RESISTANCE_2 = 'SERIE_RESISTANCE_2',
SERIE_RESISTANCE_3 = 'SERIE_RESISTANCE_3',
SERIE_REACTANCE = 'SERIE_REACTANCE',
SERIE_REACTANCE_1 = 'SERIE_REACTANCE_1',
SERIE_REACTANCE_2 = 'SERIE_REACTANCE_2',
SERIE_REACTANCE_3 = 'SERIE_REACTANCE_3',
SHUNT_CONDUCTANCE = 'SHUNT_CONDUCTANCE',
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',
MAGNETIZING_CONDUCTANCE = 'MAGNETIZING_CONDUCTANCE',
MAGNETIZING_CONDUCTANCE_1 = 'MAGNETIZING_CONDUCTANCE_1',
MAGNETIZING_CONDUCTANCE_2 = 'MAGNETIZING_CONDUCTANCE_2',
MAGNETIZING_CONDUCTANCE_3 = 'MAGNETIZING_CONDUCTANCE_3',
MAGNETIZING_SUSCEPTANCE = 'MAGNETIZING_SUSCEPTANCE',
MAGNETIZING_SUSCEPTANCE_1 = 'MAGNETIZING_SUSCEPTANCE_1',
MAGNETIZING_SUSCEPTANCE_2 = 'MAGNETIZING_SUSCEPTANCE_2',
MAGNETIZING_SUSCEPTANCE_3 = 'MAGNETIZING_SUSCEPTANCE_3',
LOAD_TYPE = 'LOAD_TYPE',
RATED_VOLTAGE_0 = 'RATED_VOLTAGE_0',
RATED_VOLTAGE_1 = 'RATED_VOLTAGE_1',
RATED_VOLTAGE_2 = 'RATED_VOLTAGE_2',
RATED_VOLTAGE_3 = 'RATED_VOLTAGE_3',
HAS_RATIO_TAP_CHANGER = 'HAS_RATIO_TAP_CHANGER',
HAS_RATIO_TAP_CHANGER_1 = 'HAS_RATIO_TAP_CHANGER_1',
HAS_RATIO_TAP_CHANGER_2 = 'HAS_RATIO_TAP_CHANGER_2',
HAS_RATIO_TAP_CHANGER_3 = 'HAS_RATIO_TAP_CHANGER_3',
LOAD_TAP_CHANGING_CAPABILITIES = 'LOAD_TAP_CHANGING_CAPABILITIES',
LOAD_TAP_CHANGING_CAPABILITIES_1 = 'LOAD_TAP_CHANGING_CAPABILITIES_1',
LOAD_TAP_CHANGING_CAPABILITIES_2 = 'LOAD_TAP_CHANGING_CAPABILITIES_2',
LOAD_TAP_CHANGING_CAPABILITIES_3 = 'LOAD_TAP_CHANGING_CAPABILITIES_3',
RATIO_REGULATION_MODE = 'RATIO_REGULATION_MODE',
RATIO_REGULATION_MODE_1 = 'RATIO_REGULATION_MODE_1',
RATIO_REGULATION_MODE_2 = 'RATIO_REGULATION_MODE_2',
RATIO_REGULATION_MODE_3 = 'RATIO_REGULATION_MODE_3',
RATIO_TARGET_V = 'RATIO_TARGET_V',
RATIO_TARGET_V1 = 'RATIO_TARGET_V1',
RATIO_TARGET_V2 = 'RATIO_TARGET_V2',
RATIO_TARGET_V3 = 'RATIO_TARGET_V3',
HAS_PHASE_TAP_CHANGER = 'HAS_PHASE_TAP_CHANGER',
HAS_PHASE_TAP_CHANGER_1 = 'HAS_PHASE_TAP_CHANGER_1',
HAS_PHASE_TAP_CHANGER_2 = 'HAS_PHASE_TAP_CHANGER_2',
HAS_PHASE_TAP_CHANGER_3 = 'HAS_PHASE_TAP_CHANGER_3',
PHASE_REGULATION_MODE = 'PHASE_REGULATION_MODE',
PHASE_REGULATION_MODE_1 = 'PHASE_REGULATION_MODE_1',
PHASE_REGULATION_MODE_2 = 'PHASE_REGULATION_MODE_2',
PHASE_REGULATION_MODE_3 = 'PHASE_REGULATION_MODE_3',
PHASE_REGULATION_VALUE = 'PHASE_REGULATION_VALUE',
PHASE_REGULATION_VALUE_1 = 'PHASE_REGULATION_VALUE_1',
PHASE_REGULATION_VALUE_2 = 'PHASE_REGULATION_VALUE_2',
PHASE_REGULATION_VALUE_3 = 'PHASE_REGULATION_VALUE_3',
PROPERTY = 'FREE_PROPERTIES',
SUBSTATION_PROPERTY = 'SUBSTATION_PROPERTIES',
SUBSTATION_PROPERTY_1 = 'SUBSTATION_PROPERTIES_1',
SUBSTATION_PROPERTY_2 = 'SUBSTATION_PROPERTIES_2',
VOLTAGE_LEVEL_PROPERTY = 'VOLTAGE_LEVEL_PROPERTIES',
VOLTAGE_LEVEL_PROPERTY_1 = 'VOLTAGE_LEVEL_PROPERTIES_1',
VOLTAGE_LEVEL_PROPERTY_2 = 'VOLTAGE_LEVEL_PROPERTIES_2',
VOLTAGE_LEVEL_PROPERTY_3 = 'VOLTAGE_LEVEL_PROPERTIES_3',
SVAR_REGULATION_MODE = 'SVAR_REGULATION_MODE',
VOLTAGE_SET_POINT = 'VOLTAGE_SET_POINT',
ACTIVE_POWER_SET_POINT = 'ACTIVE_POWER_SET_POINT',
REACTIVE_POWER_SET_POINT = 'REACTIVE_POWER_SET_POINT',
REMOTE_REGULATED_TERMINAL = 'REMOTE_REGULATED_TERMINAL', // composite rule of REGULATING_TERMINAL_VL_ID and/or REGULATING_TERMINAL_CONNECTABLE_ID
REGULATING_TERMINAL_VL_ID = 'REGULATING_TERMINAL_VL_ID',
REGULATING_TERMINAL_CONNECTABLE_ID = 'REGULATING_TERMINAL_CONNECTABLE_ID',
REGULATION_TYPE = 'REGULATION_TYPE',
AUTOMATE = 'AUTOMATE',
LOW_VOLTAGE_SET_POINT = 'LOW_VOLTAGE_SET_POINT',
HIGH_VOLTAGE_SET_POINT = 'HIGH_VOLTAGE_SET_POINT',
LOW_VOLTAGE_THRESHOLD = 'LOW_VOLTAGE_THRESHOLD',
HIGH_VOLTAGE_THRESHOLD = 'HIGH_VOLTAGE_THRESHOLD',
SUSCEPTANCE_FIX = 'SUSCEPTANCE_FIX',
PAIRED = 'PAIRED',
CONVERTERS_MODE = 'CONVERTERS_MODE',
CONVERTER_STATION_ID_1 = 'CONVERTER_STATION_ID_1',
CONVERTER_STATION_ID_2 = 'CONVERTER_STATION_ID_2',
CONVERTER_STATION_NOMINAL_VOLTAGE_1 = 'CONVERTER_STATION_NOMINAL_VOLTAGE_1',
CONVERTER_STATION_NOMINAL_VOLTAGE_2 = 'CONVERTER_STATION_NOMINAL_VOLTAGE_2',
DC_NOMINAL_VOLTAGE = 'DC_NOMINAL_VOLTAGE',
PAIRING_KEY = 'PAIRING_KEY',
TIE_LINE_ID = 'TIE_LINE_ID',
LOW_SHORT_CIRCUIT_CURRENT_LIMIT = 'LOW_SHORT_CIRCUIT_CURRENT_LIMIT',
HIGH_SHORT_CIRCUIT_CURRENT_LIMIT = 'HIGH_SHORT_CIRCUIT_CURRENT_LIMIT',
}

export enum DataType {
STRING = 'STRING',
ENUM = 'ENUM',
Expand Down
3 changes: 2 additions & 1 deletion src/components/filter/expert/expertFilterConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { Field } from 'react-querybuilder';
import { CombinatorType, DataType, FieldType, OperatorType } from './expertFilter.type';
import { CombinatorType, DataType, OperatorType } from './expertFilter.type';
import { FieldType } from '../../../utils';

export enum RULES {
EMPTY_RULE = 'emptyRule',
Expand Down
53 changes: 6 additions & 47 deletions src/components/filter/expert/expertFilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,14 @@ import {
CompositeField,
CompositeGroup,
DataType,
FieldType,
OperatorOption,
OperatorType,
RuleGroupTypeExport,
RuleTypeExport,
} from './expertFilter.type';
import { FIELDS_OPTIONS, OPERATOR_OPTIONS, RULES } from './expertFilterConstants';
import {
isBlankOrEmpty,
kiloUnitToUnit,
microUnitToUnit,
unitToKiloUnit,
unitToMicroUnit,
} from '../../../utils/conversionUtils';

const microUnits = [
FieldType.SHUNT_CONDUCTANCE_1,
FieldType.SHUNT_CONDUCTANCE_2,
FieldType.SHUNT_SUSCEPTANCE_1,
FieldType.SHUNT_SUSCEPTANCE_2,
];

const kiloUnits = [FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, FieldType.LOW_SHORT_CIRCUIT_CURRENT_LIMIT];
import { convertInputValues, convertOutputValues, isBlankOrEmpty } from '../../../utils/conversionUtils';
import { FieldType } from '../../../utils';

interface TreeNode {
[key: string]: any;
Expand Down Expand Up @@ -215,22 +200,6 @@ export const getOperators = (fieldName: string, intl: IntlShape) => {
}
};

function changeValueUnit(value: any, field: FieldType) {
if (microUnits.includes(field)) {
if (!Array.isArray(value)) {
return microUnitToUnit(value);
}
return value.map((a: number) => microUnitToUnit(a));
}
if (kiloUnits.includes(field)) {
if (!Array.isArray(value)) {
return kiloUnitToUnit(value);
}
return value.map((a: number) => kiloUnitToUnit(a));
}
return value;
}

export function exportExpertRules(query: RuleGroupType): RuleGroupTypeExport {
function transformRule(rule: RuleType): RuleTypeExport | RuleGroupTypeExport {
const isValueAnArray = Array.isArray(rule.value);
Expand All @@ -256,11 +225,11 @@ export function exportExpertRules(query: RuleGroupType): RuleGroupTypeExport {
rule.operator !== OPERATOR_OPTIONS.EXISTS.name &&
rule.operator !== OPERATOR_OPTIONS.NOT_EXISTS.name &&
dataType !== DataType.PROPERTY
? changeValueUnit(rule.value, rule.field as FieldType)
? convertOutputValues(rule.field as FieldType, rule.value)
: undefined,
values:
isValueAnArray && dataType !== DataType.PROPERTY
? changeValueUnit(rule.value, rule.field as FieldType)
? convertOutputValues(rule.field as FieldType, rule.value)
: undefined,
dataType,
propertyName: dataType === DataType.PROPERTY ? rule.value.propertyName : undefined,
Expand Down Expand Up @@ -325,22 +294,12 @@ export function importExpertRules(query: RuleGroupTypeExport): RuleGroupType {
if (rule.dataType === DataType.NUMBER) {
return rule.values
.map((value) => parseFloat(value as string))
.map((numberValue) => {
return microUnits.includes(rule.field) ? unitToMicroUnit(numberValue)! : numberValue;
})
.map((numberValue) => {
return kiloUnits.includes(rule.field) ? unitToKiloUnit(numberValue)! : numberValue;
})
.map((numberValue) => convertInputValues(rule.field, numberValue))
.sort((a, b) => a - b);
}
return rule.values.sort();
}
if (microUnits.includes(rule.field)) {
return unitToMicroUnit(parseFloat(rule.value as string));
}
if (kiloUnits.includes(rule.field)) {
return unitToKiloUnit(parseFloat(rule.value as string));
}
convertInputValues(rule.field, parseFloat(rule.value as string));
return rule.value;
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/inputs/reactQueryBuilder/ValueEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CountryValueEditor } from './CountryValueEditor';
import { TranslatedValueEditor } from './TranslatedValueEditor';
import { TextValueEditor } from './TextValueEditor';

import { DataType, FieldType } from '../../filter/expert/expertFilter.type';
import { DataType } from '../../filter/expert/expertFilter.type';
import { FieldConstants } from '../../../utils/constants/fieldConstants';
import { Substation, VoltageLevel } from '../../../utils/types/equipmentTypes';
import { ElementValueEditor } from './ElementValueEditor';
Expand All @@ -23,6 +23,7 @@ import { PropertyValueEditor } from './PropertyValueEditor';
import { FilterType } from '../../filter/constants/FilterConstants';
import { GroupValueEditor } from './compositeRuleEditor/GroupValueEditor';
import { OPERATOR_OPTIONS } from '../../filter/expert/expertFilterConstants';
import { FieldType } from '../../../utils';

const styles = {
noArrows: {
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export * from './browserConstants';
export * from './fetchStatus';
export * from './fieldConstants';
export * from '../types/fieldType';
78 changes: 74 additions & 4 deletions src/utils/conversionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 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/.
*/
import { FieldType } from './constants';

export const GRIDSUITE_DEFAULT_PRECISION: number = 13;

Expand All @@ -21,10 +22,79 @@ export function isBlankOrEmpty(value: unknown) {
return false;
}

export const unitToMicroUnit = (num: number) => (isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num * 1e6));
export function unitToMicroUnit(num: number) {
return isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num * 1e6);
}

export const microUnitToUnit = (num: number) => (isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num / 1e6));
export function microUnitToUnit(num: number) {
return isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num / 1e6);
}

export const unitToKiloUnit = (num: number) => (isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num / 1e3));
export function unitToKiloUnit(num: number) {
return isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num / 1e3);
}

export function kiloUnitToUnit(num: number) {
return isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num * 1e3);
}

export const kiloUnitToUnit = (num: number) => (isBlankOrEmpty(num) ? undefined : roundToDefaultPrecision(num * 1e3));
const microToUnit = [
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 kiloToUnit = [FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, FieldType.LOW_SHORT_CIRCUIT_CURRENT_LIMIT];

const unitToMicro = [FieldType.MAX_SUSCEPTANCE];

export function convertInputValues(field: FieldType, value: any) {
if (microToUnit.includes(field)) {
if (!Array.isArray(value)) {
return value ? unitToMicroUnit(value) : value;
}
return value.map((a: number) => unitToMicroUnit(a));
}
if (kiloToUnit.includes(field)) {
if (!Array.isArray(value)) {
return value ? unitToKiloUnit(value) : value;
}
return value.map((a: number) => unitToKiloUnit(a));
}
if (unitToMicro.includes(field)) {
if (!Array.isArray(value)) {
return value ? microUnitToUnit(value) : value;
}
return value.map((a: number) => microUnitToUnit(a));
}
return value;
}

export function convertOutputValues(field: FieldType, value: any) {
if (microToUnit.includes(field)) {
if (!Array.isArray(value)) {
return value ? microUnitToUnit(value) : value;
}
return value.map((a: number) => microUnitToUnit(a));
}
if (kiloToUnit.includes(field)) {
if (!Array.isArray(value)) {
return value ? kiloUnitToUnit(value) : value;
}
return value.map((a: number) => kiloUnitToUnit(a));
}
if (unitToMicro.includes(field)) {
if (!Array.isArray(value)) {
return value ? unitToMicroUnit(value) : value;
}
return value.map((a: number) => unitToMicroUnit(a));
}
return value;
}
Loading
Loading