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.
 
 
 

194 lines
8.2 KiB

  1. #!/usr/bin/env python3
  2. import argparse
  3. import datetime
  4. import json
  5. import math
  6. import re
  7. import sys
  8. import time
  9. import urllib.request
  10. # Column definitions
  11. columns = {
  12. 'jobid': (lambda job, pipelines: job["job_data"]["ident"], ()),
  13. 'url': (lambda job, pipelines: job["job_data"]["url"], ()),
  14. 'user': (lambda job, pipelines: job["job_data"]["started_by"], ()),
  15. 'pipenick': (lambda job, pipelines: pipelines[job["job_data"]["pipeline_id"]] if job["job_data"]["pipeline_id"] in pipelines else "unknown", ()),
  16. 'queued': (lambda job, pipelines: job["job_data"]["queued_at"], ('date',)),
  17. 'started': (lambda job, pipelines: job["job_data"]["started_at"], ('date',)),
  18. 'last active': (lambda job, pipelines: int(job["ts"]), ('date', 'coloured')),
  19. 'dl urls': (lambda job, pipelines: job["job_data"]["items_downloaded"], ()),
  20. 'dl size': (lambda job, pipelines: job["job_data"]["bytes_downloaded"], ('size',)),
  21. 'queue': (lambda job, pipelines: job["job_data"]["items_queued"] - job["job_data"]["items_downloaded"], ()),
  22. 'con': (lambda job, pipelines: job["job_data"]["concurrency"], ()),
  23. '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"])), ()),
  24. }
  25. defaultSort = 'jobid'
  26. # Parse arguments
  27. class FilterAction(argparse.Action):
  28. def __call__(self, parser, namespace, values, optionString = None):
  29. global columns
  30. match = re.match(r"^(?P<column>[A-Za-z ]+)(?P<op>[=<>^*$~])(?P<value>.*)$", values[0])
  31. if not match:
  32. raise argparse.ArgumentError('Invalid filter')
  33. filterDict = match.groupdict()
  34. filterDict["column"] = filterDict["column"].lower()
  35. assert filterDict["column"] in columns
  36. transform = (lambda x: x.lower() if isinstance(x, str) else x) if optionString in ('--ifilter', '-i') else (lambda x: x)
  37. setattr(namespace, self.dest, (filterDict, transform))
  38. def parse_sort(value):
  39. global columns
  40. sortDesc = value.startswith('-')
  41. if sortDesc:
  42. value = value[1:]
  43. value = value.lower()
  44. if value not in columns:
  45. raise argparse.ArgumentError('Invalid column name')
  46. return (value, sortDesc)
  47. class SortAction(argparse.Action):
  48. def __call__(self, parser, namespace, values, optionString = None):
  49. result = parse_sort(values[0])
  50. if getattr(namespace, self.dest, None) is None:
  51. setattr(namespace, self.dest, [])
  52. getattr(namespace, self.dest).append(result)
  53. parser = argparse.ArgumentParser(formatter_class = argparse.RawTextHelpFormatter)
  54. 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.")
  55. parser.add_argument('--filter', '-f', nargs = 1, type = str, action = FilterAction, help = '\n'.join([
  56. 'Filter the table for rows where a COLUMN has a certain VALUE. If specified multiple times, only the last value is used.',
  57. 'FILTER has the format COLUMN{=|<|>|^|*|$|~}VALUE',
  58. ' = means the value must be exactly as specified.',
  59. ' < and > mean it must be less/greater than the specified.',
  60. ' ^ and $ mean it must start/end with the specified.',
  61. ' * means it must contain the specified.',
  62. ' ~ means it must match the specified regex.',
  63. ]))
  64. parser.add_argument('--ifilter', '-i', nargs = 1, type = str, action = FilterAction, dest = 'filter', help = 'Like --filter but case-insensitive')
  65. parser.add_argument('--no-colours', '--no-colors', action = 'store_true', help = "Don't colourise the last activity column if it's been a while.")
  66. parser.add_argument('--no-table', action = 'store_true', help = 'Raw output without feeding through column(1); columns are separated by tabs.')
  67. parser.add_argument('--dates', action = 'store_true', help = 'Print dates instead of elapsed times for queued/started/last active columns.')
  68. parser.add_argument('--dashboard-regex', action = 'store_true', help = 'Instead of the normal output, compose a regular expression that can be used on the dashboard to actively watch the jobs matched by the filter. (--sort, --no-colours, --no-table, and --dates have no effect when this option is used.)')
  69. args = parser.parse_args()
  70. if not args.sort:
  71. args.sort = [parse_sort(defaultSort)]
  72. # Retrieve
  73. def fetch(url):
  74. req = urllib.request.Request(url)
  75. req.add_header('Accept', 'application/json')
  76. with urllib.request.urlopen(req) as f:
  77. if f.getcode() != 200:
  78. raise RuntimeError('Could not fetch job data')
  79. return json.load(f)
  80. jobdata = fetch('http://dashboard.at.ninjawedding.org/logs/recent?count=1')
  81. pipelinedata = fetch('http://dashboard.at.ninjawedding.org/pipelines')
  82. currentTime = time.time()
  83. # Process
  84. pipelines = {p["id"]: p["nickname"] for p in pipelinedata["pipelines"]}
  85. jobs = []
  86. for job in jobdata:
  87. jobs.append({column: columnFunc(job, pipelines) for column, (columnFunc, _) in columns.items()})
  88. if not jobs:
  89. # Nothing to do
  90. sys.exit(0)
  91. # Filter
  92. if args.filter:
  93. filterDict, transform = args.filter
  94. compFunc = {
  95. "=": lambda a, b: a == b,
  96. "<": lambda a, b: a < b,
  97. ">": lambda a, b: a > b,
  98. "^": lambda a, b: a.startswith(b),
  99. "*": lambda a, b: b in a,
  100. "$": lambda a, b: a.endswith(b),
  101. "~": lambda a, b: re.search(b, a) is not None,
  102. }[filterDict["op"]]
  103. if isinstance(jobs[0][filterDict["column"]], (int, float)):
  104. filterDict["value"] = float(filterDict["value"])
  105. jobs = [job for job in jobs if compFunc(transform(job[filterDict["column"]]), transform(filterDict["value"]))]
  106. if not jobs:
  107. sys.exit(0)
  108. if args.dashboard_regex:
  109. print('^(' + '|'.join(re.escape(job['url']) for job in jobs) + ')$')
  110. sys.exit(0)
  111. # Sort
  112. class reversor: # https://stackoverflow.com/a/56842689
  113. def __init__(self, obj):
  114. self.obj = obj
  115. def __eq__(self, other):
  116. return other.obj == self.obj
  117. def __lt__(self, other):
  118. return other.obj < self.obj
  119. sortColumns = tuple((column, descending, columns[column]) for column, descending in args.sort)
  120. if not args.dates:
  121. # Reverse sorting order for columns which have a date attribute since the column will have elapsed time
  122. sortColumns = tuple((column, not descending if 'date' in columnInfo[1] else descending, columnInfo) for column, descending, columnInfo in sortColumns)
  123. jobs = sorted(jobs, key = lambda job: tuple(job[column] if not descending else reversor(job[column]) for column, descending, _ in sortColumns))
  124. # Renderers
  125. def render_date(ts, coloured = False):
  126. global args, currentTime
  127. diff = currentTime - ts
  128. colourStr = f"\x1b[{0 if diff < 6 * 3600 else 7};31m" if coloured and diff >= 300 else ""
  129. colourEndStr = "\x1b[0m" if colourStr else ""
  130. if args.dates:
  131. return (colourStr, datetime.datetime.fromtimestamp(ts).isoformat(sep = " "), colourEndStr)
  132. if diff <= 0:
  133. return "now"
  134. elif diff < 60:
  135. return "<1 min ago"
  136. elif diff < 86400:
  137. return (colourStr, (f"{diff // 3600:.0f}h " if diff >= 3600 else "") + f"{(diff % 3600) // 60:.0f}mn ago", colourEndStr)
  138. else:
  139. return (colourStr, f"{diff // 86400:.0f}d {(diff % 86400) // 3600:.0f}h ago", colourEndStr)
  140. def render_size(size):
  141. units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
  142. unitIdx = min(int(math.log(size, 1024)), len(units) - 1) if size >= 1 else 0
  143. if unitIdx == 0:
  144. return f'{size} B' # No decimal places
  145. return f'{size / 1024 ** unitIdx:.1f} {units[unitIdx]}'
  146. renderers = {}
  147. for column, (_, columnAttr) in columns.items():
  148. if "date" in columnAttr:
  149. if "coloured" in columnAttr:
  150. renderers[column] = lambda x: render_date(x, coloured = not args.no_colours)
  151. else:
  152. renderers[column] = render_date
  153. elif "size" in columnAttr:
  154. renderers[column] = render_size
  155. elif isinstance(jobs[0][column], (int, float)):
  156. renderers[column] = str
  157. # Print
  158. output = []
  159. output.append(tuple(column.upper() for column in columns))
  160. for job in jobs:
  161. for column in renderers:
  162. job[column] = renderers[column](job[column])
  163. output.append(tuple(job[column] for column in columns))
  164. if not args.no_table:
  165. widths = tuple(max(len(field) if isinstance(field, str) else len(field[1]) for field in column) for column in zip(*output))
  166. for row in output:
  167. 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)))
  168. else:
  169. for row in output:
  170. print('\t'.join(field if isinstance(field, str) else ''.join(field) for field in row))