diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/annorations/get_request_params.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/annorations/get_request_params.js index be8e74960ae6d..f3cfd06649489 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/annorations/get_request_params.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/annorations/get_request_params.js @@ -18,14 +18,15 @@ */ import buildRequestBody from './build_request_body'; import getEsShardTimeout from '../helpers/get_es_shard_timeout'; +import { getIndexPatternObject } from '../helpers/get_index_pattern'; -export default (req, panel, annotation, capabilities) => { +export default async (req, panel, annotation, esQueryConfig, capabilities) => { const bodies = []; const timeout = getEsShardTimeout(req); + const indexPattern = annotation.index_pattern; + const indexPatternObject = await getIndexPatternObject(req, indexPattern); if (capabilities.batchRequestsSupport) { - const indexPattern = annotation.index_pattern; - bodies.push({ index: indexPattern, ignoreUnavailable: true, @@ -33,7 +34,7 @@ export default (req, panel, annotation, capabilities) => { } bodies.push({ - ...buildRequestBody(req, panel, annotation, capabilities), + ...buildRequestBody(req, panel, annotation, esQueryConfig, indexPatternObject, capabilities), timeout }); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_annotations.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_annotations.js index 8062d72d98d34..4c47f93aee1b7 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_annotations.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_annotations.js @@ -19,8 +19,6 @@ import handleAnnotationResponse from './handle_annotation_response'; import getRequestParams from './annorations/get_request_params'; -import SearchStrategiesRegister from '../search_strategies/search_strategies_register'; - function validAnnotation(annotation) { return annotation.index_pattern && annotation.time_field && @@ -29,15 +27,14 @@ function validAnnotation(annotation) { annotation.template; } -export default async (req, panel, esQueryConfig) => { +export default async (req, panel, esQueryConfig, searchStrategy, capabilities) => { const indexPattern = panel.index_pattern; - const { searchStrategy, capabilities } = await SearchStrategiesRegister.getViableStrategy(req, indexPattern); const searchRequest = searchStrategy.getSearchRequest(req, indexPattern); const annotations = panel.annotations.filter(validAnnotation); - const body = annotations - .map(annotation => getRequestParams(req, panel, annotation, esQueryConfig, capabilities)) - .reduce((acc, item) => acc.concat(item), []); + const bodiesPromises = annotations.map(annotation => getRequestParams(req, panel, annotation, esQueryConfig, capabilities)); + const body = (await Promise.all(bodiesPromises)) + .reduce((acc, items) => acc.concat(items), []); if (!body.length) return { responses: [] }; diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_series_data.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_series_data.js index 1970fbbb3bd80..ff697452bcf9b 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_series_data.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_series_data.js @@ -21,6 +21,7 @@ import handleResponseBody from './series/handle_response_body'; import handleErrorResponse from './handle_error_response'; import getAnnotations from './get_annotations'; import SearchStrategiesRegister from '../search_strategies/search_strategies_register'; +import { getEsQueryConfig } from './helpers/get_es_query_uisettings'; export async function getSeriesData(req, panel) { const indexPattern = panel.index_pattern; @@ -28,13 +29,13 @@ export async function getSeriesData(req, panel) { const searchRequest = searchStrategy.getSearchRequest(req, indexPattern); const esQueryConfig = await getEsQueryConfig(req); - const body = panel.series - .map(series => getRequestParams(req, panel, series, capabilities)) + const bodiesPromises = panel.series.map(series => getRequestParams(req, panel, series, esQueryConfig, capabilities)); + const body = (await Promise.all(bodiesPromises)) .reduce((acc, items) => acc.concat(items), []); return searchRequest.search({ body }) .then(data => { - const series = data.map(handleResponseBody(panel, esQueryConfig)); + const series = data.map(handleResponseBody(panel)); return { [panel.id]: { id: panel.id, @@ -44,7 +45,7 @@ export async function getSeriesData(req, panel) { }) .then(resp => { if (!panel.annotations || panel.annotations.length === 0) return resp; - return getAnnotations(req, panel, esQueryConfig).then(annotations => { + return getAnnotations(req, panel, esQueryConfig, searchStrategy, capabilities).then(annotations => { resp[panel.id].annotations = annotations; return resp; }); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_table_data.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_table_data.js index a1ed051c255ae..692d0df75199c 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/get_table_data.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/get_table_data.js @@ -22,20 +22,16 @@ import handleErrorResponse from './handle_error_response'; import { get } from 'lodash'; import processBucket from './table/process_bucket'; import SearchStrategiesRegister from '../search_strategies/search_strategies_register'; +import { getEsQueryConfig } from './helpers/get_es_query_uisettings'; +import { getIndexPatternObject } from './helpers/get_index_pattern'; export async function getTableData(req, panel) { const indexPattern = panel.index_pattern; - const { searchStrategy } = await SearchStrategiesRegister.getViableStrategy(req, indexPattern); + const { searchStrategy, capabilities } = await SearchStrategiesRegister.getViableStrategy(req, indexPattern); const searchRequest = searchStrategy.getSearchRequest(req, indexPattern); - const body = buildRequestBody(req, panel); - -// todo: -// const indexPatternObject = await getIndexPatternObject(req, indexPatternString); -// const params = { -// index: indexPatternString, -// ignore_throttled: !includeFrozen, -// body: buildRequestBody(req, panel, esQueryConfig, indexPatternObject) -// }; + const esQueryConfig = await getEsQueryConfig(req); + const indexPatternObject = await getIndexPatternObject(req, indexPattern); + const body = buildRequestBody(req, panel, esQueryConfig, indexPatternObject, capabilities); try { const resp = await searchRequest.search({ body }); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js index e426bb8f1dda6..e9a488db23f7e 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/annotations/date_histogram.js @@ -20,7 +20,7 @@ import _ from 'lodash'; import getBucketSize from '../../helpers/get_bucket_size'; import getTimerange from '../../helpers/get_timerange'; -export default function dateHistogram(req, panel, annotation, capabilities) { +export default function dateHistogram(req, panel, annotation, esQueryConfig, indexPatternObject, capabilities) { return next => doc => { const timeField = annotation.time_field; const { bucketSize, intervalString } = getBucketSize(req, 'auto', capabilities); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js index 7c28b84de22eb..a1d16198112fa 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/__tests__/date_histogram.js @@ -28,6 +28,9 @@ describe('dateHistogram(req, panel, series)', () => { let series; let req; let capabilities; + let config; + let indexPatternObject; + beforeEach(() => { req = { payload: { @@ -44,18 +47,23 @@ describe('dateHistogram(req, panel, series)', () => { interval: '10s' }; series = { id: 'test' }; + config = { + allowLeadingWildcards: true, + queryStringOptions: {}, + }; + indexPatternObject = {}; capabilities = new DefaultSearchCapabilities(req, true); }); it('calls next when finished', () => { const next = sinon.spy(); - dateHistogram(req, panel, series, capabilities)(next)({}); + dateHistogram(req, panel, series, config, indexPatternObject, capabilities)(next)({}); expect(next.calledOnce).to.equal(true); }); it('returns valid date histogram', () => { const next = doc => doc; - const doc = dateHistogram(req, panel, series, capabilities)(next)({}); + const doc = dateHistogram(req, panel, series, config, indexPatternObject, capabilities)(next)({}); expect(doc).to.eql({ aggs: { test: { @@ -87,7 +95,7 @@ describe('dateHistogram(req, panel, series)', () => { it('returns valid date histogram (offset by 1h)', () => { series.offset_time = '1h'; const next = doc => doc; - const doc = dateHistogram(req, panel, series, capabilities)(next)({}); + const doc = dateHistogram(req, panel, series, config, indexPatternObject, capabilities)(next)({}); expect(doc).to.eql({ aggs: { test: { @@ -122,7 +130,7 @@ describe('dateHistogram(req, panel, series)', () => { series.series_time_field = 'timestamp'; series.series_interval = '20s'; const next = doc => doc; - const doc = dateHistogram(req, panel, series, capabilities)(next)({}); + const doc = dateHistogram(req, panel, series, config, indexPatternObject, capabilities)(next)({}); expect(doc).to.eql({ aggs: { test: { diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js index 5dd613be3e805..6c4934ebc2c15 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/date_histogram.js @@ -21,7 +21,7 @@ import getBucketSize from '../../helpers/get_bucket_size'; import offsetTime from '../../offset_time'; import getIntervalAndTimefield from '../../get_interval_and_timefield'; import { set } from 'lodash'; -export default function dateHistogram(req, panel, series, capabilities) { +export default function dateHistogram(req, panel, series, esQueryConfig, indexPatternObject, capabilities) { return next => doc => { const { timeField, interval } = getIntervalAndTimefield(panel, series); const { bucketSize, intervalString } = getBucketSize(req, interval, capabilities); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/metric_buckets.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/metric_buckets.js index 764919a46b8e5..19aad8f37d825 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/metric_buckets.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/metric_buckets.js @@ -21,7 +21,7 @@ import _ from 'lodash'; import getBucketSize from '../../helpers/get_bucket_size'; import bucketTransform from '../../helpers/bucket_transform'; import getIntervalAndTimefield from '../../get_interval_and_timefield'; -export default function metricBuckets(req, panel, series, capabilities) { +export default function metricBuckets(req, panel, series, esQueryConfig, indexPatternObject, capabilities) { return next => doc => { const { interval diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/sibling_buckets.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/sibling_buckets.js index 03d61508e61bf..351ff2bf27f62 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/sibling_buckets.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/request_processors/series/sibling_buckets.js @@ -21,7 +21,7 @@ import _ from 'lodash'; import getBucketSize from '../../helpers/get_bucket_size'; import bucketTransform from '../../helpers/bucket_transform'; import getIntervalAndTimefield from '../../get_interval_and_timefield'; -export default function siblingBuckets(req, panel, series, capabilities) { +export default function siblingBuckets(req, panel, series, esQueryConfig, indexPatternObject, capabilities) { return next => doc => { const { interval diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/__tests__/build_request_body.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/__tests__/build_request_body.js index 8aa160cf6cfbb..17bdaf220c662 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/__tests__/build_request_body.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/__tests__/build_request_body.js @@ -84,34 +84,24 @@ describe('buildRequestBody(req)', () => { const panel = body.panels[0]; const series = panel.series[0]; const getSearchTimezone = sinon.spy(() => 'UTC'); - const getSearchInterval = sinon.spy(); const getValidTimeInterval = sinon.spy(() => '10s'); - const capabilities = { getSearchTimezone, - getSearchInterval, getValidTimeInterval }; - const config = { + const config = { allowLeadingWildcards: true, queryStringOptions: {}, }; - const doc = buildRequestBody({ payload: body }, panel, series, config); + const indexPatternObject = {}; + const doc = buildRequestBody({ payload: body }, panel, series, config, indexPatternObject, capabilities); expect(getSearchTimezone.calledOnce).to.equal(true); expect(doc).to.eql({ size: 0, query: { bool: { + filter: [], must: [ - { - range: { - '@timestamp': { - gte: 1485463055881, - lte: 1485463955881, - format: 'epoch_millis' - } - } - }, { bool: { must: [ @@ -124,41 +114,52 @@ describe('buildRequestBody(req)', () => { ], must_not: [] } - } - ] + }, + { + range: { + '@timestamp': { + gte: 1485463055881, + lte: 1485463955881, + format: 'epoch_millis' + } + } + }, + ], + must_not: [], + should: [], } }, - 'aggs': { - 'timeseries': { - 'aggs': { + aggs: { + timeseries: { + aggs: { 'c9b5f9c1-e403-11e6-be91-6f7688e9fac7': { - 'bucket_script': { - 'buckets_path': { - 'count': '_count' + bucket_script: { + buckets_path: { + count: '_count' }, - 'gap_policy': 'skip', - 'script': { - 'lang': 'expression', - 'source': 'count * 1' + gap_policy: 'skip', + script: { + lang: 'expression', + source: 'count * 1' } } } }, - 'date_histogram': { - 'extended_bounds': { - 'max': 1485463955881, - 'min': 1485463055881 + date_histogram: { + extended_bounds: { + max: 1485463955881, + min: 1485463055881 }, - 'field': '@timestamp', - 'interval': '10s', - 'min_doc_count': 0, - 'time_zone': 'UTC' + field: '@timestamp', + interval: '10s', + min_doc_count: 0, + time_zone: 'UTC' }, - 'meta': { - 'bucketSize': 10, - 'intervalString': '10s', - 'seriesId': 'c9b5f9c0-e403-11e6-be91-6f7688e9fac7', - 'timeField': '@timestamp' + meta: { + bucketSize: 10, + intervalString: '10s', + seriesId: 'c9b5f9c0-e403-11e6-be91-6f7688e9fac7', + timeField: '@timestamp' } } } diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/build_request_body.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/build_request_body.js index 7f17e60e235c5..57f49b923172a 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/build_request_body.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/build_request_body.js @@ -20,7 +20,6 @@ import buildProcessorFunction from '../build_processor_function'; import processors from '../request_processors/series'; -//todo: req, panel, series, esQueryConfig, indexPatter, capabilities function buildRequestBody(...args) { const processor = buildProcessorFunction(processors, ...args); const doc = processor({}); diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/get_request_params.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/get_request_params.js index 8f7cf6e3ac8b4..20980d81960ae 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/series/get_request_params.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/series/get_request_params.js @@ -18,23 +18,24 @@ */ import buildRequestBody from './build_request_body'; import getEsShardTimeout from '../helpers/get_es_shard_timeout'; +import { getIndexPatternObject } from '../helpers/get_index_pattern'; -export default (req, panel, series, esQueryConfig, capabilities) => { +export default async (req, panel, series, esQueryConfig, capabilities) => { const bodies = []; - const indexPatternString = series.override_index_pattern && series.series_index_pattern || panel.index_pattern; - const indexPatternObject = await getIndexPatternObject(req, indexPatternString); + const indexPattern = series.override_index_pattern && series.series_index_pattern || panel.index_pattern; + const indexPatternObject = await getIndexPatternObject(req, indexPattern); const timeout = getEsShardTimeout(req); if (capabilities.batchRequestsSupport) { bodies.push({ - index: indexPatternString, + index: indexPattern, ignoreUnavailable: true, }); } bodies.push({ ...buildRequestBody(req, panel, series, esQueryConfig, indexPatternObject, capabilities), - timeout + timeout, }); return bodies; diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/table/build_request_body.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/table/build_request_body.js index 7e509a5ebf2e5..349863d9cb6c0 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/table/build_request_body.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/table/build_request_body.js @@ -20,8 +20,8 @@ import buildProcessorFunction from '../build_processor_function'; import processors from '../request_processors/table'; -function buildRequestBody(req, panel, esQueryConfig, indexPattern) { - const processor = buildProcessorFunction(processors, req, panel, esQueryConfig, indexPattern); +function buildRequestBody(...args) { + const processor = buildProcessorFunction(processors, ...args); const doc = processor({}); return doc; }