gnt-job list: Add options for commonly used filters
authorMichael Hanselmann <hansmi@google.com>
Thu, 12 Apr 2012 22:08:20 +0000 (00:08 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 13 Apr 2012 13:20:35 +0000 (15:20 +0200)
While “gnt-job list” would also accept filters on the command line (e.g.
“'status == "error"'”, having shortcuts in the form of options comes in
handy.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/cli.py
lib/client/gnt_job.py

index 256562e..f18e65b 100644 (file)
@@ -2800,7 +2800,7 @@ def _WarnUnknownFields(fdefs):
 
 def GenericList(resource, fields, names, unit, separator, header, cl=None,
                 format_override=None, verbose=False, force_filter=False,
-                namefield=None):
+                namefield=None, qfilter=None):
   """Generic implementation for listing all items of a resource.
 
   @param resource: One of L{constants.QR_VIA_LUXI}
@@ -2826,12 +2826,19 @@ def GenericList(resource, fields, names, unit, separator, header, cl=None,
   @type namefield: string
   @param namefield: Name of field to use for simple filters (see
     L{qlang.MakeFilter} for details)
+  @type qfilter: list or None
+  @param qfilter: Query filter (in addition to names)
 
   """
   if not names:
     names = None
 
-  qfilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
+  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
+
+  if qfilter is None:
+    qfilter = namefilter
+  elif namefilter is not None:
+    qfilter = [qlang.OP_AND, namefilter, qfilter]
 
   if cl is None:
     cl = GetClient()
index 5641a05..7d12ab3 100644 (file)
@@ -79,10 +79,13 @@ def ListJobs(opts, args):
   fmtoverride.update(dict.fromkeys(["opstart", "opexec", "opend"],
     (lambda value: map(FormatTimestamp, value), None)))
 
+  qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
+
   return GenericList(constants.QR_JOB, selected_fields, args, None,
                      opts.separator, not opts.no_headers,
                      format_override=fmtoverride, verbose=opts.verbose,
-                     force_filter=opts.force_filter, namefield="id")
+                     force_filter=opts.force_filter, namefield="id",
+                     qfilter=qfilter)
 
 
 def ListJobFields(opts, args):
@@ -337,10 +340,43 @@ def WatchJob(opts, args):
   return retcode
 
 
+_PENDING_OPT = \
+  cli_option("--pending", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_QUEUED,
+               constants.JOB_STATUS_WAITING,
+               ]),
+             help="Show only jobs pending execution")
+
+_RUNNING_OPT = \
+  cli_option("--running", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_RUNNING,
+               ]),
+             help="Show jobs currently running only")
+
+_ERROR_OPT = \
+  cli_option("--error", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_ERROR,
+               ]),
+             help="Show failed jobs only")
+
+_FINISHED_OPT = \
+  cli_option("--finished", default=None,
+             action="store_const", dest="status_filter",
+             const=constants.JOBS_FINALIZED,
+             help="Show finished jobs only")
+
+
 commands = {
   "list": (
     ListJobs, [ArgJobId()],
-    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
+    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
+     _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT],
     "[job_id ...]",
     "Lists the jobs and their status. The available fields can be shown"
     " using the \"list-fields\" command (see the man page for details)."