Skip to content

Commit

Permalink
[backend] fix pause computation in inject start date (#2282)
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine MAZEAS <[email protected]>
  • Loading branch information
antoinemzs authored Jan 30, 2025
1 parent d47e0fe commit be37ea6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.openbas.migration;

import java.sql.Connection;
import java.sql.Statement;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.stereotype.Component;

@Component
public class V3_62__make_exercise_pause_tz_aware extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
Connection connection = context.getConnection();
Statement statement = connection.createStatement();

statement.execute(
"""
ALTER TABLE exercises ADD COLUMN exercise_pause_date_tempwithtz TIMESTAMP WITH TIME ZONE;
UPDATE exercises SET exercise_pause_date_tempwithtz = exercise_pause_date;
ALTER TABLE exercises DROP COLUMN exercise_pause_date;
ALTER TABLE exercises RENAME COLUMN exercise_pause_date_tempwithtz TO exercise_pause_date;
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -75,31 +76,36 @@ public static Instant computeInjectDate(
Instant source, int speed, Long dependsDuration, Exercise exercise) {
// Compute origin execution date
long duration = ofNullable(dependsDuration).orElse(0L) / speed;
Instant dependingStart = source;
Instant standardExecutionDate = dependingStart.plusSeconds(duration);
Instant standardExecutionDate = source.plusSeconds(duration);
// Compute execution dates with previous terminated pauses
Instant afterPausesExecutionDate = standardExecutionDate;
List<Pause> sortedPauses =
new ArrayList<>(
exercise.getPauses().stream()
.sorted(
(pause0, pause1) ->
pause0.getDate().equals(pause1.getDate())
? 0
: pause0.getDate().isBefore(pause1.getDate()) ? -1 : 1)
.toList());
long previousPauseDelay = 0L;
if (exercise != null) {
previousPauseDelay =
exercise.getPauses().stream()
.filter(pause -> pause.getDate().isBefore(standardExecutionDate))
.mapToLong(pause -> pause.getDuration().orElse(0L))
.sum();
for (Pause pause : sortedPauses) {
if (pause.getDate().isAfter(afterPausesExecutionDate)) {
break;
}
previousPauseDelay += pause.getDuration().orElse(0L);
afterPausesExecutionDate = standardExecutionDate.plusSeconds(previousPauseDelay);
}
Instant afterPausesExecutionDate = standardExecutionDate.plusSeconds(previousPauseDelay);

// Add current pause duration in date computation if needed
long currentPauseDelay = 0L;
if (exercise != null) {
currentPauseDelay =
exercise
.getCurrentPause()
.map(
last ->
last.isBefore(afterPausesExecutionDate)
? between(last, now()).getSeconds()
: 0L)
.orElse(0L);
}
long currentPauseDelay;
Instant finalAfterPausesExecutionDate = afterPausesExecutionDate;
currentPauseDelay =
exercise
.getCurrentPause()
.filter(pauseTime -> pauseTime.isBefore(finalAfterPausesExecutionDate))
.map(pauseTime -> between(pauseTime, now()).getSeconds())
.orElse(0L);
long globalPauseDelay = previousPauseDelay + currentPauseDelay;
long minuteAlignModulo = globalPauseDelay % 60;
long alignedPauseDelay =
Expand Down

0 comments on commit be37ea6

Please sign in to comment.