#4362 Draft: scheduler-logs cli updates
Opened a month ago by mikem. Modified a month ago
mikem/koji scheduler-watch  into  master

file modified
+65 -20
@@ -8411,12 +8411,22 @@ 

                        help="Filter by task ID")

      parser.add_option("--host", type="str", action="store",

                        help="Filter by host (name/ID)")

+     parser.add_option("--channel", type="str", action="store",

+                       help="Filter by task channel (name/ID)")

+     parser.add_option("--arch", type="str", action="store",

+                       help="Filter by task arch")

+     parser.add_option("--method", type="str", action="store",

+                       help="Filter by task method")

+     parser.add_option("--owner", type="str", action="store",

+                       help="Filter by task owner (name/ID)")

      parser.add_option("--from", type="float", action="store", dest="from_ts",

                        help="Logs from given timestamp")

      parser.add_option("--to", type="float", action="store", dest="to_ts",

                        help="Logs until given timestamp (included)")

      parser.add_option("--limit", action="store", type=int, default=None,

                        help="Limit data to last N items. [default: %default]")

+     parser.add_option("--watch", action="store_true",

+                       help="Monitor scheduler logs")

      (options, args) = parser.parse_args(args)

      if len(args) != 0:

          parser.error("There are no arguments for this command")
@@ -8430,39 +8440,45 @@ 

          try:

              host_id = int(options.host)

          except ValueError:

-             host_id = session.getHost(options.host)['id']

+             host_id = session.getHost(options.host, strict=True)['id']

          clauses.append(['host_id', host_id])

+     if options.channel:

+         try:

+             channel_id = int(options.channel)

+         except ValueError:

+             channel_id = session.getChannel(options.channel, strict=True)['id']

+         clauses.append(['channel_id', channel_id])

+     if options.arch:

+         clauses.append(['arch', options.arch])

+     if options.method:

+         clauses.append(['method', options.method])

+     if options.owner:

+         try:

+             owner_id = int(options.owner)

+         except ValueError:

+             owner_id = session.getUser(options.owner, strict=True)['id']

+         clauses.append(['owner', owner_id])

      if options.from_ts:

          clauses.append(['msg_ts', '>=', options.from_ts])

      if options.to_ts:

          clauses.append(['msg_ts', '<', options.to_ts])

+     if options.watch:

+         if session.hub_version < (1, 35, 0):

+             parser.error('This option is not supported with hub version %s' % session.hub_version_str)

+         if options.to_ts:

+             parser.error('The --watch option cannot be used with --to')

  

      fields = ('id', 'task_id', 'host_id', 'host_name', 'msg_ts', 'msg')

      kwargs = {'clauses': clauses, 'fields': fields}

      if session.hub_version < (1, 34, 0):

          error("Hub version is %s and doesn't support scheduler methods "

                "introduced in 1.34." % session.hub_version_str)

-     if options.limit is not None:

-         if session.hub_version >= (1, 34, 1):

-             kwargs['opts'] = {'order': '-id', 'limit': options.limit}

-     logs = session.scheduler.getLogMessages(**kwargs)

  

      if options.limit is not None:

          if session.hub_version >= (1, 34, 1):

-             # server did it for us, but we need to reverse

-             # don't use reversed() as it will be exhausted after modification loop later

-             logs.reverse()

-         else:

-             # emulate limit

-             logs = logs[-options.limit:]

-     if session.hub_version < (1, 34, 1):

-         # emulate order

-         logs.sort(key=lambda r: r['id'])

- 

-     for log in logs:

-         log['time'] = time.asctime(time.localtime(log['msg_ts']))

+             kwargs['opts'] = {'order': '-id', 'limit': options.limit}

  

-     mask = ("%(task_id)-10s %(host_name)-20s %(time)-25s %(msg)-30s")

+     mask = ("%(time)-20s %(task_id)-10s %(host_name)-30s %(msg)-30s")

      if not goptions.quiet:

          h = mask % {

              'task_id': 'Task',
@@ -8473,8 +8489,37 @@ 

          print(h)

          print('-' * len(h))

  

-     for log in logs:

-         print(mask % log)

+     while True:

+         logs = session.scheduler.getLogMessages(**kwargs)

+ 

+         if options.limit is not None:

+             if session.hub_version >= (1, 34, 1):

+                 # server did it for us, but we need to reverse

+                 # don't use reversed() as it will be exhausted after modification loop later

+                 logs.reverse()

+             else:

+                 # emulate limit

+                 logs = logs[-options.limit:]

+         if session.hub_version < (1, 34, 1):

+             # emulate order

+             logs.sort(key=lambda r: r['id'])

+ 

+         for log in logs:

+             log['time'] = koji.formatTime(log['msg_ts'])

+ 

+         for log in logs:

+             print(mask % log)

+ 

+         if not options.watch:

+             break

+         else:

+             if logs:

+                 # update query for next pass

+                 last_id = logs[-1]['id']

+                 options.limit = None

+                 kwargs['opts'] = {'order': 'id'}

+                 kwargs['clauses'] = clauses + [['id', '>', last_id]]

+             time.sleep(goptions.poll_interval)

  

  

  def handle_promote_build(goptions, session, args):

Started out just adding a --watch option, then added a few other filters since the call makes it easy to do so

Tweaked the time display a bit. Still not happy with the way the output looks. It's probably impossible with most instances to get a clean tabular format without being too wide.

Metadata