The little things give you away... A collection of various small helper stuff
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

189 lignes
7.8 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. args = parser.parse_args()
  69. if not args.sort:
  70. args.sort = [parse_sort(defaultSort)]
  71. # Retrieve
  72. def fetch(url):
  73. req = urllib.request.Request(url)
  74. req.add_header('Accept', 'application/json')
  75. with urllib.request.urlopen(req) as f:
  76. if f.getcode() != 200:
  77. raise RuntimeError('Could not fetch job data')
  78. return json.load(f)
  79. jobdata = fetch('http://dashboard.at.ninjawedding.org/logs/recent?count=1')
  80. pipelinedata = fetch('http://dashboard.at.ninjawedding.org/pipelines')
  81. currentTime = time.time()
  82. # Process
  83. pipelines = {p["id"]: p["nickname"] for p in pipelinedata["pipelines"]}
  84. jobs = []
  85. for job in jobdata:
  86. jobs.append({column: columnFunc(job, pipelines) for column, (columnFunc, _) in columns.items()})
  87. if not jobs:
  88. # Nothing to do
  89. sys.exit(0)
  90. # Filter
  91. if args.filter:
  92. filterDict, transform = args.filter
  93. compFunc = {
  94. "=": lambda a, b: a == b,
  95. "<": lambda a, b: a < b,
  96. ">": lambda a, b: a > b,
  97. "^": lambda a, b: a.startswith(b),
  98. "*": lambda a, b: b in a,
  99. "$": lambda a, b: a.endswith(b),
  100. "~": lambda a, b: re.search(b, a) is not None,
  101. }[filterDict["op"]]
  102. if isinstance(jobs[0][filterDict["column"]], (int, float)):
  103. filterDict["value"] = float(filterDict["value"])
  104. jobs = [job for job in jobs if compFunc(transform(job[filterDict["column"]]), transform(filterDict["value"]))]
  105. if not jobs:
  106. sys.exit(0)
  107. # Sort
  108. class reversor: # https://stackoverflow.com/a/56842689
  109. def __init__(self, obj):
  110. self.obj = obj
  111. def __eq__(self, other):
  112. return other.obj == self.obj
  113. def __lt__(self, other):
  114. return other.obj < self.obj
  115. sortColumns = tuple((column, descending, columns[column]) for column, descending in args.sort)
  116. if not args.dates:
  117. # Reverse sorting order for columns which have a date attribute since the column will have elapsed time
  118. sortColumns = tuple((column, not descending if 'date' in columnInfo[1] else descending, columnInfo) for column, descending, columnInfo in sortColumns)
  119. jobs = sorted(jobs, key = lambda job: tuple(job[column] if not descending else reversor(job[column]) for column, descending, _ in sortColumns))
  120. # Renderers
  121. def render_date(ts, coloured = False):
  122. global args, currentTime
  123. diff = currentTime - ts
  124. colourStr = f"\x1b[{0 if diff < 6 * 3600 else 7};31m" if coloured and diff >= 300 else ""
  125. colourEndStr = "\x1b[0m" if colourStr else ""
  126. if args.dates:
  127. return (colourStr, datetime.datetime.fromtimestamp(ts).isoformat(sep = " "), colourEndStr)
  128. if diff <= 0:
  129. return "now"
  130. elif diff < 60:
  131. return "<1 min ago"
  132. elif diff < 86400:
  133. return (colourStr, (f"{diff // 3600:.0f}h " if diff >= 3600 else "") + f"{(diff % 3600) // 60:.0f}mn ago", colourEndStr)
  134. else:
  135. return (colourStr, f"{diff // 86400:.0f}d {(diff % 86400) // 3600:.0f}h ago", colourEndStr)
  136. def render_size(size):
  137. units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
  138. unitIdx = min(int(math.log(size, 1024)), len(units) - 1) if size >= 1 else 0
  139. if unitIdx == 0:
  140. return f'{size} B' # No decimal places
  141. return f'{size / 1024 ** unitIdx:.1f} {units[unitIdx]}'
  142. renderers = {}
  143. for column, (_, columnAttr) in columns.items():
  144. if "date" in columnAttr:
  145. if "coloured" in columnAttr:
  146. renderers[column] = lambda x: render_date(x, coloured = not args.no_colours)
  147. else:
  148. renderers[column] = render_date
  149. elif "size" in columnAttr:
  150. renderers[column] = render_size
  151. elif isinstance(jobs[0][column], (int, float)):
  152. renderers[column] = str
  153. # Print
  154. output = []
  155. output.append(tuple(column.upper() for column in columns))
  156. for job in jobs:
  157. for column in renderers:
  158. job[column] = renderers[column](job[column])
  159. output.append(tuple(job[column] for column in columns))
  160. if not args.no_table:
  161. widths = tuple(max(len(field) if isinstance(field, str) else len(field[1]) for field in column) for column in zip(*output))
  162. for row in output:
  163. 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)))
  164. else:
  165. for row in output:
  166. print('\t'.join(field if isinstance(field, str) else ''.join(field) for field in row))