Skip to content

Commit e0eb1df

Browse files
committed
fix(): update build end_time field when last job finish successfully and fix build time issue
1 parent fad6de8 commit e0eb1df

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

src/api/db/build.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Build, BuildRun } from './model';
1+
import { Build, BuildRun, Job } from './model';
2+
import { getLastRun } from './job';
23

34
export function getBuilds(limit: number, offset: number): Promise<any> {
45
return new Promise((resolve, reject) => {
@@ -64,6 +65,20 @@ export function getBuild(id: number): Promise<any> {
6465
});
6566
}
6667

68+
export function getLastRunId(buildId: number): Promise<any> {
69+
return new Promise((resolve, reject) => {
70+
new Build({ id: buildId }).fetch({ withRelated: ['runs'] })
71+
.then(build => {
72+
if (!build) {
73+
reject();
74+
}
75+
const runs = build.related('runs').toJSON();
76+
77+
resolve (runs[runs.length - 1].id);
78+
});
79+
});
80+
}
81+
6782
export function insertBuild(data: any): Promise<any> {
6883
return new Promise((resolve, reject) => {
6984
new Build().save(data, { method: 'insert' }).then(build => {
@@ -91,3 +106,15 @@ export function updateBuild(data: any): Promise<boolean> {
91106
});
92107
});
93108
}
109+
110+
export function getBuildStatus(buildId: number): Promise<any> {
111+
return new Promise((resolve, reject) => {
112+
new Job()
113+
.query(q => q.where('builds_id', buildId))
114+
.fetchAll()
115+
.then(jobs => {
116+
Promise.all(jobs.map(j => getLastRun(j.id).then(r => r.status === 'success')))
117+
.then(data => resolve(data.reduce((curr, prev) => !curr ? curr : prev)));
118+
});
119+
});
120+
}

src/api/db/job.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ export function getLastRunId(jobId: number): Promise<any> {
5454
});
5555
}
5656

57+
export function getLastRun(jobId: number): Promise<any> {
58+
return new Promise((resolve, reject) => {
59+
new Job({ id: jobId }).fetch({ withRelated: ['runs'] })
60+
.then(job => {
61+
if (!job) {
62+
reject();
63+
}
64+
const runs = job.related('runs').toJSON();
65+
66+
resolve (runs[runs.length - 1]);
67+
});
68+
});
69+
}
70+
5771
export function insertJob(data: any): Promise<any> {
5872
return new Promise((resolve, reject) => {
5973
new Job(data).save(null, { method: 'insert' }).then(job => {

src/api/db/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Build extends Bookshelf.Model<any> {
1717
get hasTimestamps() { return true; }
1818
repository() { return this.belongsTo(Repository, 'repositories_id'); }
1919
jobs() { return this.hasMany(Job, 'builds_id'); }
20-
runs() { return this.hasMany(BuildRun, 'builds_id'); }
20+
runs() { return this.hasMany(BuildRun, 'build_id'); }
2121
}
2222

2323
export class BuildRun extends Bookshelf.Model<any> {

src/api/process-manager.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { startDockerImageSetupJob, startBuildProcess } from './process';
22
import { Observable, Subject, BehaviorSubject, Subscription } from 'rxjs';
3-
import { insertBuild, updateBuild, getBuild } from './db/build';
4-
import { insertBuildRun } from './db/build-run';
3+
import { insertBuild, updateBuild, getBuild, getBuildStatus, getLastRunId } from './db/build';
4+
import { insertBuildRun, updateBuildRun } from './db/build-run';
55
import * as dbJob from './db/job';
66
import * as dbJobRuns from './db/job-run';
77
import { getRepositoryOnly } from './db/repository';
@@ -315,10 +315,20 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
315315
});
316316
});
317317
}, () => {
318-
319318
dbJob.getLastRunId(jobId)
320319
.then(runId => dbJobRuns.updateJobRun(
321320
{id: runId, end_time: new Date(), status: 'success', log: process.log.join('')}))
321+
.then(() => getBuildStatus(buildId))
322+
.then(status => {
323+
if (status) {
324+
updateBuild({ id: buildId, end_time: new Date()});
325+
getLastRunId(buildId)
326+
.then(id => {
327+
updateBuildRun({ id: id, end_time: new Date()});
328+
});
329+
}
330+
Promise.resolve();
331+
})
322332
.then(() => {
323333
processes = processes.filter(proc => proc.job_id !== jobId);
324334
jobProcesses.next(processes);

src/app/components/app-build-details/app-build-details.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ <h2>{{ build?.message }}</h2>
6666
<span>{{ totalTime }}</span>
6767
</p>
6868
</div>
69-
<div class="column is-12" *ngIf="status === 'running' && build.lastBuild">
70-
<span>approximately {{ approximatelyRemainingTime }} remaining</span>
69+
<div class="column is-12" *ngIf="status === 'running' && approximatelyRemainingTime">
70+
<span name="time-left">approximately {{ approximatelyRemainingTime }} remaining</span>
7171
</div>
7272
</div>
7373
</div>

src/app/components/app-build-details/app-build-details.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class AppBuildDetailsComponent implements OnInit {
9595
});
9696

9797
let runningTime = Math.max(...this.build.jobs.map(job => {
98-
let date = new Date();
98+
let date = new Date(0);
9999
let splitted = job.time.split(':');
100100
date.setUTCMinutes(splitted[0]);
101101
date.setUTCSeconds(splitted[1]);
@@ -144,6 +144,11 @@ export class AppBuildDetailsComponent implements OnInit {
144144
e.preventDefault();
145145
e.stopPropagation();
146146

147+
if (this.getBuildStatus() === 'success') {
148+
let minJobStartTime = Math.min(...this.build.jobs.map(job => job.start_time));
149+
let maxJobEndTime = Math.max(...this.build.jobs.map(job => job.end_time));
150+
this.previousRuntime = maxJobEndTime - minJobStartTime;
151+
}
147152
this.processingBuild = true;
148153
this.socketService.emit({ type: 'restartBuild', data: { buildId: id } });
149154
}

0 commit comments

Comments
 (0)