Skip to content

Commit

Permalink
Stage overview calculate duration based on stage created and last tra…
Browse files Browse the repository at this point in the history
…nsition time gocd#8681

* Stage duration is calculated based on stage created time and last
  transitioned time now returned as part of the Stages API.
  • Loading branch information
maheshp committed Oct 27, 2020
1 parent 160b5f1 commit 32e0ab8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,22 @@ export class StageInstance {

triggeredOn(): string {
const LOCAL_TIME_FORMAT = "DD MMM, YYYY [at] HH:mm:ss";
return moment.unix(this.stageScheduledTime()).format(LOCAL_TIME_FORMAT);
return moment.unix(this.stageCreatedTimeInSecs()).format(LOCAL_TIME_FORMAT);
}

triggeredOnServerTime(): string {
const SERVER_TIME_FORMAT = "DD MMM, YYYY [at] HH:mm:ss Z [Server Time]";
const utcOffsetInMinutes = CONSTANTS.SERVER_TIMEZONE_UTC_OFFSET / 60000;
return moment.unix(this.stageScheduledTime()).utcOffset(utcOffsetInMinutes).format(SERVER_TIME_FORMAT);
return moment.unix(this.stageCreatedTimeInSecs()).utcOffset(utcOffsetInMinutes).format(SERVER_TIME_FORMAT);
}

stageDuration(): string {
if (this.isStageInProgress()) {
return `in progress`;
}

// Prototype JS has overridden the original array reduce method. Use reduceRight instead.
// This fix is only required for the VSM page
// Do not change Array.prototype.reduce, as it will affect all the arrays from VSM page
// @ts-ignore
this.json.jobs.reduce = Array.prototype.reduceRight;

// @ts-ignore
const highestJobTime = this.json.jobs.reduce((first: number, next: JobJSON) => {
const completed = next.job_state_transitions.find(t => t.state === "Completed")!;
return first < completed.state_change_time ? completed.state_change_time : first;
}, 0);

const end = moment.unix(+highestJobTime / 1000);
const start = moment.unix(this.stageScheduledTime());
const end = moment.unix(this.stageLastTransitionedTimeInSecs());
const start = moment.unix(this.stageCreatedTimeInSecs());

return JobDurationStrategyHelper.formatTimeForDisplay(moment.utc(end.diff(start)));
}
Expand Down Expand Up @@ -124,11 +112,12 @@ export class StageInstance {
return ApiRequestBuilder.POST(SparkRoutes.runStage(this.json.pipeline_name, this.json.pipeline_counter, this.json.name), ApiVersion.latest);
}

stageScheduledTime(): number {
return this.json.jobs.reduce((minTime, next) => {
const jobScheduledTime = next.scheduled_date / 1000;
return minTime < jobScheduledTime ? minTime : jobScheduledTime;
}, Infinity);
stageCreatedTimeInSecs(): number {
return this.json.created_time / 1000;
}

stageLastTransitionedTimeInSecs(): number {
return this.json.last_transitioned_time / 1000;
}

private isStageInProgress(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface StageInstanceJSON {
counter: number;
approval_type: string;
approved_by: string;
created_time: number;
last_transitioned_time: number;
cancelled_by?: string;
result: Result | string;
rerun_of_counter: number | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Stage Instance', () => {

it('should provide triggered on information', () => {
const json: StageInstance = StageInstance.fromJSON(stageInstanceJson);
expect(json.triggeredOn()).toBe(TestData.unixTime(json.stageScheduledTime()));
expect(json.triggeredOn()).toBe(TestData.unixTime(json.stageCreatedTimeInSecs()));
});

describe("Stage Duration", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class TestData {
counter: 1,
approval_type: "success",
approved_by: "admin",
created_time: 1595825414079,
last_transitioned_time: 1595831464796,
result: "Passed",
rerun_of_counter: null,
fetch_materials: true,
Expand Down

0 comments on commit 32e0ab8

Please sign in to comment.