Skip to content

Commit

Permalink
feat: format lower than 5 seconds (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
fer112233 authored Sep 13, 2023
1 parent c10ab8b commit ee6ede0
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/ui/src/components/JobCard/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format, formatDistance, getYear, isToday } from 'date-fns';
import { format, formatDistance, getYear, isToday, differenceInMilliseconds } from 'date-fns';
import React from 'react';
import s from './Timeline.module.css';
import { AppJob, Status } from '@bull-board/api/typings/app';
Expand All @@ -15,6 +15,19 @@ const formatDate = (ts: TimeStamp) => {
? format(ts, 'MM/dd HH:mm:ss')
: format(ts, 'MM/dd/yyyy HH:mm:ss');
};
const formatDuration = (finishedTs: TimeStamp, processedTs: TimeStamp) => {
const durationInMs = differenceInMilliseconds(finishedTs, processedTs);
const durationInSeconds = durationInMs / 1000;
if (durationInSeconds > 5) {
return formatDistance(finishedTs, processedTs, {
includeSeconds: true,
});
}
if (durationInSeconds >= 1) {
return `${durationInSeconds.toFixed(2)} seconds`;
}
return `${durationInMs} milliseconds`;
}

export const Timeline = function Timeline({ job, status }: { job: AppJob; status: Status }) {
return (
Expand All @@ -34,9 +47,7 @@ export const Timeline = function Timeline({ job, status }: { job: AppJob; status
<li>
<small>
{job.delay && job.delay > 0 ? 'delayed for ' : ''}
{formatDistance(job.processedOn, job.timestamp || 0, {
includeSeconds: true,
})}
{formatDuration(job.processedOn, job.timestamp || 0)}
</small>
<small>Process started at</small>
<time>{formatDate(job.processedOn)}</time>
Expand All @@ -45,9 +56,7 @@ export const Timeline = function Timeline({ job, status }: { job: AppJob; status
{!!job.finishedOn && (
<li>
<small>
{formatDistance(job.finishedOn, job.processedOn || 0, {
includeSeconds: true,
})}
{formatDuration(job.finishedOn, job.processedOn || 0)}
</small>
<small>{job.isFailed && status !== STATUSES.active ? 'Failed' : 'Finished'} at</small>
<time>{formatDate(job.finishedOn)}</time>
Expand Down

0 comments on commit ee6ede0

Please sign in to comment.