From f70cefda2e0411095ec9a889a8fff1d99175ff51 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Mon, 4 Nov 2019 14:44:58 +0100 Subject: [PATCH] mgr/dashboard: Add time-diff unittest and docs The method 'calculateDuration' should return an empty string if time diff is less then a minute. Signed-off-by: Volker Theile --- .../src/app/shared/services/time-diff.service.spec.ts | 5 +++++ .../frontend/src/app/shared/services/time-diff.service.ts | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.spec.ts index 59b52ae60399..335b85cecdda 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.spec.ts @@ -38,6 +38,11 @@ describe('TimeDiffService', () => { expect(service.calculateDuration(baseTime, new Date('2022-02-28T04:05:00'))).toBe('6d 4h 5m'); }); + it('should return an empty string if time diff is less then a minute', () => { + const ts = 1568361327000; + expect(service.calculateDuration(new Date(ts), new Date(ts + 120))).toBe(''); + }); + describe('testing duration calculation in detail', () => { const minutes = 60 * 1000; const hours = 60 * minutes; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.ts index 051d104906e7..0f882bc8f4d2 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.ts @@ -18,6 +18,12 @@ export class TimeDiffService { return duration; } + /** + * Get the duration in the format '[Nd] [Nh] [Nm]', e.g. '2d 1h 15m'. + * @param ms The time in milliseconds. + * @return The duration. An empty string is returned if the duration is + * less than a minute. + */ private getDuration(ms: number): string { const date = new Date(ms); const h = date.getUTCHours(); -- 2.47.3