-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathls.ts
137 lines (112 loc) · 3.93 KB
/
ls.ts
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
import { Args, Flags } from '@oclif/core';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/apify_command.js';
import { prettyPrintStatus } from '../../lib/commands/pretty-print-status.js';
import { resolveActorContext } from '../../lib/commands/resolve-actor-context.js';
import { CompactMode, ResponsiveTable } from '../../lib/commands/responsive-table.js';
import { error, simpleLog } from '../../lib/outputs.js';
import { getLoggedClientOrThrow, MultilineTimestampFormatter, ShortDurationFormatter } from '../../lib/utils.js';
const table = new ResponsiveTable({
allColumns: ['ID', 'Status', 'Results', 'Usage', 'Started At', 'Took', 'Build No.', 'Origin'],
mandatoryColumns: ['ID', 'Status', 'Results', 'Usage', 'Started At', 'Took'],
columnAlignments: {
Results: 'right',
Usage: 'right',
Took: 'right',
'Build No.': 'right',
},
});
export class RunsLsCommand extends ApifyCommand<typeof RunsLsCommand> {
static override description = 'Lists all runs of the Actor.';
static override flags = {
offset: Flags.integer({
description: 'Number of runs that will be skipped.',
default: 0,
}),
limit: Flags.integer({
description: 'Number of runs that will be listed.',
default: 10,
}),
desc: Flags.boolean({
description: 'Sort runs in descending order.',
default: false,
}),
compact: Flags.boolean({
description: 'Display a compact table.',
default: false,
char: 'c',
}),
};
static override args = {
actorId: Args.string({
description:
'Optional Actor ID or Name to list runs for. By default, it will use the Actor from the current directory.',
}),
};
static override enableJsonFlag = true;
async run() {
const { desc, limit, offset, compact, json } = this.flags;
const { actorId } = this.args;
const client = await getLoggedClientOrThrow();
// Should we allow users to list any runs, not just actor-specific runs? Right now it works like `builds ls`, requiring an actor
const ctx = await resolveActorContext({ providedActorNameOrId: actorId, client });
if (!ctx.valid) {
error({
message: `${ctx.reason}. Please run this command in an Actor directory, or specify the Actor ID.`,
});
return;
}
const allRuns = await client.actor(ctx.id).runs().list({ desc, limit, offset });
if (json) {
return allRuns;
}
if (!allRuns.items.length) {
simpleLog({
message: 'There are no recent runs found for this Actor.',
});
return;
}
const message = [
`${chalk.reset('Showing')} ${chalk.yellow(allRuns.items.length)} out of ${chalk.yellow(allRuns.total)} runs for Actor ${chalk.yellow(ctx.userFriendlyId)} (${chalk.gray(ctx.id)})`,
];
const datasetInfos = new Map(
await Promise.all(
allRuns.items.map(async (run) =>
client
.dataset(run.defaultDatasetId)
.get()
.then(
(data) => [run.id, chalk.yellow(data?.itemCount ?? 0)] as const,
() => [run.id, chalk.gray('N/A')] as const,
),
),
),
);
for (const run of allRuns.items) {
let tookString: string;
if (run.finishedAt) {
const diff = run.finishedAt.getTime() - run.startedAt.getTime();
tookString = chalk.gray(`${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
} else {
const diff = Date.now() - run.startedAt.getTime();
tookString = chalk.gray(`Running for ${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
}
table.pushRow({
ID: chalk.gray(run.id),
Status: prettyPrintStatus(run.status),
Results: datasetInfos.get(run.id) || chalk.gray('N/A'),
Usage: chalk.cyan(`$${(run.usageTotalUsd ?? 0).toFixed(3)}`),
'Started At': MultilineTimestampFormatter.display(run.startedAt),
Took: tookString,
'Build No.': run.buildNumber,
Origin: run.meta.origin ?? 'UNKNOWN',
});
}
message.push(table.render(compact ? CompactMode.VeryCompact : CompactMode.WebLikeCompact));
simpleLog({
message: message.join('\n'),
stdout: true,
});
return undefined;
}
}