@esri/solution-velocity 4.1.2-alpha.0 → 5.0.0

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.
@@ -1,550 +1,550 @@
1
- /** @license
2
- * Copyright 2021 Esri
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import { getVelocityUrlBase, replaceInTemplate, getProp, fail, BASE_NAMES, PROP_NAMES } from "@esri/solution-common";
17
- /**
18
- * Common function to build urls for reading and interacting with the velocity api
19
- *
20
- *
21
- * @param authentication Credentials for the requests
22
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
23
- * @param type The type of velocity item we are constructing a url for
24
- * @param id Optional The id of the velocity item we are constructing a url for
25
- * @param isDeploy Optional Is this being constructed as a part of deployment
26
- * @param urlPrefix Optional prefix args necessary for some url construction
27
- * @param urlSuffix Optional suffix args necessary for some url construction
28
- *
29
- * @returns a promise that will resolve the constructed url
30
- *
31
- */
32
- export function getVelocityUrl(authentication, templateDictionary, type, id = "", isDeploy = false, urlPrefix = "", urlSuffix = "") {
33
- return getVelocityUrlBase(authentication, templateDictionary).then(url => {
34
- if (url) {
35
- const _type = type === "Real Time Analytic"
36
- ? "analytics/realtime"
37
- : type === "Big Data Analytic"
38
- ? "analytics/bigdata"
39
- : type.toLowerCase();
40
- const suffix = urlSuffix ? `/${urlSuffix}` : "";
41
- const prefix = urlPrefix ? `/${urlPrefix}` : "";
42
- return Promise.resolve(isDeploy
43
- ? `${url}/iot/${_type}${prefix}${suffix}`
44
- : id
45
- ? `${url}/iot/${_type}${prefix}/${id}${suffix}/?f=json&token=${authentication.token}`
46
- : `${url}/iot/${_type}${prefix}${suffix}/?f=json&token=${authentication.token}`);
47
- }
48
- else {
49
- return Promise.resolve(url);
50
- }
51
- });
52
- }
53
- /**
54
- * Handles the creation of velocity items.
55
- *
56
- * @param authentication Credentials for the requests
57
- * @param template The current itemTemplate that is being used for deployment
58
- * @param data The velocity item data used to create the items.
59
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
60
- * @param autoStart This can be leveraged to start certain velocity items after they are created.
61
- *
62
- * @returns a promise that will resolve an object containing the item, id, type, and post process flag
63
- *
64
- */
65
- export function postVelocityData(authentication, template, data, templateDictionary, autoStart = false) {
66
- return getVelocityUrl(authentication, templateDictionary, template.type, undefined, true).then(url => {
67
- if (url) {
68
- return getTitle(authentication, data.label, url).then((titleInfo) => {
69
- const titles = titleInfo.titles;
70
- data.label = titleInfo.label;
71
- data.id = "";
72
- const body = replaceInTemplate(data, templateDictionary);
73
- const dataOutputs = (data.outputs ? data.outputs : data.output ? [data.output] : []).map((o) => {
74
- return {
75
- id: o.id,
76
- name: o.properties[`${o.name}.name`]
77
- };
78
- });
79
- const feeds = (body.feeds ? body.feeds : body.feed ? [body.feed] : []).map((o) => {
80
- return {
81
- id: o.id ? o.id : o.properties[`${o.name}.portalItemId`] || "",
82
- name: o.label ? o.label : data.label
83
- };
84
- });
85
- return _validateOutputs(authentication, templateDictionary, template.type, body, titles, dataOutputs, feeds).then(updatedBody => {
86
- return _fetch(authentication, url, "POST", updatedBody).then(rr => {
87
- template.item.url = `${url}/${rr.id}`;
88
- template.item.title = data.label;
89
- // Update the template dictionary
90
- templateDictionary[template.itemId]["url"] = template.item.url;
91
- templateDictionary[template.itemId]["label"] = data.label;
92
- templateDictionary[template.itemId]["itemId"] = rr.id;
93
- const finalResult = {
94
- item: replaceInTemplate(template.item, templateDictionary),
95
- id: rr.id,
96
- type: template.type,
97
- postProcess: false
98
- };
99
- if (autoStart) {
100
- return _validateAndStart(authentication, templateDictionary, template, rr.id).then(() => {
101
- return Promise.resolve(finalResult);
102
- });
103
- }
104
- else {
105
- return Promise.resolve(finalResult);
106
- }
107
- });
108
- });
109
- });
110
- }
111
- else {
112
- return Promise.reject(fail("Velocity NOT Supported by Organization"));
113
- }
114
- });
115
- }
116
- /**
117
- * Velocity item titles must be unique across the organization.
118
- * Check and ensure we set a unique title
119
- *
120
- * @param authentication Credentials for the requests
121
- * @param label The current label of the item from the solution template
122
- * @param url The base velocity url for checking status
123
- *
124
- * @returns a promise that will resolve a unique title
125
- *
126
- */
127
- export function getTitle(authentication, label, url) {
128
- return _fetch(authentication, `${url}StatusList?view=admin`, "GET").then(items => {
129
- const titles = items && Array.isArray(items)
130
- ? items.map(item => {
131
- return { title: item.label };
132
- })
133
- : [];
134
- return Promise.resolve({ label: getUniqueTitle(label, { titles }, "titles"), titles: titles.map(t => t.title) });
135
- });
136
- }
137
- /**
138
- * Validate the data that will be used and handle any reported issues with the outputs.
139
- * The output names must be unique across the organization.
140
- *
141
- * This function will update the data arg that is passed in with a unique name.
142
- *
143
- * @param authentication Credentials for the requests
144
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
145
- * @param type The type of velocity item
146
- * @param data The data used to construct the velocity item
147
- * @param titles The list of know titles that exist in the org
148
- * @param dataOutputs The velocity items output objects
149
- * @param feeds The velocity items feed objects
150
- *
151
- * @returns a promise that will resolve the data object passed in with any necessary changes.
152
- *
153
- * @private
154
- */
155
- export function _validateOutputs(authentication, templateDictionary, type, data, titles, dataOutputs = [], feeds = []) {
156
- if (dataOutputs.length > 0 || feeds.length > 0) {
157
- return validate(authentication, templateDictionary, type, "", data).then((validateResults) => {
158
- const names = _validateMessages(validateResults);
159
- if (names.length > 0) {
160
- /* istanbul ignore else */
161
- if (dataOutputs.length > 0) {
162
- _updateDataOutput(dataOutputs, data, names);
163
- }
164
- /* istanbul ignore else */
165
- if (feeds.length > 0) {
166
- _updateFeed(feeds, data, names.concat(titles));
167
- }
168
- return _validateOutputs(authentication, templateDictionary, type, data, titles, dataOutputs, feeds);
169
- }
170
- else {
171
- return Promise.resolve(data);
172
- }
173
- });
174
- }
175
- else {
176
- return Promise.resolve(data);
177
- }
178
- }
179
- /**
180
- * Check the validate results for any name conflicts and store the conflicting names.
181
- *
182
- * @param validateResults The results object to check for name conflict errors
183
- *
184
- * @returns a list of names that already exist in the org
185
- *
186
- * @private
187
- */
188
- export function _validateMessages(validateResults) {
189
- let messages = getProp(validateResults, "validation.messages");
190
- const nodes = getProp(validateResults, "nodes");
191
- /* istanbul ignore else */
192
- if (nodes && Array.isArray(nodes)) {
193
- nodes.forEach(node => {
194
- messages = messages.concat(getProp(node, "validation.messages") || []);
195
- });
196
- }
197
- let names = [];
198
- /* istanbul ignore else */
199
- if (messages && Array.isArray(messages)) {
200
- messages.forEach(message => {
201
- // I don't see a way to ask for all output names that exist
202
- // velocityUrl + /outputs/ just gives you generic defaults not what currently exists
203
- const nameErrors = [
204
- "VALIDATION_ANALYTICS__MULTIPLE_CREATE_FEATURE_LAYER_OUTPUTS_REFERENCE_SAME_LAYER_NAME",
205
- "VALIDATION_ANALYTICS__MULTIPLE_CREATE_STREAM_LAYER_OUTPUTS_REFERENCE_SAME_LAYER_NAME",
206
- "ITEM_MANAGER__CREATE_ANALYTIC_FAILED_DUPLICATE_OUTPUT_NAMES_IN_ORGANIZATION_NOT_ALLOWED",
207
- "ITEM_MANAGER__CREATE_BIG_DATA_ANALYTIC_FAILED_DUPLICATE_NAMES_NOT_ALLOWED",
208
- "ITEM_MANAGER__CREATE_REAL_TIME_ANALYTIC_FAILED_DUPLICATE_NAMES_NOT_ALLOWED",
209
- "ITEM_MANAGER__CREATE_FEED_FAILED_DUPLICATE_NAME"
210
- ];
211
- // The names returned here seem to replace " " with "_" so they do not match exactly
212
- /* istanbul ignore else */
213
- if (nameErrors.indexOf(message.key) > -1) {
214
- names = names.concat(message.args);
215
- }
216
- });
217
- }
218
- return names;
219
- }
220
- /**
221
- * Updates the feed object with a new name when validation fails.
222
- *
223
- * @param feeds The feed objects from the velocity item.
224
- * @param data The full data object used for deploying the velocity item.
225
- * @param names The names that failed due to duplicate error in validation.
226
- *
227
- * @private
228
- */
229
- export function _updateFeed(feeds, data, names) {
230
- feeds.forEach(f => {
231
- const update = _getOutputLabel(names, f);
232
- /* istanbul ignore else */
233
- if (update) {
234
- data.label = update.label;
235
- f.name = update.label;
236
- }
237
- });
238
- }
239
- /**
240
- * Updates the data object with a new name when validation fails.
241
- *
242
- * @param dataOutputs The data output objects from the velocity item.
243
- * @param data The full data object used for deploying the velocity item.
244
- * @param names The names that failed due to duplicate error in validation.
245
- *
246
- * @private
247
- */
248
- export function _updateDataOutput(dataOutputs, data, names) {
249
- dataOutputs.forEach(dataOutput => {
250
- const update = _getOutputLabel(names, dataOutput);
251
- /* istanbul ignore else */
252
- if (update) {
253
- const _outputs = (data.outputs ? data.outputs : data.output ? [data.output] : []).map((_dataOutput) => {
254
- /* istanbul ignore else */
255
- if (_dataOutput.id === update.id) {
256
- /* istanbul ignore else */
257
- if (_dataOutput.properties) {
258
- const nameProp = `${_dataOutput.name}.name`;
259
- /* istanbul ignore else */
260
- if (Object.keys(_dataOutput.properties).indexOf(nameProp) > -1) {
261
- _dataOutput.properties[nameProp] = update.label;
262
- }
263
- }
264
- }
265
- return _dataOutput;
266
- });
267
- /* istanbul ignore else */
268
- if (data.outputs) {
269
- data.outputs = _outputs;
270
- }
271
- /* istanbul ignore else */
272
- if (data.output) {
273
- data.output = _outputs[0];
274
- }
275
- }
276
- });
277
- }
278
- /**
279
- * Get a unique label for the item.
280
- *
281
- * @param names The names that failed due to duplicate error in validation.
282
- * @param dataOutput The current data output that is being evaluated.
283
- *
284
- * @returns an object with a unique label and the outputs id when a name
285
- * conflict is found...otherwise returns undefined
286
- *
287
- * @private
288
- */
289
- export function _getOutputLabel(names, dataOutput) {
290
- const titles = names.map((name) => {
291
- return { title: name };
292
- });
293
- const label = getUniqueTitle(dataOutput.name, { titles }, "titles");
294
- return label !== dataOutput.name
295
- ? {
296
- label,
297
- id: dataOutput.id
298
- }
299
- : undefined;
300
- }
301
- /**
302
- * Will return the provided title if it does not exist as a property
303
- * in one of the objects at the defined path. Otherwise the title will
304
- * have a numerical value attached.
305
- *
306
- * This is based on "getUniqueTitle" from common but adds the "_" replacement check for velocity names.
307
- * Could switch to using common if Velocity has a way to get a list of all names that are already used.
308
- *
309
- * @param title The root title to test
310
- * @param templateDictionary Hash of the facts
311
- * @param path to the objects to evaluate for potantial name clashes
312
- *
313
- * @returns string The unique title to use
314
- *
315
- */
316
- export function getUniqueTitle(title, templateDictionary, path) {
317
- title = title ? title.trim() : "_";
318
- const objs = getProp(templateDictionary, path) || [];
319
- const titles = objs.map(obj => {
320
- return obj.title;
321
- });
322
- let newTitle = title;
323
- let i = 0;
324
- // replace added for velocitcy
325
- // validation seems to add "_" to names listed in outputs..so no way to compare without hacking the name
326
- while (titles.indexOf(newTitle) > -1 ||
327
- titles.indexOf(newTitle.replace(/ /g, "_")) > -1) {
328
- i++;
329
- newTitle = title + " " + i;
330
- }
331
- return newTitle;
332
- }
333
- /**
334
- * Start the item if validation passes and the item is executable.
335
- *
336
- * @param authentication Credentials for the requests
337
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
338
- * @param template the item template that has the details for deployment
339
- * @param id the new id for the velocity item that was deployed
340
- *
341
- * @returns a promise that will resolve with the validation results
342
- * or the start results when validation indicates the item is executable
343
- *
344
- * @private
345
- */
346
- export function _validateAndStart(authentication, templateDictionary, template, id) {
347
- return validate(authentication, templateDictionary, template.type, id).then(validateResult => {
348
- if (validateResult.executable) {
349
- return start(authentication, templateDictionary, template.type, id);
350
- }
351
- else {
352
- return Promise.resolve(validateResult);
353
- }
354
- });
355
- }
356
- /**
357
- * Validate the velocity item.
358
- * Used to help find and handle duplicate name errors.
359
- *
360
- * @param authentication Credentials for the requests
361
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
362
- * @param type The type of velocity item we are constructing a url for
363
- * @param id? Optional The id of the velocity item we are constructing a url for
364
- * @param body? Optional the request body to validate.
365
- *
366
- * @returns a promise that will resolve with an object containing messages
367
- * indicating any issues found when validating such as name conflict errors
368
- *
369
- */
370
- export function validate(authentication, templateDictionary, type, id, body) {
371
- // /iot/feed/validate/{id}/
372
- // /iot/analytics/realtime/validate/{id}/
373
- return getVelocityUrl(authentication, templateDictionary, type, id, false, "validate", "").then(url => {
374
- return _fetch(authentication, url, "POST", body).then(result => {
375
- return Promise.resolve(result);
376
- });
377
- });
378
- }
379
- /**
380
- * Start the given velocity item that has been deployed.
381
- *
382
- * @param authentication Credentials for the requests
383
- * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
384
- * @param type The type of velocity item we are constructing a url for
385
- * @param id? Optional The id of the velocity item we are constructing a url for
386
- *
387
- * @returns a promise that will resolve with the result of the start call
388
- *
389
- */
390
- export function start(authentication, templateDictionary, type, id) {
391
- // /iot/feed/{id}/start/
392
- // /iot/analytics/realtime/{id}/start/
393
- return getVelocityUrl(authentication, templateDictionary, type, id, false, "", "start").then(url => {
394
- return _fetch(authentication, url, "GET").then(result => {
395
- return Promise.resolve(result);
396
- });
397
- });
398
- }
399
- /**
400
- * Gets the required request options for requests to the velocity API.
401
- *
402
- * @param authentication Credentials for the requests
403
- * @param method Indicate if "GET" or "POST"
404
- *
405
- * @returns generic request options used for various calls to velocity api
406
- *
407
- * @private
408
- */
409
- export function _getRequestOpts(authentication, method) {
410
- return {
411
- headers: {
412
- Accept: "application/json",
413
- "Content-Type": "application/json",
414
- Authorization: "token=" + authentication.token
415
- },
416
- method
417
- };
418
- }
419
- /**
420
- * Generic fetch function for making calls to the velocity API.
421
- *
422
- * @param authentication Credentials for the requests
423
- * @param url The url from the velocity API to handle reading and writing
424
- * @param method The method for the request "GET" or "POST"
425
- * @param body The body for POST requests
426
- *
427
- * @returns a promise that will resolve with the result of the fetch call
428
- *
429
- * @private
430
- */
431
- export function _fetch(authentication, url, method, // GET or POST
432
- body) {
433
- const requestOpts = _getRequestOpts(authentication, method);
434
- /* istanbul ignore else */
435
- if (body) {
436
- requestOpts.body = JSON.stringify(body);
437
- }
438
- return fetch(url, requestOpts).then(r => Promise.resolve(r.json()));
439
- }
440
- /**
441
- * Remove key properties if the dependency was removed due to having the "IoTFeatureLayer" typeKeyword
442
- * This function will update the input template.
443
- *
444
- * @param template The template that for the velocity item
445
- *
446
- */
447
- export function cleanDataSourcesAndFeeds(template, velocityUrl) {
448
- const dependencies = template.dependencies;
449
- [
450
- getProp(template, "data.sources") ? template.data.sources : [],
451
- getProp(template, "data.source") ? [template.data.source] : [],
452
- getProp(template, "data.feeds") ? template.data.feeds : [],
453
- getProp(template, "data.feed") ? [template.data.feed] : []
454
- ].forEach(d => _removeIdProps(d, dependencies, velocityUrl));
455
- [
456
- getProp(template, "data.outputs") ? template.data.outputs : [],
457
- getProp(template, "data.output") ? [template.data.output] : []
458
- ].forEach(outputs => _removeIdPropsAndSetName(outputs, dependencies));
459
- }
460
- /**
461
- * Remove key properties from the input source or feed
462
- *
463
- * @param sourcesOrFeeds The list of dataSources or feeds
464
- * @param dependencies The list of dependencies
465
- *
466
- * @private
467
- */
468
- export function _removeIdProps(sourcesOrFeeds, dependencies, velocityUrl) {
469
- sourcesOrFeeds.forEach(dataSource => {
470
- const idProp = "feature-layer.portalItemId";
471
- const layerIdProp = "feature-layer.layerId";
472
- /* istanbul ignore else */
473
- if (dataSource.properties) {
474
- /* istanbul ignore else */
475
- if (dataSource.properties[idProp]) {
476
- const id = dataSource.properties[idProp];
477
- /* istanbul ignore else */
478
- if (id && dependencies.indexOf(id) < 0) {
479
- delete dataSource.properties[idProp];
480
- delete dataSource.properties[layerIdProp];
481
- }
482
- }
483
- const urlProp = "simulator.url";
484
- const url = dataSource.properties[urlProp];
485
- // only remove velocity based simulator urls
486
- // otherwise we will leave as is with no templatization
487
- /* istanbul ignore else */
488
- if (url && url.indexOf(velocityUrl) > -1) {
489
- delete dataSource.properties[urlProp];
490
- }
491
- }
492
- });
493
- }
494
- /**
495
- * Remove key properties from the outputs.
496
- *
497
- * @param outputs The list of outputs
498
- * @param dependencies The list of dependencies
499
- *
500
- * @private
501
- */
502
- export function _removeIdPropsAndSetName(outputs, dependencies) {
503
- outputs.forEach(output => {
504
- /* istanbul ignore else */
505
- if (output.properties) {
506
- const names = getProp(output, "name") ? [output.name] : BASE_NAMES;
507
- names.forEach(n => {
508
- PROP_NAMES.forEach(p => _removeProp(output.properties, n + p, dependencies));
509
- });
510
- _updateName(output.properties);
511
- }
512
- });
513
- }
514
- /**
515
- * Generic helper function to remove key properties .
516
- *
517
- * @param props the list of props to update
518
- * @param prop the individual prop to remove
519
- * @param dependencies The list of dependencies
520
- *
521
- * @private
522
- */
523
- export function _removeProp(props, prop, dependencies) {
524
- const id = props[prop];
525
- /* istanbul ignore else */
526
- if (id && dependencies.indexOf(id) < 0) {
527
- delete props[prop];
528
- }
529
- }
530
- /**
531
- * Update the feature layer name to include the solution item id.
532
- *
533
- * @param props the list of props to update
534
- *
535
- * @private
536
- */
537
- export function _updateName(props) {
538
- [
539
- "feat-lyr-new.name",
540
- "stream-lyr-new.name",
541
- "feat-lyr-existing.name"
542
- ].forEach(n => {
543
- const name = props[n];
544
- /* istanbul ignore else */
545
- if (name && name.indexOf("{{solutionItemId}}") < 0) {
546
- props[n] = `${name}_{{solutionItemId}}`;
547
- }
548
- });
549
- }
1
+ /** @license
2
+ * Copyright 2021 Esri
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { getVelocityUrlBase, replaceInTemplate, getProp, fail, BASE_NAMES, PROP_NAMES } from "@esri/solution-common";
17
+ /**
18
+ * Common function to build urls for reading and interacting with the velocity api
19
+ *
20
+ *
21
+ * @param authentication Credentials for the requests
22
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
23
+ * @param type The type of velocity item we are constructing a url for
24
+ * @param id Optional The id of the velocity item we are constructing a url for
25
+ * @param isDeploy Optional Is this being constructed as a part of deployment
26
+ * @param urlPrefix Optional prefix args necessary for some url construction
27
+ * @param urlSuffix Optional suffix args necessary for some url construction
28
+ *
29
+ * @returns a promise that will resolve the constructed url
30
+ *
31
+ */
32
+ export function getVelocityUrl(authentication, templateDictionary, type, id = "", isDeploy = false, urlPrefix = "", urlSuffix = "") {
33
+ return getVelocityUrlBase(authentication, templateDictionary).then(url => {
34
+ if (url) {
35
+ const _type = type === "Real Time Analytic"
36
+ ? "analytics/realtime"
37
+ : type === "Big Data Analytic"
38
+ ? "analytics/bigdata"
39
+ : type.toLowerCase();
40
+ const suffix = urlSuffix ? `/${urlSuffix}` : "";
41
+ const prefix = urlPrefix ? `/${urlPrefix}` : "";
42
+ return Promise.resolve(isDeploy
43
+ ? `${url}/iot/${_type}${prefix}${suffix}`
44
+ : id
45
+ ? `${url}/iot/${_type}${prefix}/${id}${suffix}/?f=json&token=${authentication.token}`
46
+ : `${url}/iot/${_type}${prefix}${suffix}/?f=json&token=${authentication.token}`);
47
+ }
48
+ else {
49
+ return Promise.resolve(url);
50
+ }
51
+ });
52
+ }
53
+ /**
54
+ * Handles the creation of velocity items.
55
+ *
56
+ * @param authentication Credentials for the requests
57
+ * @param template The current itemTemplate that is being used for deployment
58
+ * @param data The velocity item data used to create the items.
59
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
60
+ * @param autoStart This can be leveraged to start certain velocity items after they are created.
61
+ *
62
+ * @returns a promise that will resolve an object containing the item, id, type, and post process flag
63
+ *
64
+ */
65
+ export function postVelocityData(authentication, template, data, templateDictionary, autoStart = false) {
66
+ return getVelocityUrl(authentication, templateDictionary, template.type, undefined, true).then(url => {
67
+ if (url) {
68
+ return getTitle(authentication, data.label, url).then((titleInfo) => {
69
+ const titles = titleInfo.titles;
70
+ data.label = titleInfo.label;
71
+ data.id = "";
72
+ const body = replaceInTemplate(data, templateDictionary);
73
+ const dataOutputs = (data.outputs ? data.outputs : data.output ? [data.output] : []).map((o) => {
74
+ return {
75
+ id: o.id,
76
+ name: o.properties[`${o.name}.name`]
77
+ };
78
+ });
79
+ const feeds = (body.feeds ? body.feeds : body.feed ? [body.feed] : []).map((o) => {
80
+ return {
81
+ id: o.id ? o.id : o.properties[`${o.name}.portalItemId`] || "",
82
+ name: o.label ? o.label : data.label
83
+ };
84
+ });
85
+ return _validateOutputs(authentication, templateDictionary, template.type, body, titles, dataOutputs, feeds).then(updatedBody => {
86
+ return _fetch(authentication, url, "POST", updatedBody).then(rr => {
87
+ template.item.url = `${url}/${rr.id}`;
88
+ template.item.title = data.label;
89
+ // Update the template dictionary
90
+ templateDictionary[template.itemId]["url"] = template.item.url;
91
+ templateDictionary[template.itemId]["label"] = data.label;
92
+ templateDictionary[template.itemId]["itemId"] = rr.id;
93
+ const finalResult = {
94
+ item: replaceInTemplate(template.item, templateDictionary),
95
+ id: rr.id,
96
+ type: template.type,
97
+ postProcess: false
98
+ };
99
+ if (autoStart) {
100
+ return _validateAndStart(authentication, templateDictionary, template, rr.id).then(() => {
101
+ return Promise.resolve(finalResult);
102
+ });
103
+ }
104
+ else {
105
+ return Promise.resolve(finalResult);
106
+ }
107
+ });
108
+ });
109
+ });
110
+ }
111
+ else {
112
+ return Promise.reject(fail("Velocity NOT Supported by Organization"));
113
+ }
114
+ });
115
+ }
116
+ /**
117
+ * Velocity item titles must be unique across the organization.
118
+ * Check and ensure we set a unique title
119
+ *
120
+ * @param authentication Credentials for the requests
121
+ * @param label The current label of the item from the solution template
122
+ * @param url The base velocity url for checking status
123
+ *
124
+ * @returns a promise that will resolve a unique title
125
+ *
126
+ */
127
+ export function getTitle(authentication, label, url) {
128
+ return _fetch(authentication, `${url}StatusList?view=admin`, "GET").then(items => {
129
+ const titles = items && Array.isArray(items)
130
+ ? items.map(item => {
131
+ return { title: item.label };
132
+ })
133
+ : [];
134
+ return Promise.resolve({ label: getUniqueTitle(label, { titles }, "titles"), titles: titles.map(t => t.title) });
135
+ });
136
+ }
137
+ /**
138
+ * Validate the data that will be used and handle any reported issues with the outputs.
139
+ * The output names must be unique across the organization.
140
+ *
141
+ * This function will update the data arg that is passed in with a unique name.
142
+ *
143
+ * @param authentication Credentials for the requests
144
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
145
+ * @param type The type of velocity item
146
+ * @param data The data used to construct the velocity item
147
+ * @param titles The list of know titles that exist in the org
148
+ * @param dataOutputs The velocity items output objects
149
+ * @param feeds The velocity items feed objects
150
+ *
151
+ * @returns a promise that will resolve the data object passed in with any necessary changes.
152
+ *
153
+ * @private
154
+ */
155
+ export function _validateOutputs(authentication, templateDictionary, type, data, titles, dataOutputs = [], feeds = []) {
156
+ if (dataOutputs.length > 0 || feeds.length > 0) {
157
+ return validate(authentication, templateDictionary, type, "", data).then((validateResults) => {
158
+ const names = _validateMessages(validateResults);
159
+ if (names.length > 0) {
160
+ /* istanbul ignore else */
161
+ if (dataOutputs.length > 0) {
162
+ _updateDataOutput(dataOutputs, data, names);
163
+ }
164
+ /* istanbul ignore else */
165
+ if (feeds.length > 0) {
166
+ _updateFeed(feeds, data, names.concat(titles));
167
+ }
168
+ return _validateOutputs(authentication, templateDictionary, type, data, titles, dataOutputs, feeds);
169
+ }
170
+ else {
171
+ return Promise.resolve(data);
172
+ }
173
+ });
174
+ }
175
+ else {
176
+ return Promise.resolve(data);
177
+ }
178
+ }
179
+ /**
180
+ * Check the validate results for any name conflicts and store the conflicting names.
181
+ *
182
+ * @param validateResults The results object to check for name conflict errors
183
+ *
184
+ * @returns a list of names that already exist in the org
185
+ *
186
+ * @private
187
+ */
188
+ export function _validateMessages(validateResults) {
189
+ let messages = getProp(validateResults, "validation.messages");
190
+ const nodes = getProp(validateResults, "nodes");
191
+ /* istanbul ignore else */
192
+ if (nodes && Array.isArray(nodes)) {
193
+ nodes.forEach(node => {
194
+ messages = messages.concat(getProp(node, "validation.messages") || []);
195
+ });
196
+ }
197
+ let names = [];
198
+ /* istanbul ignore else */
199
+ if (messages && Array.isArray(messages)) {
200
+ messages.forEach(message => {
201
+ // I don't see a way to ask for all output names that exist
202
+ // velocityUrl + /outputs/ just gives you generic defaults not what currently exists
203
+ const nameErrors = [
204
+ "VALIDATION_ANALYTICS__MULTIPLE_CREATE_FEATURE_LAYER_OUTPUTS_REFERENCE_SAME_LAYER_NAME",
205
+ "VALIDATION_ANALYTICS__MULTIPLE_CREATE_STREAM_LAYER_OUTPUTS_REFERENCE_SAME_LAYER_NAME",
206
+ "ITEM_MANAGER__CREATE_ANALYTIC_FAILED_DUPLICATE_OUTPUT_NAMES_IN_ORGANIZATION_NOT_ALLOWED",
207
+ "ITEM_MANAGER__CREATE_BIG_DATA_ANALYTIC_FAILED_DUPLICATE_NAMES_NOT_ALLOWED",
208
+ "ITEM_MANAGER__CREATE_REAL_TIME_ANALYTIC_FAILED_DUPLICATE_NAMES_NOT_ALLOWED",
209
+ "ITEM_MANAGER__CREATE_FEED_FAILED_DUPLICATE_NAME"
210
+ ];
211
+ // The names returned here seem to replace " " with "_" so they do not match exactly
212
+ /* istanbul ignore else */
213
+ if (nameErrors.indexOf(message.key) > -1) {
214
+ names = names.concat(message.args);
215
+ }
216
+ });
217
+ }
218
+ return names;
219
+ }
220
+ /**
221
+ * Updates the feed object with a new name when validation fails.
222
+ *
223
+ * @param feeds The feed objects from the velocity item.
224
+ * @param data The full data object used for deploying the velocity item.
225
+ * @param names The names that failed due to duplicate error in validation.
226
+ *
227
+ * @private
228
+ */
229
+ export function _updateFeed(feeds, data, names) {
230
+ feeds.forEach(f => {
231
+ const update = _getOutputLabel(names, f);
232
+ /* istanbul ignore else */
233
+ if (update) {
234
+ data.label = update.label;
235
+ f.name = update.label;
236
+ }
237
+ });
238
+ }
239
+ /**
240
+ * Updates the data object with a new name when validation fails.
241
+ *
242
+ * @param dataOutputs The data output objects from the velocity item.
243
+ * @param data The full data object used for deploying the velocity item.
244
+ * @param names The names that failed due to duplicate error in validation.
245
+ *
246
+ * @private
247
+ */
248
+ export function _updateDataOutput(dataOutputs, data, names) {
249
+ dataOutputs.forEach(dataOutput => {
250
+ const update = _getOutputLabel(names, dataOutput);
251
+ /* istanbul ignore else */
252
+ if (update) {
253
+ const _outputs = (data.outputs ? data.outputs : data.output ? [data.output] : []).map((_dataOutput) => {
254
+ /* istanbul ignore else */
255
+ if (_dataOutput.id === update.id) {
256
+ /* istanbul ignore else */
257
+ if (_dataOutput.properties) {
258
+ const nameProp = `${_dataOutput.name}.name`;
259
+ /* istanbul ignore else */
260
+ if (Object.keys(_dataOutput.properties).indexOf(nameProp) > -1) {
261
+ _dataOutput.properties[nameProp] = update.label;
262
+ }
263
+ }
264
+ }
265
+ return _dataOutput;
266
+ });
267
+ /* istanbul ignore else */
268
+ if (data.outputs) {
269
+ data.outputs = _outputs;
270
+ }
271
+ /* istanbul ignore else */
272
+ if (data.output) {
273
+ data.output = _outputs[0];
274
+ }
275
+ }
276
+ });
277
+ }
278
+ /**
279
+ * Get a unique label for the item.
280
+ *
281
+ * @param names The names that failed due to duplicate error in validation.
282
+ * @param dataOutput The current data output that is being evaluated.
283
+ *
284
+ * @returns an object with a unique label and the outputs id when a name
285
+ * conflict is found...otherwise returns undefined
286
+ *
287
+ * @private
288
+ */
289
+ export function _getOutputLabel(names, dataOutput) {
290
+ const titles = names.map((name) => {
291
+ return { title: name };
292
+ });
293
+ const label = getUniqueTitle(dataOutput.name, { titles }, "titles");
294
+ return label !== dataOutput.name
295
+ ? {
296
+ label,
297
+ id: dataOutput.id
298
+ }
299
+ : undefined;
300
+ }
301
+ /**
302
+ * Will return the provided title if it does not exist as a property
303
+ * in one of the objects at the defined path. Otherwise the title will
304
+ * have a numerical value attached.
305
+ *
306
+ * This is based on "getUniqueTitle" from common but adds the "_" replacement check for velocity names.
307
+ * Could switch to using common if Velocity has a way to get a list of all names that are already used.
308
+ *
309
+ * @param title The root title to test
310
+ * @param templateDictionary Hash of the facts
311
+ * @param path to the objects to evaluate for potantial name clashes
312
+ *
313
+ * @returns string The unique title to use
314
+ *
315
+ */
316
+ export function getUniqueTitle(title, templateDictionary, path) {
317
+ title = title ? title.trim() : "_";
318
+ const objs = getProp(templateDictionary, path) || [];
319
+ const titles = objs.map(obj => {
320
+ return obj.title;
321
+ });
322
+ let newTitle = title;
323
+ let i = 0;
324
+ // replace added for velocitcy
325
+ // validation seems to add "_" to names listed in outputs..so no way to compare without hacking the name
326
+ while (titles.indexOf(newTitle) > -1 ||
327
+ titles.indexOf(newTitle.replace(/ /g, "_")) > -1) {
328
+ i++;
329
+ newTitle = title + " " + i;
330
+ }
331
+ return newTitle;
332
+ }
333
+ /**
334
+ * Start the item if validation passes and the item is executable.
335
+ *
336
+ * @param authentication Credentials for the requests
337
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
338
+ * @param template the item template that has the details for deployment
339
+ * @param id the new id for the velocity item that was deployed
340
+ *
341
+ * @returns a promise that will resolve with the validation results
342
+ * or the start results when validation indicates the item is executable
343
+ *
344
+ * @private
345
+ */
346
+ export function _validateAndStart(authentication, templateDictionary, template, id) {
347
+ return validate(authentication, templateDictionary, template.type, id).then(validateResult => {
348
+ if (validateResult.executable) {
349
+ return start(authentication, templateDictionary, template.type, id);
350
+ }
351
+ else {
352
+ return Promise.resolve(validateResult);
353
+ }
354
+ });
355
+ }
356
+ /**
357
+ * Validate the velocity item.
358
+ * Used to help find and handle duplicate name errors.
359
+ *
360
+ * @param authentication Credentials for the requests
361
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
362
+ * @param type The type of velocity item we are constructing a url for
363
+ * @param id? Optional The id of the velocity item we are constructing a url for
364
+ * @param body? Optional the request body to validate.
365
+ *
366
+ * @returns a promise that will resolve with an object containing messages
367
+ * indicating any issues found when validating such as name conflict errors
368
+ *
369
+ */
370
+ export function validate(authentication, templateDictionary, type, id, body) {
371
+ // /iot/feed/validate/{id}/
372
+ // /iot/analytics/realtime/validate/{id}/
373
+ return getVelocityUrl(authentication, templateDictionary, type, id, false, "validate", "").then(url => {
374
+ return _fetch(authentication, url, "POST", body).then(result => {
375
+ return Promise.resolve(result);
376
+ });
377
+ });
378
+ }
379
+ /**
380
+ * Start the given velocity item that has been deployed.
381
+ *
382
+ * @param authentication Credentials for the requests
383
+ * @param templateDictionary Hash of facts: folder id, org URL, adlib replacements
384
+ * @param type The type of velocity item we are constructing a url for
385
+ * @param id? Optional The id of the velocity item we are constructing a url for
386
+ *
387
+ * @returns a promise that will resolve with the result of the start call
388
+ *
389
+ */
390
+ export function start(authentication, templateDictionary, type, id) {
391
+ // /iot/feed/{id}/start/
392
+ // /iot/analytics/realtime/{id}/start/
393
+ return getVelocityUrl(authentication, templateDictionary, type, id, false, "", "start").then(url => {
394
+ return _fetch(authentication, url, "GET").then(result => {
395
+ return Promise.resolve(result);
396
+ });
397
+ });
398
+ }
399
+ /**
400
+ * Gets the required request options for requests to the velocity API.
401
+ *
402
+ * @param authentication Credentials for the requests
403
+ * @param method Indicate if "GET" or "POST"
404
+ *
405
+ * @returns generic request options used for various calls to velocity api
406
+ *
407
+ * @private
408
+ */
409
+ export function _getRequestOpts(authentication, method) {
410
+ return {
411
+ headers: {
412
+ Accept: "application/json",
413
+ "Content-Type": "application/json",
414
+ Authorization: "token=" + authentication.token
415
+ },
416
+ method
417
+ };
418
+ }
419
+ /**
420
+ * Generic fetch function for making calls to the velocity API.
421
+ *
422
+ * @param authentication Credentials for the requests
423
+ * @param url The url from the velocity API to handle reading and writing
424
+ * @param method The method for the request "GET" or "POST"
425
+ * @param body The body for POST requests
426
+ *
427
+ * @returns a promise that will resolve with the result of the fetch call
428
+ *
429
+ * @private
430
+ */
431
+ export function _fetch(authentication, url, method, // GET or POST
432
+ body) {
433
+ const requestOpts = _getRequestOpts(authentication, method);
434
+ /* istanbul ignore else */
435
+ if (body) {
436
+ requestOpts.body = JSON.stringify(body);
437
+ }
438
+ return fetch(url, requestOpts).then(r => Promise.resolve(r.json()));
439
+ }
440
+ /**
441
+ * Remove key properties if the dependency was removed due to having the "IoTFeatureLayer" typeKeyword
442
+ * This function will update the input template.
443
+ *
444
+ * @param template The template that for the velocity item
445
+ *
446
+ */
447
+ export function cleanDataSourcesAndFeeds(template, velocityUrl) {
448
+ const dependencies = template.dependencies;
449
+ [
450
+ getProp(template, "data.sources") ? template.data.sources : [],
451
+ getProp(template, "data.source") ? [template.data.source] : [],
452
+ getProp(template, "data.feeds") ? template.data.feeds : [],
453
+ getProp(template, "data.feed") ? [template.data.feed] : []
454
+ ].forEach(d => _removeIdProps(d, dependencies, velocityUrl));
455
+ [
456
+ getProp(template, "data.outputs") ? template.data.outputs : [],
457
+ getProp(template, "data.output") ? [template.data.output] : []
458
+ ].forEach(outputs => _removeIdPropsAndSetName(outputs, dependencies));
459
+ }
460
+ /**
461
+ * Remove key properties from the input source or feed
462
+ *
463
+ * @param sourcesOrFeeds The list of dataSources or feeds
464
+ * @param dependencies The list of dependencies
465
+ *
466
+ * @private
467
+ */
468
+ export function _removeIdProps(sourcesOrFeeds, dependencies, velocityUrl) {
469
+ sourcesOrFeeds.forEach(dataSource => {
470
+ const idProp = "feature-layer.portalItemId";
471
+ const layerIdProp = "feature-layer.layerId";
472
+ /* istanbul ignore else */
473
+ if (dataSource.properties) {
474
+ /* istanbul ignore else */
475
+ if (dataSource.properties[idProp]) {
476
+ const id = dataSource.properties[idProp];
477
+ /* istanbul ignore else */
478
+ if (id && dependencies.indexOf(id) < 0) {
479
+ delete dataSource.properties[idProp];
480
+ delete dataSource.properties[layerIdProp];
481
+ }
482
+ }
483
+ const urlProp = "simulator.url";
484
+ const url = dataSource.properties[urlProp];
485
+ // only remove velocity based simulator urls
486
+ // otherwise we will leave as is with no templatization
487
+ /* istanbul ignore else */
488
+ if (url && url.indexOf(velocityUrl) > -1) {
489
+ delete dataSource.properties[urlProp];
490
+ }
491
+ }
492
+ });
493
+ }
494
+ /**
495
+ * Remove key properties from the outputs.
496
+ *
497
+ * @param outputs The list of outputs
498
+ * @param dependencies The list of dependencies
499
+ *
500
+ * @private
501
+ */
502
+ export function _removeIdPropsAndSetName(outputs, dependencies) {
503
+ outputs.forEach(output => {
504
+ /* istanbul ignore else */
505
+ if (output.properties) {
506
+ const names = getProp(output, "name") ? [output.name] : BASE_NAMES;
507
+ names.forEach(n => {
508
+ PROP_NAMES.forEach(p => _removeProp(output.properties, n + p, dependencies));
509
+ });
510
+ _updateName(output.properties);
511
+ }
512
+ });
513
+ }
514
+ /**
515
+ * Generic helper function to remove key properties .
516
+ *
517
+ * @param props the list of props to update
518
+ * @param prop the individual prop to remove
519
+ * @param dependencies The list of dependencies
520
+ *
521
+ * @private
522
+ */
523
+ export function _removeProp(props, prop, dependencies) {
524
+ const id = props[prop];
525
+ /* istanbul ignore else */
526
+ if (id && dependencies.indexOf(id) < 0) {
527
+ delete props[prop];
528
+ }
529
+ }
530
+ /**
531
+ * Update the feature layer name to include the solution item id.
532
+ *
533
+ * @param props the list of props to update
534
+ *
535
+ * @private
536
+ */
537
+ export function _updateName(props) {
538
+ [
539
+ "feat-lyr-new.name",
540
+ "stream-lyr-new.name",
541
+ "feat-lyr-existing.name"
542
+ ].forEach(n => {
543
+ const name = props[n];
544
+ /* istanbul ignore else */
545
+ if (name && name.indexOf("{{solutionItemId}}") < 0) {
546
+ props[n] = `${name}_{{solutionItemId}}`;
547
+ }
548
+ });
549
+ }
550
550
  //# sourceMappingURL=velocity-helpers.js.map