"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.assistantRouteRepository = void 0; var _datemath = _interopRequireDefault(require("@elastic/datemath")); var t = _interopRequireWildcard(require("io-ts")); var _lodash = require("lodash"); var _document_type = require("../../../common/document_type"); var _environment_filter_values = require("../../../common/environment_filter_values"); var _rollup = require("../../../common/rollup"); var _service_health_status = require("../../../common/service_health_status"); var _get_apm_alerts_client = require("../../lib/helpers/get_apm_alerts_client"); var _get_apm_event_client = require("../../lib/helpers/get_apm_event_client"); var _get_ml_client = require("../../lib/helpers/get_ml_client"); var _get_random_sampler = require("../../lib/helpers/get_random_sampler"); var _create_apm_server_route = require("../apm_routes/create_apm_server_route"); var _get_services_items = require("../services/get_services/get_services_items"); var _get_apm_correlation_values = require("./get_apm_correlation_values"); var _get_apm_downstream_dependencies = require("./get_apm_downstream_dependencies"); var _get_apm_error_document = require("./get_apm_error_document"); var _get_apm_service_summary = require("./get_apm_service_summary"); var _get_apm_timeseries = require("./get_apm_timeseries"); 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 getApmTimeSeriesRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'POST /internal/apm/assistant/get_apm_timeseries', options: { tags: ['access:apm', 'access:ai_assistant'] }, params: t.type({ body: _get_apm_timeseries.getApmTimeseriesRt }), handler: async resources => { const body = resources.params.body; const apmEventClient = await (0, _get_apm_event_client.getApmEventClient)(resources); const timeseries = await (0, _get_apm_timeseries.getApmTimeseries)({ apmEventClient, arguments: body }); return { content: timeseries.map(series => (0, _lodash.omit)(series, 'data')), data: timeseries }; } }); const getApmServiceSummaryRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'GET /internal/apm/assistant/get_service_summary', options: { tags: ['access:apm', 'access:ai_assistant'] }, params: t.type({ query: _get_apm_service_summary.serviceSummaryRouteRt }), handler: async resources => { const args = resources.params.query; const { context, request, plugins, logger } = resources; const [apmEventClient, annotationsClient, esClient, apmAlertsClient, mlClient] = await Promise.all([(0, _get_apm_event_client.getApmEventClient)(resources), plugins.observability.setup.getScopedAnnotationsClient(context, request), context.core.then(coreContext => coreContext.elasticsearch.client.asCurrentUser), (0, _get_apm_alerts_client.getApmAlertsClient)(resources), (0, _get_ml_client.getMlClient)(resources)]); return { content: await (0, _get_apm_service_summary.getApmServiceSummary)({ apmEventClient, annotationsClient, esClient, apmAlertsClient, mlClient, logger, arguments: args }) }; } }); const getDownstreamDependenciesRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'GET /internal/apm/assistant/get_downstream_dependencies', params: t.type({ query: _get_apm_downstream_dependencies.downstreamDependenciesRouteRt }), options: { tags: ['access:apm'] }, handler: async resources => { const { params } = resources; const apmEventClient = await (0, _get_apm_event_client.getApmEventClient)(resources); const { query } = params; return { content: await (0, _get_apm_downstream_dependencies.getAssistantDownstreamDependencies)({ arguments: query, apmEventClient }) }; } }); const getApmCorrelationValuesRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'POST /internal/apm/assistant/get_correlation_values', params: t.type({ body: _get_apm_correlation_values.correlationValuesRouteRt }), options: { tags: ['access:apm'] }, handler: async resources => { const { params } = resources; const apmEventClient = await (0, _get_apm_event_client.getApmEventClient)(resources); const { body } = params; return { content: await (0, _get_apm_correlation_values.getApmCorrelationValues)({ arguments: body, apmEventClient }) }; } }); const getApmErrorDocRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'GET /internal/apm/assistant/get_error_document', params: t.type({ query: _get_apm_error_document.errorRouteRt }), options: { tags: ['access:apm'] }, handler: async resources => { const { params } = resources; const apmEventClient = await (0, _get_apm_event_client.getApmEventClient)(resources); const { query } = params; return { content: await (0, _get_apm_error_document.getApmErrorDocument)({ apmEventClient, arguments: query }) }; } }); const getApmServicesListRoute = (0, _create_apm_server_route.createApmServerRoute)({ endpoint: 'POST /internal/apm/assistant/get_services_list', params: t.type({ body: t.intersection([t.type({ start: t.string, end: t.string }), t.partial({ 'service.environment': t.string, healthStatus: t.array(t.union([t.literal(_service_health_status.ServiceHealthStatus.unknown), t.literal(_service_health_status.ServiceHealthStatus.healthy), t.literal(_service_health_status.ServiceHealthStatus.warning), t.literal(_service_health_status.ServiceHealthStatus.critical)])) })]) }), options: { tags: ['access:apm'] }, handler: async resources => { var _datemath$parse, _datemath$parse2; const { params } = resources; const { body } = params; const { healthStatus } = body; const [apmEventClient, apmAlertsClient, mlClient, randomSampler] = await Promise.all([(0, _get_apm_event_client.getApmEventClient)(resources), (0, _get_apm_alerts_client.getApmAlertsClient)(resources), (0, _get_ml_client.getMlClient)(resources), (0, _get_random_sampler.getRandomSampler)({ security: resources.plugins.security, probability: 1, request: resources.request })]); const start = (_datemath$parse = _datemath.default.parse(body.start)) === null || _datemath$parse === void 0 ? void 0 : _datemath$parse.valueOf(); const end = (_datemath$parse2 = _datemath.default.parse(body.end)) === null || _datemath$parse2 === void 0 ? void 0 : _datemath$parse2.valueOf(); const serviceItems = await (0, _get_services_items.getServicesItems)({ apmAlertsClient, apmEventClient, documentType: _document_type.ApmDocumentType.TransactionMetric, start, end, environment: body['service.environment'] || _environment_filter_values.ENVIRONMENT_ALL.value, kuery: '', logger: resources.logger, randomSampler, rollupInterval: _rollup.RollupInterval.OneMinute, serviceGroup: null, mlClient }); let mappedItems = serviceItems.items.map(item => { var _item$alertsCount, _item$healthStatus; return { 'service.name': item.serviceName, 'agent.name': item.agentName, alertsCount: (_item$alertsCount = item.alertsCount) !== null && _item$alertsCount !== void 0 ? _item$alertsCount : 0, healthStatus: (_item$healthStatus = item.healthStatus) !== null && _item$healthStatus !== void 0 ? _item$healthStatus : _service_health_status.ServiceHealthStatus.unknown, 'service.environment': item.environments, 'transaction.type': item.transactionType }; }); if (healthStatus && healthStatus.length) { mappedItems = mappedItems.filter(item => healthStatus.includes(item.healthStatus)); } return { content: mappedItems }; } }); const assistantRouteRepository = { ...getApmTimeSeriesRoute, ...getApmServiceSummaryRoute, ...getApmErrorDocRoute, ...getApmCorrelationValuesRoute, ...getDownstreamDependenciesRoute, ...getApmServicesListRoute }; exports.assistantRouteRepository = assistantRouteRepository;