-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapediver-cli.ts
More file actions
executable file
·608 lines (598 loc) · 22.6 KB
/
shapediver-cli.ts
File metadata and controls
executable file
·608 lines (598 loc) · 22.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/usr/bin/env node_modules/.bin/ts-node
import { SdPlatformError } from '@shapediver/sdk.platform-api-sdk-v1';
import { SdGeometryError, processError } from '@shapediver/sdk.geometry-api-sdk-v2';
import { runShapeDiverGeoJsonModel } from './src/ShapeDiver/GeometryBackendUtils';
import { NotifyUsersOrganizationFilter } from './src/ShapeDiver/PlatformBackendUtils';
import {
createAndUploadModel,
displayLatestModels,
displayModelAccessData,
displayModelInfoGeometry,
displayModelInfoPlatform,
displayModelsByModelViewUrl,
displayUserCreditUsage,
fetchModelAnalytics,
notifyUsersAboutDecommissioning,
notifyUsersPlatform,
publishModel,
sdTFExample,
sdTFParse,
} from './src/ShapeDiver/Utils';
const yargs = require('yargs');
yargs(process.argv.slice(2))
.command(
'model-access-data',
'Display access data for a model (identified by id, guid, or slug)',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description: 'Model identifier (slug, id, guid)',
type: 'string',
demandOption: true,
},
e: {
alias: 'export',
description: 'Allow exports',
type: 'boolean',
},
b: {
alias: 'backend',
description: 'Backend access (non-browser access)',
type: 'boolean',
},
});
},
async (argv) => {
await displayModelAccessData(argv.i as string, argv.e as boolean, argv.b as boolean);
}
)
.command(
'list-latest-models',
'Query and display latest models',
(yargs) => {
yargs.options({
l: {
alias: 'limit',
description: 'How many models to query',
type: 'number',
},
o: {
alias: 'own',
description: 'Filter models owned by the querying user',
type: 'boolean',
},
});
},
async (argv) => {
await displayLatestModels(
argv.l ? (argv.l as number) : 10,
typeof argv.o === 'boolean' ? (argv.o as boolean) : true
);
}
)
.command(
'list-models-by-backend',
'Query models by backend system and export them',
(yargs) => {
yargs.options({
m: {
alias: 'model-view-url',
description: 'The model view URL to search models by',
type: 'string',
demandOption: true,
},
j: {
alias: 'filename',
description: 'The filename to export to (json)',
type: 'string',
},
});
},
async (argv) => {
await displayModelsByModelViewUrl(argv.m, argv.j);
}
)
.command(
'fetch-model-analytics',
'Fetch analytics for previously exported models',
(yargs) => {
yargs.options({
f: {
alias: 'timestamp-from',
description: 'Timestamp in format YYYY or YYYYMM or YYYYMMDD or YYYYMMDDhh',
type: 'string',
demandOption: true,
},
t: {
alias: 'timestamp-to',
description: 'Timestamp in format YYYY or YYYYMM or YYYYMMDD or YYYYMMDDhh',
type: 'string',
demandOption: true,
},
j: {
alias: 'filename',
description: 'The filename to read and write to (json)',
type: 'string',
},
});
},
async (argv) => {
await fetchModelAnalytics(argv.f, argv.t, argv.j);
}
)
.command(
'notify-of-decommissioning',
'Notify users about decommissioning of models',
(yargs) => {
yargs.options({
j: {
alias: 'filename',
description: 'The filename to read and write to (json)',
type: 'string',
},
});
},
async (argv) => {
await notifyUsersAboutDecommissioning(argv.j);
}
)
.command(
'model-info-platform',
'Get platform backend information of a model',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description: 'Model identifier (slug, id, guid)',
type: 'string',
demandOption: true,
},
});
},
async (argv) => {
await displayModelInfoPlatform(argv.i as string);
}
)
.command(
'model-info-geometry',
'Get geometry backend information of a model',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description: 'Model identifier (slug, id, guid)',
type: 'string',
demandOption: true,
},
});
},
async (argv) => {
await displayModelInfoGeometry(argv.i as string);
}
)
.command(
'upload-model',
'Create and upload model, wait for its confirmation, publish it (private visibility)',
(yargs) => {
yargs.options({
f: {
alias: 'filename',
description: 'Filename of the model to be uploaded',
type: 'string',
demandOption: true,
},
t: {
alias: 'title',
description: 'Title of the model',
type: 'string',
},
});
},
async (argv) => {
await createAndUploadModel(argv.f as string, argv.t as string);
}
)
.command(
'publish-model',
'Publish a model whose checking process resulted in status "pending".',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description: 'Model identifier (slug, id, guid)',
type: 'string',
demandOption: true,
},
});
},
async (argv) => {
await publishModel(argv.i as string);
}
)
.command(
'credit-usage',
'Query per-user credit usage.',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description:
'User identifier (slug, id, username), defaults to authenticated user',
type: 'string',
},
d: {
alias: 'days',
description: 'Number of past days to query, defaults to 31',
type: 'number',
},
f: {
alias: 'from',
description:
'Query from this daystimestamp (YYYYMMDD), use this instead of --days',
type: 'string',
},
t: {
alias: 'to',
description:
'Query to this daystimestamp (YYYYMMDD), use this instead of --days',
type: 'string',
},
});
},
async (argv) => {
await displayUserCreditUsage(
argv.i as string,
argv.d ? (argv.d as number) : 31,
argv.f as string,
argv.t as string
);
}
)
.command(
'sdtf-example',
'Run a computation of a model which has sdTF inputs and outputs.',
(yargs) => {
yargs.options({
i: {
alias: 'id',
description: 'Model identifier (slug, id, guid)',
type: 'string',
demandOption: true,
},
f: {
alias: 'filename',
description: 'Path to the sdTF file to be used',
type: 'string',
},
s: {
alias: 'save',
description: 'Save the resulting sdTF files',
type: 'boolean',
},
});
},
async (argv) => {
await sdTFExample(
argv.i as string,
argv.f as string,
typeof argv.s === 'boolean' ? (argv.s as boolean) : false
);
}
)
.command(
'geojson-example',
'Run a computation of a model which provides a geojson input and output.',
(yargs) => {
yargs.options({
t: {
alias: 'ticket',
description:
'Ticket to be used (embedding ticket when calling from browser, backend ticket when calling from command line)',
type: 'string',
demandOption: true,
},
m: {
alias: 'modelViewUrl',
description: 'Model View URL (API endpoint) to be used',
type: 'string',
demandOption: true,
},
g: {
alias: 'geojson',
description: 'GeoJSON input data',
type: 'string',
demandOption: true,
},
});
},
async (argv) => {
const result = await runShapeDiverGeoJsonModel(
argv.t as string,
argv.m as string,
argv.g as string
);
console.log(`GeoJSON result: ${result}`);
}
)
.command(
'parse-sdtf',
'Parse an sdTF file and prints some information about the contents.',
(yargs) => {
yargs.options({
f: {
alias: 'filename',
description: 'Path to the sdTF file to be parsed',
type: 'string',
demandOption: true,
},
});
},
async (argv) => {
await sdTFParse(argv.f as string);
}
)
.command(
'notify-users',
'Notify our users about updates, maintenance, etc.',
(yargs) => {
yargs.options({
p: {
alias: 'plan-name',
description: 'Name of subscribed chargebee plan (query using LIKE operator).',
type: 'string',
},
pe: {
alias: 'plan-name-exact',
description: 'Exact name of subscribed chargebee plan.',
type: 'string',
},
f: {
alias: 'features-true',
description:
"Filters user based on features of a user. Check where individual feature is true. If array, concat with ','. example: admin,exports,imports ",
type: 'string',
},
n: {
alias: 'features-false',
description:
"Filters user based on features of a user. Check where individual feature is false. If array, concat with ','. example: admin,exports,imports ",
type: 'string',
},
o: {
alias: 'has-organization',
description: `If users should be in organization. Can be one of 'y', 'n' or empty. If 'y' user must be in organization.
If 'n' filters users without organization. Empty does nothing.`,
type: 'string',
},
r: {
alias: 'organization-role',
description: `The organization role. Checks for user in organization role or in roles. If array, concat with ','. example: owner,user `,
type: 'string',
},
dryrun: {
alias: 'dry-run',
description:
'If true (default), notifactions are not created. Just returns and prints list of users which are fetched.',
type: 'boolean',
},
h: {
alias: 'href',
description:
'Link to add to the notification. Can be used to link release notes etc.',
type: 'string',
},
t: {
alias: 'type',
description: 'The notification type.',
type: 'string',
demandOption: true,
},
d: {
alias: 'description',
description: 'The description of the notification.',
type: 'string',
demandOption: true,
},
offset: {
alias: 'offset',
description: `Offset to start querying users at.`,
type: 'string',
},
});
},
async (argv) => {
// handle features
let features_true: Array<string> | string | undefined = undefined;
if (argv.f && (argv.f as string).indexOf(',') > 0) {
features_true = (argv.f as string).split(',');
} else if (argv.f && argv.f != '') {
features_true = argv.f;
}
let features_false: Array<string> | string | undefined = undefined;
if (argv.n && (argv.n as string).indexOf(',') > 0) {
features_false = (argv.n as string).split(',');
} else if (argv.n && argv.n != '') {
features_false = argv.n;
}
// handle organization filter
let organization_filter = NotifyUsersOrganizationFilter.NoFilter;
if (argv.o === 'y') {
organization_filter = NotifyUsersOrganizationFilter.InOrganization;
} else if (argv.o === 'n') {
organization_filter = NotifyUsersOrganizationFilter.NotInOrganization;
}
// handle organization roles
let organization_roles: Array<string> | string | undefined = undefined;
if (argv.r && (argv.r as string).indexOf(',') > 0) {
organization_roles = (argv.r as string).split(',');
} else if (argv.r && argv.r != '') {
organization_roles = argv.r;
}
// handle dry run
const dry_run =
typeof argv.dryrun === 'boolean'
? (argv.dryrun as boolean)
: argv.dryrun === 'false'
? false
: true;
await notifyUsersPlatform(
{
subscribed_plan_name: argv.p,
subscribed_plan_name_exact: argv.pe,
features_of_user_true_value: features_true,
features_of_user_false_value: features_false,
organization_filter,
organization_roles,
dry_run,
offset: argv.offset,
},
{
href: argv.h,
type: argv.t,
description: argv.d,
}
);
}
)
.command('*', '', () => {
console.log('');
console.log('Examples:');
console.log('');
console.log(
'"shapediver-cli.ts model-access-data -i IDENTIFIER" - Get embedding access data for model (IDENTIFIER = slug, id, guid).'
);
console.log(
'"shapediver-cli.ts model-access-data -i IDENTIFIER -b" - Get backend access data for model.'
);
console.log(
'"shapediver-cli.ts model-access-data -i IDENTIFIER -b -e" - Get backend access data for model, allowing exports.'
);
console.log('');
console.log(
'"shapediver-cli.ts list-latest-models" - List 10 latest models owned by the user.'
);
console.log(
'"shapediver-cli.ts list-latest-models --own false" - List 10 latest models which the user has access to.'
);
console.log(
'"shapediver-cli.ts list-latest-models --limit 3" - List 3 latest models howned by the user.'
);
console.log('');
console.log(
'"shapediver-cli.ts model-info-platform -i IDENTIFIER" - Get platform backend information of a model (user, basic properties, domains, tags, decoration).'
);
console.log(
'"shapediver-cli.ts model-info-geometry -i IDENTIFIER" - Get geometry backend information of a model (parameters, outputs, exports).'
);
console.log('');
console.log(
'"shapediver-cli.ts upload-model -f FILENAME -t TITLE" - Create and upload model, wait for checking process, publish model (private visibility).'
);
console.log(
'"shapediver-cli.ts publish-model -i IDENTIFIER" - Publish a model whose checking process resulted in status "pending".'
);
console.log('');
console.log(
'"shapediver-cli.ts credit-usage" - Query credit usage for the past 31 days.'
);
console.log(
'"shapediver-cli.ts credit-usage -d 90" - Query credit usage for the past 90 days.'
);
console.log('"shapediver-cli.ts credit-usage --from 20220901 --to 20220930"');
console.log('');
console.log(
'"shapediver-cli.ts sdtf-example -i IDENTIFIER" - Run a computation of a model which has sdTF inputs and outputs.'
);
console.log('"shapediver-cli.ts sdtf-example -i IDENTIFIER -f SDTF_FILE"');
console.log(
' - Use given sdTF file as input data.'
);
console.log(
'"shapediver-cli.ts sdtf-example -i IDENTIFIER -s" - Save the resulting sdTF files.'
);
console.log('');
console.log(
'"shapediver-cli.ts parse-sdtf -f SDTF_FILE" - Parse an sdTF file and prints some information about the contents'
);
console.log('');
console.log(
'"shapediver-cli.ts notify-users" - Send notifications to users according to the specified filter criteria.'
);
console.log(
' The creator of the notification will be set to "platform".'
);
console.log(
' The class of the notification will be set to "account".'
);
console.log(
' The level of the notification will be set to "info".'
);
console.log(
' -d DESCRIPTION - Description to use for notification.'
);
console.log(
' -t TYPE - Type of notification. Typical types are:'
);
console.log(
' "viewer-update"'
);
console.log(
' "platform-frontend-update"'
);
console.log(
' "platform-backend-update"'
);
console.log(
' "geometry-backend-update"'
);
console.log(
' "plugin-update"'
);
console.log(
' "generic-update"'
);
console.log(' "maintenance"');
console.log(
' -h URL - Optional URL to add to notification.'
);
console.log(
' -p PLAN_NAME - Search users by name of the plan they are subscribed to, case sensitive!'
);
console.log(
" -o 'y' or 'n' - Search users which are (are not) members of an org."
);
console.log(
' -f FEATURES - Search users by features.'
);
console.log(
' -n FEATURES - Search users by features which they do not have.'
);
console.log(
' --dryrun false - Used to disable dry run mode.'
);
console.log(
' --offset OFFSET - Offset to start querying users at.'
);
console.log('');
console.log(' Example: Notify all organization admins and owners:');
console.log(
' "./shapediver-cli.ts notify-users -t TYPE -d DESCRIPTION -o y -r owner,admin"'
);
console.log('');
console.log(' Example: Notify all users subscribed to plans called "%Business%":');
console.log(
' "./shapediver-cli.ts notify-users -t TYPE -d DESCRIPTION -o n -p Business"'
);
console.log('');
})
.fail(async function (_msg, err, _yargs) {
const e = await processError(err);
let prefix: string;
if (e instanceof SdPlatformError) prefix = `Platform Error:`;
else if (e instanceof SdGeometryError) prefix = `Geometry Error:`;
else prefix = 'Generic Error:';
console.error(prefix, e.message);
// console.error('\n', err);
process.exit(1);
}).argv;