Skip to content

Commit

Permalink
Fixes #12757 - Fixing field fetching for index patterns. (#12771)
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker authored Jul 11, 2017
1 parent 0d502ae commit df40112
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -10,17 +11,18 @@ 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;
}

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(<Component vis={this.vis} visData={visData} renderComplete={resolve}/>, this.el);
});
Expand Down
29 changes: 1 addition & 28 deletions src/core_plugins/metrics/public/kbn_vis_types/request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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-*'
]);
});
});
27 changes: 27 additions & 0 deletions src/core_plugins/metrics/public/lib/extract_index_patterns.js
Original file line number Diff line number Diff line change
@@ -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);

}

0 comments on commit df40112

Please sign in to comment.