diff --git a/src/core_plugins/metrics/public/kbn_vis_types/editor_controller.js b/src/core_plugins/metrics/public/kbn_vis_types/editor_controller.js index d78b9fd9fc36c..525d9a4bacf02 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/editor_controller.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/editor_controller.js @@ -1,6 +1,7 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { FetchFieldsProvider } from '../lib/fetch_fields'; +import { extractIndexPatterns } from '../lib/extract_index_patterns'; const AUTO_APPLY_KEY = 'metrics_autoApply'; function ReactEditorControllerProvider(Private, localStorage) { @@ -10,6 +11,7 @@ function ReactEditorControllerProvider(Private, localStorage) { constructor(el, vis) { this.el = el; this.vis = vis; + this.vis.fields = {}; const autoApply = localStorage.get(AUTO_APPLY_KEY); vis.autoApply = autoApply != null ? autoApply : true; @@ -17,10 +19,10 @@ function ReactEditorControllerProvider(Private, localStorage) { render(visData) { this.visData = visData; - + const indexPatterns = extractIndexPatterns(this.vis); return new Promise((resolve) => { - fetchFields(this.vis.params.index_pattern).then(fields => { - this.vis.fields = fields; + fetchFields(indexPatterns).then(fields => { + this.vis.fields = { ...fields, ...this.vis.fields }; const Component = this.vis.type.editorConfig.component; render(, this.el); }); diff --git a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js index 2282bdb02b131..41b296ed0bc3e 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js @@ -22,34 +22,7 @@ const MetricsRequestHandlerProvider = function (Private, Notifier, config, timef const maxBuckets = config.get('metrics:max_buckets'); validateInterval(timefilter, panel, maxBuckets); return $http.post('../api/metrics/vis/data', params) - .success(resp => { - - const patternsToFetch = []; - // Fetch any missing index patterns - if (!vis.fields) vis.fields = {}; - - if (!vis.fields[vis.params.index_pattern]) { - patternsToFetch.push(vis.params.index_pattern); - } - - vis.params.series.forEach(series => { - if (series.override_index_pattern && - !vis.fields[series.series_index_pattern]) { - patternsToFetch.push(series.series_index_pattern); - } - }); - - if (vis.params.annotations) { - vis.params.annotations.forEach(item => { - if (item.index_pattern && - !vis.fields[item.index_pattern]) { - patternsToFetch.push(item.index_pattern); - } - }); - } - - resolve(resp); - }) + .success(resolve) .error(resp => { resolve({}); const err = new Error(resp.message); diff --git a/src/core_plugins/metrics/public/lib/__tests__/extract_index_pattern.js b/src/core_plugins/metrics/public/lib/__tests__/extract_index_pattern.js new file mode 100644 index 0000000000000..ea181c396df40 --- /dev/null +++ b/src/core_plugins/metrics/public/lib/__tests__/extract_index_pattern.js @@ -0,0 +1,47 @@ +import { extractIndexPatterns } from '../extract_index_patterns'; +import { expect } from 'chai'; +describe('extractIndexPatterns(vis)', () => { + let vis; + beforeEach(() => { + vis = { + fields: { + '*': [] + }, + params: { + index_pattern: '*', + series: [ + { + override_index_pattern: 1, + series_index_pattern: 'example-1-*' + }, + { + override_index_pattern: 1, + series_index_pattern: 'example-2-*' + } + ], + annotations: [ + { index_pattern: 'notes-*' }, + { index_pattern: 'example-1-*' } + ] + } + }; + }); + + it('should return index patterns', () => { + vis.fields = {}; + expect(extractIndexPatterns(vis)).to.eql([ + '*', + 'example-1-*', + 'example-2-*', + 'notes-*' + ]); + }); + + it('should return index patterns that do not exist in vis.fields', () => { + expect(extractIndexPatterns(vis)).to.eql([ + 'example-1-*', + 'example-2-*', + 'notes-*' + ]); + }); +}); diff --git a/src/core_plugins/metrics/public/lib/extract_index_patterns.js b/src/core_plugins/metrics/public/lib/extract_index_patterns.js new file mode 100644 index 0000000000000..81bb6a0cd7105 --- /dev/null +++ b/src/core_plugins/metrics/public/lib/extract_index_patterns.js @@ -0,0 +1,27 @@ +import { uniq } from 'lodash'; +export function extractIndexPatterns(vis) { + const patternsToFetch = []; + + if (!vis.fields[vis.params.index_pattern]) { + patternsToFetch.push(vis.params.index_pattern); + } + + vis.params.series.forEach(series => { + const indexPattern = series.series_index_pattern; + if (series.override_index_pattern && !vis.fields[indexPattern]) { + patternsToFetch.push(indexPattern); + } + }); + + if (vis.params.annotations) { + vis.params.annotations.forEach(item => { + const indexPattern = item.index_pattern; + if (indexPattern && !vis.fields[indexPattern]) { + patternsToFetch.push(indexPattern); + } + }); + } + + return uniq(patternsToFetch); + +}