"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCrashesByLocation = getCrashesByLocation;
var _common = require("@kbn/observability-plugin/common");
var _server = require("@kbn/observability-plugin/server");
var _apm = require("../../../common/es_fields/apm");
var _get_offset_in_ms = require("../../../common/utils/get_offset_in_ms");
var _get_bucket_size = require("../../../common/utils/get_bucket_size");
var _environment_query = require("../../../common/utils/environment_query");
/*
 * 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.
 */

async function getCrashesByLocation({
  kuery,
  apmEventClient,
  serviceName,
  environment,
  start,
  end,
  locationField,
  offset
}) {
  var _response$aggregation, _response$aggregation2, _response$aggregation3, _response$aggregation4, _response$aggregation5, _response$aggregation6, _response$aggregation7, _response$aggregation8, _response$aggregation9, _response$aggregation10, _response$aggregation11, _response$aggregation12;
  const {
    startWithOffset,
    endWithOffset
  } = (0, _get_offset_in_ms.getOffsetInMs)({
    start,
    end,
    offset
  });
  const {
    intervalString
  } = (0, _get_bucket_size.getBucketSize)({
    start: startWithOffset,
    end: endWithOffset,
    minBucketSize: 60
  });
  const aggs = {
    crashes: {
      filter: {
        term: {
          [_apm.ERROR_TYPE]: 'crash'
        }
      },
      aggs: {
        crashesByLocation: {
          terms: {
            field: locationField
          }
        }
      }
    }
  };
  const response = await apmEventClient.search('get_mobile_location_crashes', {
    apm: {
      events: [_common.ProcessorEvent.error]
    },
    body: {
      track_total_hits: false,
      size: 0,
      query: {
        bool: {
          filter: [...(0, _server.termQuery)(_apm.SERVICE_NAME, serviceName), ...(0, _server.rangeQuery)(startWithOffset, endWithOffset), ...(0, _environment_query.environmentQuery)(environment), ...(0, _server.kqlQuery)(kuery)]
        }
      },
      aggs: {
        timeseries: {
          date_histogram: {
            field: '@timestamp',
            fixed_interval: intervalString,
            min_doc_count: 0
          },
          aggs
        },
        ...aggs
      }
    }
  });
  return {
    location: (_response$aggregation = response.aggregations) === null || _response$aggregation === void 0 ? void 0 : (_response$aggregation2 = _response$aggregation.crashes) === null || _response$aggregation2 === void 0 ? void 0 : (_response$aggregation3 = _response$aggregation2.crashesByLocation) === null || _response$aggregation3 === void 0 ? void 0 : (_response$aggregation4 = _response$aggregation3.buckets[0]) === null || _response$aggregation4 === void 0 ? void 0 : _response$aggregation4.key,
    value: (_response$aggregation5 = (_response$aggregation6 = response.aggregations) === null || _response$aggregation6 === void 0 ? void 0 : (_response$aggregation7 = _response$aggregation6.crashes) === null || _response$aggregation7 === void 0 ? void 0 : (_response$aggregation8 = _response$aggregation7.crashesByLocation) === null || _response$aggregation8 === void 0 ? void 0 : (_response$aggregation9 = _response$aggregation8.buckets[0]) === null || _response$aggregation9 === void 0 ? void 0 : _response$aggregation9.doc_count) !== null && _response$aggregation5 !== void 0 ? _response$aggregation5 : 0,
    timeseries: (_response$aggregation10 = (_response$aggregation11 = response.aggregations) === null || _response$aggregation11 === void 0 ? void 0 : (_response$aggregation12 = _response$aggregation11.timeseries) === null || _response$aggregation12 === void 0 ? void 0 : _response$aggregation12.buckets.map(bucket => {
      var _response$aggregation13, _response$aggregation14, _response$aggregation15, _response$aggregation16, _response$aggregation17;
      return {
        x: bucket.key,
        y: (_response$aggregation13 = (_response$aggregation14 = response.aggregations) === null || _response$aggregation14 === void 0 ? void 0 : (_response$aggregation15 = _response$aggregation14.crashes) === null || _response$aggregation15 === void 0 ? void 0 : (_response$aggregation16 = _response$aggregation15.crashesByLocation) === null || _response$aggregation16 === void 0 ? void 0 : (_response$aggregation17 = _response$aggregation16.buckets[0]) === null || _response$aggregation17 === void 0 ? void 0 : _response$aggregation17.doc_count) !== null && _response$aggregation13 !== void 0 ? _response$aggregation13 : 0
      };
    })) !== null && _response$aggregation10 !== void 0 ? _response$aggregation10 : []
  };
}