Skip to content

Commit

Permalink
front: refacto formatPower restriction helpers function, split code i…
Browse files Browse the repository at this point in the history
…n different testable blocks to improve readability and maintanability
  • Loading branch information
Math-R committed Jun 26, 2024
1 parent 2cf5596 commit 3b4edbf
Showing 1 changed file with 65 additions and 116 deletions.
181 changes: 65 additions & 116 deletions front/src/modules/powerRestriction/helpers/formatPowerRestrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,135 +5,84 @@ import type { PowerRestrictionV2 } from 'applications/operationalStudies/consts'
import type { IntervalItem } from 'common/IntervalsEditor/types';
import type { PathStep } from 'reducers/osrdconf/types';

const addNoPowerRestrictions = (
acc: IntervalItem[],
from: number,
to: number,
changePoints: number[]
): void => {
changePoints.forEach((changePoint, idx, array) => {
const begin = idx === 0 ? from : array[idx - 1];
const end = idx === changePoints.length - 1 ? to : changePoint;
acc.push({
begin,
end,
value: 'NO_POWER_RESTRICTION',
});
});
};

const appendFinalNoPowerRestriction = (
ranges: IntervalItem[],
pathLength: number,
electrificationChangePoints: number[]
): void => {
const lastEnd = ranges.length ? ranges[ranges.length - 1].end : 0;
if (lastEnd < pathLength) {
const finalChangePoints = electrificationChangePoints.filter((cp) => cp > lastEnd);
if (finalChangePoints.length > 0) {
addNoPowerRestrictions(ranges, lastEnd, pathLength, finalChangePoints);
} else {
ranges.push({ begin: lastEnd, end: pathLength, value: 'NO_POWER_RESTRICTION' });
}
}
};

const reducePowerRestrictions =
(pathStepById: Record<string, PathStep>, electrificationChangePoints: number[]) =>
(acc: IntervalItem[], restriction: PowerRestrictionV2, index: number): IntervalItem[] => {
const fromPathStep = pathStepById[restriction.from];
const toPathStep = pathStepById[restriction.to];
const from = fromPathStep?.positionOnPath;
const to = toPathStep?.positionOnPath;
const prevEnd = isEmpty(acc) ? 0 : acc[acc.length - 1].end;

if (from !== undefined && to !== undefined) {
if (index === 0 || from > prevEnd) {
const gapChangePoints = electrificationChangePoints.filter(
(cp) => cp > prevEnd && cp < from
);
if (gapChangePoints.length > 0) {
addNoPowerRestrictions(acc, prevEnd, from, gapChangePoints);
} else {
acc.push({ begin: prevEnd, end: from, value: 'NO_POWER_RESTRICTION' });
}
}
acc.push({ begin: from, end: to, value: restriction.code });
}
return acc;
};

const formatPowerRestrictions = (
powerRestrictionRanges: PowerRestrictionV2[],
changePoints: number[],
pathSteps: PathStep[],
pathLength: number
) => {
// créer un dictionnaire de pathSteps par id
): IntervalItem[] => {
const pathStepById = keyBy(pathSteps, 'id');
const electrificationChangePoints = sortBy(changePoints, (position) => position);

// parcourir toutes les restrictions de puissance
const formattedPowerRestrictionRanges = powerRestrictionRanges.reduce(
(acc, restriction, index) => {
// on récupère la position du from de la restriction courante
const fromPathStep = pathStepById[restriction.from];
const toPathStep = pathStepById[restriction.to];
const from = fromPathStep?.positionOnPath;
const to = toPathStep?.positionOnPath;
const prevEnd = isEmpty(acc) ? 0 : acc[acc.length - 1].end;

// on regarde s'il y a un trou entre la restriction précédente et la courante
if (from !== undefined && to !== undefined) {
if (index === 0 || from > acc[acc.length - 1].end) {
// s'il y a un trou, on regarde s'il y a des électrificationChangePoints dedans
const insideChangePoints = electrificationChangePoints.filter(
(changePoint) => changePoint > prevEnd && changePoint < from
);
if (insideChangePoints.length) {
// si oui, on crée des ranges avec NO_POWER_RESTRICTION en valeur
insideChangePoints.forEach((changePoint, idx) => {
if (idx === 0) {
acc.push({
begin: prevEnd,
end: changePoint,
value: 'NO_POWER_RESTRICTION',
});
}
acc.push({
begin: changePoint,
end: idx === insideChangePoints.length - 1 ? from : insideChangePoints[idx + 1],
value: 'NO_POWER_RESTRICTION',
});
});
} else {
// si non, on crée une seule range avec NO_POWER_RESTRICTION
acc.push({
begin: prevEnd,
end: from,
value: 'NO_POWER_RESTRICTION',
});
}
}

// on ajoute la restriction de puissance
acc.push({
begin: from,
end: to,
value: restriction.code,
});
}
return acc;
},
reducePowerRestrictions(pathStepById, electrificationChangePoints),
[] as IntervalItem[]
);

// on regarde s'il y a des trous
// s'il y a un trou, on regarde s'il y a des électrificationChangePoints dedans
// si oui, on crée des ranges avec NO_POWER_RESTRICTION en valeur
// si non, on crée une seule range avec NO_POWER_RESTRICTION

if (formattedPowerRestrictionRanges.length !== 0) {
const lastPowerRestrictionEnd =
formattedPowerRestrictionRanges[formattedPowerRestrictionRanges.length - 1].end;
if (lastPowerRestrictionEnd !== pathLength) {
const insideChangePoints = electrificationChangePoints.filter(
(changePoint) => changePoint > lastPowerRestrictionEnd
);
if (insideChangePoints.length) {
insideChangePoints.forEach((changePoint, idx) => {
if (idx === 0) {
formattedPowerRestrictionRanges.push({
begin: lastPowerRestrictionEnd,
end: changePoint,
value: 'NO_POWER_RESTRICTION',
});
}
formattedPowerRestrictionRanges.push({
begin: changePoint,
end: idx === insideChangePoints.length - 1 ? pathLength : insideChangePoints[idx + 1],
value: 'NO_POWER_RESTRICTION',
});
});
} else {
formattedPowerRestrictionRanges.push({
begin: lastPowerRestrictionEnd,
end: pathLength,
value: 'NO_POWER_RESTRICTION',
});
}
}
} else if (electrificationChangePoints.length) {
electrificationChangePoints.forEach((changePoint, idx) => {
if (idx === 0) {
formattedPowerRestrictionRanges.push({
begin: 0,
end: changePoint,
value: 'NO_POWER_RESTRICTION',
});
}
formattedPowerRestrictionRanges.push({
begin: changePoint,
end:
idx === electrificationChangePoints.length - 1
? pathLength
: electrificationChangePoints[idx + 1],
value: 'NO_POWER_RESTRICTION',
});
});
} else {
formattedPowerRestrictionRanges.push({
begin: 0,
end: pathLength,
value: 'NO_POWER_RESTRICTION',
});
}
// begin: formattedPowerRestrictionRanges[formattedPowerRestrictionRanges.length -1].end
// end: position du dernier pathStep ou longueur totale du chemin ?
appendFinalNoPowerRestriction(
formattedPowerRestrictionRanges,
pathLength,
electrificationChangePoints
);

return formattedPowerRestrictionRanges;
};

export default formatPowerRestrictions;

0 comments on commit 3b4edbf

Please sign in to comment.