-
Notifications
You must be signed in to change notification settings - Fork 935
add list runs command #1037
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
Closed
add list runs command #1037
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
189564b
add list runs command
hamelsmu dff2308
refactor and add json option
hamelsmu cbb063a
add --as-json && fix stdout
hamelsmu 8cf880f
add cli check
hamelsmu 56c016f
fix black formatting
hamelsmu 9905fe6
get rid of extra arg
hamelsmu 94cfad0
fix black formatting
hamelsmu 58aa9fe
add default behavior
hamelsmu fbba6be
add input validation in cli
hamelsmu 6bb6b4c
use regular echo
hamelsmu fc3acd6
add --quiet flag for tests
hamelsmu e0682b1
add --user and --my-runs flags
hamelsmu 050aa4e
add --namespace flag
hamelsmu 742b080
make user namespace the default, use namespace() filtering, add --sho…
hamelsmu 2363ef5
replace --show-all with --all
hamelsmu 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 hidden or 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 hidden or 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,105 @@ | ||
| from metaflow._vendor import click | ||
| from metaflow import Flow, namespace | ||
| from metaflow import util | ||
| from metaflow.exception import MetaflowNotFound, CommandException | ||
| import json | ||
|
|
||
|
|
||
| @click.group() | ||
| def cli(): | ||
| pass | ||
|
|
||
|
|
||
| @cli.group(help="List objects pertaining to your flow.") | ||
| def list(): | ||
| pass | ||
|
|
||
|
|
||
| def _fetch_runs(flow_name, num_runs): | ||
| counter = 1 | ||
| try: | ||
| flow = Flow(flow_name) | ||
| except MetaflowNotFound: | ||
| flow = None | ||
| run_list = [] | ||
| if flow: | ||
| for run in Flow(flow_name).runs(): | ||
| if counter > num_runs: | ||
| break | ||
| counter += 1 | ||
| run_list.append( | ||
| dict( | ||
| created=str(run.created_at), | ||
| name=flow_name, | ||
| id=run.id, | ||
| status=run.successful, | ||
| finished=run.finished, | ||
| tags=[t for t in run.tags], | ||
| ) | ||
| ) | ||
| return run_list | ||
|
|
||
|
|
||
| @click.option( | ||
| "--num-runs", | ||
| default=10, | ||
| type=click.IntRange(1, None), | ||
| help="Number of runs to show.", | ||
| ) | ||
| @click.option( | ||
| "--as-json", | ||
| default=False, | ||
| is_flag=True, | ||
| help="Print run list as a JSON object", | ||
| ) | ||
| @click.option( | ||
| "--file", | ||
| default=None, | ||
| help="Save the run list to file as json.", | ||
hamelsmu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| @click.option("--user", default=None, help="List runs for the given user.") | ||
| @click.option( | ||
| "--namespace", "ns", default=None, help="List runs only for the given namespace." | ||
| ) | ||
| @click.option( | ||
| "--all", | ||
| default=False, | ||
| is_flag=True, | ||
| help="List runs from the global namespace instead of the current user.", | ||
| ) | ||
| @list.command(help="List recent runs for your flow.") | ||
| @click.pass_context | ||
| def runs(ctx, num_runs, as_json, file, user, all, ns): | ||
| if user and all: | ||
| raise CommandException("--user and --all are mutually exclusive.") | ||
| if user and ns: | ||
| raise CommandException("--user and --namespace are mutually exclusive.") | ||
| if all and ns: | ||
| raise CommandException("--all and --namespace are mutually exclusive.") | ||
|
|
||
| if all: | ||
| namespace(None) | ||
| elif user: | ||
| namespace("user:{}".format(user)) | ||
| elif ns: | ||
| namespace(ns) | ||
| else: | ||
| namespace("user:{}".format(util.get_username())) | ||
|
|
||
| run_list = _fetch_runs(ctx.obj.flow.name, num_runs) | ||
| if not run_list: | ||
| ctx.obj.echo("No runs found for flow: {name}".format(name=ctx.obj.flow.name)) | ||
hamelsmu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
|
|
||
| if file: | ||
| with open(file, "w") as f: | ||
| json.dump(run_list, f) | ||
| if as_json: | ||
| ctx.obj.echo(json.dumps(run_list, indent=4), err=False) | ||
| else: | ||
| for run in run_list: | ||
| ctx.obj.echo( | ||
| "{created} {name} [{id}] (Successful:{status} Finished:{finished})".format( | ||
| **run | ||
| ) | ||
| ) | ||
This file contains hidden or 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 hidden or 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,36 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from metaflow_test import MetaflowTest, assert_equals, steps | ||
|
|
||
|
|
||
| class ListRunsTest(MetaflowTest): | ||
| """ | ||
| Test that tags are assigned properly. | ||
| """ | ||
|
|
||
| PRIORITY = 1 | ||
|
|
||
| @steps(1, ["all"]) | ||
| def step_all(self): | ||
| self.flow_run_id = current.run_id | ||
|
|
||
| def check_results(self, flow, checker): | ||
| if type(checker).__name__ == "CliCheck": | ||
| from metaflow import Flow | ||
|
|
||
| cli_run_list = checker.list_runs() | ||
| assert cli_run_list, "No Runs Returned in CLI for {name}".format( | ||
| name=flow.name | ||
| ) | ||
|
|
||
| latest_run_id = Flow(flow.name).latest_run.data.flow_run_id | ||
| latest_run_found = False | ||
| for run in cli_run_list: | ||
| assert_equals(run["name"], flow.name) | ||
| if run["id"] == latest_run_id: | ||
| latest_run_found = True | ||
|
|
||
| assert ( | ||
| latest_run_found | ||
| ), "Latest run {id} for {name} not listed in cli.".format( | ||
| id=latest_run_id, name=flow.name | ||
| ) |
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.
We need to set the correct namespace here to ensure that the
FloworRuncan be found.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.
@romain-intel perhaps you can shed some light here as I think I am confused b/w what Savin is saying and what you are saying elsewhere?
Happy to have a sync conversation as well around this. Thank you both for your help