@hpcc-js/other 3.4.7 → 3.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ESP.ts CHANGED
@@ -1,451 +1,451 @@
1
- import { Utility } from "@hpcc-js/common";
2
- import * as Comms from "./Comms.ts";
3
-
4
- function nestedRowFix(row) {
5
- if (row.Row && row.Row instanceof Array) {
6
- return row.Row.map(nestedRowFix);
7
- } else if (row instanceof Object) {
8
- for (const key in row) {
9
- row[key] = nestedRowFix(row[key]);
10
- }
11
- }
12
- return row;
13
- }
14
-
15
- // Basic Comms ---
16
- let enableBasicCommsCache = false;
17
- let basicCommsCache = {};
18
- function BasicComms() {
19
- Comms.Basic.call(this);
20
- }
21
- BasicComms.prototype = Object.create(Comms.Basic.prototype);
22
-
23
- BasicComms.prototype.jsonp = function (url, request) {
24
- const requestStr = JSON.stringify(request);
25
- if (enableBasicCommsCache && basicCommsCache[url] && basicCommsCache[url][requestStr]) {
26
- return Promise.resolve(basicCommsCache[url][requestStr]);
27
- }
28
- return Comms.Basic.prototype.jsonp.apply(this, arguments).then(function (response) {
29
- if (enableBasicCommsCache) {
30
- if (!basicCommsCache[url]) {
31
- basicCommsCache[url] = {};
32
- }
33
- basicCommsCache[url][requestStr] = response;
34
- }
35
- return response;
36
- });
37
- };
38
-
39
- // WsWorkunits ---
40
- function WsWorkunits(baseUrl) {
41
- BasicComms.call(this);
42
-
43
- this.url(baseUrl + "WsWorkunits/");
44
- }
45
- WsWorkunits.prototype = Object.create(BasicComms.prototype);
46
-
47
- WsWorkunits.prototype.wuQuery = function (options) {
48
- const url = this.getUrl({
49
- pathname: "WsWorkunits/WUQuery.json"
50
- });
51
- const request = {
52
- Wuid: "",
53
- Type: "",
54
- Cluster: "",
55
- RoxieCluster: "",
56
- Owner: "",
57
- State: "",
58
- StartDate: "",
59
- EndDate: "",
60
- ECL: "",
61
- Jobname: "",
62
- LogicalFile: "",
63
- LogicalFileSearchType: "",
64
- /*
65
- ApplicationValues>
66
- ApplicationValue>
67
- Application: "",
68
- Name: "",
69
- Value: "",
70
- /ApplicationValue>
71
- /ApplicationValues>
72
- */
73
- After: "",
74
- Before: "",
75
- Count: "",
76
- PageSize: 100,
77
- PageStartFrom: 0,
78
- PageEndAt: "",
79
- LastNDays: "",
80
- Sortby: "",
81
- Descending: 0,
82
- CacheHint: ""
83
- };
84
- for (const key in options) {
85
- request[key] = options[key];
86
- }
87
- return this.jsonp(url, request).then(function (response) {
88
- if (response.WUQueryResponse && response.WUQueryResponse.Workunits) {
89
- return response.WUQueryResponse.Workunits.ECLWorkunit;
90
- }
91
- return [];
92
- });
93
- };
94
-
95
- // Workunit ---
96
- function Workunit(baseUrl, wuid) {
97
- BasicComms.call(this);
98
-
99
- this.url(baseUrl + "WsWorkunits/");
100
- this._wuid = wuid;
101
- }
102
- Workunit.prototype = Object.create(BasicComms.prototype);
103
-
104
- Workunit.prototype.wuInfo = function (options) {
105
- const url = this.getUrl({
106
- pathname: "WsWorkunits/WUInfo.json"
107
- });
108
- const request = {
109
- Wuid: this._wuid,
110
- TruncateEclTo64k: true,
111
- IncludeExceptions: false,
112
- IncludeGraphs: false,
113
- IncludeSourceFiles: false,
114
- IncludeResults: false,
115
- IncludeResultsViewNames: false,
116
- IncludeVariables: false,
117
- IncludeTimers: false,
118
- IncludeResourceURLs: false,
119
- IncludeDebugValues: false,
120
- IncludeApplicationValues: false,
121
- IncludeWorkflows: false,
122
- IncludeXmlSchemas: false,
123
- SuppressResultSchemas: true
124
- };
125
- for (const key in options) {
126
- request[key] = options[key];
127
- }
128
- return this.jsonp(url, request).then(function (response) {
129
- if (enableBasicCommsCache) {
130
- const retVal = { WUInfoResponse: { Workunit: {} } };
131
- for (const key in options) {
132
- const includeKey = key.substring(7);
133
- retVal.WUInfoResponse.Workunit[includeKey] = response.WUInfoResponse.Workunit[includeKey];
134
- }
135
- basicCommsCache[url][JSON.stringify(request)] = retVal;
136
- }
137
- return response;
138
- });
139
- };
140
-
141
- Workunit.prototype.wuUpdate = function (options) {
142
- const url = this.getUrl({
143
- pathname: "WsWorkunits/WUUpdate.json"
144
- });
145
- const request = {
146
- Wuid: this._wuid
147
- };
148
- for (const key in options) {
149
- request[key] = options[key];
150
- }
151
- return this.post(url, request);
152
- };
153
-
154
- Workunit.prototype.appData = function (appID, key, _) {
155
- if (arguments.length === 2) {
156
- return this.wuInfo({
157
- IncludeApplicationValues: true
158
- }).then(function (response) {
159
- let persistString;
160
- if (response.WUInfoResponse && response.WUInfoResponse.Workunit && response.WUInfoResponse.Workunit.ApplicationValues && response.WUInfoResponse.Workunit.ApplicationValues.ApplicationValue) {
161
- response.WUInfoResponse.Workunit.ApplicationValues.ApplicationValue.filter(function (row) {
162
- return row.Application === appID && row.Name === key;
163
- }).forEach(function (row) {
164
- persistString = row.Value;
165
- });
166
- }
167
- return persistString;
168
- });
169
- } else if (arguments.length === 3) {
170
- return this.wuUpdate({
171
- "ApplicationValues.ApplicationValue.0.Application": appID,
172
- "ApplicationValues.ApplicationValue.0.Name": key,
173
- "ApplicationValues.ApplicationValue.0.Value": _,
174
- "ApplicationValues.ApplicationValue.itemcount": 1
175
- });
176
- }
177
- };
178
-
179
- Workunit.prototype.results = function () {
180
- const context = this;
181
- return this.wuInfo({
182
- IncludeResults: true
183
- }).then(function (response) {
184
- let retVal = [];
185
- if (Utility.exists("WUInfoResponse.Workunit.Results.ECLResult", response)) {
186
- retVal = response.WUInfoResponse.Workunit.Results.ECLResult.map(function (result) {
187
- return new WUResult(context.getUrl({ pathname: "WsWorkunits/" }), context._wuid, result.Name);
188
- });
189
- }
190
- return retVal;
191
- });
192
- };
193
-
194
- Workunit.prototype.result = function (dataSource, resultName) {
195
- dataSource = dataSource || this._wuid;
196
- return createResult(dataSource, resultName);
197
- };
198
-
199
- // Workunit Result ---
200
- function WUResult(baseUrl, wuid, name) {
201
- BasicComms.call(this);
202
- this.url(baseUrl + "WUResult.json");
203
- this._wuid = wuid;
204
- this._name = name;
205
- this._xmlSchema = null;
206
- }
207
- WUResult.prototype = Object.create(BasicComms.prototype);
208
-
209
- WUResult.prototype.wuid = function (_) {
210
- if (!arguments.length) return this._wuid;
211
- this._wuid = _;
212
- return this;
213
- };
214
-
215
- WUResult.prototype.name = function (_) {
216
- if (!arguments.length) return this._name;
217
- this._name = _;
218
- return this;
219
- };
220
-
221
- WUResult.prototype.query = function (options, filter) {
222
- options = options || {};
223
- filter = filter || {};
224
- const request = {
225
- Wuid: this._wuid,
226
- ResultName: this._name,
227
- SuppressXmlSchema: true,
228
- Start: 0,
229
- Count: -1
230
- };
231
- for (const key in options) {
232
- request[key] = options[key];
233
- }
234
- let filterIdx = 0;
235
- for (const fKey in filter) {
236
- request["FilterBy.NamedValue." + filterIdx + ".Name"] = fKey;
237
- request["FilterBy.NamedValue." + filterIdx + ".Value"] = filter[fKey];
238
- ++filterIdx;
239
- }
240
- if (filterIdx) {
241
- request["FilterBy.NamedValue.itemcount"] = filterIdx;
242
- }
243
- const context = this;
244
- return this.jsonp(this.url(), request).then(function (response) {
245
- if (response.WUResultResponse &&
246
- response.WUResultResponse.Result &&
247
- response.WUResultResponse.Result[context._name]) {
248
- if (enableBasicCommsCache) {
249
- basicCommsCache[context.url()][JSON.stringify(request)] = {
250
- WUResultResponse: {
251
- Result: response.WUResultResponse.Result
252
- }
253
- };
254
- }
255
- context._xmlSchema = response.WUResultResponse.Result.XmlSchema;
256
- return nestedRowFix(response.WUResultResponse.Result[context._name]);
257
- }
258
- return [];
259
- });
260
- };
261
-
262
- // Logical File ---
263
- function LogicalFile(baseUrl, logicalName) {
264
- BasicComms.call(this);
265
- this.url(baseUrl + "WUResult.json");
266
- this._logicalName = logicalName;
267
- this._xmlSchema = null;
268
- }
269
- LogicalFile.prototype = Object.create(BasicComms.prototype);
270
-
271
- LogicalFile.prototype.query = function (options, filter) {
272
- options = options || {};
273
- filter = filter || {};
274
- const request = {
275
- Cluster: "hthor", // TODO: Should not be needed ---
276
- LogicalName: this._logicalName,
277
- SuppressXmlSchema: this._xmlSchema !== null,
278
- Start: 0,
279
- Count: -1
280
- };
281
- for (const key in options) {
282
- request[key] = options[key];
283
- }
284
- let filterIdx = 0;
285
- for (const fKey in filter) {
286
- request["FilterBy.NamedValue." + filterIdx + ".Name"] = fKey;
287
- request["FilterBy.NamedValue." + filterIdx + ".Value"] = filter[fKey];
288
- ++filterIdx;
289
- }
290
- if (filterIdx) {
291
- request["FilterBy.NamedValue.itemcount"] = filterIdx;
292
- }
293
- const context = this;
294
- return this.jsonp(this.url(), request).then(function (response) {
295
- if (response.WUResultResponse &&
296
- response.WUResultResponse.Result &&
297
- response.WUResultResponse.Result.Row) {
298
- context._xmlSchema = response.WUResultResponse.Result.XmlSchema;
299
- return nestedRowFix(response.WUResultResponse.Result.Row);
300
- }
301
- return [];
302
- });
303
- };
304
-
305
- // Roxie Query ---
306
- function RoxieQuery(baseUrl, resultName) {
307
- BasicComms.call(this);
308
- const urlParts = baseUrl.split("/");
309
- let queryName = urlParts.pop();
310
- if (queryName.toLowerCase() === "json") {
311
- queryName = urlParts.pop();
312
- }
313
- this._queryName = queryName;
314
- this._resultName = resultName;
315
- this.url(urlParts.join("/") + "/" + queryName + "/json");
316
- }
317
- RoxieQuery.prototype = Object.create(BasicComms.prototype);
318
-
319
- function trimRight(str) {
320
- if (str && str.replace) {
321
- return str.replace(/ +$/, "");
322
- }
323
- return str;
324
- }
325
-
326
- function postFilter(results, filter) {
327
- return results.filter(function (row) {
328
- for (const key in filter) {
329
- if (row[key] !== undefined && trimRight(filter[key]) !== trimRight(row[key])) {
330
- return false;
331
- }
332
- }
333
- return true;
334
- });
335
- }
336
-
337
- function locateRoxieResponse(response) {
338
- // v5 and v6 compatible ---
339
- for (const key in response) {
340
- if (response[key].Row && response[key].Row instanceof Array) {
341
- return response;
342
- }
343
- const retVal = locateRoxieResponse(response[key]);
344
- if (retVal) {
345
- return retVal;
346
- }
347
- }
348
- return null;
349
- }
350
-
351
- RoxieQuery.prototype.query = function (options, filter) {
352
- options = options || {};
353
- filter = filter || {};
354
- const request = {
355
- };
356
- for (const key in options) {
357
- request[key] = options[key];
358
- }
359
- for (const fKey in filter) {
360
- request[fKey] = filter[fKey];
361
- }
362
- const context = this;
363
- return this.jsonp(this.url(), request).then(function (response) {
364
- response = locateRoxieResponse(response);
365
- if (response) {
366
- if (context._resultName) {
367
- if (response && response[context._resultName] && response[context._resultName].Row) {
368
- return nestedRowFix(postFilter(response[context._resultName].Row, filter));
369
- }
370
- } else {
371
- for (const key in response) {
372
- if (response[key].Row) {
373
- return nestedRowFix(postFilter(response[key].Row, filter));
374
- }
375
- }
376
- }
377
- }
378
- return [];
379
- });
380
- };
381
-
382
- export function createResult(_espUrl, dataSource, resultName?) {
383
- const espUrl = new Comms.ESPUrl()
384
- .url(_espUrl)
385
- ;
386
- if (dataSource.indexOf("http") === 0) {
387
- return new RoxieQuery(dataSource, resultName);
388
- } else if (dataSource.indexOf("~") === 0 || dataSource.indexOf("::") >= 0) {
389
- return new LogicalFile(espUrl.getUrl({ pathname: "WsWorkunits/" }), dataSource);
390
- } else if (dataSource) {
391
- return new WUResult(espUrl.getUrl({ pathname: "WsWorkunits/" }), dataSource, resultName);
392
- }
393
- return null;
394
- }
395
-
396
- export function enableCache(_?: any): any | undefined {
397
- if (!arguments.length) return enableBasicCommsCache;
398
- enableBasicCommsCache = _;
399
- if (!_) {
400
- basicCommsCache = {};
401
- }
402
- }
403
- export function cache(_?: any): any | undefined {
404
- if (!arguments.length) return basicCommsCache;
405
- basicCommsCache = _;
406
- }
407
- export function createConnection(url) {
408
- url = url || document.URL;
409
- const testURL = new Comms.ESPUrl()
410
- .url(url)
411
- ;
412
- if (testURL.isWsWorkunits()) {
413
- const espConnection = Comms.createESPConnection(url);
414
- if (espConnection instanceof Comms.WsWorkunits && espConnection.wuid()) {
415
- return new Workunit(espConnection.getUrl({ pathname: "" }), espConnection.wuid())
416
- .url(url)
417
- ;
418
- }
419
- }
420
- return null;
421
- }
422
- export function flattenResult(result, mappings) {
423
- const retVal = {
424
- columns: [],
425
- data: []
426
- };
427
- if (result && result.length) {
428
- const colIdx = {};
429
- if (mappings && mappings.length) {
430
- mappings.forEach(function (mapping) {
431
- colIdx[mapping.value.toLowerCase()] = retVal.columns.length;
432
- retVal.columns.push(mapping.key);
433
- });
434
- } else {
435
- for (const key in result[0]) {
436
- colIdx[key.toLowerCase()] = retVal.columns.length;
437
- retVal.columns.push(key);
438
- }
439
- }
440
- result.forEach(function (row, rowIdx) {
441
- const rowArr = [];
442
- for (const key in row) {
443
- if (colIdx[key.toLowerCase()] !== undefined) {
444
- rowArr[colIdx[key.toLowerCase()]] = row[key];
445
- }
446
- }
447
- retVal.data.push(rowArr);
448
- });
449
- }
450
- return retVal;
451
- }
1
+ import { Utility } from "@hpcc-js/common";
2
+ import * as Comms from "./Comms.ts";
3
+
4
+ function nestedRowFix(row) {
5
+ if (row.Row && row.Row instanceof Array) {
6
+ return row.Row.map(nestedRowFix);
7
+ } else if (row instanceof Object) {
8
+ for (const key in row) {
9
+ row[key] = nestedRowFix(row[key]);
10
+ }
11
+ }
12
+ return row;
13
+ }
14
+
15
+ // Basic Comms ---
16
+ let enableBasicCommsCache = false;
17
+ let basicCommsCache = {};
18
+ function BasicComms() {
19
+ Comms.Basic.call(this);
20
+ }
21
+ BasicComms.prototype = Object.create(Comms.Basic.prototype);
22
+
23
+ BasicComms.prototype.jsonp = function (url, request) {
24
+ const requestStr = JSON.stringify(request);
25
+ if (enableBasicCommsCache && basicCommsCache[url] && basicCommsCache[url][requestStr]) {
26
+ return Promise.resolve(basicCommsCache[url][requestStr]);
27
+ }
28
+ return Comms.Basic.prototype.jsonp.apply(this, arguments).then(function (response) {
29
+ if (enableBasicCommsCache) {
30
+ if (!basicCommsCache[url]) {
31
+ basicCommsCache[url] = {};
32
+ }
33
+ basicCommsCache[url][requestStr] = response;
34
+ }
35
+ return response;
36
+ });
37
+ };
38
+
39
+ // WsWorkunits ---
40
+ function WsWorkunits(baseUrl) {
41
+ BasicComms.call(this);
42
+
43
+ this.url(baseUrl + "WsWorkunits/");
44
+ }
45
+ WsWorkunits.prototype = Object.create(BasicComms.prototype);
46
+
47
+ WsWorkunits.prototype.wuQuery = function (options) {
48
+ const url = this.getUrl({
49
+ pathname: "WsWorkunits/WUQuery.json"
50
+ });
51
+ const request = {
52
+ Wuid: "",
53
+ Type: "",
54
+ Cluster: "",
55
+ RoxieCluster: "",
56
+ Owner: "",
57
+ State: "",
58
+ StartDate: "",
59
+ EndDate: "",
60
+ ECL: "",
61
+ Jobname: "",
62
+ LogicalFile: "",
63
+ LogicalFileSearchType: "",
64
+ /*
65
+ ApplicationValues>
66
+ ApplicationValue>
67
+ Application: "",
68
+ Name: "",
69
+ Value: "",
70
+ /ApplicationValue>
71
+ /ApplicationValues>
72
+ */
73
+ After: "",
74
+ Before: "",
75
+ Count: "",
76
+ PageSize: 100,
77
+ PageStartFrom: 0,
78
+ PageEndAt: "",
79
+ LastNDays: "",
80
+ Sortby: "",
81
+ Descending: 0,
82
+ CacheHint: ""
83
+ };
84
+ for (const key in options) {
85
+ request[key] = options[key];
86
+ }
87
+ return this.jsonp(url, request).then(function (response) {
88
+ if (response.WUQueryResponse && response.WUQueryResponse.Workunits) {
89
+ return response.WUQueryResponse.Workunits.ECLWorkunit;
90
+ }
91
+ return [];
92
+ });
93
+ };
94
+
95
+ // Workunit ---
96
+ function Workunit(baseUrl, wuid) {
97
+ BasicComms.call(this);
98
+
99
+ this.url(baseUrl + "WsWorkunits/");
100
+ this._wuid = wuid;
101
+ }
102
+ Workunit.prototype = Object.create(BasicComms.prototype);
103
+
104
+ Workunit.prototype.wuInfo = function (options) {
105
+ const url = this.getUrl({
106
+ pathname: "WsWorkunits/WUInfo.json"
107
+ });
108
+ const request = {
109
+ Wuid: this._wuid,
110
+ TruncateEclTo64k: true,
111
+ IncludeExceptions: false,
112
+ IncludeGraphs: false,
113
+ IncludeSourceFiles: false,
114
+ IncludeResults: false,
115
+ IncludeResultsViewNames: false,
116
+ IncludeVariables: false,
117
+ IncludeTimers: false,
118
+ IncludeResourceURLs: false,
119
+ IncludeDebugValues: false,
120
+ IncludeApplicationValues: false,
121
+ IncludeWorkflows: false,
122
+ IncludeXmlSchemas: false,
123
+ SuppressResultSchemas: true
124
+ };
125
+ for (const key in options) {
126
+ request[key] = options[key];
127
+ }
128
+ return this.jsonp(url, request).then(function (response) {
129
+ if (enableBasicCommsCache) {
130
+ const retVal = { WUInfoResponse: { Workunit: {} } };
131
+ for (const key in options) {
132
+ const includeKey = key.substring(7);
133
+ retVal.WUInfoResponse.Workunit[includeKey] = response.WUInfoResponse.Workunit[includeKey];
134
+ }
135
+ basicCommsCache[url][JSON.stringify(request)] = retVal;
136
+ }
137
+ return response;
138
+ });
139
+ };
140
+
141
+ Workunit.prototype.wuUpdate = function (options) {
142
+ const url = this.getUrl({
143
+ pathname: "WsWorkunits/WUUpdate.json"
144
+ });
145
+ const request = {
146
+ Wuid: this._wuid
147
+ };
148
+ for (const key in options) {
149
+ request[key] = options[key];
150
+ }
151
+ return this.post(url, request);
152
+ };
153
+
154
+ Workunit.prototype.appData = function (appID, key, _) {
155
+ if (arguments.length === 2) {
156
+ return this.wuInfo({
157
+ IncludeApplicationValues: true
158
+ }).then(function (response) {
159
+ let persistString;
160
+ if (response.WUInfoResponse && response.WUInfoResponse.Workunit && response.WUInfoResponse.Workunit.ApplicationValues && response.WUInfoResponse.Workunit.ApplicationValues.ApplicationValue) {
161
+ response.WUInfoResponse.Workunit.ApplicationValues.ApplicationValue.filter(function (row) {
162
+ return row.Application === appID && row.Name === key;
163
+ }).forEach(function (row) {
164
+ persistString = row.Value;
165
+ });
166
+ }
167
+ return persistString;
168
+ });
169
+ } else if (arguments.length === 3) {
170
+ return this.wuUpdate({
171
+ "ApplicationValues.ApplicationValue.0.Application": appID,
172
+ "ApplicationValues.ApplicationValue.0.Name": key,
173
+ "ApplicationValues.ApplicationValue.0.Value": _,
174
+ "ApplicationValues.ApplicationValue.itemcount": 1
175
+ });
176
+ }
177
+ };
178
+
179
+ Workunit.prototype.results = function () {
180
+ const context = this;
181
+ return this.wuInfo({
182
+ IncludeResults: true
183
+ }).then(function (response) {
184
+ let retVal = [];
185
+ if (Utility.exists("WUInfoResponse.Workunit.Results.ECLResult", response)) {
186
+ retVal = response.WUInfoResponse.Workunit.Results.ECLResult.map(function (result) {
187
+ return new WUResult(context.getUrl({ pathname: "WsWorkunits/" }), context._wuid, result.Name);
188
+ });
189
+ }
190
+ return retVal;
191
+ });
192
+ };
193
+
194
+ Workunit.prototype.result = function (dataSource, resultName) {
195
+ dataSource = dataSource || this._wuid;
196
+ return createResult(dataSource, resultName);
197
+ };
198
+
199
+ // Workunit Result ---
200
+ function WUResult(baseUrl, wuid, name) {
201
+ BasicComms.call(this);
202
+ this.url(baseUrl + "WUResult.json");
203
+ this._wuid = wuid;
204
+ this._name = name;
205
+ this._xmlSchema = null;
206
+ }
207
+ WUResult.prototype = Object.create(BasicComms.prototype);
208
+
209
+ WUResult.prototype.wuid = function (_) {
210
+ if (!arguments.length) return this._wuid;
211
+ this._wuid = _;
212
+ return this;
213
+ };
214
+
215
+ WUResult.prototype.name = function (_) {
216
+ if (!arguments.length) return this._name;
217
+ this._name = _;
218
+ return this;
219
+ };
220
+
221
+ WUResult.prototype.query = function (options, filter) {
222
+ options = options || {};
223
+ filter = filter || {};
224
+ const request = {
225
+ Wuid: this._wuid,
226
+ ResultName: this._name,
227
+ SuppressXmlSchema: true,
228
+ Start: 0,
229
+ Count: -1
230
+ };
231
+ for (const key in options) {
232
+ request[key] = options[key];
233
+ }
234
+ let filterIdx = 0;
235
+ for (const fKey in filter) {
236
+ request["FilterBy.NamedValue." + filterIdx + ".Name"] = fKey;
237
+ request["FilterBy.NamedValue." + filterIdx + ".Value"] = filter[fKey];
238
+ ++filterIdx;
239
+ }
240
+ if (filterIdx) {
241
+ request["FilterBy.NamedValue.itemcount"] = filterIdx;
242
+ }
243
+ const context = this;
244
+ return this.jsonp(this.url(), request).then(function (response) {
245
+ if (response.WUResultResponse &&
246
+ response.WUResultResponse.Result &&
247
+ response.WUResultResponse.Result[context._name]) {
248
+ if (enableBasicCommsCache) {
249
+ basicCommsCache[context.url()][JSON.stringify(request)] = {
250
+ WUResultResponse: {
251
+ Result: response.WUResultResponse.Result
252
+ }
253
+ };
254
+ }
255
+ context._xmlSchema = response.WUResultResponse.Result.XmlSchema;
256
+ return nestedRowFix(response.WUResultResponse.Result[context._name]);
257
+ }
258
+ return [];
259
+ });
260
+ };
261
+
262
+ // Logical File ---
263
+ function LogicalFile(baseUrl, logicalName) {
264
+ BasicComms.call(this);
265
+ this.url(baseUrl + "WUResult.json");
266
+ this._logicalName = logicalName;
267
+ this._xmlSchema = null;
268
+ }
269
+ LogicalFile.prototype = Object.create(BasicComms.prototype);
270
+
271
+ LogicalFile.prototype.query = function (options, filter) {
272
+ options = options || {};
273
+ filter = filter || {};
274
+ const request = {
275
+ Cluster: "hthor", // TODO: Should not be needed ---
276
+ LogicalName: this._logicalName,
277
+ SuppressXmlSchema: this._xmlSchema !== null,
278
+ Start: 0,
279
+ Count: -1
280
+ };
281
+ for (const key in options) {
282
+ request[key] = options[key];
283
+ }
284
+ let filterIdx = 0;
285
+ for (const fKey in filter) {
286
+ request["FilterBy.NamedValue." + filterIdx + ".Name"] = fKey;
287
+ request["FilterBy.NamedValue." + filterIdx + ".Value"] = filter[fKey];
288
+ ++filterIdx;
289
+ }
290
+ if (filterIdx) {
291
+ request["FilterBy.NamedValue.itemcount"] = filterIdx;
292
+ }
293
+ const context = this;
294
+ return this.jsonp(this.url(), request).then(function (response) {
295
+ if (response.WUResultResponse &&
296
+ response.WUResultResponse.Result &&
297
+ response.WUResultResponse.Result.Row) {
298
+ context._xmlSchema = response.WUResultResponse.Result.XmlSchema;
299
+ return nestedRowFix(response.WUResultResponse.Result.Row);
300
+ }
301
+ return [];
302
+ });
303
+ };
304
+
305
+ // Roxie Query ---
306
+ function RoxieQuery(baseUrl, resultName) {
307
+ BasicComms.call(this);
308
+ const urlParts = baseUrl.split("/");
309
+ let queryName = urlParts.pop();
310
+ if (queryName.toLowerCase() === "json") {
311
+ queryName = urlParts.pop();
312
+ }
313
+ this._queryName = queryName;
314
+ this._resultName = resultName;
315
+ this.url(urlParts.join("/") + "/" + queryName + "/json");
316
+ }
317
+ RoxieQuery.prototype = Object.create(BasicComms.prototype);
318
+
319
+ function trimRight(str) {
320
+ if (str && str.replace) {
321
+ return str.replace(/ +$/, "");
322
+ }
323
+ return str;
324
+ }
325
+
326
+ function postFilter(results, filter) {
327
+ return results.filter(function (row) {
328
+ for (const key in filter) {
329
+ if (row[key] !== undefined && trimRight(filter[key]) !== trimRight(row[key])) {
330
+ return false;
331
+ }
332
+ }
333
+ return true;
334
+ });
335
+ }
336
+
337
+ function locateRoxieResponse(response) {
338
+ // v5 and v6 compatible ---
339
+ for (const key in response) {
340
+ if (response[key].Row && response[key].Row instanceof Array) {
341
+ return response;
342
+ }
343
+ const retVal = locateRoxieResponse(response[key]);
344
+ if (retVal) {
345
+ return retVal;
346
+ }
347
+ }
348
+ return null;
349
+ }
350
+
351
+ RoxieQuery.prototype.query = function (options, filter) {
352
+ options = options || {};
353
+ filter = filter || {};
354
+ const request = {
355
+ };
356
+ for (const key in options) {
357
+ request[key] = options[key];
358
+ }
359
+ for (const fKey in filter) {
360
+ request[fKey] = filter[fKey];
361
+ }
362
+ const context = this;
363
+ return this.jsonp(this.url(), request).then(function (response) {
364
+ response = locateRoxieResponse(response);
365
+ if (response) {
366
+ if (context._resultName) {
367
+ if (response && response[context._resultName] && response[context._resultName].Row) {
368
+ return nestedRowFix(postFilter(response[context._resultName].Row, filter));
369
+ }
370
+ } else {
371
+ for (const key in response) {
372
+ if (response[key].Row) {
373
+ return nestedRowFix(postFilter(response[key].Row, filter));
374
+ }
375
+ }
376
+ }
377
+ }
378
+ return [];
379
+ });
380
+ };
381
+
382
+ export function createResult(_espUrl, dataSource, resultName?) {
383
+ const espUrl = new Comms.ESPUrl()
384
+ .url(_espUrl)
385
+ ;
386
+ if (dataSource.indexOf("http") === 0) {
387
+ return new RoxieQuery(dataSource, resultName);
388
+ } else if (dataSource.indexOf("~") === 0 || dataSource.indexOf("::") >= 0) {
389
+ return new LogicalFile(espUrl.getUrl({ pathname: "WsWorkunits/" }), dataSource);
390
+ } else if (dataSource) {
391
+ return new WUResult(espUrl.getUrl({ pathname: "WsWorkunits/" }), dataSource, resultName);
392
+ }
393
+ return null;
394
+ }
395
+
396
+ export function enableCache(_?: any): any | undefined {
397
+ if (!arguments.length) return enableBasicCommsCache;
398
+ enableBasicCommsCache = _;
399
+ if (!_) {
400
+ basicCommsCache = {};
401
+ }
402
+ }
403
+ export function cache(_?: any): any | undefined {
404
+ if (!arguments.length) return basicCommsCache;
405
+ basicCommsCache = _;
406
+ }
407
+ export function createConnection(url) {
408
+ url = url || document.URL;
409
+ const testURL = new Comms.ESPUrl()
410
+ .url(url)
411
+ ;
412
+ if (testURL.isWsWorkunits()) {
413
+ const espConnection = Comms.createESPConnection(url);
414
+ if (espConnection instanceof Comms.WsWorkunits && espConnection.wuid()) {
415
+ return new Workunit(espConnection.getUrl({ pathname: "" }), espConnection.wuid())
416
+ .url(url)
417
+ ;
418
+ }
419
+ }
420
+ return null;
421
+ }
422
+ export function flattenResult(result, mappings) {
423
+ const retVal = {
424
+ columns: [],
425
+ data: []
426
+ };
427
+ if (result && result.length) {
428
+ const colIdx = {};
429
+ if (mappings && mappings.length) {
430
+ mappings.forEach(function (mapping) {
431
+ colIdx[mapping.value.toLowerCase()] = retVal.columns.length;
432
+ retVal.columns.push(mapping.key);
433
+ });
434
+ } else {
435
+ for (const key in result[0]) {
436
+ colIdx[key.toLowerCase()] = retVal.columns.length;
437
+ retVal.columns.push(key);
438
+ }
439
+ }
440
+ result.forEach(function (row, rowIdx) {
441
+ const rowArr = [];
442
+ for (const key in row) {
443
+ if (colIdx[key.toLowerCase()] !== undefined) {
444
+ rowArr[colIdx[key.toLowerCase()]] = row[key];
445
+ }
446
+ }
447
+ retVal.data.push(rowArr);
448
+ });
449
+ }
450
+ return retVal;
451
+ }