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.
 
 
 

272 lines
13 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. parser.error('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. parser.error('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('--replace-concurrency', nargs = 1, metavar = 'CON', type = int, help = 'Replace the delay values with the specified ones. (con-d-commands mode only)')
  85. parser.add_argument('--replace-delay', nargs = 2, metavar = ('MIN', 'MAX'), type = int, help = 'Replace the delay values with the specified ones. (con-d-commands mode only)')
  86. 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)')
  87. args = parser.parse_args()
  88. if args.mode == 'format' and not args.format:
  89. print('Error: when using format mode, --format is required.', file = sys.stderr)
  90. sys.exit(1)
  91. if not args.sort:
  92. args.sort = [parse_sort(defaultSort)]
  93. if args.mode == 'con-d-commands':
  94. args.mode = 'format'
  95. args.format = '!con {jobid} {con}\n!d {jobid} {delay_min} {delay_max}'
  96. else:
  97. args.replace_concurrency = None
  98. args.replace_delay = None
  99. # Retrieve
  100. def fetch(url):
  101. req = urllib.request.Request(url)
  102. req.add_header('Accept', 'application/json')
  103. with urllib.request.urlopen(req) as f:
  104. if f.getcode() != 200:
  105. raise RuntimeError('Could not fetch job data')
  106. return json.load(f)
  107. jobdata = fetch('http://dashboard.at.ninjawedding.org/logs/recent?count=1')
  108. pipelinedata = fetch('http://dashboard.at.ninjawedding.org/pipelines')
  109. currentTime = time.time()
  110. # Process
  111. pipelines = {p["id"]: p["nickname"] for p in pipelinedata["pipelines"]}
  112. jobs = []
  113. for job in jobdata:
  114. jobs.append({column: columnFunc(job, pipelines) for column, (columnFunc, _) in columns.items()})
  115. if not jobs:
  116. # Nothing to do
  117. sys.exit(0)
  118. # Filter
  119. if args.filter:
  120. filterDict, transform = args.filter
  121. compFunc = {
  122. "=": lambda a, b: a == b,
  123. "<": lambda a, b: a < b,
  124. ">": lambda a, b: a > b,
  125. "^": lambda a, b: a.startswith(b),
  126. "*": lambda a, b: b in a,
  127. "$": lambda a, b: a.endswith(b),
  128. "~": lambda a, b: re.search(b, a) is not None,
  129. }[filterDict["op"]]
  130. if isinstance(jobs[0][filterDict["column"]], (int, float)):
  131. filterDict["value"] = float(filterDict["value"])
  132. jobs = [job for job in jobs if compFunc(transform(job[filterDict["column"]]), transform(filterDict["value"]))]
  133. if not jobs:
  134. sys.exit(0)
  135. # Sort
  136. class reversor: # https://stackoverflow.com/a/56842689
  137. def __init__(self, obj):
  138. self.obj = obj
  139. def __eq__(self, other):
  140. return other.obj == self.obj
  141. def __lt__(self, other):
  142. return other.obj < self.obj
  143. sortColumns = tuple((column, descending, columns[column]) for column, descending in args.sort)
  144. if not args.dates:
  145. # Reverse sorting order for columns which have a date attribute since the column will have elapsed time
  146. sortColumns = tuple((column, not descending if 'date' in columnInfo[1] else descending, columnInfo) for column, descending, columnInfo in sortColumns)
  147. jobs = sorted(jobs, key = lambda job: tuple(job[column] if not descending else reversor(job[column]) for column, descending, _ in sortColumns))
  148. # Concurrency and delay overrides if specified and relevant
  149. if args.replace_concurrency is not None or args.replace_delay is not None:
  150. for job in jobs:
  151. if args.replace_concurrency is not None:
  152. job['con'] = args.replace_concurrency[0]
  153. if args.replace_delay is not None:
  154. job['delay min'] = args.replace_delay[0]
  155. job['delay max'] = args.replace_delay[1]
  156. # Non-table output modes
  157. if args.mode == 'dashboard-regex':
  158. print('^(' + '|'.join(re.escape(job['url']) for job in jobs) + ')$')
  159. sys.exit(0)
  160. elif args.mode == 'format':
  161. for job in jobs:
  162. print(args.format.format(**{key.replace(' ', '_'): value for key, value in job.items()}))
  163. sys.exit(0)
  164. # Renderers
  165. def render_date(ts, coloured = False):
  166. global args, currentTime
  167. diff = currentTime - ts
  168. colourStr = f"\x1b[{0 if diff < 6 * 3600 else 7};31m" if coloured and diff >= 300 else ""
  169. colourEndStr = "\x1b[0m" if colourStr else ""
  170. if args.dates:
  171. return (colourStr, datetime.datetime.fromtimestamp(ts).isoformat(sep = " "), colourEndStr)
  172. if diff <= 0:
  173. return "now"
  174. elif diff < 60:
  175. return "<1 min ago"
  176. elif diff < 86400:
  177. return (colourStr, (f"{diff // 3600:.0f}h " if diff >= 3600 else "") + f"{(diff % 3600) // 60:.0f}mn ago", colourEndStr)
  178. else:
  179. return (colourStr, f"{diff // 86400:.0f}d {(diff % 86400) // 3600:.0f}h ago", colourEndStr)
  180. def render_size(size):
  181. units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
  182. unitIdx = min(int(math.log(size, 1024)), len(units) - 1) if size >= 1 else 0
  183. if unitIdx == 0:
  184. return f'{size} B' # No decimal places
  185. return f'{size / 1024 ** unitIdx:.1f} {units[unitIdx]}'
  186. renderers = {}
  187. for column, (_, columnAttr) in columns.items():
  188. if "date" in columnAttr:
  189. if "coloured" in columnAttr:
  190. renderers[column] = lambda x: render_date(x, coloured = not args.no_colours)
  191. else:
  192. renderers[column] = render_date
  193. elif "size" in columnAttr:
  194. renderers[column] = render_size
  195. elif isinstance(jobs[0][column], (int, float)):
  196. renderers[column] = str
  197. for job in jobs:
  198. for column in renderers:
  199. job[column] = renderers[column](job[column])
  200. # Truncate if applicable
  201. printableColumns = {column: colDef for column, colDef in columns.items() if 'hidden' not in colDef[1]}
  202. if not args.no_table and not args.no_truncate:
  203. 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}
  204. minWidthsD = {column: len(column) for column in printableColumns}
  205. try:
  206. termWidth = os.get_terminal_size().columns
  207. except OSError as e:
  208. if e.errno == 25:
  209. # Inappropriate ioctl for device (stdout not a terminal, happens e.g. when redirecting or piping)
  210. # Silently ignore this and don't truncate
  211. termWidth = float('Inf')
  212. else:
  213. raise
  214. overage = sum(x + 2 for x in widthsD.values()) - 2 - termWidth
  215. if overage > 0:
  216. if sum((widthsD[column] if 'truncatable' not in colDef[1] else minWidthsD[column]) + 2 for column, colDef in printableColumns.items()) - 2 > termWidth:
  217. # 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
  218. print('Sorry, cannot truncate columns to terminal width', file = sys.stderr)
  219. else:
  220. # Distribute overage to truncatable columns proportionally to each column's length over the minimum
  221. truncatableColumns = {column: colDef for column, colDef in columns.items() if 'truncatable' in colDef[1]}
  222. totalOverMin = sum(widthsD[column] - minWidthsD[column] for column in truncatableColumns)
  223. trWidthsD = {column: math.floor(widthsD[column] - (widthsD[column] - minWidthsD[column]) / totalOverMin * overage) for column in truncatableColumns}
  224. if sum(widthsD[column] - trWidthsD[column] for column in truncatableColumns) - overage == 1:
  225. # Truncated one more character than necessary due to the flooring; add it again to the shortest column
  226. trWidthsD[min(trWidthsD, key = trWidthsD.get)] += 1
  227. for job in jobs:
  228. for column in truncatableColumns:
  229. if len(job[column]) > trWidthsD[column]:
  230. job[column] = job[column][:trWidthsD[column] - 1] + '…'
  231. # Print
  232. output = []
  233. output.append(tuple(column.upper() for column in columns if "hidden" not in columns[column][1]))
  234. for job in jobs:
  235. output.append(tuple(job[column] for column in columns if "hidden" not in columns[column][1]))
  236. if not args.no_table:
  237. widths = tuple(max(len(field) if isinstance(field, str) else len(field[1]) for field in column) for column in zip(*output))
  238. for row in output:
  239. 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)))
  240. else:
  241. for row in output:
  242. print('\t'.join(field if isinstance(field, str) else ''.join(field) for field in row))