"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Kpi = void 0; var _react = _interopRequireWildcard(require("react")); var _i18n = require("@kbn/i18n"); var _lens = require("../../../../../components/lens"); var _visualizations = require("../../../../../common/visualizations"); var _build = require("../../../../../utils/filters/build"); var _use_data_view = require("../../hooks/use_data_view"); var _use_unified_search = require("../../hooks/use_unified_search"); var _use_hosts_view = require("../../hooks/use_hosts_view"); var _use_host_count = require("../../hooks/use_host_count"); var _use_after_loaded_state = require("../../hooks/use_after_loaded_state"); 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; you may not use this file except in compliance with the Elastic License * 2.0. */ const Kpi = ({ id, title, layers, toolTip, height }) => { const { searchCriteria } = (0, _use_unified_search.useUnifiedSearchContext)(); const { dataView } = (0, _use_data_view.useMetricsDataViewContext)(); const { requestTs, hostNodes, loading: hostsLoading } = (0, _use_hosts_view.useHostsViewContext)(); const { data: hostCountData, isRequestRunning: hostCountLoading } = (0, _use_host_count.useHostCountContext)(); const shouldUseSearchCriteria = hostNodes.length === 0; const loading = hostsLoading || hostCountLoading; const getSubtitle = () => { var _hostCountData$count$; return searchCriteria.limit < ((_hostCountData$count$ = hostCountData === null || hostCountData === void 0 ? void 0 : hostCountData.count.value) !== null && _hostCountData$count$ !== void 0 ? _hostCountData$count$ : 0) ? _i18n.i18n.translate('xpack.infra.hostsViewPage.kpi.subtitle.average.limit', { defaultMessage: 'Average (of {limit} hosts)', values: { limit: searchCriteria.limit } }) : _visualizations.AVERAGE_SUBTITLE; }; const filters = (0, _react.useMemo)(() => { return shouldUseSearchCriteria ? searchCriteria.filters : [(0, _build.buildCombinedHostsFilter)({ field: 'host.name', values: hostNodes.map(p => p.name), dataView })]; }, [dataView, hostNodes, searchCriteria.filters, shouldUseSearchCriteria]); // prevents requestTs and searchCriteria state from reloading the chart // we want it to reload only once the table has finished loading const { afterLoadedState } = (0, _use_after_loaded_state.useAfterLoadedState)(loading, { lastReloadRequestTime: requestTs, dateRange: searchCriteria.dateRange, query: shouldUseSearchCriteria ? searchCriteria.query : undefined, filters }); return /*#__PURE__*/_react.default.createElement(_lens.LensChart, { id: `hostsViewKPI-${id}`, dataView: dataView, dateRange: afterLoadedState.dateRange, filters: afterLoadedState.filters, layers: { ...layers, options: { ...layers.options, subtitle: getSubtitle() } }, lastReloadRequestTime: afterLoadedState.lastReloadRequestTime, loading: loading, height: height, query: afterLoadedState.query, title: title, toolTip: /*#__PURE__*/_react.default.createElement(_lens.TooltipContent, { description: toolTip }), visualizationType: "lnsMetric", disableTriggers: true, hidePanelTitles: true }); }; exports.Kpi = Kpi;