diff --git a/public/app/core/specs/kbn.test.ts b/public/app/core/specs/kbn.test.ts index dfa665e3205..e621cdef632 100644 --- a/public/app/core/specs/kbn.test.ts +++ b/public/app/core/specs/kbn.test.ts @@ -399,6 +399,77 @@ describe('duration', () => { }); }); +describe('clock', () => { + it('null', () => { + const str = kbn.toClock(null, 0); + expect(str).toBe(''); + }); + it('size less than 1 second', () => { + const str = kbn.toClock(999, 0); + expect(str).toBe('999ms'); + }); + describe('size less than 1 minute', () => { + it('default', () => { + const str = kbn.toClock(59999); + expect(str).toBe('59s:999ms'); + }); + it('decimals equals 0', () => { + const str = kbn.toClock(59999, 0); + expect(str).toBe('59s'); + }); + }); + describe('size less than 1 hour', () => { + it('default', () => { + const str = kbn.toClock(3599999); + expect(str).toBe('59m:59s:999ms'); + }); + it('decimals equals 0', () => { + const str = kbn.toClock(3599999, 0); + expect(str).toBe('59m'); + }); + it('decimals equals 1', () => { + const str = kbn.toClock(3599999, 1); + expect(str).toBe('59m:59s'); + }); + }); + describe('size greater than or equal 1 hour', () => { + it('default', () => { + const str = kbn.toClock(7199999); + expect(str).toBe('01h:59m:59s:999ms'); + }); + it('decimals equals 0', () => { + const str = kbn.toClock(7199999, 0); + expect(str).toBe('01h'); + }); + it('decimals equals 1', () => { + const str = kbn.toClock(7199999, 1); + expect(str).toBe('01h:59m'); + }); + it('decimals equals 2', () => { + const str = kbn.toClock(7199999, 2); + expect(str).toBe('01h:59m:59s'); + }); + }); + describe('size greater than or equal 1 day', () => { + it('default', () => { + const str = kbn.toClock(89999999); + expect(str).toBe('24h:59m:59s:999ms'); + }); + it('decimals equals 0', () => { + const str = kbn.toClock(89999999, 0); + expect(str).toBe('24h'); + }); + it('decimals equals 1', () => { + const str = kbn.toClock(89999999, 1); + expect(str).toBe('24h:59m'); + }); + it('decimals equals 2', () => { + const str = kbn.toClock(89999999, 2); + expect(str).toBe('24h:59m:59s'); + }); + }); +}); + describe('volume', () => { it('1000m3', () => { const str = kbn.valueFormats['m3'](1000, 1, null); diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index bd69f2e89d9..398ad9bb3e7 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -808,6 +808,51 @@ kbn.toDuration = (size, decimals, timeScale) => { return strings.join(', '); }; +kbn.toClock = (size, decimals) => { + if (size === null) { + return ''; + } + + // < 1 second + if (size < 1000) { + return moment.utc(size).format('SSS\\m\\s'); + } + + // < 1 minute + if (size < 60000) { + let format = 'ss\\s:SSS\\m\\s'; + if (decimals === 0) { + format = 'ss\\s'; + } + return moment.utc(size).format(format); + } + + // < 1 hour + if (size < 3600000) { + let format = 'mm\\m:ss\\s:SSS\\m\\s'; + if (decimals === 0) { + format = 'mm\\m'; + } else if (decimals === 1) { + format = 'mm\\m:ss\\s'; + } + return moment.utc(size).format(format); + } + + let format = 'mm\\m:ss\\s:SSS\\m\\s'; + + const hours = `${('0' + Math.floor(moment.duration(size, 'milliseconds').asHours())).slice(-2)}h`; + + if (decimals === 0) { + format = ''; + } else if (decimals === 1) { + format = 'mm\\m'; + } else if (decimals === 2) { + format = 'mm\\m:ss\\s'; + } + + return format ? `${hours}:${moment.utc(size).format(format)}` : hours; +}; + kbn.valueFormats.dtdurationms = (size, decimals) => { return kbn.toDuration(size, decimals, 'millisecond'); }; @@ -824,6 +869,14 @@ kbn.valueFormats.timeticks = (size, decimals, scaledDecimals) => { return kbn.valueFormats.s(size / 100, decimals, scaledDecimals); }; +kbn.valueFormats.clockms = (size, decimals) => { + return kbn.toClock(size, decimals); +}; + +kbn.valueFormats.clocks = (size, decimals) => { + return kbn.toClock(size * 1000, decimals); +}; + kbn.valueFormats.dateTimeAsIso = (epoch, isUtc) => { const time = isUtc ? moment.utc(epoch) : moment(epoch); @@ -901,6 +954,8 @@ kbn.getUnitFormats = () => { { text: 'duration (s)', value: 'dtdurations' }, { text: 'duration (hh:mm:ss)', value: 'dthms' }, { text: 'Timeticks (s/100)', value: 'timeticks' }, + { text: 'clock (ms)', value: 'clockms' }, + { text: 'clock (s)', value: 'clocks' }, ], }, {