Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix formatting of durations so it doesn't just display seconds #3620

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import api from "shared/api";
import { PorterAppBuildEvent, PorterAppPreDeployEvent } from "./types";
import { PorterAppRecord } from "../../../AppView";
import { match } from "ts-pattern";
import { differenceInSeconds, formatDuration } from 'date-fns';
import { differenceInSeconds, intervalToDuration } from 'date-fns';

export const getDuration = (event: PorterAppPreDeployEvent | PorterAppBuildEvent): string => {
const startTimeStamp = match(event)
Expand All @@ -17,8 +17,18 @@ export const getDuration = (event: PorterAppPreDeployEvent | PorterAppBuildEvent
const endTimeStamp = event.metadata.end_time ? new Date(event.metadata.end_time).getTime() : Date.now()

const timeDifferenceInSeconds = differenceInSeconds(endTimeStamp, startTimeStamp);

return formatDuration({ seconds: timeDifferenceInSeconds });
const duration = intervalToDuration({ start: 0, end: timeDifferenceInSeconds * 1000 });
if (duration.weeks) {
return `${duration.weeks}w ${duration.days}d ${duration.hours}h`
} else if (duration.days) {
return `${duration.days}d ${duration.hours}h ${duration.minutes}m`
} else if (duration.hours) {
return `${duration.hours}h ${duration.minutes}m ${duration.seconds}s`
} else if (duration.minutes) {
return `${duration.minutes}m ${duration.seconds}s`
} else {
return `${duration.seconds}s`
}
};

export const getStatusIcon = (status: string) => {
Expand Down
16 changes: 13 additions & 3 deletions dashboard/src/main/home/app-dashboard/validate-apply/jobs/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JobRun } from "lib/hooks/useJobs";
import { timeFrom } from "shared/string_utils";
import { differenceInSeconds, formatDuration } from 'date-fns';
import { differenceInSeconds, intervalToDuration } from 'date-fns';

export const ranFor = (start: string, end?: string | number) => {
const duration = timeFrom(start, end);
Expand All @@ -19,6 +19,16 @@ export const getDuration = (jobRun: JobRun): string => {
const endTimeStamp = jobRun.status.completionTime ? new Date(jobRun.status.completionTime).getTime() : Date.now()

const timeDifferenceInSeconds = differenceInSeconds(endTimeStamp, startTimeStamp);

return formatDuration({ seconds: timeDifferenceInSeconds });
const duration = intervalToDuration({ start: 0, end: timeDifferenceInSeconds * 1000 });
if (duration.weeks) {
return `${duration.weeks}w ${duration.days}d ${duration.hours}h`
} else if (duration.days) {
return `${duration.days}d ${duration.hours}h ${duration.minutes}m`
} else if (duration.hours) {
return `${duration.hours}h ${duration.minutes}m ${duration.seconds}s`
} else if (duration.minutes) {
return `${duration.minutes}m ${duration.seconds}s`
} else {
return `${duration.seconds}s`
}
};