-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsym-scatterplotmatrix.js
More file actions
355 lines (294 loc) · 13.6 KB
/
sym-scatterplotmatrix.js
File metadata and controls
355 lines (294 loc) · 13.6 KB
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
//Copyright 2017, by David Golverdingen
//Licensed 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.
// This symbol depends on scatterplotMatrix.js and that file should be placed in the ext folder
(function (CS) {
// symbol definition
var definition = {
typeName: "scatterplotmatrix",
displayName: "Magion Scatterplot Matrix",
// Has to be multiple, otherwise a symbol will be created for each attribute
datasourceBehavior: CS.Extensibility.Enums.DatasourceBehaviors.Multiple,
visObjectType: symbolVis,
getDefaultConfig: function () {
// Timeseries is not usable for getting evenly spaced data for multiple sources.
// A multiple data source is still needed to be able to add extra sources but traffic is limited by selecting ModeSingleton.
return {
Title: "Magion Scatterplot Matrix",
DataShape: 'TimeSeries',
DataQueryMode: CS.Extensibility.Enums.DataQueryMode.ModeSingleton,
Height: 700,
Width: 700,
NumberOfCurrentPoints: 50,
FilterMultiplier: 1.5,
RemoveOutliers: false,
ShowBasicStatistics: false,
ShowRegression: false,
ShowPearson: false,
ShowSpearman: false
};
},
iconUrl: "/Scripts/app/editor/symbols/ext/Icons/Magion.png",
configTitle: "Format Scatterplot Matrix",
inject: ["$http", "timeProvider", "$q", "log"]
};
function symbolVis() { };
CS.deriveVisualizationFromBase(symbolVis);
// Init symbol
symbolVis.prototype.init = function (scope, elem, $http, timeProvider, $q, log) {
this.onDataUpdate = dataUpdate;
this.onConfigChange = configChanged;
this.onResize = resize;
var isNotInitialised = true;
var config = {
withCredentials: true
};
// Make the id of scatterplot div unique
var id = "scatterMatrix_" + Math.random().toString(36).substr(2, 16);
var element = document.getElementById("scatterMatrix");
element.id = id;
scope.lastMessage = "";
scope.lastPath = [];
scope.lastData = [];
// Data update
function dataUpdate(dataUpdate) {
// Check if update is detailed data or only values
if (dataUpdate && dataUpdate.Data.length > 0 && dataUpdate.Data[0].Label !== undefined) {
// Get urls
var urls = createPIWebAPIurls(dataUpdate.Data);
// Get direct value links
getPIWebAPIlinks(urls).then(function (dataLinksArray) {
scope.symbol.dataLinksArray = dataLinksArray;
// Get data from links
getDataFromPIWebAPILinks(dataLinksArray).then(function (data) {
scope.lastData = parseAndCheckDataForScatterplot(data);
if (scope.lastData.plotData.length >= 4) {
createScatterMatrix(scope.lastData, id, scope.config);
}
else {
scope.ErrorMessage = "Dataset to small";
logError();
}
},handleError)
isNotInitialised = false;
},handleError);
}
// Only values, just used old data
else {
if (!isNotInitialised) {
// Get data from links
getDataFromPIWebAPILinks(scope.symbol.dataLinksArray).then(function (data) {
scope.lastData = parseAndCheckDataForScatterplot(data);
if (scope.lastData.plotData.length >= 4) {
createScatterMatrix(scope.lastData, id, scope.config);
}
else {
scope.ErrorMessage = "Dataset to small";
logError();
}
},handleError)
}
}
};
var isChanged = false;
// Config changed, will also be trigger on first initialization of the symbol
function configChanged(newConfig, oldConfig) {
if (!newConfig || !oldConfig || angular.equals(newConfig, oldConfig)) {
return;
}
// Check interval changed and new data needed
if (newConfig.NumberOfCurrentPoints !== oldConfig.NumberOfCurrentPoints) {
if (!isChanged && !isNotInitialised) {
// Debounce lots of config changes
isChanged = true;
setTimeout(function () {
// Get data from links
getDataFromPIWebAPILinks(scope.symbol.dataLinksArray).then(function (data) {
scope.lastData = parseAndCheckDataForScatterplot(data);
if (scope.lastData.plotData.length >= 4) {
createScatterMatrix(scope.lastData, id, scope.config);
isChanged = false;
}
else {
scope.ErrorMessage = "Dataset to small";
logError();
}
},handleError)
},500);
}
}
else {
if (!isNotInitialised) {
createScatterMatrix(scope.lastData, id, scope.config);
}
}
};
// Create PIWebAPI url for PI or AF paths
function createPIWebAPIurls(dataArray) {
var urls = [];
var datasourceArray = scope.symbol.DataSources
scope.lastPath = [];
for (var i = 0; i < dataArray.length; i++) {
// Check for pi or af point
var url = "";
if (datasourceArray[i].indexOf("pi:") == 0) {
url = CS.ClientSettings.PIWebAPIUrl + "\\points?path=" + dataArray[i].Path;
}
else {
url = CS.ClientSettings.PIWebAPIUrl + "\\attributes?path=" + dataArray[i].Path;
}
urls.push(url);
scope.lastPath.push(dataArray[i].Path);
}
return urls;
};
// Resolve every url and get the direct PIWebAPI data links
function getPIWebAPIlinks(urls) {
var deferred = $q.defer();
if (urls.length > 0) {
// Create data promises
var dataPromises = [];
urls.forEach(function (url) {
dataPromises.push($http.get(url, config));
});
// Resolve data promises
$q.all(dataPromises).then(function (dataPoints) {
var dataLinksArray = [];
// Create value promises
dataPoints.forEach(function (point, index) {
dataLinksArray.push({
name: point.data.Name,
path: scope.lastPath[index],
valueLink: point.data.Links.Value,
interpolatedLink: point.data.Links.InterpolatedData
});
});
deferred.resolve(dataLinksArray);
}, handleError);
}
else {
deferred.reject();
}
return deferred.promise;
};
// Get data from links
function getDataFromPIWebAPILinks(dataLinksArray) {
var deferred = $q.defer();
if (dataLinksArray.length > 0) {
// The current time interval is divided by the number of points to show to give the time interval of the interpolated data
// Use the server side dates to get the interval
var start = new Date(timeProvider.getServerStartTime());
var stop = new Date(timeProvider.getServerEndTime());
var duration = (stop.getTime() - start.getTime()) / 1000; // seconds
var interval = duration;
if (+scope.config.NumberOfCurrentPoints !== 1) {
interval = duration / (+scope.config.NumberOfCurrentPoints - 1);
}
// Query with the coresight start and end date. In "now" modus this will use * for the end date.
// This will result in a broken interpolation on now, resulting in a "no data" object.
var isRealtime = timeProvider.getDisplayEndTime() === "*";
var queryInterpolated = "?startTime=" + timeProvider.getServerStartTime() + "&endTime=" + timeProvider.getServerEndTime() + "&interval=" + (interval) + "s";
var dataResult = [];
var valuePromises = [];
var interpolatedPromises = [];
// Create value promises
dataLinksArray.forEach(function (dataLinks) {
dataResult.push({ name: dataLinks.name, path: dataLinks.path, values: [] });
valuePromises.push($http.get(dataLinks.valueLink, config));
interpolatedPromises.push($http.get(dataLinks.interpolatedLink + queryInterpolated, config));
});
// Resolve interpolated promises
$q.all(interpolatedPromises).then(function (interpolatedData) {
// Resolve value promises
$q.all(valuePromises).then(function (valueData) {
// Replace no data with last value
for (var i = 0; i < interpolatedData.length; i++) {
var copy = angular.copy(interpolatedData[i].data.Items)
if (isRealtime) {
copy[copy.length - 1] = angular.copy(valueData[i].data);
}
dataResult[i].values = copy;
}
deferred.resolve(dataResult);
}, handleError);
}, handleError);
}
else {
deferred.reject();
}
return deferred.promise;
};
// Parse data for scatterplot
function parseAndCheckDataForScatterplot(data) {
dataSet = {
plotData: [],
plotNames: [],
plotPaths: []
};
valuesSet = [];
data.forEach(function (set) {
dataSet.plotNames.push(set.name);
dataSet.plotPaths.push(set.path);
valuesSet.push(set.values);
});
// Create value pairs and add to collection if values are ok
for (var i = 0; i < valuesSet[0].length; i++) {
var valueCollection = []
valuesSet.forEach(function (value) {
valueCollection.push(value[i].Value);
})
var valuesOk = true;
valueCollection.forEach(function (value) {
if (isNaN(value)) {
valuesOk = false;
}
});
if (valuesOk) {
dataSet.plotData.push(valueCollection);
}
}
return dataSet;
};
// Handle errors
function handleError(error) {
scope.ErrorMessage = "";
if (error.data !== undefined) {
if (error.data.Errors !== undefined) {
error.data.Errors.forEach(function (e) {
scope.ErrorMessage += e;
});
scope.Error = true;
logError();
}
}
};
// Log error message
function logError() {
if (scope.ErrorMessage !== scope.lastMessage) {
scope.lastMessage = scope.ErrorMessage;
log.add(scope.config.Title, log.Severity.Error, scope.ErrorMessage);
}
}
var isResized = false;
// Resize function
function resize(width, height) {
if (!isResized) {
isResized = true;
setTimeout(function () {
isResized = false;
}, 1000);
}
// this triggers a config change
scope.config.Width = width;
scope.config.Height = height;
};
};
CS.symbolCatalog.register(definition);
})(window.PIVisualization || window.Coresight);