Skip to content

Commit

Permalink
feat: runs ls (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu authored Sep 11, 2024
1 parent 4cdef34 commit dd84d37
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
runs-on: ${{ matrix.os }}

steps:
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apify-cli",
"version": "0.20.7",
"version": "0.21.0",
"description": "Apify command-line interface (CLI) helps you manage the Apify cloud platform and develop, build, and deploy Apify Actors.",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -74,7 +74,7 @@
"archiver": "~7.0.1",
"axios": "~1.7.3",
"chalk": "~5.3.0",
"cli-table": "^0.3.11",
"cli-table3": "^0.6.5",
"computer-name": "~0.1.0",
"configparser": "~0.3.10",
"cors": "~2.8.5",
Expand Down Expand Up @@ -113,7 +113,6 @@
"@types/adm-zip": "^0.5.5",
"@types/archiver": "^6.0.2",
"@types/chai": "^4.3.17",
"@types/cli-table": "^0",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/fs-extra": "^11",
Expand Down
55 changes: 17 additions & 38 deletions src/commands/builds/ls.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
import { Flags } from '@oclif/core';
import type { BuildCollectionClientListItem } from 'apify-client';
import chalk from 'chalk';
import Table from 'cli-table';

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 { ResponsiveTable } from '../../lib/commands/responsive-table.js';
import { error, simpleLog } from '../../lib/outputs.js';
import { getLoggedClientOrThrow, objectGroupBy, ShortDurationFormatter } from '../../lib/utils.js';

const tableFactory = (compact = false) => {
const options: Record<string, unknown> = {
head: ['Number', 'ID', 'Status', 'Took'],
style: {
head: ['cyan', 'cyan', 'cyan', 'cyan'],
compact,
},
};

if (compact) {
options.chars = {
'mid': '',
'left-mid': '',
'mid-mid': '',
'right-mid': '',
middle: ' ',
'top-mid': '─',
'bottom-mid': '─',
};
}

return new Table<[string, string, string, string]>(options);
};
const tableFactory = () =>
new ResponsiveTable({
allColumns: ['Number', 'ID', 'Status', 'Took'],
mandatoryColumns: ['Number', 'ID', 'Status', 'Took'],
});

export class BuildLsCommand extends ApifyCommand<typeof BuildLsCommand> {
static override description = 'Lists all builds of the Actor.';
Expand Down Expand Up @@ -132,7 +114,6 @@ export class BuildLsCommand extends ApifyCommand<typeof BuildLsCommand> {
const latestBuildTag = actorInfo.versions.find((v) => v.versionNumber === actorVersion)?.buildTag;
const table = this.generateTableForActorVersion({
buildsForVersion,
compact,
buildTagToActorVersion,
});

Expand All @@ -142,7 +123,7 @@ export class BuildLsCommand extends ApifyCommand<typeof BuildLsCommand> {

const message = [
chalk.reset(`Builds for Actor Version ${chalk.yellow(actorVersion)}${latestBuildTagMessage}`),
table.toString(),
table.render(compact),
'',
];

Expand All @@ -155,15 +136,13 @@ export class BuildLsCommand extends ApifyCommand<typeof BuildLsCommand> {
}

private generateTableForActorVersion({
compact,
buildsForVersion,
buildTagToActorVersion,
}: {
compact: boolean;
buildsForVersion: BuildCollectionClientListItem[];
buildTagToActorVersion: Record<string, string>;
}) {
const table = tableFactory(compact);
const table = tableFactory();

for (const build of buildsForVersion) {
// TODO: untyped field, https://github.com/apify/apify-client-js/issues/526
Expand All @@ -173,23 +152,23 @@ export class BuildLsCommand extends ApifyCommand<typeof BuildLsCommand> {
? ` (${chalk.yellow(buildTagToActorVersion[buildNumber])})`
: '';

const tableRow: [string, string, string, string] = [
`${buildNumber}${hasTag}`,
chalk.gray(build.id),
prettyPrintStatus(build.status),
'',
];
let finishedAt: string;

if (build.finishedAt) {
const diff = build.finishedAt.getTime() - build.startedAt.getTime();

tableRow[3] = chalk.gray(`${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
finishedAt = chalk.gray(`${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
} else {
const diff = Date.now() - build.startedAt.getTime();
tableRow[3] = chalk.gray(`Running for ${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
finishedAt = chalk.gray(`Running for ${ShortDurationFormatter.format(diff, undefined, { left: '' })}`);
}

table.push(tableRow);
table.pushRow({
Number: `${buildNumber}${hasTag}`,
ID: chalk.gray(build.id),
Status: prettyPrintStatus(build.status),
Took: finishedAt,
});
}

return table;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
dependenciesInstalled = true;
} else {
warning({
message: `Python Actors require Python 3.8 or higher, but you have Python ${pythonVersion}!`,
message: `Python Actors require Python 3.9 or higher, but you have Python ${pythonVersion}!`,
});
warning({
message: 'Please install Python 3.8 or higher to be able to run Python Actors locally.',
message: 'Please install Python 3.9 or higher to be able to run Python Actors locally.',
});
}
} else {
warning({
message:
'No Python detected! Please install Python 3.8 or higher to be able to run Python Actors locally.',
'No Python detected! Please install Python 3.9 or higher to be able to run Python Actors locally.',
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ export class RunCommand extends ApifyCommand<typeof RunCommand> {
}
} else {
error({
message: `Python Actors require Python 3.8 or higher, but you have Python ${pythonVersion}!`,
message: `Python Actors require Python 3.9 or higher, but you have Python ${pythonVersion}!`,
});
error({
message: 'Please install Python 3.8 or higher to be able to run Python Actors locally.',
message: 'Please install Python 3.9 or higher to be able to run Python Actors locally.',
});
}
} else {
error({
message:
'No Python detected! Please install Python 3.8 or higher to be able to run Python Actors locally.',
'No Python detected! Please install Python 3.9 or higher to be able to run Python Actors locally.',
});
}
}
Expand Down
135 changes: 135 additions & 0 deletions src/commands/runs/ls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { Flags } from '@oclif/core';
import { Timestamp } from '@sapphire/timestamp';
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 { ResponsiveTable } from '../../lib/commands/responsive-table.js';
import { error, simpleLog } from '../../lib/outputs.js';
import { getLoggedClientOrThrow, ShortDurationFormatter } from '../../lib/utils.js';

const multilineTimestampFormatter = new Timestamp(`YYYY-MM-DD[\n]HH:mm:ss`);

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 = {
actor: Flags.string({
description:
'Optional Actor ID or Name to list runs for. By default, it will use the Actor from the current directory.',
}),
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 enableJsonFlag = true;

async run() {
const { actor, desc, limit, offset, compact, json } = this.flags;

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: actor, client });

if (!ctx.valid) {
error({
message: `${ctx.reason}. Please run this command in an Actor directory, or specify the Actor ID by running this command with "--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));

simpleLog({
message: message.join('\n'),
});

return undefined;
}
}
Loading

0 comments on commit dd84d37

Please sign in to comment.