The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

258 lines
12 KiB

  1. #!/usr/bin/env python3
  2. import argparse
  3. import datetime
  4. import itertools
  5. import json
  6. import math
  7. import os
  8. import re
  9. import sys
  10. import time
  11. import urllib.request
  12. # Column definitions
  13. columns = {
  14. 'jobid': (lambda job, pipelines: job["job_data"]["ident"], ()),
  15. 'url': (lambda job, pipelines: job["job_data"]["url"], ('truncatable',)),
  16. 'user': (lambda job, pipelines: job["job_data"]["started_by"], ()),
  17. 'pipenick': (lambda job, pipelines: pipelines[job["job_data"]["pipeline_id"]] if job["job_data"]["pipeline_id"] in pipelines else "unknown", ()),
  18. 'queued': (lambda job, pipelines: job["job_data"]["queued_at"], ('date',)),
  19. 'started': (lambda job, pipelines: job["job_data"]["started_at"], ('date',)),
  20. 'last active': (lambda job, pipelines: int(job["ts"]), ('date', 'coloured')),
  21. 'dl urls': (lambda job, pipelines: job["job_data"]["items_downloaded"], ()),
  22. 'dl size': (lambda job, pipelines: job["job_data"]["bytes_downloaded"], ('size',)),
  23. 'queue': (lambda job, pipelines: job["job_data"]["items_queued"] - job["job_data"]["items_downloaded"], ()),
  24. 'con': (lambda job, pipelines: job["job_data"]["concurrency"], ()),
  25. 'delay min': (lambda job, pipelines: int(job["job_data"]["delay_min"]), ('hidden',)),
  26. 'delay max': (lambda job, pipelines: int(job["job_data"]["delay_max"]), ('hidden',)),
  27. 'delay': (lambda job, pipelines: str(int(job["job_data"]["delay_min"])) + '-' + str(int(job["job_data"]["delay_max"])) if job["job_data"]["delay_min"] != job["job_data"]["delay_max"] else str(int(job["job_data"]["delay_min"])), ()),
  28. }
  29. defaultSort = 'jobid'
  30. # Validate
  31. if any('truncatable' in colDef[1] and any(x in colDef[1] for x in ('date', 'coloured', 'size')) for colDef in columns.values()):
  32. # Truncation code can't handle renderers
  33. raise RuntimeError('Invalid column definitions: cannot combine date/coloured/size with truncatable')
  34. # Parse arguments
  35. class FilterAction(argparse.Action):
  36. def __call__(self, parser, namespace, values, optionString = None):
  37. global columns
  38. match = re.match(r"^(?P<column>[A-Za-z ]+)(?P<op>[=<>^*$~])(?P<value>.*)$", values[0])
  39. if not match:
  40. raise argparse.ArgumentError('Invalid filter')
  41. filterDict = match.groupdict()
  42. filterDict["column"] = filterDict["column"].lower()
  43. assert filterDict["column"] in columns
  44. transform = (lambda x: x.lower() if isinstance(x, str) else x) if optionString in ('--ifilter', '-i') else (lambda x: x)
  45. setattr(namespace, self.dest, (filterDict, transform))
  46. def parse_sort(value):
  47. global columns
  48. sortDesc = value.startswith('-')
  49. if sortDesc:
  50. value = value[1:]
  51. value = value.lower()
  52. if value not in columns:
  53. raise argparse.ArgumentError('Invalid column name')
  54. return (value, sortDesc)
  55. class SortAction(argparse.Action):
  56. def __call__(self, parser, namespace, values, optionString = None):
  57. result = parse_sort(values[0])
  58. if getattr(namespace, self.dest, None) is None:
  59. setattr(namespace, self.dest, [])
  60. getattr(namespace, self.dest).append(result)
  61. parser = argparse.ArgumentParser(formatter_class = argparse.RawTextHelpFormatter)
  62. parser.add_argument('--filter', '-f', nargs = 1, type = str, action = FilterAction, help = '\n'.join([
  63. 'Filter the table for rows where a COLUMN has a certain VALUE. If specified multiple times, only the last value is used.',
  64. 'FILTER has the format COLUMN{=|<|>|^|*|$|~}VALUE',
  65. ' = means the value must be exactly as specified.',
  66. ' < and > mean it must be less/greater than the specified.',
  67. ' ^ and $ mean it must start/end with the specified.',
  68. ' * means it must contain the specified.',
  69. ' ~ means it must match the specified regex.',
  70. ]))
  71. parser.add_argument('--ifilter', '-i', nargs = 1, type = str, action = FilterAction, dest = 'filter', help = 'Like --filter but case-insensitive')
  72. parser.add_argument('--sort', '-s', nargs = 1, type = str, action = SortAction, help = "Sort the table by a COLUMN (descending if preceded by '-'). This can be used multiple times to refine the sorting.")
  73. parser.add_argument('--mode', choices = ('table', 'dashboard-regex', 'con-d-commands', 'format'), default = 'table', help = '\n'.join([
  74. 'Output modes:',
  75. ' table: print a table of the matched jobs',
  76. ' dashboard-regex: compose a regular expression that can be used on the dashboard to actively watch the jobs matched by the filter',
  77. ' con-d-commands: print !con and !d commands for the current settings',
  78. ' format: print some output for each job, separated by newlines; this requires the --format option',
  79. ]))
  80. parser.add_argument('--no-colours', '--no-colors', action = 'store_true', help = "Don't colourise the last activity column if it's been a while. (Table mode only)")
  81. parser.add_argument('--no-table', action = 'store_true', help = 'Raw output without feeding through column(1); columns are separated by tabs. (Table mode only)')
  82. parser.add_argument('--no-truncate', action = 'store_true', help = 'Disable truncating long values if the terminal width would be exceeded. (Table mode without --no-table only)')
  83. parser.add_argument('--dates', action = 'store_true', help = 'Print dates instead of elapsed times for queued/started/last active columns. (Table mode only)')
  84. parser.add_argument('--format', help = 'Output format for the format mode; this must be a Python format string and can use any column name in lower-case with spaces replaced by underscores; e.g. "{url} {last_active}". (Format mode only)')
  85. args = parser.parse_args()
  86. if args.mode == 'format' and not args.format:
  87. print('Error: when using format mode, --format is required.', file = sys.stderr)
  88. sys.exit(1)
  89. if not args.sort:
  90. args.sort = [parse_sort(defaultSort)]
  91. if args.mode == 'con-d-commands':
  92. args.mode = 'format'
  93. args.format = '!con {jobid} {con}\n!d {jobid} {delay_min} {delay_max}'
  94. # Retrieve
  95. def fetch(url):
  96. req = urllib.request.Request(url)
  97. req.add_header('Accept', 'application/json')
  98. with urllib.request.urlopen(req) as f:
  99. if f.getcode() != 200:
  100. raise RuntimeError('Could not fetch job data')
  101. return json.load(f)
  102. jobdata = fetch('http://dashboard.at.ninjawedding.org/logs/recent?count=1')
  103. pipelinedata = fetch('http://dashboard.at.ninjawedding.org/pipelines')
  104. currentTime = time.time()
  105. # Process
  106. pipelines = {p["id"]: p["nickname"] for p in pipelinedata["pipelines"]}
  107. jobs = []
  108. for job in jobdata:
  109. jobs.append({column: columnFunc(job, pipelines) for column, (columnFunc, _) in columns.items()})
  110. if not jobs:
  111. # Nothing to do
  112. sys.exit(0)
  113. # Filter
  114. if args.filter:
  115. filterDict, transform = args.filter
  116. compFunc = {
  117. "=": lambda a, b: a == b,
  118. "<": lambda a, b: a < b,
  119. ">": lambda a, b: a > b,
  120. "^": lambda a, b: a.startswith(b),
  121. "*": lambda a, b: b in a,
  122. "$": lambda a, b: a.endswith(b),
  123. "~": lambda a, b: re.search(b, a) is not None,
  124. }[filterDict["op"]]
  125. if isinstance(jobs[0][filterDict["column"]], (int, float)):
  126. filterDict["value"] = float(filterDict["value"])
  127. jobs = [job for job in jobs if compFunc(transform(job[filterDict["column"]]), transform(filterDict["value"]))]
  128. if not jobs:
  129. sys.exit(0)
  130. # Sort
  131. class reversor: # https://stackoverflow.com/a/56842689
  132. def __init__(self, obj):
  133. self.obj = obj
  134. def __eq__(self, other):
  135. return other.obj == self.obj
  136. def __lt__(self, other):
  137. return other.obj < self.obj
  138. sortColumns = tuple((column, descending, columns[column]) for column, descending in args.sort)
  139. if not args.dates:
  140. # Reverse sorting order for columns which have a date attribute since the column will have elapsed time
  141. sortColumns = tuple((column, not descending if 'date' in columnInfo[1] else descending, columnInfo) for column, descending, columnInfo in sortColumns)
  142. jobs = sorted(jobs, key = lambda job: tuple(job[column] if not descending else reversor(job[column]) for column, descending, _ in sortColumns))
  143. # Non-table output modes
  144. if args.mode == 'dashboard-regex':
  145. print('^(' + '|'.join(re.escape(job['url']) for job in jobs) + ')$')
  146. sys.exit(0)
  147. elif args.mode == 'format':
  148. for job in jobs:
  149. print(args.format.format(**{key.replace(' ', '_'): value for key, value in job.items()}))
  150. sys.exit(0)
  151. # Renderers
  152. def render_date(ts, coloured = False):
  153. global args, currentTime
  154. diff = currentTime - ts
  155. colourStr = f"\x1b[{0 if diff < 6 * 3600 else 7};31m" if coloured and diff >= 300 else ""
  156. colourEndStr = "\x1b[0m" if colourStr else ""
  157. if args.dates:
  158. return (colourStr, datetime.datetime.fromtimestamp(ts).isoformat(sep = " "), colourEndStr)
  159. if diff <= 0:
  160. return "now"
  161. elif diff < 60:
  162. return "<1 min ago"
  163. elif diff < 86400:
  164. return (colourStr, (f"{diff // 3600:.0f}h " if diff >= 3600 else "") + f"{(diff % 3600) // 60:.0f}mn ago", colourEndStr)
  165. else:
  166. return (colourStr, f"{diff // 86400:.0f}d {(diff % 86400) // 3600:.0f}h ago", colourEndStr)
  167. def render_size(size):
  168. units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
  169. unitIdx = min(int(math.log(size, 1024)), len(units) - 1) if size >= 1 else 0
  170. if unitIdx == 0:
  171. return f'{size} B' # No decimal places
  172. return f'{size / 1024 ** unitIdx:.1f} {units[unitIdx]}'
  173. renderers = {}
  174. for column, (_, columnAttr) in columns.items():
  175. if "date" in columnAttr:
  176. if "coloured" in columnAttr:
  177. renderers[column] = lambda x: render_date(x, coloured = not args.no_colours)
  178. else:
  179. renderers[column] = render_date
  180. elif "size" in columnAttr:
  181. renderers[column] = render_size
  182. elif isinstance(jobs[0][column], (int, float)):
  183. renderers[column] = str
  184. for job in jobs:
  185. for column in renderers:
  186. job[column] = renderers[column](job[column])
  187. # Truncate if applicable
  188. printableColumns = {column: colDef for column, colDef in columns.items() if 'hidden' not in colDef[1]}
  189. if not args.no_table and not args.no_truncate:
  190. widthsD = {column: max(itertools.chain((len(column),), (len(job[column]) if isinstance(job[column], str) else len(job[column][1]) for job in jobs))) for column in printableColumns}
  191. minWidthsD = {column: len(column) for column in printableColumns}
  192. try:
  193. termWidth = os.get_terminal_size().columns
  194. except OSError as e:
  195. if e.errno == 25:
  196. # Inappropriate ioctl for device (stdout not a terminal, happens e.g. when redirecting or piping)
  197. # Silently ignore this and don't truncate
  198. termWidth = float('Inf')
  199. else:
  200. raise
  201. overage = sum(x + 2 for x in widthsD.values()) - 2 - termWidth
  202. if overage > 0:
  203. if sum((widthsD[column] if 'truncatable' not in colDef[1] else minWidthsD[column]) + 2 for column, colDef in printableColumns.items()) - 2 > termWidth:
  204. # Even truncating all truncatable columns to the minimum width is not sufficient, i.e. can't match this terminal width. Print a warning and proceed normally
  205. print('Sorry, cannot truncate columns to terminal width', file = sys.stderr)
  206. else:
  207. # Distribute overage to truncatable columns proportionally to each column's length over the minimum
  208. truncatableColumns = {column: colDef for column, colDef in columns.items() if 'truncatable' in colDef[1]}
  209. totalOverMin = sum(widthsD[column] - minWidthsD[column] for column in truncatableColumns)
  210. trWidthsD = {column: math.floor(widthsD[column] - (widthsD[column] - minWidthsD[column]) / totalOverMin * overage) for column in truncatableColumns}
  211. if sum(widthsD[column] - trWidthsD[column] for column in truncatableColumns) - overage == 1:
  212. # Truncated one more character than necessary due to the flooring; add it again to the shortest column
  213. trWidthsD[min(trWidthsD, key = trWidthsD.get)] += 1
  214. for job in jobs:
  215. for column in truncatableColumns:
  216. if len(job[column]) > trWidthsD[column]:
  217. job[column] = job[column][:trWidthsD[column] - 1] + '…'
  218. # Print
  219. output = []
  220. output.append(tuple(column.upper() for column in columns if "hidden" not in columns[column][1]))
  221. for job in jobs:
  222. output.append(tuple(job[column] for column in columns if "hidden" not in columns[column][1]))
  223. if not args.no_table:
  224. widths = tuple(max(len(field) if isinstance(field, str) else len(field[1]) for field in column) for column in zip(*output))
  225. for row in output:
  226. print(' '.join((value.ljust(width) if isinstance(value, str) else ''.join((value[0], value[1], value[2], ' ' * (width - len(value[1]))))) for value, width in zip(row, widths)))
  227. else:
  228. for row in output:
  229. print('\t'.join(field if isinstance(field, str) else ''.join(field) for field in row))