Skip to content

Commit 24786cb

Browse files
phixMephixwslulciuc
authored
Fixing time duration display (#1950)
* Fixing time module and tests. * Better tests and constants. Co-authored-by: phix <peter.hicks@astronomer.io> Co-authored-by: Willy Lulciuc <willy@datakin.com>
1 parent 6b047e1 commit 24786cb

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

web/src/__tests__/reducers/jobs.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

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

@@ -17,3 +18,45 @@ describe('jobs reducer', () => {
1718
expect(jobsReducer(initialState, action)).toStrictEqual({ isLoading: false, result: jobs, init: true })
1819
})
1920
})
21+
22+
describe('stopWatchDuration', () => {
23+
const oneMinute = 60 * 1000;
24+
const oneHour = 60 * oneMinute;
25+
const oneDay = 24 * oneHour;
26+
27+
it('more than one week', () => {
28+
const value = stopWatchDuration(oneDay * 9)
29+
expect("9d 0h 0m 0s").toBe(value);
30+
})
31+
32+
it('more than one day', () => {
33+
const value = stopWatchDuration(oneDay + oneHour)
34+
expect("1d 1h 0m 0s").toBe(value);
35+
})
36+
37+
it('less than one day', () => {
38+
const value = stopWatchDuration(oneDay - 1000);
39+
expect("23h 59m 59s").toBe(value);
40+
})
41+
42+
it('less than one hour', () => {
43+
const value = stopWatchDuration(oneHour - 1000);
44+
expect("59m 59s").toBe(value);
45+
})
46+
47+
it('less than one minute', () => {
48+
const value = stopWatchDuration(oneMinute - 1000);
49+
expect("0m 59s").toBe(value);
50+
})
51+
52+
it('less than one second', () => {
53+
const value = stopWatchDuration(999);
54+
expect("999 ms").toBe(value);
55+
})
56+
57+
it('no time', () => {
58+
const value = stopWatchDuration(0);
59+
expect("0").toBe(value);
60+
})
61+
62+
})

web/src/helpers/time.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ export function stopWatchDuration(durationMs: number) {
1414
if (duration.asMilliseconds() === 0) {
1515
return '0'
1616
}
17-
18-
if (duration.asHours() > 1) {
19-
return `${duration.hours()}h ${addLeadingZero(duration.seconds())}s`
17+
if (duration.asHours() > 24) {
18+
return `${duration.days()}d ${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s`
19+
}
20+
if (duration.asMinutes() > 60) {
21+
return `${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s`
2022
}
2123
if (duration.asSeconds() > 1) {
2224
return `${duration.minutes()}m ${addLeadingZero(duration.seconds())}s`
2325
} else {
24-
return `${duration.milliseconds()} ms`
26+
return `${duration.asMilliseconds()} ms`
2527
}
2628
}

0 commit comments

Comments
 (0)