"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeIntervalParamEditor = TimeIntervalParamEditor; var _lodash = require("lodash"); var _react = _interopRequireWildcard(require("react")); var _eui = require("@elastic/eui"); var _i18n = require("@kbn/i18n"); var _i18nReact = require("@kbn/i18n-react"); var _public = require("@kbn/data-plugin/public"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ const { parseEsInterval, InvalidEsCalendarIntervalError } = _public.search.aggs; // we check if Elasticsearch interval is valid to show a user appropriate error message // e.g. there is the case when a user inputs '14d' but it's '2w' in expression equivalent and the request will fail // we don't check it for 0ms because the overall time range has not yet been set function isValidCalendarInterval(interval) { if (interval === '0ms') { return { isValidCalendarValue: true }; } try { parseEsInterval(interval); return { isValidCalendarValue: true }; } catch (e) { if (e instanceof InvalidEsCalendarIntervalError) { return { isValidCalendarValue: false, error: e.message }; } return { isValidCalendarValue: true }; } } const errorMessage = _i18n.i18n.translate('visDefaultEditor.controls.timeInterval.invalidFormatErrorMessage', { defaultMessage: 'Invalid interval format.' }); const autoInterval = 'auto'; function validateInterval(agg, value, definedOption, timeBase) { var _agg$buckets2; if (definedOption) { var _agg$buckets; return { isValid: true, interval: (_agg$buckets = agg.buckets) === null || _agg$buckets === void 0 ? void 0 : _agg$buckets.getInterval() }; } if (!value || value === autoInterval && !definedOption) { return { isValid: false }; } if (!timeBase) { // we check if Elasticsearch interval is valid ES interval to show a user appropriate error message // we don't check if there is timeBase const { isValidCalendarValue, error } = isValidCalendarInterval(value); if (!isValidCalendarValue) { return { isValid: false, error }; } } const isValid = _public.search.aggs.isValidInterval(value, timeBase); if (!isValid) { return { isValid: false, error: errorMessage }; } const interval = (_agg$buckets2 = agg.buckets) === null || _agg$buckets2 === void 0 ? void 0 : _agg$buckets2.getInterval(); const { isValidCalendarValue, error } = isValidCalendarInterval(interval.expression); if (!isValidCalendarValue) { return { isValid: false, error }; } return { isValid, interval }; } function TimeIntervalParamEditor({ agg, aggParam, editorConfig, value, setValue, showValidation, setTouched, setValidity }) { const timeBase = (0, _lodash.get)(editorConfig, 'interval.timeBase'); const options = timeBase ? [] : (aggParam.options || []).reduce((filtered, option) => { if (option.enabled ? option.enabled(agg) : true) { filtered.push({ label: option.display, key: option.val }); } return filtered; }, []); let selectedOptions = []; let definedOption; if (value) { definedOption = (0, _lodash.find)(options, { key: value }); selectedOptions = definedOption ? [definedOption] : value === autoInterval ? [] : [{ label: value, key: 'custom' }]; } const { isValid, error, interval } = validateInterval(agg, value, definedOption, timeBase); const scaledHelpText = interval && interval.scaled ? /*#__PURE__*/_react.default.createElement("strong", { "data-test-subj": "currentlyScaledText", className: "eui-displayBlock" }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "visDefaultEditor.controls.timeInterval.scaledHelpText", defaultMessage: "Currently scaled to {bucketDescription}", values: { bucketDescription: (0, _lodash.get)(interval, 'description') || '' } }), ' ', /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, { position: "right", type: "questionInCircle", content: interval.scale <= 1 ? tooManyBucketsTooltip : tooLargeBucketsTooltip })) : null; const helpText = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, scaledHelpText, (0, _lodash.get)(editorConfig, 'interval.help') || selectOptionHelpText); const onCustomInterval = customValue => setValue(customValue.trim()); const onChange = opts => { const selectedOpt = (0, _lodash.get)(opts, '0'); setValue(selectedOpt ? selectedOpt.key : ''); }; (0, _react.useEffect)(() => { setValidity(isValid); }, [isValid, setValidity]); return /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, { display: "rowCompressed", error: error, fullWidth: true, helpText: helpText, isInvalid: showValidation && !isValid, label: _i18n.i18n.translate('visDefaultEditor.controls.timeInterval.minimumIntervalLabel', { defaultMessage: 'Minimum interval' }) }, /*#__PURE__*/_react.default.createElement(_eui.EuiComboBox, { compressed: true, fullWidth: true, "data-test-subj": "visEditorInterval", isInvalid: showValidation && !isValid, noSuggestions: !!timeBase, onChange: onChange, onCreateOption: onCustomInterval, options: options, selectedOptions: selectedOptions, singleSelection: { asPlainText: true }, placeholder: _i18n.i18n.translate('visDefaultEditor.controls.timeInterval.selectIntervalPlaceholder', { defaultMessage: 'Select an interval' }), onBlur: setTouched })); } const tooManyBucketsTooltip = /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "visDefaultEditor.controls.timeInterval.createsTooManyBucketsTooltip", defaultMessage: "This interval creates too many buckets to show in the selected time range, so it has been scaled up." }); const tooLargeBucketsTooltip = /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "visDefaultEditor.controls.timeInterval.createsTooLargeBucketsTooltip", defaultMessage: "This interval creates buckets that are too large to show in the selected time range, so it has been scaled down." }); const selectOptionHelpText = /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "visDefaultEditor.controls.timeInterval.selectOptionHelpText", defaultMessage: "Select an option or create a custom value. Examples: 30s, 20m, 24h, 2d, 1w, 1M" });