"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ServerlessFunctions = ServerlessFunctions;
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _react = _interopRequireWildcard(require("react"));
var _formatters = require("../../../../../common/utils/formatters");
var _use_apm_service_context = require("../../../../context/apm_service/use_apm_service_context");
var _use_apm_params = require("../../../../hooks/use_apm_params");
var _use_fetcher = require("../../../../hooks/use_fetcher");
var _use_time_range = require("../../../../hooks/use_time_range");
var _serverless_function_name_link = require("./serverless_function_name_link");
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.
 */

function ServerlessFunctions() {
  const {
    query: {
      environment,
      kuery,
      rangeFrom,
      rangeTo
    }
  } = (0, _use_apm_params.useApmParams)('/services/{serviceName}/metrics');
  const {
    start,
    end
  } = (0, _use_time_range.useTimeRange)({
    rangeFrom,
    rangeTo
  });
  const {
    serviceName
  } = (0, _use_apm_service_context.useApmServiceContext)();
  const {
    data = {
      serverlessFunctionsOverview: []
    },
    status
  } = (0, _use_fetcher.useFetcher)(callApmApi => {
    if (!start || !end) {
      return undefined;
    }
    return callApmApi('GET /internal/apm/services/{serviceName}/metrics/serverless/functions_overview', {
      params: {
        path: {
          serviceName
        },
        query: {
          kuery,
          environment,
          start,
          end
        }
      }
    });
  }, [kuery, environment, serviceName, start, end]);
  const columns = [{
    field: 'serverlessFunctionName',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.functionName', {
      defaultMessage: 'Function name'
    }),
    sortable: true,
    truncateText: true,
    render: (_, item) => {
      return /*#__PURE__*/_react.default.createElement(_serverless_function_name_link.ServerlessFunctionNameLink, {
        serverlessFunctionName: item.serverlessFunctionName,
        serverlessId: item.serverlessId
      });
    }
  }, {
    field: 'serverlessDurationAvg',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.functionDuration', {
      defaultMessage: 'Function duration'
    }),
    sortable: true,
    render: (_, {
      serverlessDurationAvg
    }) => {
      return (0, _formatters.asMillisecondDuration)(serverlessDurationAvg);
    }
  }, {
    field: 'billedDurationAvg',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.billedDuration', {
      defaultMessage: 'Billed duration'
    }),
    sortable: true,
    render: (_, {
      billedDurationAvg
    }) => {
      return (0, _formatters.asMillisecondDuration)(billedDurationAvg);
    }
  }, {
    field: 'avgMemoryUsed',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.memoryUsageAvg', {
      defaultMessage: 'Memory usage avg.'
    }),
    sortable: true,
    render: (_, {
      avgMemoryUsed
    }) => {
      return (0, _formatters.asDynamicBytes)(avgMemoryUsed);
    }
  }, {
    field: 'memorySize',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.memorySize', {
      defaultMessage: 'Memory size'
    }),
    sortable: true,
    render: (_, {
      memorySize
    }) => {
      return (0, _formatters.asDynamicBytes)(memorySize);
    }
  }, {
    field: 'coldStartCount',
    name: _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.coldStart', {
      defaultMessage: 'Cold start'
    }),
    sortable: true
  }];
  const isLoading = status === _use_fetcher.FETCH_STATUS.LOADING;
  const sorting = (0, _react.useMemo)(() => ({
    sort: {
      field: 'serverlessDurationAvg',
      direction: 'desc'
    }
  }), []);
  return /*#__PURE__*/_react.default.createElement(_eui.EuiPanel, {
    hasBorder: true
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    direction: "column"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    alignItems: "center"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiTitle, {
    size: "s"
  }, /*#__PURE__*/_react.default.createElement("h2", null, _i18n.i18n.translate('xpack.apm.serverlessMetrics.serverlessFunctions.title', {
    defaultMessage: 'Lambda functions'
  })))))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiInMemoryTable, {
    loading: isLoading,
    items: data.serverlessFunctionsOverview,
    columns: columns,
    pagination: {
      showPerPageOptions: false,
      pageSize: 5
    },
    sorting: sorting
  }))));
}