Skip to content

Commit

Permalink
clean up control initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Sep 11, 2017
1 parent 8ca7fdf commit b099c90
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import { rangeControlFactory } from './range_control_factory';
import { listControlFactory } from './list_control_factory';

export function controlFactory(controlParamsArray, kbnApi, callback) {
const createRequestPromises = controlParamsArray.filter((control) => {
// ignore controls that do not have indexPattern or field
return control.indexPattern && control.fieldName;
})
.map(async (controlParams, index) => {
let factory = null;
switch (controlParams.type) {
case 'range':
factory = rangeControlFactory;
break;
case 'list':
factory = listControlFactory;
break;
}

if (factory) {
return factory(
controlParams,
kbnApi,
callback.bind(null, index));
}
});
Promise.all(createRequestPromises).then(searchRequests => {
// control factory may return nothing if Control does not require a SearchRequest for initialization
// remove empty elements from array before fetch
const validSearchRequests = searchRequests.filter((request) => {
if (request) {
return true;
}
return false;
});
if (validSearchRequests.length > 0) {
kbnApi.fetch.these(validSearchRequests);
}
});
export function controlFactory(controlParams) {
let factory = null;
switch (controlParams.type) {
case 'range':
factory = rangeControlFactory;
break;
case 'list':
factory = listControlFactory;
break;
default:
throw new Error(`Unhandled control type ${controlParams.type}`);
}
return factory;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ListControl extends Control {
}
}

export async function listControlFactory(controlParams, kbnApi, callback) {
export async function listControlFactory(controlParams, kbnApi) {
const indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
const searchSource = new kbnApi.SearchSource();
searchSource.inherits(false); //Do not filter by time so can not inherit from rootSearchSource
Expand All @@ -52,14 +52,15 @@ export async function listControlFactory(controlParams, kbnApi, callback) {
defer.resolve = resolve;
defer.reject = reject;
});
defer.promise.then((resp) => {
callback(new ListControl(
controlParams,
new PhraseFilterManager(controlParams.fieldName, indexPattern, kbnApi.queryFilter),
_.get(resp, 'aggregations.termsAgg.buckets', []).map((bucket) => {
return { label: bucket.key, value: bucket.key };
})
));
});
return searchSource._createRequest(defer);
kbnApi.fetch.these([searchSource._createRequest(defer)]);

const resp = await defer.promise;

return new ListControl(
controlParams,
new PhraseFilterManager(controlParams.fieldName, indexPattern, kbnApi.queryFilter),
_.get(resp, 'aggregations.termsAgg.buckets', []).map((bucket) => {
return { label: bucket.key, value: bucket.key };
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RangeControl extends Control {
}
}

export async function rangeControlFactory(controlParams, kbnApi, callback) {
export async function rangeControlFactory(controlParams, kbnApi) {
const indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
const searchSource = new kbnApi.SearchSource();
searchSource.inherits(false); //Do not filter by time so can not inherit from rootSearchSource
Expand All @@ -43,16 +43,17 @@ export async function rangeControlFactory(controlParams, kbnApi, callback) {
defer.resolve = resolve;
defer.reject = reject;
});
defer.promise.then((resp) => {
const min = _.get(resp, 'aggregations.minAgg.value');
const max = _.get(resp, 'aggregations.maxAgg.value');
const emptyValue = { min: min, max: min };
callback(new RangeControl(
controlParams,
new RangeFilterManager(controlParams.fieldName, indexPattern, kbnApi.queryFilter, emptyValue),
min,
max
));
});
return searchSource._createRequest(defer);
kbnApi.fetch.these([searchSource._createRequest(defer)]);

const resp = await defer.promise;

const min = _.get(resp, 'aggregations.minAgg.value');
const max = _.get(resp, 'aggregations.maxAgg.value');
const emptyValue = { min: min, max: min };
return new RangeControl(
controlParams,
new RangeFilterManager(controlParams.fieldName, indexPattern, kbnApi.queryFilter, emptyValue),
min,
max
);
}
31 changes: 16 additions & 15 deletions src/core_plugins/input_control_vis/public/vis_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class VisController {
this.vis.API.queryFilter.on('update', this.queryBarUpdateHandler);
}

render(visData, status) {
async render(visData, status) {
if (status.params) {
this.initControls();
this.controls = [];
this.controls = await this.initControls();
this.drawVis();
return;
}
return new Promise(() => {});
return;
}

resize() {}

destroy() {
this.vis.API.queryFilter.off('update', this.queryBarUpdateHandler);
unmountComponentAtNode(this.el);
Expand All @@ -42,16 +43,16 @@ class VisController {
this.el);
}

initControls() {
this.controls = [];

controlFactory(
this.vis.params.controls,
this.vis.API,
(index, control) => {
this.controls[index] = control;
this.drawVis();
}
async initControls() {
return await Promise.all(
this.vis.params.controls.filter((controlParams) => {
// ignore controls that do not have indexPattern or field
return controlParams.indexPattern && controlParams.fieldName;
})
.map((controlParams) => {
const factory = controlFactory(controlParams);
return factory(controlParams, this.vis.API);
})
);
}

Expand Down

0 comments on commit b099c90

Please sign in to comment.