@get-dx/cli 0.3.0 → 0.3.1

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/dist/cli.js CHANGED
@@ -3,6 +3,7 @@ import { authCommand } from "./commands/auth.js";
3
3
  import { catalogCommand } from "./commands/catalog.js";
4
4
  import { initCommand } from "./commands/init.js";
5
5
  import { scorecardsCommand } from "./commands/scorecards.js";
6
+ import { snapshotsCommand } from "./commands/snapshots.js";
6
7
  import { studioCommand } from "./commands/studio.js";
7
8
  import { teamsCommand } from "./commands/teams.js";
8
9
  import { workflowsCommand } from "./commands/workflows.js";
@@ -36,6 +37,7 @@ function createProgram() {
36
37
  program.addCommand(catalogCommand());
37
38
  program.addCommand(initCommand());
38
39
  program.addCommand(scorecardsCommand());
40
+ program.addCommand(snapshotsCommand());
39
41
  program.addCommand(studioCommand());
40
42
  program.addCommand(teamsCommand());
41
43
  program.addCommand(workflowsCommand());
@@ -0,0 +1,379 @@
1
+ import { Command } from "commander";
2
+ import { createExampleText, getContext, parsePositiveIntOption, wrapAction, } from "../commandHelpers.js";
3
+ import { CliError, EXIT_CODES } from "../errors.js";
4
+ import { request } from "../http.js";
5
+ import { renderJson, renderRichText } from "../renderers.js";
6
+ import { buildRuntime } from "../runtime.js";
7
+ import * as ui from "../ui.js";
8
+ export function snapshotsCommand() {
9
+ const snapshots = new Command()
10
+ .name("snapshots")
11
+ .description("Work with DX snapshots");
12
+ const csatComments = new Command()
13
+ .name("csatComments")
14
+ .description("Work with snapshot CSAT comments");
15
+ csatComments
16
+ .command("list")
17
+ .description("List CSAT comments for a snapshot")
18
+ .option("--id <id>", "The unique ID of the snapshot")
19
+ .option("--cursor <cursor>", "Cursor for the next page of results")
20
+ .option("--limit <n>", "Max comments per page (default is 50, max is 100)", (value) => parseLimitOption(value, "--limit"))
21
+ .addHelpText("afterAll", createExampleText([
22
+ {
23
+ label: "List CSAT comments for a snapshot",
24
+ command: "dx snapshots csatComments list --id MjUyNbaY",
25
+ },
26
+ {
27
+ label: "List CSAT comments as JSON",
28
+ command: "dx --json snapshots csatComments list --id MjUyNbaY",
29
+ },
30
+ {
31
+ label: "Fetch the next page using a cursor from the prior response",
32
+ command: "dx snapshots csatComments list --id MjUyNbaY --cursor xuvkgfq9t0ty --limit 100",
33
+ },
34
+ ]))
35
+ .action(wrapAction(async (options, command) => {
36
+ const id = parseRequiredTextOption(options.id, "--id");
37
+ const runtime = buildRuntime(getContext(command));
38
+ const response = await listSnapshotCsatComments(runtime, {
39
+ id,
40
+ cursor: options.cursor,
41
+ limit: options.limit,
42
+ });
43
+ if (runtime.context.json) {
44
+ renderJson(response);
45
+ }
46
+ else {
47
+ renderSnapshotCsatComments(extractCsatComments(response), response.response_metadata?.next_cursor ?? null, id, runtime);
48
+ }
49
+ }));
50
+ snapshots.addCommand(csatComments);
51
+ const driverComments = new Command()
52
+ .name("driverComments")
53
+ .description("Work with snapshot driver comments");
54
+ driverComments
55
+ .command("list")
56
+ .description("List driver comments for a snapshot")
57
+ .option("--id <id>", "The unique ID of the snapshot")
58
+ .option("--cursor <cursor>", "Cursor for the next page of results")
59
+ .option("--limit <n>", "Max comments per page (default is 50, max is 100)", (value) => parseLimitOption(value, "--limit"))
60
+ .addHelpText("afterAll", createExampleText([
61
+ {
62
+ label: "List driver comments for a snapshot",
63
+ command: "dx snapshots driverComments list --id MjUyNbaY",
64
+ },
65
+ {
66
+ label: "List driver comments as JSON",
67
+ command: "dx --json snapshots driverComments list --id MjUyNbaY",
68
+ },
69
+ {
70
+ label: "Fetch the next page using a cursor from the prior response",
71
+ command: "dx snapshots driverComments list --id MjUyNbaY --cursor xuvkgfq9t0ty --limit 100",
72
+ },
73
+ ]))
74
+ .action(wrapAction(async (options, command) => {
75
+ const id = parseRequiredTextOption(options.id, "--id");
76
+ const runtime = buildRuntime(getContext(command));
77
+ const response = await listSnapshotDriverComments(runtime, {
78
+ id,
79
+ cursor: options.cursor,
80
+ limit: options.limit,
81
+ });
82
+ if (runtime.context.json) {
83
+ renderJson(response);
84
+ }
85
+ else {
86
+ renderSnapshotDriverComments(extractDriverComments(response), response.response_metadata?.next_cursor ?? null, id, runtime);
87
+ }
88
+ }));
89
+ snapshots.addCommand(driverComments);
90
+ snapshots
91
+ .command("info")
92
+ .description("Retrieve results for a single snapshot")
93
+ .option("--id <snapshot_id>", "The unique ID of the snapshot")
94
+ .addHelpText("afterAll", createExampleText([
95
+ {
96
+ label: "Fetch info for a snapshot",
97
+ command: "dx snapshots info --id MjUyNbaY",
98
+ },
99
+ {
100
+ label: "Fetch snapshot info as JSON",
101
+ command: "dx --json snapshots info --id MjUyNbaY",
102
+ },
103
+ ]))
104
+ .action(wrapAction(async (options, command) => {
105
+ const snapshotId = parseRequiredTextOption(options.id, "--id");
106
+ const runtime = buildRuntime(getContext(command));
107
+ const response = await getSnapshotInfo(runtime, snapshotId);
108
+ if (runtime.context.json) {
109
+ renderJson(response);
110
+ }
111
+ else {
112
+ renderSnapshotInfo(response.snapshot, snapshotId, runtime);
113
+ }
114
+ }));
115
+ snapshots
116
+ .command("list")
117
+ .description("List DX snapshots")
118
+ .addHelpText("afterAll", createExampleText([
119
+ {
120
+ label: "List snapshots",
121
+ command: "dx snapshots list",
122
+ },
123
+ {
124
+ label: "List snapshots as JSON",
125
+ command: "dx --json snapshots list",
126
+ },
127
+ ]))
128
+ .action(wrapAction(async (_options, command) => {
129
+ const runtime = buildRuntime(getContext(command));
130
+ const response = await listSnapshots(runtime);
131
+ if (runtime.context.json) {
132
+ renderJson(response);
133
+ }
134
+ else {
135
+ renderSnapshots(response.snapshots, runtime);
136
+ }
137
+ }));
138
+ return snapshots;
139
+ }
140
+ async function listSnapshotDriverComments(runtime, options) {
141
+ const response = await request(runtime, "/snapshots.driverComments.list", {
142
+ method: "GET",
143
+ query: {
144
+ id: options.id,
145
+ cursor: options.cursor,
146
+ limit: options.limit,
147
+ },
148
+ });
149
+ return response.body;
150
+ }
151
+ async function listSnapshotCsatComments(runtime, options) {
152
+ const response = await request(runtime, "/snapshots.csatComments.list", {
153
+ method: "GET",
154
+ query: {
155
+ id: options.id,
156
+ cursor: options.cursor,
157
+ limit: options.limit,
158
+ },
159
+ });
160
+ return response.body;
161
+ }
162
+ async function getSnapshotInfo(runtime, snapshotId) {
163
+ const response = await request(runtime, "/snapshots.info", {
164
+ method: "GET",
165
+ query: { snapshot_id: snapshotId },
166
+ });
167
+ return response.body;
168
+ }
169
+ async function listSnapshots(runtime) {
170
+ const response = await request(runtime, "/snapshots.list", {
171
+ method: "GET",
172
+ query: {
173
+ ordering: "completed_at",
174
+ },
175
+ });
176
+ return response.body;
177
+ }
178
+ function renderSnapshotDriverComments(comments, nextCursor, snapshotId, runtime) {
179
+ const blocks = [ui.h1("Snapshot Driver Comments")];
180
+ blocks.push(ui.p(`Displaying ${ui.bold(comments.length.toString())} comments.`));
181
+ blocks.push(ui.p(ui.link(snapshotWebLink(snapshotId, "drivers/comments", runtime))));
182
+ if (nextCursor) {
183
+ blocks.push(ui.p(`Next cursor: ${ui.code(nextCursor)}`));
184
+ }
185
+ if (comments.length === 0) {
186
+ blocks.push(ui.p(ui.dim("(None)")));
187
+ renderRichText(blocks);
188
+ return;
189
+ }
190
+ for (const comment of comments) {
191
+ blocks.push(ui.h2(formatCommentHeading(comment)));
192
+ const details = [
193
+ optionalDetail("Email", formatText(comment.email), comment.email),
194
+ optionalDetail("Driver", formatText(comment.driver_name ?? comment.driver), comment.driver_name ?? comment.driver),
195
+ optionalDetail("Driver ID", formatText(comment.driver_id), comment.driver_id),
196
+ optionalDetail("Team", formatText(comment.team_name), comment.team_name),
197
+ optionalDetail("Team ID", formatText(comment.team_id), comment.team_id),
198
+ optionalDetail("Item", formatText(comment.item_name), comment.item_name),
199
+ optionalDetail("Item ID", formatText(comment.item_id), comment.item_id),
200
+ optionalDetail("Snapshot ID", formatText(comment.snapshot_id), comment.snapshot_id),
201
+ optionalDetail("Timestamp", formatTimestamp(comment.timestamp), comment.timestamp),
202
+ ].filter((detail) => detail !== null);
203
+ if (details.length > 0) {
204
+ blocks.push(ui.dl(details, { termWidth: 11 }));
205
+ }
206
+ blocks.push(ui.p(formatText(comment.text ?? comment.comment)));
207
+ }
208
+ renderRichText(blocks);
209
+ }
210
+ function renderSnapshotCsatComments(comments, nextCursor, snapshotId, runtime) {
211
+ const blocks = [ui.h1("Snapshot CSAT Comments")];
212
+ blocks.push(ui.p(`Displaying ${ui.bold(comments.length.toString())} comments.`));
213
+ blocks.push(ui.p(ui.link(snapshotWebLink(snapshotId, "csat/comments", runtime))));
214
+ if (nextCursor) {
215
+ blocks.push(ui.p(`Next cursor: ${ui.code(nextCursor)}`));
216
+ }
217
+ if (comments.length === 0) {
218
+ blocks.push(ui.p(ui.dim("(None)")));
219
+ renderRichText(blocks);
220
+ return;
221
+ }
222
+ for (const comment of comments) {
223
+ blocks.push(ui.h2(formatCommentHeading(comment)));
224
+ const details = [
225
+ optionalDetail("Email", formatText(comment.email), comment.email),
226
+ optionalDetail("Team", formatText(comment.team_name), comment.team_name),
227
+ optionalDetail("Team ID", formatText(comment.team_id), comment.team_id),
228
+ optionalDetail("Item", formatText(comment.item_name), comment.item_name),
229
+ optionalDetail("Item ID", formatText(comment.item_id), comment.item_id),
230
+ optionalDetail("Snapshot ID", formatText(comment.snapshot_id), comment.snapshot_id),
231
+ optionalDetail("Timestamp", formatTimestamp(comment.timestamp), comment.timestamp),
232
+ ].filter((detail) => detail !== null);
233
+ if (details.length > 0) {
234
+ blocks.push(ui.dl(details, { termWidth: 11 }));
235
+ }
236
+ blocks.push(ui.p(formatText(comment.text ?? comment.comment)));
237
+ }
238
+ renderRichText(blocks);
239
+ }
240
+ function renderSnapshotInfo(snapshot, snapshotId, runtime) {
241
+ const teamScores = snapshot.team_scores ?? [];
242
+ const blocks = [ui.h1("Snapshot Information")];
243
+ blocks.push(ui.p(`Displaying ${ui.bold(teamScores.length.toString())} team scores.`));
244
+ blocks.push(ui.p(ui.link(snapshotWebLink(snapshotId, "drivers", runtime))));
245
+ if (teamScores.length === 0) {
246
+ blocks.push(ui.p(ui.dim("(None)")));
247
+ renderRichText(blocks);
248
+ return;
249
+ }
250
+ for (const teamScore of teamScores) {
251
+ blocks.push(ui.h2("Team Score"));
252
+ blocks.push(ui.dl([
253
+ optionalDetail("Team name", formatText(teamScore.snapshot_team?.name), teamScore.snapshot_team?.name),
254
+ optionalDetail("Team ID", formatText(teamScore.snapshot_team?.team_id), teamScore.snapshot_team?.team_id),
255
+ optionalDetail("item_name", formatText(teamScore.item_name), teamScore.item_name),
256
+ optionalDetail("Response count", formatNumber(teamScore.response_count), teamScore.response_count),
257
+ optionalDetail("Score", formatNumber(teamScore.score), teamScore.score),
258
+ ].filter((detail) => detail !== null), { termWidth: 16 }));
259
+ blocks.push(ui.h3("Benchmarks"));
260
+ blocks.push(ui.p(formatBenchmarkTable(teamScore)));
261
+ }
262
+ renderRichText(blocks);
263
+ }
264
+ function renderSnapshots(snapshots, runtime) {
265
+ const blocks = [ui.h1("Snapshots")];
266
+ blocks.push(ui.p(`Displaying ${ui.bold(snapshots.length.toString())} snapshots.`));
267
+ if (snapshots.length === 0) {
268
+ blocks.push(ui.p(ui.dim("(None)")));
269
+ renderRichText(blocks);
270
+ return;
271
+ }
272
+ for (const snapshot of snapshots) {
273
+ blocks.push(ui.h2(ui.code(snapshot.id)));
274
+ blocks.push(ui.dl([
275
+ optionalDetail("Account ID", formatText(snapshot.account_id), snapshot.account_id),
276
+ optionalDetail("Scheduled for", formatText(snapshot.scheduled_for), snapshot.scheduled_for),
277
+ optionalDetail("Completed", formatTimestamp(snapshot.completed_at), snapshot.completed_at),
278
+ optionalDetail("Completed count", formatNumber(snapshot.completed_count), snapshot.completed_count),
279
+ optionalDetail("Total count", formatNumber(snapshot.total_count), snapshot.total_count),
280
+ optionalDetail("Last result change", formatTimestamp(snapshot.last_result_change_at), snapshot.last_result_change_at),
281
+ optionalDetail("Deleted", formatTimestamp(snapshot.deleted_at), snapshot.deleted_at),
282
+ ui.dli("Web link", ui.link(snapshotWebLink(snapshot.id, "drivers", runtime))),
283
+ ].filter((detail) => detail !== null), { termWidth: 18 }));
284
+ }
285
+ renderRichText(blocks);
286
+ }
287
+ function extractDriverComments(response) {
288
+ return (response.driver_comments ??
289
+ response.driverComments ??
290
+ response.drivercomments ??
291
+ response.comments ??
292
+ []);
293
+ }
294
+ function extractCsatComments(response) {
295
+ return (response.csat_comments ??
296
+ response.csatComments ??
297
+ response.csatcomments ??
298
+ response.comments ??
299
+ []);
300
+ }
301
+ function formatCommentHeading(comment) {
302
+ const heading = comment.item_name ??
303
+ comment.driver_name ??
304
+ comment.driver ??
305
+ comment.email ??
306
+ comment.item_id ??
307
+ comment.driver_id;
308
+ return typeof heading === "string" && heading.trim().length > 0
309
+ ? heading
310
+ : "Comment";
311
+ }
312
+ function formatText(value) {
313
+ return typeof value === "string" && value.trim().length > 0
314
+ ? value
315
+ : ui.dim("(None)");
316
+ }
317
+ function formatNumber(value) {
318
+ return typeof value === "number" && Number.isFinite(value)
319
+ ? value.toString()
320
+ : ui.dim("(None)");
321
+ }
322
+ function formatTimestamp(value) {
323
+ if (typeof value !== "string" || value.trim().length === 0) {
324
+ return "";
325
+ }
326
+ const trimmed = value.trim();
327
+ const seconds = Number(trimmed);
328
+ if (Number.isFinite(seconds) && seconds > 0) {
329
+ return ui.timestampSummary(new Date(seconds * 1000).toISOString());
330
+ }
331
+ return Number.isNaN(Date.parse(trimmed))
332
+ ? trimmed
333
+ : ui.timestampSummary(trimmed);
334
+ }
335
+ function optionalDetail(term, value, rawValue) {
336
+ if (rawValue === undefined || rawValue === null) {
337
+ return null;
338
+ }
339
+ if (typeof rawValue === "string" && rawValue.trim().length === 0) {
340
+ return null;
341
+ }
342
+ return ui.dli(term, value);
343
+ }
344
+ function formatBenchmarkTable(teamScore) {
345
+ const rows = [
346
+ ["vs_prev", formatBenchmarkNumber(teamScore.vs_prev)],
347
+ ["vs_org", formatBenchmarkNumber(teamScore.vs_org)],
348
+ ["vs_50th", formatBenchmarkNumber(teamScore.vs_50th)],
349
+ ["vs_75th", formatBenchmarkNumber(teamScore.vs_75th)],
350
+ ["vs_90th", formatBenchmarkNumber(teamScore.vs_90th)],
351
+ ];
352
+ const columns = ["Benchmark", "Value"];
353
+ const widths = columns.map((column, index) => Math.max(column.length, ...rows.map((row) => row[index].length)));
354
+ const border = `+${widths.map((width) => "-".repeat(width + 2)).join("+")}+`;
355
+ const header = `| ${columns.map((column, index) => column.padEnd(widths[index])).join(" | ")} |`;
356
+ const body = rows.map((row) => `| ${row.map((cell, index) => cell.padEnd(widths[index])).join(" | ")} |`);
357
+ return [border, header, border, ...body, border].join("\n");
358
+ }
359
+ function formatBenchmarkNumber(value) {
360
+ return typeof value === "number" && Number.isFinite(value)
361
+ ? value.toString()
362
+ : "(None)";
363
+ }
364
+ function snapshotWebLink(snapshotId, section, runtime) {
365
+ return ui.webLink(`/snapshots/${snapshotId}/${section}`, runtime);
366
+ }
367
+ function parseRequiredTextOption(value, flag) {
368
+ if (typeof value !== "string" || value.trim().length === 0) {
369
+ throw new CliError(`${flag} is required`, EXIT_CODES.ARGUMENT_ERROR);
370
+ }
371
+ return value.trim();
372
+ }
373
+ function parseLimitOption(value, flag) {
374
+ const limit = parsePositiveIntOption(value, flag);
375
+ if (limit > 100) {
376
+ throw new CliError(`${flag} must be at most 100`, EXIT_CODES.ARGUMENT_ERROR);
377
+ }
378
+ return limit;
379
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@get-dx/cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "DX CLI",
5
5
  "author": "DX <developers@getdx.com>",
6
6
  "bugs": {