Skip to content

Commit

Permalink
✨ : add execution time for jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Aug 6, 2019
1 parent bd6f2c4 commit b467dea
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 19 deletions.
42 changes: 33 additions & 9 deletions src/main/java/io/codeka/gaia/bo/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.io.StringWriter;
import java.io.Writer;
import java.time.LocalDate;
import java.time.Duration;
import java.time.LocalDateTime;

/**
Expand All @@ -17,15 +17,11 @@ public class Job {

private String stackId;

public LocalDateTime getDateTime() {
return dateTime;
}
private LocalDateTime startDateTime;

public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
private LocalDateTime endDateTime;

private LocalDateTime dateTime;
private Long executionTime;

@Transient
private StringWriter stringWriter = new StringWriter();
Expand Down Expand Up @@ -65,18 +61,23 @@ public JobStatus getStatus(){
public void start(JobType jobType) {
this.jobStatus = JobStatus.RUNNING;
this.jobType = jobType;
this.startDateTime = LocalDateTime.now();
}

public void end(){
public void end() {
this.jobStatus = JobStatus.FINISHED;
// getting final logs
this.logs = this.stringWriter.toString();
this.endDateTime = LocalDateTime.now();
this.executionTime = Duration.between(startDateTime, endDateTime).toMillis();
}

public void fail() {
this.jobStatus = JobStatus.FAILED;
// getting final logs
this.logs = this.stringWriter.toString();
this.endDateTime = LocalDateTime.now();
this.executionTime = Duration.between(startDateTime, endDateTime).toMillis();
}

public String getStackId() {
Expand All @@ -99,4 +100,27 @@ public void setCliVersion(String cliVersion) {
this.cliVersion = cliVersion;
}

public LocalDateTime getStartDateTime() {
return startDateTime;
}

public void setStartDateTime(LocalDateTime startDateTime) {
this.startDateTime = startDateTime;
}

public LocalDateTime getEndDateTime() {
return endDateTime;
}

public void setEndDateTime(LocalDateTime endDateTime) {
this.endDateTime = endDateTime;
}

public Long getExecutionTime() {
return executionTime;
}

public void setExecutionTime(Long executionTime) {
this.executionTime = executionTime;
}
}
6 changes: 0 additions & 6 deletions src/main/java/io/codeka/gaia/controller/StackController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.codeka.gaia.controller;

import io.codeka.gaia.bo.Job;
import io.codeka.gaia.bo.Settings;
import io.codeka.gaia.repository.JobRepository;
import io.codeka.gaia.repository.StackRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
Expand All @@ -13,10 +12,8 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.UUID;


@Controller
public class StackController {

Expand Down Expand Up @@ -69,7 +66,6 @@ public String applyStack(@PathVariable String stackId, Model model){
var job = new Job();
job.setId(UUID.randomUUID().toString());
job.setStackId(stackId);
job.setDateTime(LocalDateTime.now());

model.addAttribute("jobId", job.getId());

Expand All @@ -95,7 +91,6 @@ public String previewStack(@PathVariable String stackId, Model model){
var job = new Job();
job.setId(UUID.randomUUID().toString());
job.setStackId(stackId);
job.setDateTime(LocalDateTime.now());

model.addAttribute("jobId", job.getId());

Expand Down Expand Up @@ -141,7 +136,6 @@ public String stopStack(@PathVariable String stackId, Model model){
var job = new Job();
job.setId(UUID.randomUUID().toString());
job.setStackId(stackId);
job.setDateTime(LocalDateTime.now());

model.addAttribute("jobId", job.getId());

Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/templates/stack.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<script src="/webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="/webjars/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="/webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="/webjars/momentjs/2.24.0/min/moment.min.js"></script>

<script src="/webjars/vue/2.5.16/vue.js"></script>
<script src="/webjars/bootstrap-vue/2.0.0-rc.26/dist/bootstrap-vue.js"></script>
Expand Down Expand Up @@ -144,7 +145,10 @@ <h2><span><i class="fas fa-history"></i> Job history</span></h2>
<div class="job_detail">
<div><span class="job_attr_id">Type</span><span class="job_attr_value" :class="job.type">{{job.type}}</span></div>
<div><span class="job_attr_id">Status</span><span class="job_attr_value" :class="job.status">{{job.status}}</span></div>
<div><span class="job_attr_id">Date</span><span class="job_attr_value">{{job.dateTime | dateTime}}</span></div>
<div>
<span class="job_attr_id">Date</span><span class="job_attr_value">{{job.startDateTime | dateTime}}</span>
({{job.executionTime}}&nbsp;ms)
</div>
</div>
</li>
</ul>
Expand All @@ -169,8 +173,8 @@ <h2><span><i class="fas fa-history"></i> Job history</span></h2>
});

Vue.filter('dateTime', function (value) {
if (!value) return '';
return new Date(value).toLocaleString();
if (!value || !moment(value).isValid()) return '';
return moment(value).format('L LTS');
})
</script>

Expand Down
81 changes: 80 additions & 1 deletion src/test/java/io/codeka/gaia/bo/JobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import org.junit.jupiter.api.Test;

import java.io.PrintWriter;
import java.time.Duration;
import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class JobTest {

@Test
void getLogs_shouldReturnOutputStreamResult(){
void getLogs_shouldReturnOutputStreamResult() {
var job = new Job();

PrintWriter printWriter = new PrintWriter(job.getLogsWriter());
Expand All @@ -21,4 +24,80 @@ void getLogs_shouldReturnOutputStreamResult(){
assertEquals("Test Line 1\nTest Line 2\n", logs);
}

@Test
void start_shouldSetStatusToRunning() {
var job = new Job();

job.start(null);

assertEquals(JobStatus.RUNNING, job.getStatus());
}

@Test
void start_shouldSetType() {
var job = new Job();

job.start(JobType.RUN);

assertEquals(JobType.RUN, job.getType());
}

@Test
void start_shouldSetStartDateTime() {
var job = new Job();
job.setStartDateTime(null);

job.start(null);

assertThat(job.getStartDateTime()).isNotNull().isEqualToIgnoringSeconds(LocalDateTime.now());
}

@Test
void end_shouldSetStatusToFinished() {
var job = new Job();
job.setStartDateTime(LocalDateTime.now());

job.end();

assertEquals(JobStatus.FINISHED, job.getStatus());
}

@Test
void end_shouldSetEndDateTime() {
var job = new Job();
job.setStartDateTime(LocalDateTime.now());
job.setEndDateTime(null);
job.setExecutionTime(null);

job.end();
var timer = Duration.between(job.getStartDateTime(), job.getEndDateTime()).toMillis();

assertThat(job.getEndDateTime()).isNotNull().isEqualToIgnoringSeconds(LocalDateTime.now());
assertThat(job.getExecutionTime()).isNotNull().isEqualTo(timer);
}

@Test
void fail_shouldSetStatusToFinished() {
var job = new Job();
job.setStartDateTime(LocalDateTime.now());

job.fail();

assertEquals(JobStatus.FAILED, job.getStatus());
}

@Test
void fail_shouldSetEndDateTime() {
var job = new Job();
job.setStartDateTime(LocalDateTime.now());
job.setEndDateTime(null);
job.setExecutionTime(null);

job.fail();
var timer = Duration.between(job.getStartDateTime(), job.getEndDateTime()).toMillis();

assertThat(job.getEndDateTime()).isNotNull().isEqualToIgnoringSeconds(LocalDateTime.now());
assertThat(job.getExecutionTime()).isNotNull().isEqualTo(timer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void jobShouldBeSavedWithLogs() throws IOException {
job.setStackId("42");

job.getLogsWriter().write("some logs");
job.start(null);
job.end();

jobRepository.save(job);
Expand Down

0 comments on commit b467dea

Please sign in to comment.