"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TopNFunctionsGrid = void 0; var _eui = require("@elastic/eui"); var _i18n = require("@kbn/i18n"); var _lodash = require("lodash"); var _react = _interopRequireWildcard(require("react")); var _public = require("@kbn/observability-shared-plugin/public"); var _functions = require("../../../common/functions"); var _cpu_label_with_hint = require("../cpu_label_with_hint"); var _frame_information_tooltip = require("../frame_information_window/frame_information_tooltip"); var _label_with_hint = require("../label_with_hint"); var _function_row = require("./function_row"); var _utils = require("./utils"); 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 TopNFunctionsGrid = /*#__PURE__*/(0, _react.forwardRef)(({ topNFunctions, comparisonTopNFunctions, totalSeconds, isDifferentialView, baselineScaleFactor, comparisonScaleFactor, onFrameClick, onScroll, showDiffColumn = false, pageIndex, onChangePage, sortField, sortDirection, onChangeSort, dataTestSubj = 'topNFunctionsGrid' }, ref) => { const [selectedRow, setSelectedRow] = (0, _react.useState)(); const trackProfilingEvent = (0, _public.useUiTracker)({ app: 'profiling' }); function onSort(newSortingColumns) { const lastItem = (0, _lodash.last)(newSortingColumns); if (lastItem) { onChangeSort(lastItem); } } const totalCount = (0, _react.useMemo)(() => { if (!topNFunctions || !topNFunctions.TotalCount) { return 0; } return topNFunctions.TotalCount; }, [topNFunctions]); const rows = (0, _react.useMemo)(() => { return (0, _utils.getFunctionsRows)({ baselineScaleFactor, comparisonScaleFactor, comparisonTopNFunctions, topNFunctions, totalSeconds }); }, [baselineScaleFactor, comparisonScaleFactor, comparisonTopNFunctions, topNFunctions, totalSeconds]); const { columns, leadingControlColumns } = (0, _react.useMemo)(() => { const gridColumns = [{ id: _functions.TopNFunctionSortField.Rank, initialWidth: isDifferentialView ? 50 : 90, displayAsText: _i18n.i18n.translate('xpack.profiling.functionsView.rankColumnLabel', { defaultMessage: 'Rank' }) }, { id: _functions.TopNFunctionSortField.Frame, displayAsText: _i18n.i18n.translate('xpack.profiling.functionsView.functionColumnLabel', { defaultMessage: 'Function' }) }, { id: _functions.TopNFunctionSortField.Samples, initialWidth: isDifferentialView ? 100 : 200, schema: 'samples', display: /*#__PURE__*/_react.default.createElement(_label_with_hint.LabelWithHint, { label: _i18n.i18n.translate('xpack.profiling.functionsView.samplesColumnLabel', { defaultMessage: 'Samples' }), hint: _i18n.i18n.translate('xpack.profiling.functionsView.samplesColumnLabel.hint', { defaultMessage: 'Estimated values' }), labelSize: "s", labelStyle: { fontWeight: 700 }, iconSize: "s" }) }, { id: _functions.TopNFunctionSortField.SelfCPU, initialWidth: isDifferentialView ? 100 : 200, display: /*#__PURE__*/_react.default.createElement(_cpu_label_with_hint.CPULabelWithHint, { type: "self", labelSize: "s", labelStyle: { fontWeight: 700 }, iconSize: "s" }) }, { id: _functions.TopNFunctionSortField.TotalCPU, initialWidth: isDifferentialView ? 100 : 200, display: /*#__PURE__*/_react.default.createElement(_cpu_label_with_hint.CPULabelWithHint, { type: "total", labelSize: "s", labelStyle: { fontWeight: 700 }, iconSize: "s" }) }]; const gridLeadingControlColumns = []; if (showDiffColumn) { gridColumns.push({ initialWidth: 60, id: _functions.TopNFunctionSortField.Diff, displayAsText: _i18n.i18n.translate('xpack.profiling.functionsView.diffColumnLabel', { defaultMessage: 'Diff' }) }); } if (!isDifferentialView) { gridColumns.push({ id: _functions.TopNFunctionSortField.AnnualizedCo2, initialWidth: isDifferentialView ? 100 : 200, schema: 'numeric', display: /*#__PURE__*/_react.default.createElement(_label_with_hint.LabelWithHint, { label: _i18n.i18n.translate('xpack.profiling.functionsView.annualizedCo2', { defaultMessage: 'Annualized CO2' }), hint: _i18n.i18n.translate('xpack.profiling.functionsView.annualizedCo2.hint', { defaultMessage: 'Indicates the CO2 emission of the function and any functions called by it.' }), labelSize: "s", labelStyle: { fontWeight: 700 }, iconSize: "s" }) }, { id: _functions.TopNFunctionSortField.AnnualizedDollarCost, initialWidth: isDifferentialView ? 100 : 200, display: /*#__PURE__*/_react.default.createElement(_label_with_hint.LabelWithHint, { label: _i18n.i18n.translate('xpack.profiling.functionsView.annualizedDollarCost', { defaultMessage: `Annualized dollar cost` }), hint: _i18n.i18n.translate('xpack.profiling.functionsView.annualizedDollarCost.hint', { defaultMessage: `Indicates the Dollar cost of the function and any functions called by it.` }), labelSize: "s", labelStyle: { fontWeight: 700 }, iconSize: "s" }) }); gridLeadingControlColumns.push({ id: 'actions', width: 40, headerCellRender() { return /*#__PURE__*/_react.default.createElement(_eui.EuiScreenReaderOnly, null, /*#__PURE__*/_react.default.createElement("span", null, "Controls")); }, rowCellRender: function RowCellRender({ rowIndex }) { function handleOnClick() { trackProfilingEvent({ metric: 'topN_function_details_click' }); setSelectedRow(rows[rowIndex]); } return /*#__PURE__*/_react.default.createElement(_eui.EuiButtonIcon, { "aria-label": "Show actions", iconType: "expand", color: "text", onClick: handleOnClick }); } }); } return { columns: gridColumns, leadingControlColumns: gridLeadingControlColumns }; }, [isDifferentialView, rows, showDiffColumn, trackProfilingEvent]); const [visibleColumns, setVisibleColumns] = (0, _react.useState)(columns.map(({ id }) => id)); function RenderCellValue({ rowIndex, columnId, setCellProps }) { const data = rows[rowIndex]; if (data) { return /*#__PURE__*/_react.default.createElement(_function_row.FunctionRow, { functionRow: data, columnId: columnId, totalCount: totalCount, onFrameClick: onFrameClick, setCellProps: setCellProps }); } return null; } return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiDataGrid, { "data-test-subj": dataTestSubj, ref: ref, "aria-label": "TopN functions", columns: columns, columnVisibility: { visibleColumns, setVisibleColumns }, rowCount: totalCount > 100 ? 100 : totalCount, renderCellValue: RenderCellValue, inMemory: { level: 'sorting' }, sorting: { columns: [{ id: sortField, direction: sortDirection }], onSort }, leadingControlColumns: leadingControlColumns, pagination: { pageIndex, pageSize: 50, // Left it empty on purpose as it is a required property on the pagination onChangeItemsPerPage: () => {}, onChangePage }, rowHeightsOptions: { defaultHeight: 'auto' }, toolbarVisibility: { showColumnSelector: false, showKeyboardShortcuts: !isDifferentialView, showDisplaySelector: !isDifferentialView, showFullScreenSelector: !isDifferentialView, showSortSelector: false }, virtualizationOptions: { onScroll }, schemaDetectors: [{ type: 'samples', comparator: (a, b, direction) => { const aNumber = parseFloat(a.replace(/,/g, '')); const bNumber = parseFloat(b.replace(/,/g, '')); if (aNumber < bNumber) { return direction === 'desc' ? 1 : -1; } if (aNumber > bNumber) { return direction === 'desc' ? -1 : 1; } return 0; }, detector: a => { return 1; }, icon: '', sortTextAsc: 'Low-High', sortTextDesc: 'High-Low' }] }), selectedRow && /*#__PURE__*/_react.default.createElement(_frame_information_tooltip.FrameInformationTooltip, { onClose: () => { setSelectedRow(undefined); }, frame: { addressOrLine: selectedRow.frame.AddressOrLine, countExclusive: selectedRow.selfCPU, countInclusive: selectedRow.totalCPU, exeFileName: selectedRow.frame.ExeFileName, fileID: selectedRow.frame.FileID, frameType: selectedRow.frame.FrameType, functionName: selectedRow.frame.FunctionName, sourceFileName: selectedRow.frame.SourceFilename, sourceLine: selectedRow.frame.SourceLine }, totalSeconds: totalSeconds, totalSamples: totalCount })); }); exports.TopNFunctionsGrid = TopNFunctionsGrid;