kibana/x-pack/plugins/monitoring/common/format_timestamp_to_duration.js
Chris Roberson 3a396027f6
[Monitoring] Migrate server to NP (#56675)
* First pass

* First pass

* Add new routes

* Getting closer

* Remove legacy server code, and other fixes

* Register the plugin with xpack

* Pass a legacy client to telemetry

* Suport callWithInternalUser

* Remove this

* More NP work

* Fix some tests

* Fix broken test

* Move over new telemetry changes, and fix other issues

* Fix TODO item

* Reuse the same schema as elasticsearch module

* Use a singular config definition here

* Disable this for now

* Use the right method

* Use custom config again

* Tweak the config to make this optional

* Remove these

* Remove these unnecessary files

* Fix jest test

* Fix some linting issues

* Fix type issue

* Fix localization issues

* Use the elasticsearch config

* Remove todos

* Fix this check

* Move kibana alerting over

* PR feedback

* Use new metrics core service

* Change config for xpack_api_polling_frequency_millis

* Make sure this is disabled for now

* Disable both

* Update this to the new function

* Tighten up legacy api needs

* Check for existence

* Fix jest tests

* Cleaning up the plugin definition

* Create custom type in our plugin

* Revert this change

* Fix CI issues

* Add these tests back

* Just use a different collector type

* Handle errors better

* Use custom type

* PR feedback

* Fix type issues

* PR feedback
2020-03-20 14:02:15 -04:00

55 lines
2.1 KiB
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import moment from 'moment';
import 'moment-duration-format';
import {
FORMAT_DURATION_TEMPLATE_TINY,
FORMAT_DURATION_TEMPLATE_SHORT,
FORMAT_DURATION_TEMPLATE_LONG,
CALCULATE_DURATION_SINCE,
CALCULATE_DURATION_UNTIL,
} from './constants';
/*
* Formats a timestamp string
* @param timestamp: ISO time string
* @param calculationFlag: control "since" or "until" logic
* @param initialTime {Object} moment object (not required)
* @return string
*/
export function formatTimestampToDuration(timestamp, calculationFlag, initialTime) {
initialTime = initialTime || moment();
let timeDuration;
if (calculationFlag === CALCULATE_DURATION_SINCE) {
timeDuration = moment.duration(initialTime - moment(timestamp)); // since: now - timestamp
} else if (calculationFlag === CALCULATE_DURATION_UNTIL) {
timeDuration = moment.duration(moment(timestamp) - initialTime); // until: timestamp - now
} else {
throw new Error(
'[formatTimestampToDuration] requires a [calculationFlag] parameter to specify format as "since" or "until" the given time.'
);
}
// See https://github.com/elastic/x-pack-kibana/issues/3554
let duration;
if (Math.abs(initialTime.diff(timestamp, 'months')) >= 1) {
// time diff is greater than 1 month, show months / days
duration = moment.duration(timeDuration).format(FORMAT_DURATION_TEMPLATE_LONG);
} else if (Math.abs(initialTime.diff(timestamp, 'minutes')) >= 1) {
// time diff is less than 1 month but greater than a minute, show days / hours / minutes
duration = moment.duration(timeDuration).format(FORMAT_DURATION_TEMPLATE_SHORT);
} else {
// time diff is less than a minute, show seconds
duration = moment.duration(timeDuration).format(FORMAT_DURATION_TEMPLATE_TINY);
}
return duration
.replace(/ 0 mins$/, '')
.replace(/ 0 hrs$/, '')
.replace(/ 0 days$/, ''); // See https://github.com/jsmreese/moment-duration-format/issues/64
}