-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: runs ls
#640
Merged
Merged
feat: runs ls
#640
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ac49251
feat: runs ls
vladfrangu bacc80e
chore: support terminal sizes of < 100 columns (80columns is min)
vladfrangu f891cce
chore: post rebase cleanup
vladfrangu a1edce6
chore: realign columns
vladfrangu 082ac19
chore: reusable "responsive" table
vladfrangu d04b776
chore: alignment issues
vladfrangu 870779a
Merge branch 'master' into feat/run-ls
vladfrangu 135701e
chore: bump to 0.21, drop python 3.8
vladfrangu d028181
chore: "better" todo comment
vladfrangu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is effectively N+1 problem, which is never a good thing to have in your code. let's try asking on slack if there is some way to get the data with a single API request
it's not a huge deal if there wont be too many items in the list (and we use default limit of 10, so fini'ish as long as the user doesn't try to list last 200 runs...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed on slack, we'll keep this because we can't do much at this time, and implement a spinner in the future