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

Fixing time duration display #1950

Merged
merged 4 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions web/src/__tests__/reducers/jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as actionTypes from '../../store/actionCreators/actionTypes'
import jobsReducer, { initialState } from '../../store/reducers/jobs'
import { stopWatchDuration } from "../../helpers/time";

const jobs = require('../../../docker/db/data/jobs.json')

Expand All @@ -17,3 +18,45 @@ describe('jobs reducer', () => {
expect(jobsReducer(initialState, action)).toStrictEqual({ isLoading: false, result: jobs, init: true })
})
})

describe('stopWatchDuration', () => {
const oneMinute = 60 * 1000;
const oneHour = 60 * oneMinute;
const oneDay = 24 * oneHour;

it('more than one week', () => {
const value = stopWatchDuration(oneDay * 9)
expect("9d 0h 0m 0s").toBe(value);
})

it('more than one day', () => {
const value = stopWatchDuration(oneDay + oneHour)
expect("1d 1h 0m 0s").toBe(value);
})

it('less than one day', () => {
const value = stopWatchDuration(oneDay - 1000);
expect("23h 59m 59s").toBe(value);
})

it('less than one hour', () => {
const value = stopWatchDuration(oneHour - 1000);
expect("59m 59s").toBe(value);
})

it('less than one minute', () => {
const value = stopWatchDuration(oneMinute - 1000);
expect("0m 59s").toBe(value);
})

it('less than one second', () => {
const value = stopWatchDuration(999);
expect("999 ms").toBe(value);
})

it('no time', () => {
const value = stopWatchDuration(0);
expect("0").toBe(value);
})

})
10 changes: 6 additions & 4 deletions web/src/helpers/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export function stopWatchDuration(durationMs: number) {
if (duration.asMilliseconds() === 0) {
return '0'
}

if (duration.asHours() > 1) {
return `${duration.hours()}h ${addLeadingZero(duration.seconds())}s`
if (duration.asHours() > 24) {
return `${duration.days()}d ${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s`
}
if (duration.asMinutes() > 60) {
return `${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s`
}
if (duration.asSeconds() > 1) {
return `${duration.minutes()}m ${addLeadingZero(duration.seconds())}s`
} else {
return `${duration.milliseconds()} ms`
return `${duration.asMilliseconds()} ms`
}
}