"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.APMServiceContext = void 0; exports.ApmServiceContextProvider = ApmServiceContextProvider; exports.getOrRedirectToTransactionType = getOrRedirectToTransactionType; exports.getTransactionType = getTransactionType; var _react = _interopRequireWildcard(require("react")); var _reactRouterDom = require("react-router-dom"); var _transaction_types = require("../../../common/transaction_types"); var _use_service_transaction_types_fetcher = require("./use_service_transaction_types_fetcher"); var _use_service_agent_fetcher = require("./use_service_agent_fetcher"); var _use_apm_params = require("../../hooks/use_apm_params"); var _use_time_range = require("../../hooks/use_time_range"); var _use_fallback_to_transactions_fetcher = require("../../hooks/use_fallback_to_transactions_fetcher"); var _url_helpers = require("../../components/shared/links/url_helpers"); var _use_fetcher = require("../../hooks/use_fetcher"); 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 APMServiceContext = /*#__PURE__*/(0, _react.createContext)({ serviceName: '', transactionTypeStatus: _use_fetcher.FETCH_STATUS.NOT_INITIATED, transactionTypes: [], fallbackToTransactions: false, serviceAgentStatus: _use_fetcher.FETCH_STATUS.NOT_INITIATED }); exports.APMServiceContext = APMServiceContext; function ApmServiceContextProvider({ children }) { const history = (0, _reactRouterDom.useHistory)(); const { path: { serviceName }, query, query: { kuery, rangeFrom, rangeTo } } = (0, _use_apm_params.useAnyOfApmParams)('/services/{serviceName}', '/mobile-services/{serviceName}'); const { start, end } = (0, _use_time_range.useTimeRange)({ rangeFrom, rangeTo }); const { agentName, runtimeName, serverlessType, status: serviceAgentStatus } = (0, _use_service_agent_fetcher.useServiceAgentFetcher)({ serviceName, start, end }); const { transactionTypes, status: transactionTypeStatus } = (0, _use_service_transaction_types_fetcher.useServiceTransactionTypesFetcher)({ serviceName, start, end }); const currentTransactionType = getOrRedirectToTransactionType({ transactionType: query.transactionType, transactionTypes, agentName, history }); const { fallbackToTransactions } = (0, _use_fallback_to_transactions_fetcher.useFallbackToTransactionsFetcher)({ kuery }); return /*#__PURE__*/_react.default.createElement(APMServiceContext.Provider, { value: { serviceName, agentName, serverlessType, transactionType: currentTransactionType, transactionTypeStatus, transactionTypes, runtimeName, fallbackToTransactions, serviceAgentStatus }, children: children }); } const isTypeExistsInTransactionTypesList = ({ transactionType, transactionTypes }) => !!transactionType && transactionTypes.includes(transactionType); const isNoAgentAndNoTransactionTypes = ({ transactionTypes, agentName }) => !agentName || transactionTypes.length === 0; function getTransactionType({ transactionType, transactionTypes, agentName }) { const isTransactionTypeExists = isTypeExistsInTransactionTypesList({ transactionType, transactionTypes }); if (isTransactionTypeExists) return transactionType; const isNoAgentAndNoTransactionTypesExists = isNoAgentAndNoTransactionTypes({ transactionTypes, agentName }); if (isNoAgentAndNoTransactionTypesExists) return undefined; const defaultTransactionType = (0, _transaction_types.getDefaultTransactionType)(agentName); // If the default transaction type is not in transactionTypes the first in the list is returned const currentTransactionType = transactionTypes.includes(defaultTransactionType) ? defaultTransactionType : transactionTypes[0]; return currentTransactionType; } function getOrRedirectToTransactionType({ transactionType, transactionTypes, agentName, history }) { const isTransactionTypeExists = isTypeExistsInTransactionTypesList({ transactionType, transactionTypes }); if (isTransactionTypeExists) return transactionType; const isNoAgentAndNoTransactionTypesExists = isNoAgentAndNoTransactionTypes({ transactionTypes, agentName }); if (isNoAgentAndNoTransactionTypesExists) return undefined; const currentTransactionType = getTransactionType({ transactionTypes, transactionType, agentName }); // Replace transactionType in the URL in case it is not one of the types returned by the API (0, _url_helpers.replace)(history, { query: { transactionType: currentTransactionType } }); return currentTransactionType; }