Skip to content

Commit

Permalink
♻️ : change calculation of the duration of a job
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Oct 25, 2019
1 parent d383cee commit 5847f55
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/main/java/io/codeka/gaia/stacks/bo/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import io.codeka.gaia.teams.User;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
Expand Down Expand Up @@ -47,6 +46,11 @@ public void end(JobStatus jobStatus) {
this.status = jobStatus;
}

public void proceed(JobStatus jobStatus) {
this.endDateTime = null;
this.status = jobStatus;
}

public void reset() {
this.status = null;
this.startDateTime = null;
Expand Down Expand Up @@ -127,12 +131,9 @@ public void setUser(User user) {
}

public Long getExecutionTime() {
if (CollectionUtils.isEmpty(this.steps)) {
if (this.startDateTime == null || this.endDateTime == null) {
return null;
}
return this.steps.stream()
.map(Step::getExecutionTime)
.filter(Objects::nonNull)
.reduce(null, (a, b) -> a == null ? b : a + b);
return Duration.between(this.startDateTime, this.endDateTime).toMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PlanFinishedState implements JobState {
@Override
public void apply(JobWorkflow jobWorkflow) {
var job = jobWorkflow.getJob();
job.setStatus(JobStatus.APPLY_STARTED);
job.proceed(JobStatus.APPLY_STARTED);

var step = new Step(StepType.APPLY, job.getId());
job.getSteps().add(step);
Expand Down
49 changes: 35 additions & 14 deletions src/test/java/io/codeka/gaia/stacks/bo/JobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.codeka.gaia.teams.User;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -116,6 +117,31 @@ void reset_shouldResetSteps() {
assertThat(job.getSteps()).isNotNull().isEmpty();
}

@Test
void proceed_shouldSetStatus() {
// given
var job = new Job();

// when
job.proceed(JobStatus.APPLY_STARTED);

// then
assertEquals(JobStatus.APPLY_STARTED, job.getStatus());
}

@Test
void proceed_shouldResetEndDateTime() {
// given
var job = new Job();
job.setEndDateTime(LocalDateTime.now());

// when
job.proceed(null);

// then
assertNull(job.getEndDateTime());
}

@Test
void job_shouldSetId() {
var job = new Job(null, null, null);
Expand Down Expand Up @@ -154,28 +180,25 @@ void job_shouldSetUser() {
}

@Test
void getExecutionTime_shouldReturnSumOfStepExecutionTime() {
void getExecutionTime_shouldReturnDuration_betweenStartDateTime_andEndDateTime() {
// given
var job = new Job();
var step = new Step();
step.setExecutionTime(10L);
job.getSteps().add(step);
step = new Step();
step.setExecutionTime(20L);
job.getSteps().add(step);
job.getSteps().add(new Step());
job.setStartDateTime(LocalDateTime.now());
job.setEndDateTime(LocalDateTime.now());
var timer = Duration.between(job.getStartDateTime(), job.getEndDateTime()).toMillis();

// when
var result = job.getExecutionTime();

// then
assertEquals(30L, result);
assertThat(result).isNotNull().isEqualTo(timer);
}

@Test
void getExecutionTime_shouldReturnNullIfNoSteps() {
void getExecutionTime_shouldReturnNull_whenStartDateTimeIsNull() {
// given
var job = new Job();
job.setEndDateTime(LocalDateTime.now());

// when
var result = job.getExecutionTime();
Expand All @@ -185,12 +208,10 @@ void getExecutionTime_shouldReturnNullIfNoSteps() {
}

@Test
void getExecutionTime_shouldReturnNullIfNoStepsWithExecutionTime() {
void getExecutionTime_shouldReturnNull_whenEndDateTimeIsNull() {
// given
var job = new Job();
job.getSteps().add(new Step());
job.getSteps().add(new Step());
job.getSteps().add(new Step());
job.setStartDateTime(LocalDateTime.now());

// when
var result = job.getExecutionTime();
Expand Down

0 comments on commit 5847f55

Please sign in to comment.