Revision 9665bb3a lib/query.py

b/lib/query.py
2133 2133
  return _PrepareFieldList(fields, [])
2134 2134

  
2135 2135

  
2136
def _JobUnavailInner(fn, ctx, (job_id, job)): # pylint: disable=W0613
2137
  """Return L{_FS_UNAVAIL} if job is None.
2138

  
2139
  When listing specifc jobs (e.g. "gnt-job list 1 2 3"), a job may not be
2140
  found, in which case this function converts it to L{_FS_UNAVAIL}.
2141

  
2142
  """
2143
  if job is None:
2144
    return _FS_UNAVAIL
2145
  else:
2146
    return fn(job)
2147

  
2148

  
2149
def _JobUnavail(inner):
2150
  """Wrapper for L{_JobUnavailInner}.
2151

  
2152
  """
2153
  return compat.partial(_JobUnavailInner, inner)
2154

  
2155

  
2156
def _PerJobOpInner(fn, job):
2157
  """Executes a function per opcode in a job.
2158

  
2159
  """
2160
  return map(fn, job.ops)
2161

  
2162

  
2163
def _PerJobOp(fn):
2164
  """Wrapper for L{_PerJobOpInner}.
2165

  
2166
  """
2167
  return _JobUnavail(compat.partial(_PerJobOpInner, fn))
2168

  
2169

  
2170
def _JobTimestampInner(fn, job):
2171
  """Converts unavailable timestamp to L{_FS_UNAVAIL}.
2172

  
2173
  """
2174
  timestamp = fn(job)
2175

  
2176
  if timestamp is None:
2177
    return _FS_UNAVAIL
2178
  else:
2179
    return timestamp
2180

  
2181

  
2182
def _JobTimestamp(fn):
2183
  """Wrapper for L{_JobTimestampInner}.
2184

  
2185
  """
2186
  return _JobUnavail(compat.partial(_JobTimestampInner, fn))
2187

  
2188

  
2189
def _BuildJobFields():
2190
  """Builds list of fields for job queries.
2191

  
2192
  """
2193
  fields = [
2194
    (_MakeField("id", "ID", QFT_TEXT, "Job ID"),
2195
     None, 0, lambda _, (job_id, job): job_id),
2196
    (_MakeField("status", "Status", QFT_TEXT, "Job status"),
2197
     None, 0, _JobUnavail(lambda job: job.CalcStatus())),
2198
    (_MakeField("priority", "Priority", QFT_NUMBER,
2199
                ("Current job priority (%s to %s)" %
2200
                 (constants.OP_PRIO_LOWEST, constants.OP_PRIO_HIGHEST))),
2201
     None, 0, _JobUnavail(lambda job: job.CalcPriority())),
2202
    (_MakeField("ops", "OpCodes", QFT_OTHER, "List of all opcodes"),
2203
     None, 0, _PerJobOp(lambda op: op.input.__getstate__())),
2204
    (_MakeField("opresult", "OpCode_result", QFT_OTHER,
2205
                "List of opcodes results"),
2206
     None, 0, _PerJobOp(operator.attrgetter("result"))),
2207
    (_MakeField("opstatus", "OpCode_status", QFT_OTHER,
2208
                "List of opcodes status"),
2209
     None, 0, _PerJobOp(operator.attrgetter("status"))),
2210
    (_MakeField("oplog", "OpCode_log", QFT_OTHER,
2211
                "List of opcode output logs"),
2212
     None, 0, _PerJobOp(operator.attrgetter("log"))),
2213
    (_MakeField("opstart", "OpCode_start", QFT_OTHER,
2214
                "List of opcode start timestamps (before acquiring locks)"),
2215
     None, 0, _PerJobOp(operator.attrgetter("start_timestamp"))),
2216
    (_MakeField("opexec", "OpCode_exec", QFT_OTHER,
2217
                "List of opcode execution start timestamps (after acquiring"
2218
                " locks)"),
2219
     None, 0, _PerJobOp(operator.attrgetter("exec_timestamp"))),
2220
    (_MakeField("opend", "OpCode_end", QFT_OTHER,
2221
                "List of opcode execution end timestamps"),
2222
     None, 0, _PerJobOp(operator.attrgetter("end_timestamp"))),
2223
    (_MakeField("oppriority", "OpCode_prio", QFT_OTHER,
2224
                "List of opcode priorities"),
2225
     None, 0, _PerJobOp(operator.attrgetter("priority"))),
2226
    (_MakeField("received_ts", "Received", QFT_OTHER,
2227
                "Timestamp of when job was received"),
2228
     None, 0, _JobTimestamp(operator.attrgetter("received_timestamp"))),
2229
    (_MakeField("start_ts", "Start", QFT_OTHER,
2230
                "Timestamp of job start"),
2231
     None, 0, _JobTimestamp(operator.attrgetter("start_timestamp"))),
2232
    (_MakeField("end_ts", "End", QFT_OTHER,
2233
                "Timestamp of job end"),
2234
     None, 0, _JobTimestamp(operator.attrgetter("end_timestamp"))),
2235
    (_MakeField("summary", "Summary", QFT_OTHER,
2236
                "List of per-opcode summaries"),
2237
     None, 0, _PerJobOp(lambda op: op.input.Summary())),
2238
    ]
2239

  
2240
  return _PrepareFieldList(fields, [])
2241

  
2242

  
2136 2243
#: Fields available for node queries
2137 2244
NODE_FIELDS = _BuildNodeFields()
2138 2245

  
......
2148 2255
#: Fields available for operating system queries
2149 2256
OS_FIELDS = _BuildOsFields()
2150 2257

  
2258
#: Fields available for job queries
2259
JOB_FIELDS = _BuildJobFields()
2260

  
2151 2261
#: All available resources
2152 2262
ALL_FIELDS = {
2153 2263
  constants.QR_INSTANCE: INSTANCE_FIELDS,
......
2155 2265
  constants.QR_LOCK: LOCK_FIELDS,
2156 2266
  constants.QR_GROUP: GROUP_FIELDS,
2157 2267
  constants.QR_OS: OS_FIELDS,
2268
  constants.QR_JOB: JOB_FIELDS,
2158 2269
  }
2159 2270

  
2160 2271
#: All available field lists

Also available in: Unified diff