@vertexvis/api-client-node 0.17.3 → 0.20.3

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.
@@ -74,9 +74,10 @@ function pollQueuedJob({ id, getQueuedJob, allow404 = false, limit, polling: { i
74
74
  let attempts = 1;
75
75
  let pollRes = yield poll(attempts);
76
76
  /* eslint-disable no-await-in-loop */
77
- while ((allowed404(allow404, pollRes.status) ||
78
- validJob(pollRes.res) ||
79
- isClientError(pollRes.res)) &&
77
+ while (!completeJob(pollRes.res) &&
78
+ (allowed404(allow404, pollRes.status) ||
79
+ validJob(pollRes.res) ||
80
+ isClientError(pollRes.res)) &&
80
81
  attempts <= maxAttempts) {
81
82
  attempts += 1;
82
83
  yield (0, utils_1.delay)(intervalMs);
@@ -85,7 +86,7 @@ function pollQueuedJob({ id, getQueuedJob, allow404 = false, limit, polling: { i
85
86
  /* eslint-enable no-await-in-loop */
86
87
  // At this point, the result is one of the following,
87
88
  // - An item of type `T` after being redirected to it
88
- // - A QueuedJob (after either exceeding `maxAttempts` or with `error` status)
89
+ // - A QueuedJob (after either exceeding `maxAttempts` or with `complete` or `error` status)
89
90
  // - A Failure
90
91
  return Object.assign(Object.assign({}, pollRes), { attempts, id });
91
92
  });
@@ -115,10 +116,16 @@ function allowed404(allow404, status) {
115
116
  function validJob(r) {
116
117
  return (0, utils_1.isQueuedJob)(r) && !isStatusError(r);
117
118
  }
119
+ function completeJob(r) {
120
+ return (0, utils_1.isQueuedJob)(r) && isStatusComplete(r);
121
+ }
118
122
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
119
123
  function isQueuedJobError(obj) {
120
124
  return (0, utils_1.isQueuedJob)(obj) && isStatusError(obj);
121
125
  }
126
+ function isStatusComplete(job) {
127
+ return job.data.attributes.status === 'complete';
128
+ }
122
129
  function isStatusError(job) {
123
130
  return job.data.attributes.status === 'error';
124
131
  }
@@ -17,6 +17,22 @@ export interface CreateSceneAndSceneItemsReq extends BaseReq {
17
17
  /** Whether or not to return queued scene items. */
18
18
  readonly returnQueued?: boolean;
19
19
  }
20
+ export interface CreateSceneAndSceneItemsReqEXPERIMENTAL extends BaseReq {
21
+ /** A list of {@link CreateSceneItemRequest}. */
22
+ readonly createSceneItemReqs: Array<Array<CreateSceneItemRequest>>;
23
+ /** Function returning a {@link CreateSceneRequest}. */
24
+ readonly createSceneReq: () => CreateSceneRequest;
25
+ /** Whether or not to fail if any scene item fails initial validation. */
26
+ readonly failFast?: boolean;
27
+ /** How many requests to run in parallel. */
28
+ readonly parallelism: number;
29
+ /** {@link Polling} */
30
+ readonly polling?: Polling;
31
+ /** Callback with total number of requests and number complete. */
32
+ onProgress?: (complete: number, total: number) => void;
33
+ /** Whether or not to return queued scene items. */
34
+ readonly returnQueued?: boolean;
35
+ }
20
36
  export interface CreateSceneAndSceneItemsRes {
21
37
  readonly errors: QueuedSceneItem[];
22
38
  readonly scene: Scene;
@@ -90,13 +106,13 @@ export declare function createSceneAndSceneItems({ client, createSceneItemReqs,
90
106
  */
91
107
  export declare function createSceneItems({ client, createSceneItemReqs, failFast, onProgress, parallelism, sceneId, }: CreateSceneItemsReq): Promise<CreateSceneItemsRes>;
92
108
  /**
93
- * Create a scene with scene items.
109
+ * Create a scene with scene items using experimental strategy.
94
110
  */
95
- export declare function createSceneAndSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, createSceneReq, failFast, onMsg, onProgress, parallelism, polling, returnQueued, verbose, }: CreateSceneAndSceneItemsReq): Promise<CreateSceneAndSceneItemsResEXPERIMENTAL>;
111
+ export declare function createSceneAndSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, createSceneReq, failFast, onMsg, onProgress, parallelism, polling, returnQueued, verbose, }: CreateSceneAndSceneItemsReqEXPERIMENTAL): Promise<CreateSceneAndSceneItemsResEXPERIMENTAL>;
96
112
  /**
97
113
  * Create scene items within a scene.
98
114
  */
99
- export declare function createSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, failFast, onProgress, parallelism, sceneId, }: CreateSceneItemsReq): Promise<CreateSceneItemsResEXPERIMENTAL>;
115
+ export declare function createSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, failFast, parallelism, sceneId, }: CreateSceneItemsReq): Promise<CreateSceneItemsResEXPERIMENTAL>;
100
116
  /**
101
117
  * Delete all scenes.
102
118
  *
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.updateScene = exports.renderScene = exports.pollSceneReady = exports.deleteAllScenes = exports.createSceneItemsEXPERIMENTAL = exports.createSceneAndSceneItemsEXPERIMENTAL = exports.createSceneItems = exports.createSceneAndSceneItems = void 0;
16
16
  const p_limit_1 = __importDefault(require("p-limit"));
17
+ const process_1 = require("process");
17
18
  const index_1 = require("../../index");
18
19
  const index_2 = require("../index");
19
20
  const utils_1 = require("../utils");
@@ -121,45 +122,93 @@ function createSceneItems({ client, createSceneItemReqs, failFast, onProgress, p
121
122
  }
122
123
  exports.createSceneItems = createSceneItems;
123
124
  /**
124
- * Create a scene with scene items.
125
+ * Create a scene with scene items using experimental strategy.
125
126
  */
126
127
  function createSceneAndSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, createSceneReq, failFast = false, onMsg = console.log, onProgress, parallelism, polling = { intervalMs: index_2.PollIntervalMs, maxAttempts: index_2.MaxAttempts }, returnQueued = false, verbose, }) {
127
128
  return __awaiter(this, void 0, void 0, function* () {
129
+ const startTime = process_1.hrtime.bigint();
130
+ if (verbose)
131
+ onMsg(`Creating scene...`);
128
132
  const scene = (yield client.scenes.createScene({ createSceneRequest: createSceneReq() })).data;
129
133
  const sceneId = scene.data.id;
130
- const createRes = yield createSceneItemsEXPERIMENTAL({
131
- client,
132
- createSceneItemReqs,
133
- failFast,
134
- onProgress,
135
- parallelism,
136
- sceneId,
137
- });
138
- const { a: queuedOps, b: errors } = (0, index_2.partition)(createRes.queuedBatchOps, (i) => (0, index_2.isQueuedJob)(i.res));
139
- const queued = returnQueued ? createRes.queuedBatchOps : [];
140
- // Nothing succeeded, return early as something is likely wrong
141
- if (queuedOps.length === 0 || errors.length === createRes.chunks) {
142
- return { errors, queued, scene, sceneItemErrors: [] };
143
- }
144
134
  if (verbose)
145
- onMsg(`Polling for completed scene-item batches...`);
146
- const limit = (0, p_limit_1.default)(Math.min(parallelism, 20));
147
- function poll({ ops, res, }) {
148
- return __awaiter(this, void 0, void 0, function* () {
149
- const r = yield (0, queued_jobs_1.pollQueuedJob)({
150
- id: res.data.id,
151
- getQueuedJob: (id, cancelToken) => client.batches.getQueuedBatch({ id }, { cancelToken }),
152
- allow404: true,
153
- limit,
154
- polling,
155
- });
156
- if ((0, queued_jobs_1.isPollError)(r.res)) {
157
- failFast ? (0, queued_jobs_1.throwOnError)(r) : errors.push({ ops, res: r.res });
158
- }
159
- return r;
135
+ onMsg(`Creating scene items...`);
136
+ let itemCount = 0;
137
+ let batchQueuedOps = [];
138
+ let batchErrors = [];
139
+ let sceneItemErrors = [];
140
+ for (let depth = 0; depth < createSceneItemReqs.length; depth++) {
141
+ const createItemReqs = createSceneItemReqs[depth];
142
+ itemCount += createItemReqs.length;
143
+ if (verbose)
144
+ onMsg(`Creating ${createItemReqs.length} scene items at depth ${depth}...`);
145
+ // Await is used intentionally to defer loop iteration
146
+ // until all scene items have been created at each depth.
147
+ // eslint-disable-next-line no-await-in-loop
148
+ const createRes = yield createSceneItemsEXPERIMENTAL({
149
+ client,
150
+ createSceneItemReqs: createItemReqs,
151
+ failFast,
152
+ onProgress,
153
+ parallelism,
154
+ sceneId,
160
155
  });
156
+ const { a: queuedOps, b: errors } = (0, index_2.partition)(createRes.queuedBatchOps, (i) => (0, index_2.isQueuedJob)(i.res));
157
+ batchQueuedOps = batchQueuedOps.concat(queuedOps);
158
+ if (errors.length) {
159
+ batchErrors = batchErrors.concat(errors);
160
+ if (verbose)
161
+ onMsg(`WARNING: ${errors.length} scene item batch errors at depth ${depth}.`);
162
+ }
163
+ // Nothing succeeded, return early as something is likely wrong
164
+ if (queuedOps.length === 0 || errors.length === createRes.chunks) {
165
+ return {
166
+ errors,
167
+ queued: returnQueued ? createRes.queuedBatchOps : [],
168
+ scene,
169
+ sceneItemErrors: [],
170
+ };
171
+ }
172
+ const limit = (0, p_limit_1.default)(Math.min(parallelism, 20));
173
+ function poll({ ops, res, }) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ const r = yield (0, queued_jobs_1.pollQueuedJob)({
176
+ id: res.data.id,
177
+ getQueuedJob: (id, cancelToken) => client.batches.getQueuedBatch({ id }, { cancelToken }),
178
+ allow404: true,
179
+ limit,
180
+ polling,
181
+ });
182
+ if ((0, queued_jobs_1.isPollError)(r.res)) {
183
+ failFast ? (0, queued_jobs_1.throwOnError)(r) : errors.push({ ops, res: r.res });
184
+ }
185
+ return r;
186
+ });
187
+ }
188
+ // eslint-disable-next-line no-await-in-loop
189
+ const batchRes = yield Promise.all(queuedOps.map((is) => limit(poll, is)));
190
+ const batchItemErrors = batchRes
191
+ .flatMap((b, i) => (0, queued_jobs_1.isBatch)(b.res)
192
+ ? b.res['vertexvis/batch:results'].map((r, j) => (0, utils_1.isApiError)(r)
193
+ ? { req: queuedOps[i].ops[j].data, res: r }
194
+ : undefined)
195
+ : [])
196
+ .filter(index_2.defined);
197
+ if (batchItemErrors.length) {
198
+ sceneItemErrors = sceneItemErrors.concat(batchItemErrors);
199
+ if (verbose)
200
+ onMsg(`WARNING: ${batchItemErrors.length} scene item creation errors at depth ${depth}.`);
201
+ }
202
+ }
203
+ if (verbose) {
204
+ onMsg(`Scene item creation complete for ${itemCount} scene items with max depth of ${createSceneItemReqs.length - 1}.`);
205
+ if (batchErrors.length) {
206
+ onMsg(` Batch errors: ${batchErrors.length}`);
207
+ }
208
+ if (sceneItemErrors.length) {
209
+ onMsg(` Scene item errors: ${sceneItemErrors.length}`);
210
+ }
161
211
  }
162
- const batchRes = yield Promise.all(queuedOps.map((is) => limit(poll, is)));
163
212
  if (verbose)
164
213
  onMsg(`Committing scene and polling until ready...`);
165
214
  yield updateScene({
@@ -169,24 +218,30 @@ function createSceneAndSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, cre
169
218
  });
170
219
  yield pollSceneReady({ client, id: sceneId, onMsg, polling, verbose });
171
220
  if (verbose)
172
- onMsg(`Fitting scene's camera to scene-items...`);
221
+ onMsg(`Fitting scene's camera to scene items...`);
222
+ const sceneResult = (yield updateScene({
223
+ attributes: {
224
+ camera: { type: index_1.CameraFitTypeEnum.FitVisibleSceneItems },
225
+ },
226
+ client,
227
+ sceneId,
228
+ })).scene;
229
+ if (verbose) {
230
+ const formatTime = (seconds) => {
231
+ const h = Math.floor(seconds / 3600);
232
+ const m = Math.floor((seconds % 3600) / 60);
233
+ const s = Math.round(seconds % 60);
234
+ return [h, m > 9 ? m : h ? '0' + m : m || '0', s > 9 ? s : '0' + s]
235
+ .filter(Boolean)
236
+ .join(':');
237
+ };
238
+ onMsg(`Scene creation completed in ${formatTime(Number(process_1.hrtime.bigint() - startTime) / 1000000000)}.`);
239
+ }
173
240
  return {
174
- errors,
175
- queued,
176
- scene: (yield updateScene({
177
- attributes: {
178
- camera: { type: index_1.CameraFitTypeEnum.FitVisibleSceneItems },
179
- },
180
- client,
181
- sceneId,
182
- })).scene,
183
- sceneItemErrors: batchRes
184
- .flatMap((b, i) => (0, queued_jobs_1.isBatch)(b.res)
185
- ? b.res['vertexvis/batch:results'].map((r, j) => (0, utils_1.isApiError)(r)
186
- ? { req: queuedOps[i].ops[j].data, res: r }
187
- : undefined)
188
- : [])
189
- .filter(index_2.defined),
241
+ errors: batchErrors,
242
+ queued: batchQueuedOps,
243
+ scene: sceneResult,
244
+ sceneItemErrors,
190
245
  };
191
246
  });
192
247
  }
@@ -194,11 +249,10 @@ exports.createSceneAndSceneItemsEXPERIMENTAL = createSceneAndSceneItemsEXPERIMEN
194
249
  /**
195
250
  * Create scene items within a scene.
196
251
  */
197
- function createSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, failFast, onProgress, parallelism, sceneId, }) {
252
+ function createSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, failFast, parallelism, sceneId, }) {
198
253
  return __awaiter(this, void 0, void 0, function* () {
199
254
  const limit = (0, p_limit_1.default)(parallelism);
200
255
  const batchSize = 500;
201
- let complete = 0;
202
256
  const opChunks = (0, utils_1.arrayChunked)(createSceneItemReqs.map((req) => ({
203
257
  data: req.data,
204
258
  op: index_1.BatchOperationOpEnum.Add,
@@ -222,10 +276,6 @@ function createSceneItemsEXPERIMENTAL({ client, createSceneItemReqs, failFast, o
222
276
  else
223
277
  throw error;
224
278
  }
225
- if (onProgress != null) {
226
- complete += opChunk.length;
227
- onProgress(complete, createSceneItemReqs.length);
228
- }
229
279
  return { ops, res };
230
280
  }), opChunk)));
231
281
  return { chunks: opChunks.length, queuedBatchOps };
@@ -1 +1 @@
1
- export declare const version = "0.17.3";
1
+ export declare const version = "0.20.3";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = '0.17.3';
4
+ exports.version = '0.20.3';