forked from Azure/ibex-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSourceConnector.ts
399 lines (319 loc) · 13 KB
/
DataSourceConnector.ts
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import alt from '../alt';
import * as _ from 'lodash';
import { IDataSourcePlugin } from './plugins/DataSourcePlugin';
import DialogsActions from '../components/generic/Dialogs/DialogsActions';
import datasourcePluginsMappings from './plugins/PluginsMapping';
import VisibilityActions from '../actions/VisibilityActions';
import VisibilityStore from '../stores/VisibilityStore';
import * as formats from '../utils/data-formats';
const DataFormatTypes = formats.DataFormatTypes;
export interface IDataSource {
id: string;
config: any;
plugin: IDataSourcePlugin;
action: any;
store: any;
initialized: boolean;
}
export interface IDataSourceDictionary {
[key: string]: IDataSource;
}
export interface IExtrapolationResult {
dataSources: { [key: string]: IDataSource };
dependencies: { [key: string]: any };
}
export class DataSourceConnector {
private static dataSources: IDataSourceDictionary = {};
static createDataSource(dataSourceConfig: any, connections: IConnections) {
var config = dataSourceConfig || {};
if (!config.id || !config.type) {
throw new Error('Data source configuration must contain id and type');
}
// Dynamically load the plugin from the plugins directory
var path = datasourcePluginsMappings[config.type];
var PluginClass = require('./plugins/' + path);
var plugin: any = new PluginClass.default(config, connections);
// Creating actions class
var ActionClass = DataSourceConnector.createActionClass(plugin);
// Creating store class
var StoreClass = DataSourceConnector.createStoreClass(config, plugin, ActionClass);
DataSourceConnector.dataSources[config.id] = {
id: config.id,
config,
plugin,
action: ActionClass,
store: StoreClass,
initialized: false
};
return DataSourceConnector.dataSources[config.id];
}
static createDataSources(dsContainer: IDataSourceContainer, connections: IConnections) {
dsContainer.dataSources.forEach(source => {
var dataSource = DataSourceConnector.createDataSource(source, connections);
DataSourceConnector.connectDataSource(dataSource);
});
DataSourceConnector.initializeDataSources();
}
static refreshDs() {
let topLevelDataSources = _.filter(DataSourceConnector.dataSources, ds => !ds.config.dependencies);
topLevelDataSources.forEach(dataSource => {
dataSource.action.refresh.defer();
});
}
static initializeDataSources() {
// Call initialize methods
Object.keys(this.dataSources).forEach(sourceDSId => {
var sourceDS = this.dataSources[sourceDSId];
if (sourceDS.initialized) { return; }
if (typeof sourceDS.action['initialize'] === 'function') {
sourceDS.action.initialize.defer();
}
sourceDS.initialized = true;
});
}
static extrapolateDependencies(dependencies: IStringDictionary, args?: IDictionary): IExtrapolationResult {
var result: IExtrapolationResult = {
dataSources: {},
dependencies: {}
};
Object.keys(dependencies || {}).forEach(key => {
// Find relevant store
let dependency = dependencies[key] || '';
// Checking if this is a constant value
if (dependency.startsWith('::')) {
result.dependencies[key] = dependency.substr(2);
return;
}
// Checking if this is a config value
if (dependency.startsWith('connection:')) {
const connection = dependency.substr(dependency.indexOf(':') + 1);
if (Object.keys(DataSourceConnector.dataSources).length < 1) {
throw new Error('Connection error, couldn\'t find any data sources.');
}
// Selects first data source to get connections
const dataSource: IDataSource = DataSourceConnector.dataSources[
Object.keys(DataSourceConnector.dataSources)[0]];
if (!dataSource || !dataSource.plugin.hasOwnProperty('connections')) {
throw new Error('Tried to resolve connections reference path, but couldn\'t find any connections.');
}
const connections = dataSource.plugin['connections'];
const path = connection.split('.');
if (path.length !== 2) {
throw new Error('Expected connection reference dot path consisting of 2 components.');
}
if (!connections.hasOwnProperty(path[0]) || !connections[path[0]].hasOwnProperty(path[1])) {
throw new Error('Unable to resolve connection reference path:' + connection);
}
result.dependencies[key] = connections[path[0]][path[1]];
return;
}
let dependsUpon = dependency.split(':');
let dataSourceName = dependsUpon[0];
if (dataSourceName === 'args' && args) {
if (dependsUpon.length < 2) {
throw new Error('When padding arguments, you need to provide a specific argument name');
}
let valueName = dependsUpon[1];
result.dependencies[key] = args[valueName];
} else {
let dataSource = DataSourceConnector.dataSources[dataSourceName];
if (!dataSource) {
throw new Error(`Could not find data source for dependency ${dependency}.
If your want to use a constant value, write "value:some value"`);
}
let valueName = dependsUpon.length > 1 ? dependsUpon[1] : dataSource.plugin.defaultProperty;
var state = dataSource.store.getState();
result.dependencies[key] = state[valueName];
result.dataSources[dataSource.id] = dataSource;
}
});
// Checking to see if any of the dependencies control visibility
let visibilityFlags = {};
let updateVisibility = false;
Object.keys(result.dependencies).forEach(key => {
if (key === 'visible') {
visibilityFlags[dependencies[key]] = result.dependencies[key];
updateVisibility = true;
}
});
if (updateVisibility) {
(VisibilityActions.setFlags as any).defer(visibilityFlags);
}
return result;
}
static triggerAction(action: string, params: IStringDictionary, args: IDictionary) {
var actionLocation = action.split(':');
if (actionLocation.length !== 2 && actionLocation.length !== 3) {
throw new Error(`Action triggers should be in format of "dataSource:action", this is not met by ${action}`);
}
var dataSourceName = actionLocation[0];
var actionName = actionLocation[1];
var selectedValuesProperty = 'selectedValues';
if (actionLocation.length === 3) {
selectedValuesProperty = actionLocation[2];
args = { [selectedValuesProperty]: args };
}
if (dataSourceName === 'dialog') {
var extrapolation = DataSourceConnector.extrapolateDependencies(params, args);
DialogsActions.openDialog(actionName, extrapolation.dependencies);
} else {
var dataSource = DataSourceConnector.dataSources[dataSourceName];
if (!dataSource) {
throw new Error(`Data source ${dataSourceName} was not found`);
}
dataSource.action[actionName].call(dataSource.action, args);
}
}
static getDataSources(): IDataSourceDictionary {
return this.dataSources;
}
static getDataSource(name: string): IDataSource {
return this.dataSources[name];
}
static handleDataFormat(
format: string | formats.IDataFormat,
plugin: IDataSourcePlugin,
state: any,
dependencies: IDictionary) {
if (!format) { return null; }
const prevState = DataSourceConnector.dataSources[plugin._props.id].store.getState();
let result = {};
let formatName = (typeof format === 'string' ? format : format.type) || DataFormatTypes.none.toString();
if (formatName && typeof formats[formatName] === 'function') {
let additionalValues = formats[formatName](format, state, dependencies, plugin, prevState) || {};
Object.assign(result, additionalValues);
}
return result;
}
private static connectDataSource(sourceDS: IDataSource) {
// Connect sources and dependencies
sourceDS.store.listen((state) => {
Object.keys(this.dataSources).forEach(checkDSId => {
let checkDS = this.dataSources[checkDSId];
let dependencies = checkDS.plugin.getDependencies() || {};
let populatedDependencies = {};
let connected = _.find(_.keys(dependencies), dependencyKey => {
let dependencyValue = dependencies[dependencyKey] || '';
if (typeof dependencyValue === 'string' && dependencyValue.length > 0) {
if (dependencyValue === sourceDS.id) {
let defaultProperty = sourceDS.plugin.defaultProperty || 'value';
populatedDependencies[dependencyKey] = state[defaultProperty];
return true;
} else if (dependencyValue.startsWith(sourceDS.id + ':')) {
let property = dependencyValue.substr(sourceDS.id.length + 1);
populatedDependencies[dependencyKey] = _.get(state, property);
return true;
}
}
return false;
});
if (connected) {
// Todo: add check that all dependencies are met
checkDS.action.updateDependencies.defer(populatedDependencies);
}
});
// Checking visibility flags
let visibilityState = VisibilityStore.getState() || {};
let flags = visibilityState.flags || {};
let updatedFlags = {};
let shouldUpdate = false;
Object.keys(flags).forEach(visibilityKey => {
let keyParts = visibilityKey.split(':');
if (keyParts[0] === sourceDS.id) {
updatedFlags[visibilityKey] = sourceDS.store.getState()[keyParts[1]];
shouldUpdate = true;
}
});
if (shouldUpdate) {
(VisibilityActions.setFlags as any).defer(updatedFlags);
}
});
}
private static createActionClass(plugin: IDataSourcePlugin): any {
class NewActionClass {
constructor() { }
}
plugin.getActions().forEach(action => {
if (typeof plugin[action] === 'function') {
// This method will be called with an action is dispatched
NewActionClass.prototype[action] = function (...args: Array<any>) {
// Collecting depedencies from all relevant stores
var extrapolation;
if (args.length === 1) {
extrapolation = DataSourceConnector.extrapolateDependencies(plugin.getDependencies(), args[0]);
} else {
extrapolation = DataSourceConnector.extrapolateDependencies(plugin.getDependencies());
}
// Calling action with arguments
let result = plugin[action].call(this, extrapolation.dependencies, ...args) || {};
// Checking is result is a dispatcher or a direct value
if (typeof result === 'function') {
return (dispatch) => {
result(function (obj: any) {
obj = obj || {};
let fullResult = DataSourceConnector.callibrateResult(obj, plugin, extrapolation.dependencies);
dispatch(fullResult);
});
};
} else {
let fullResult = DataSourceConnector.callibrateResult(result, plugin, extrapolation.dependencies);
return fullResult;
}
};
} else {
// Adding generic actions that are directly proxied to the store
alt.addActions(action, <any> NewActionClass);
}
});
// Binding the class to Alt and the plugin
var ActionClass = alt.createActions(<any> NewActionClass);
plugin.bind(ActionClass);
return ActionClass;
}
private static createStoreClass(config: any, plugin: any, ActionClass: any): any {
var bindings = [];
plugin.getActions().forEach(action => {
bindings.push(ActionClass[action]);
});
class NewStoreClass {
constructor() {
(<any> this).bindListeners({ updateState: bindings });
}
updateState(newData: any) {
(<any> this).setState(newData);
}
}
var StoreClass = alt.createStore(NewStoreClass as any, config.id + '-Store');
return StoreClass;
}
private static callibrateResult(result: any, plugin: IDataSourcePlugin, dependencies: IDictionary): any {
let defaultProperty = plugin.defaultProperty || 'value';
// In case result is not an object, push result into an object
if (typeof result !== 'object') {
var resultObj = {};
resultObj[defaultProperty] = result;
result = resultObj;
}
// Callibrate calculated values
const calculated = plugin._props.calculated;
let state = DataSourceConnector.dataSources[plugin._props.id].store.getState();
state = _.extend(state, result);
if (typeof calculated === 'function') {
let additionalValues = calculated(state, dependencies) || {};
Object.assign(result, additionalValues);
}
if (Array.isArray(calculated)) {
calculated.forEach(calc => {
let additionalValues = calc(state, dependencies) || {};
Object.assign(result, additionalValues);
});
}
state = _.extend(state, result);
let format = plugin.getFormat();
let formatExtract = DataSourceConnector.handleDataFormat(format, plugin, state, dependencies);
if (formatExtract) {
Object.assign(result, formatExtract);
}
return result;
}
}