Skip to content

Commit 636e5ee

Browse files
[UX] Support dstack ps -n NUM
1 parent 0c391d8 commit 636e5ee

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/dstack/_internal/cli/commands/ps.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ def _register(self):
3636
help="Watch statuses of runs in realtime",
3737
action="store_true",
3838
)
39+
self._parser.add_argument(
40+
"-n",
41+
"--last",
42+
help="Show only the last N runs. Implies --all",
43+
type=int,
44+
default=None,
45+
)
3946

4047
def _command(self, args: argparse.Namespace):
4148
super()._command(args)
42-
runs = self.api.runs.list(all=args.all)
49+
runs = self.api.runs.list(all=args.all, limit=args.last)
4350
if not args.watch:
4451
console.print(run_utils.get_runs_table(runs, verbose=args.verbose))
4552
return
@@ -49,6 +56,6 @@ def _command(self, args: argparse.Namespace):
4956
while True:
5057
live.update(run_utils.get_runs_table(runs, verbose=args.verbose))
5158
time.sleep(LIVE_TABLE_PROVISION_INTERVAL_SECS)
52-
runs = self.api.runs.list(all=args.all)
59+
runs = self.api.runs.list(all=args.all, limit=args.last)
5360
except KeyboardInterrupt:
5461
pass

src/dstack/api/_public/runs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,29 +691,32 @@ def exec_plan(
691691
logger.warning("The exec_plan() method is deprecated in favor of apply_plan().")
692692
return self.apply_plan(run_plan=run_plan, repo=repo, reserve_ports=reserve_ports)
693693

694-
def list(self, all: bool = False) -> List[Run]:
694+
def list(self, all: bool = False, limit: Optional[int] = None) -> List[Run]:
695695
"""
696696
List runs.
697697
698698
Args:
699699
all: Show all runs (active and finished) if `True`.
700+
limit: Limit the number of runs to return. Must be less than 100.
700701
701702
Returns:
702703
List of runs.
703704
"""
704705
# Return only one page of latest runs (<=100). Returning all the pages may be costly.
705706
# TODO: Consider introducing `since` filter with a reasonable default.
706-
only_active = not all
707+
only_active = not all and limit is None
707708
runs = self._api_client.runs.list(
708709
project_name=self._project,
709710
repo_id=None,
710711
only_active=only_active,
712+
limit=limit or 100,
711713
)
712714
if only_active and len(runs) == 0:
713715
runs = self._api_client.runs.list(
714716
project_name=self._project,
715717
repo_id=None,
716-
)[:1]
718+
limit=1,
719+
)
717720
return [self._model_to_run(run) for run in runs]
718721

719722
def get(self, run_name: str) -> Optional[Run]:

0 commit comments

Comments
 (0)