/
vis.js
331 lines (291 loc) · 10.4 KB
/
vis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @name Vis
*
* @description This class consists of aggs, params, listeners, title, and type.
* - Aggs: Instances of AggConfig.
* - Params: The settings in the Options tab.
*
* Not to be confused with vislib/vis.js.
*/
import { EventEmitter } from 'events';
import _ from 'lodash';
import { VisTypesRegistryProvider } from '../registry/vis_types';
import { AggConfigs } from './agg_configs';
import { PersistedState } from '../persisted_state';
import { onBrushEvent } from '../utils/brush_event';
import { FilterBarQueryFilterProvider } from '../filter_bar/query_filter';
import { FilterBarPushFiltersProvider } from '../filter_bar/push_filters';
import { updateVisualizationConfig } from './vis_update';
import { SearchSourceProvider } from '../courier/search_source';
import { SavedObjectsClientProvider } from '../saved_objects';
import { timefilter } from 'ui/timefilter';
import { Inspector } from '../inspector';
import { RequestAdapter, DataAdapter } from '../inspector/adapters';
const getTerms = (table, columnIndex, rowIndex) => {
if (rowIndex === -1) {
return [];
}
// get only rows where cell value matches current row for all the fields before columnIndex
const rows = table.rows.filter(row => {
return table.columns.every((column, i) => {
return row[column.id] === table.rows[rowIndex][column.id] || i >= columnIndex;
});
});
const terms = rows.map(row => row[table.columns[columnIndex].id]);
return [...new Set(terms.filter(term => {
const notOther = term !== '__other__';
const notMissing = term !== '__missing__';
return notOther && notMissing;
}))];
};
export function VisProvider(Private, indexPatterns, getAppState) {
const visTypes = Private(VisTypesRegistryProvider);
const queryFilter = Private(FilterBarQueryFilterProvider);
const SearchSource = Private(SearchSourceProvider);
const savedObjectsClient = Private(SavedObjectsClientProvider);
const filterBarPushFilters = Private(FilterBarPushFiltersProvider);
class Vis extends EventEmitter {
constructor(indexPattern, visState) {
super();
visState = visState || {};
if (_.isString(visState)) {
visState = {
type: visState
};
}
this.indexPattern = indexPattern;
this._setUiState(new PersistedState());
this.setCurrentState(visState);
this.setState(this.getCurrentState(), false);
// Session state is for storing information that is transitory, and will not be saved with the visualization.
// For instance, map bounds, which depends on the view port, browser window size, etc.
this.sessionState = {};
this.API = {
savedObjectsClient: savedObjectsClient,
SearchSource: SearchSource,
indexPatterns: indexPatterns,
timeFilter: timefilter,
queryFilter: queryFilter,
events: {
// the filter method will be removed in the near feature
// you should rather use addFilter method below
filter: (event) => {
let data = event.datum.aggConfigResult;
const filters = [];
while (data.$parent) {
const { key, rawData } = data.$parent;
const { table, column, row } = rawData;
filters.push(this.API.events.createFilter(table, column, row, key));
data = data.$parent;
}
const appState = getAppState();
filterBarPushFilters(appState)(_.flatten(filters));
},
createFilter: (data, columnIndex, rowIndex, cellValue) => {
const { aggConfig, id: columnId } = data.columns[columnIndex];
let filter = [];
const value = rowIndex > -1 ? data.rows[rowIndex][columnId] : cellValue;
if (value === null || value === undefined) {
return;
}
if (aggConfig.type.name === 'terms' && aggConfig.params.otherBucket) {
const terms = getTerms(data, columnIndex, rowIndex);
filter = aggConfig.createFilter(value, { terms });
} else {
filter = aggConfig.createFilter(value);
}
return filter;
},
addFilter: (data, columnIndex, rowIndex, cellValue) => {
const filter = this.API.events.createFilter(data, columnIndex, rowIndex, cellValue);
queryFilter.addFilters(filter);
}, brush: (event) => {
onBrushEvent(event, getAppState());
}
},
inspectorAdapters: this._getActiveInspectorAdapters(),
getAppState,
};
}
/**
* Open the inspector for this visualization.
* @return {InspectorSession} the handler for the session of this inspector.
*/
openInspector() {
return Inspector.open(this.API.inspectorAdapters, {
title: this.title
});
}
hasInspector() {
return Inspector.isAvailable(this.API.inspectorAdapters);
}
/**
* Returns an object of all inspectors for this vis object.
* This must only be called after this.type has properly be initialized,
* since we need to read out data from the the vis type to check which
* inspectors are available.
*/
_getActiveInspectorAdapters() {
const adapters = {};
const { inspectorAdapters: typeAdapters } = this.type;
// Add the requests inspector adapters if the vis type explicitly requested it via
// inspectorAdapters.requests: true in its definition or if it's using the courier
// request handler, since that will automatically log its requests.
if (typeAdapters && typeAdapters.requests || this.type.requestHandler === 'courier') {
adapters.requests = new RequestAdapter();
}
// Add the data inspector adapter if the vis type requested it or if the
// vis is using courier, since we know that courier supports logging
// its data.
if (typeAdapters && typeAdapters.data || this.type.requestHandler === 'courier') {
adapters.data = new DataAdapter();
}
// Add all inspectors, that are explicitly registered with this vis type
if (typeAdapters && typeAdapters.custom) {
Object.entries(typeAdapters.custom).forEach(([key, Adapter]) => {
adapters[key] = new Adapter();
});
}
return adapters;
}
setCurrentState(state) {
this.title = state.title || '';
const type = state.type || this.type;
if (_.isString(type)) {
this.type = visTypes.byName[type];
if (!this.type) {
throw new Error(`Invalid type "${type}"`);
}
} else {
this.type = type;
}
this.params = _.defaults({},
_.cloneDeep(state.params || {}),
_.cloneDeep(this.type.visConfig.defaults || {})
);
updateVisualizationConfig(state.params, this.params);
this.aggs = new AggConfigs(this.indexPattern, state.aggs, this.type.schemas.all);
}
setState(state, updateCurrentState = true) {
this._state = _.cloneDeep(state);
if (updateCurrentState) {
this.setCurrentState(this._state);
}
}
updateState() {
this.setState(this.getCurrentState(true));
this.emit('update');
}
forceReload() {
this.emit('reload');
}
getCurrentState(includeDisabled) {
return {
title: this.title,
type: this.type.name,
params: _.cloneDeep(this.params),
aggs: this.aggs
.map(agg => agg.toJSON())
.filter(agg => includeDisabled || agg.enabled)
.filter(Boolean)
};
}
getSerializableState(state) {
return {
title: state.title,
type: state.type,
params: _.cloneDeep(state.params),
aggs: state.aggs
.map(agg => agg.toJSON())
.filter(agg => agg.enabled)
.filter(Boolean)
};
}
copyCurrentState(includeDisabled = false) {
const state = this.getCurrentState(includeDisabled);
state.aggs = new AggConfigs(this.indexPattern, state.aggs, this.type.schemas.all);
return state;
}
getStateInternal(includeDisabled) {
return {
title: this._state.title,
type: this._state.type,
params: this._state.params,
aggs: this._state.aggs
.filter(agg => includeDisabled || agg.enabled)
};
}
getEnabledState() {
return this.getStateInternal(false);
}
getAggConfig() {
return this.aggs.clone({ enabledOnly: true });
}
getState() {
return this.getStateInternal(true);
}
isHierarchical() {
if (_.isFunction(this.type.hierarchicalData)) {
return !!this.type.hierarchicalData(this);
} else {
return !!this.type.hierarchicalData;
}
}
hasSchemaAgg(schemaName, aggTypeName) {
const aggs = this.aggs.bySchemaName[schemaName] || [];
return aggs.some(function (agg) {
if (!agg.type || !agg.type.name) return false;
return agg.type.name === aggTypeName;
});
}
hasUiState() {
return !!this.__uiState;
}
/***
* this should not be used outside of visualize
* @param uiState
* @private
*/
_setUiState(uiState) {
if (uiState instanceof PersistedState) {
this.__uiState = uiState;
}
}
getUiState() {
return this.__uiState;
}
/**
* Currently this is only used to extract map-specific information
* (e.g. mapZoom, mapCenter).
*/
uiStateVal(key, val) {
if (this.hasUiState()) {
if (_.isUndefined(val)) {
return this.__uiState.get(key);
}
return this.__uiState.set(key, val);
}
return val;
}
}
Vis.prototype.type = 'histogram';
return Vis;
}