|
| 1 | +import argparse |
| 2 | +from dataclasses import asdict |
| 3 | + |
| 4 | +from dstack._internal.cli.commands import APIBaseCommand |
| 5 | +from dstack._internal.cli.services.events import EventListFilters, EventPaginator, print_event |
| 6 | +from dstack._internal.cli.utils.common import ( |
| 7 | + get_start_time, |
| 8 | +) |
| 9 | +from dstack._internal.core.models.events import EventTargetType |
| 10 | +from dstack._internal.server.schemas.events import LIST_EVENTS_DEFAULT_LIMIT |
| 11 | +from dstack.api import Client |
| 12 | + |
| 13 | + |
| 14 | +class EventCommand(APIBaseCommand): |
| 15 | + NAME = "event" |
| 16 | + DESCRIPTION = "View events" |
| 17 | + |
| 18 | + def _register(self): |
| 19 | + super()._register() |
| 20 | + self._parser.set_defaults(subfunc=self._list) |
| 21 | + subparsers = self._parser.add_subparsers(dest="action") |
| 22 | + |
| 23 | + list_parser = subparsers.add_parser( |
| 24 | + "list", |
| 25 | + help="List events within the selected project", |
| 26 | + formatter_class=self._parser.formatter_class, |
| 27 | + ) |
| 28 | + list_parser.set_defaults(subfunc=self._list) |
| 29 | + |
| 30 | + for parser in [self._parser, list_parser]: |
| 31 | + parser.add_argument( |
| 32 | + "--since", |
| 33 | + help=( |
| 34 | + "Only show events newer than the specified date." |
| 35 | + " Can be a duration (e.g. 10s, 5m, 1d) or an RFC 3339 string (e.g. 2023-09-24T15:30:00Z)." |
| 36 | + f" If not specified, show the last {LIST_EVENTS_DEFAULT_LIMIT} events." |
| 37 | + ), |
| 38 | + type=str, |
| 39 | + ) |
| 40 | + target_filters_group = parser.add_mutually_exclusive_group() |
| 41 | + target_filters_group.add_argument( |
| 42 | + "--target-fleet", |
| 43 | + action="append", |
| 44 | + metavar="NAME", |
| 45 | + dest="target_fleets", |
| 46 | + help="Only show events that target the specified fleets", |
| 47 | + ) |
| 48 | + target_filters_group.add_argument( |
| 49 | + "--target-run", |
| 50 | + action="append", |
| 51 | + metavar="NAME", |
| 52 | + dest="target_runs", |
| 53 | + help="Only show events that target the specified runs", |
| 54 | + ) |
| 55 | + within_filters_group = parser.add_mutually_exclusive_group() |
| 56 | + within_filters_group.add_argument( |
| 57 | + "--within-fleet", |
| 58 | + action="append", |
| 59 | + metavar="NAME", |
| 60 | + dest="within_fleets", |
| 61 | + help="Only show events that target the specified fleets or instances within those fleets", |
| 62 | + ) |
| 63 | + within_filters_group.add_argument( |
| 64 | + "--within-run", |
| 65 | + action="append", |
| 66 | + metavar="NAME", |
| 67 | + dest="within_runs", |
| 68 | + help="Only show events that target the specified runs or jobs within those runs", |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + "--include-target-type", |
| 72 | + action="append", |
| 73 | + metavar="TYPE", |
| 74 | + type=EventTargetType, |
| 75 | + dest="include_target_types", |
| 76 | + help="Only show events that target entities of the specified types", |
| 77 | + ) |
| 78 | + |
| 79 | + def _command(self, args: argparse.Namespace): |
| 80 | + super()._command(args) |
| 81 | + args.subfunc(args) |
| 82 | + |
| 83 | + def _list(self, args: argparse.Namespace): |
| 84 | + since = get_start_time(args.since) |
| 85 | + filters = _build_filters(args, self.api) |
| 86 | + |
| 87 | + if since is not None: |
| 88 | + events = EventPaginator(self.api.client.events).list( |
| 89 | + filters=filters, since=since, ascending=True |
| 90 | + ) |
| 91 | + else: |
| 92 | + events = reversed(self.api.client.events.list(ascending=False, **asdict(filters))) |
| 93 | + try: |
| 94 | + for event in events: |
| 95 | + print_event(event) |
| 96 | + except KeyboardInterrupt: |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | +def _build_filters(args: argparse.Namespace, api: Client) -> EventListFilters: |
| 101 | + filters = EventListFilters() |
| 102 | + |
| 103 | + if args.target_fleets: |
| 104 | + filters.target_fleets = [ |
| 105 | + api.client.fleets.get(api.project, name).id for name in args.target_fleets |
| 106 | + ] |
| 107 | + elif args.target_runs: |
| 108 | + filters.target_runs = [ |
| 109 | + api.client.runs.get(api.project, name).id for name in args.target_runs |
| 110 | + ] |
| 111 | + |
| 112 | + if args.within_fleets: |
| 113 | + filters.within_fleets = [ |
| 114 | + api.client.fleets.get(api.project, name).id for name in args.within_fleets |
| 115 | + ] |
| 116 | + elif args.within_runs: |
| 117 | + filters.within_runs = [ |
| 118 | + api.client.runs.get(api.project, name).id for name in args.within_runs |
| 119 | + ] |
| 120 | + else: |
| 121 | + filters.within_projects = [api.client.projects.get(api.project).project_id] |
| 122 | + |
| 123 | + if args.include_target_types: |
| 124 | + filters.include_target_types = args.include_target_types |
| 125 | + |
| 126 | + return filters |
0 commit comments