color output of ws broadcast connection status

This commit is contained in:
chris meyers
2020-04-01 12:53:12 -04:00
parent c00f1505d7
commit 8bbae0cc3a

View File

@@ -3,6 +3,7 @@
import logging import logging
import asyncio import asyncio
import datetime import datetime
import re
from datetime import datetime as dt from datetime import datetime as dt
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
@@ -25,19 +26,25 @@ class Command(BaseCommand):
parser.add_argument('--status', dest='status', action='store_true', parser.add_argument('--status', dest='status', action='store_true',
help='print the internal state of any running broadcast websocket') help='print the internal state of any running broadcast websocket')
@classmethod
def display_len(cls, s):
return len(re.sub('\x1b.*?m', '', s))
@classmethod @classmethod
def _format_lines(cls, host_stats, padding=5): def _format_lines(cls, host_stats, padding=5):
widths = [0 for i in host_stats[0]] widths = [0 for i in host_stats[0]]
for entry in host_stats: for entry in host_stats:
for i, e in enumerate(entry): for i, e in enumerate(entry):
if len(e) > widths[i]: if Command.display_len(e) > widths[i]:
widths[i] = len(e) widths[i] = Command.display_len(e)
paddings = [padding for i in widths] paddings = [padding for i in widths]
lines = [] lines = []
for entry in host_stats: for entry in host_stats:
line = "" line = ""
for pad, width, value in zip(paddings, widths, entry): for pad, width, value in zip(paddings, widths, entry):
if len(value) > Command.display_len(value):
width += len(value) - Command.display_len(value)
total_width = width + pad total_width = width + pad
line += f'{value:{total_width}}' line += f'{value:{total_width}}'
lines.append(line) lines.append(line)
@@ -47,6 +54,7 @@ class Command(BaseCommand):
def get_connection_status(cls, me, hostnames, data): def get_connection_status(cls, me, hostnames, data):
host_stats = [('hostame', 'state', 'start time', 'duration (sec)')] host_stats = [('hostame', 'state', 'start time', 'duration (sec)')]
for h in hostnames: for h in hostnames:
connection_color = '91' # red
h = safe_name(h) h = safe_name(h)
prefix = f'awx_{h}' prefix = f'awx_{h}'
connection_state = data.get(f'{prefix}_connection', 'N/A') connection_state = data.get(f'{prefix}_connection', 'N/A')
@@ -55,11 +63,14 @@ class Command(BaseCommand):
if connection_state is None: if connection_state is None:
connection_state = 'unknown' connection_state = 'unknown'
if connection_state == 'connected': if connection_state == 'connected':
connection_color = '92' # green
connection_started = data.get(f'{prefix}_connection_start', 'Error') connection_started = data.get(f'{prefix}_connection_start', 'Error')
if connection_started != 'Error': if connection_started != 'Error':
connection_started = datetime.datetime.fromtimestamp(connection_started) connection_started = datetime.datetime.fromtimestamp(connection_started)
connection_duration = (dt.now() - connection_started).total_seconds() connection_duration = (dt.now() - connection_started).total_seconds()
connection_state = f'\033[{connection_color}m{connection_state}\033[0m'
host_stats.append((h, connection_state, str(connection_started), str(connection_duration))) host_stats.append((h, connection_state, str(connection_started), str(connection_duration)))
return host_stats return host_stats