Skip to content

Commit

Permalink
fix(frontend): truncate time intervals
Browse files Browse the repository at this point in the history
Add a check for `tMax`, the duration of a simulation, and truncate any time intervals that exceed it.
  • Loading branch information
eatyourgreens committed Nov 27, 2024
1 parent 69b0031 commit 1f34d82
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions frontend-v2/src/features/results/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,30 @@ export function valuesPerInterval(
simulation?: SimulateResponse,
) {
const times = simulation?.time || [];
const tMax = times[times.length - 1];
const values = variable && simulation ? simulation.outputs[variable.id] : [];
return timeIntervals.map((interval) => {
if (values.length === 0) {
return [];
}
const startIndex = times.findIndex((t) => t >= interval.start_time);
const endIndex = times.findIndex((t) => t >= interval.end_time);
const startTime = Math.min(tMax, interval.start_time);
const endTime = Math.min(tMax, interval.end_time);
const startIndex = times.findIndex((t) => t >= startTime);
const endIndex = times.findIndex((t) => t >= endTime);
const start =
startIndex > 0
? interpolate(
[times[startIndex - 1], times[startIndex]],
[values[startIndex - 1], values[startIndex]],
interval.start_time,
startTime,
)
: values[0];
const end =
endIndex > -1
? interpolate(
[times[endIndex - 1], times[endIndex]],
[values[endIndex - 1], values[endIndex]],
interval.end_time,
endTime,
)
: values[values.length - 1];
const intervalValues = [
Expand All @@ -74,14 +77,18 @@ export function timesPerInterval(
times: number[],
timeIntervals: TimeIntervalRead[],
) {
const tMax = times[times.length - 1];
return timeIntervals.map((interval) => {
const startIndex = times.findIndex((t) => t >= interval.start_time);
const endIndex = times.findIndex((t) => t >= interval.end_time);
return [
interval.start_time,
const startTime = Math.min(tMax, interval.start_time);
const endTime = Math.min(tMax, interval.end_time);
const startIndex = times.findIndex((t) => t >= startTime);
const endIndex = times.findIndex((t) => t >= endTime);
const intervalTimes = [
startTime,
...times.slice(startIndex + 1, endIndex - 1),
interval.end_time,
endTime,
];
return intervalTimes;
});
}

Expand Down

0 comments on commit 1f34d82

Please sign in to comment.