Skip to content

Commit

Permalink
show relative time for pipeline run duration
Browse files Browse the repository at this point in the history
  • Loading branch information
vikram-raj committed May 18, 2020
1 parent 39acb36 commit 507d199
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formatDuration } from '@console/internal/components/utils/datetime';
import { runStatus } from '../../../../utils/pipeline-augment';
import { calculateRelativeTime } from '../../../../utils/pipeline-utils';

enum TerminatedReasons {
Completed = 'Completed',
Expand Down Expand Up @@ -31,22 +31,14 @@ const getMatchingStep = (step, status: TaskStatus): TaskStatusStep => {
});
};

const calculateDurationFormatted = ({ startedAt, finishedAt }): string => {
const date = new Date(finishedAt).getTime() - new Date(startedAt).getTime();
return formatDuration(date);
};

const getMatchingStepDuration = (matchingStep?: TaskStatusStep) => {
if (!matchingStep) return '';

if (matchingStep.terminated) {
return calculateDurationFormatted(matchingStep.terminated);
return calculateRelativeTime(matchingStep.terminated.startedAt);
}
if (matchingStep.running) {
return calculateDurationFormatted({
startedAt: matchingStep.running.startedAt,
finishedAt: Date.now(),
});
return calculateRelativeTime(matchingStep.running.startedAt);
}

return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('pipeline-utils ', () => {
it('expect duration to be a time formatted string for PipelineRun with start and end Time', () => {
const duration = pipelineRunDuration(mockRunDurationTest[2]);
expect(duration).not.toBeNull();
expect(duration).toBe('1m 13s');
expect(duration).toBe('less than a minute');
});
it('expect annotation to return an empty object if keyValue pair is not passed', () => {
const annotation = getSecretAnnotations(null);
Expand Down
33 changes: 26 additions & 7 deletions frontend/packages/dev-console/src/utils/pipeline-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,31 @@ export const getPipelineRunWorkspaces = (
);
};

export const calculateRelativeTime = (startTime: string, completionTime?: string) => {
const start = new Date(startTime).getTime();
const end = completionTime ? new Date(completionTime).getTime() : new Date().getTime();
const secondsAgo = (end - start) / 1000;
const minutesAgo = secondsAgo / 60;
const hoursAgo = minutesAgo / 60;

// For running pipelines duration must be current time - starttime.
if (minutesAgo > 90) {
const count = Math.round(hoursAgo);
return `about ${count} hours`;
}
if (minutesAgo > 45) {
return 'less than a hour';
}
if (secondsAgo > 90) {
const count = Math.round(minutesAgo);
return `about ${count} minutes`;
}
if (secondsAgo > 45) {
return 'less than a minute';
}
return 'a few seconds';
};

export const pipelineRunDuration = (run: PipelineRun): string => {
const startTime = _.get(run, ['status', 'startTime'], null);
const completionTime = _.get(run, ['status', 'completionTime'], null);
Expand All @@ -322,13 +347,7 @@ export const pipelineRunDuration = (run: PipelineRun): string => {
if (!startTime || (!completionTime && pipelineRunStatus(run) !== 'Running')) {
return '-';
}
const start = new Date(startTime).getTime();

// For running pipelines duration must be current time - starttime.
const duration = completionTime
? new Date(completionTime).getTime() - start
: new Date().getTime() - start;
return formatDuration(duration);
return calculateRelativeTime(startTime, completionTime);
};

export const updateServiceAccount = (
Expand Down

0 comments on commit 507d199

Please sign in to comment.