Skip to content

Commit d0e2a2c

Browse files
authored
Fix alert row handling and text encoding (#121)
1 parent fc267c3 commit d0e2a2c

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

alertaclient/commands/cmd_top.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
def cli(obj):
1010
"""Display alerts like unix "top" command."""
1111
client = obj['client']
12+
timezone = obj['timezone']
1213

13-
screen = Screen(client)
14+
screen = Screen(client, timezone)
1415
screen.run()

alertaclient/top.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class Screen(object):
1414
ALIGN_RIGHT = 'R'
1515
ALIGN_CENTRE = 'C'
1616

17-
def __init__(self, client):
17+
def __init__(self, client, timezone):
1818
self.client = client
19+
self.timezone = timezone
1920

21+
self.screen = None
2022
self.lines = None
2123
self.cols = None
2224

@@ -45,17 +47,18 @@ def main(self, stdscr):
4547
COLOR_BLACK = curses.color_pair(7)
4648

4749
self.SEVERITY_MAP = {
50+
'security': ["Sec", COLOR_BLACK],
4851
'critical': ["Crit", COLOR_RED],
4952
'major': ["Majr", COLOR_MAGENTA],
5053
'minor': ["Minr", COLOR_YELLOW],
5154
'warning': ["Warn", COLOR_BLUE],
5255
'indeterminate': ["Ind ", COLOR_CYAN],
5356
'cleared': ["Clr", COLOR_GREEN],
5457
'normal': ["Norm", COLOR_GREEN],
55-
'inform': ["Info", COLOR_GREEN],
5658
'ok': ["Ok", COLOR_GREEN],
59+
'informational': ["Info", COLOR_GREEN],
5760
'debug': ["Dbug", COLOR_BLACK],
58-
'auth': ["Sec", COLOR_BLACK],
61+
'trace': ["Trce", COLOR_BLACK],
5962
'unknown': ["Unkn", COLOR_BLACK]
6063
}
6164

@@ -85,31 +88,47 @@ def update(self):
8588
self._addstr(0, 'C', 'alerta {}'.format(version), curses.A_BOLD)
8689
self._addstr(0, 'R', '{}'.format(now.strftime('%H:%M:%S %d/%m/%y')), curses.A_BOLD)
8790

88-
# draw bars
91+
# TODO - draw bars
8992

9093
# draw alerts
91-
self._addstr(2, 1, 'Sev. Time Dupl. Env. Service Resource Event Value ', curses.A_UNDERLINE)
94+
text_width = self.cols - 95
95+
self._addstr(2, 1, 'Sev. Time Dupl. Customer Env. Service Resource Group Event' +
96+
' Value Text' + ' ' * (text_width - 4), curses.A_UNDERLINE)
9297

9398
def short_sev(severity):
94-
return self.SEVERITY_MAP[severity][0]
99+
return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[0]
95100

96101
def color(severity):
97-
return self.SEVERITY_MAP[severity][1]
102+
return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[1]
98103

99104
r = self.client.http.get('/alerts')
100105
alerts = [Alert.parse(a) for a in r['alerts']]
101106
last_time = DateTime.parse(r['lastTime'])
107+
102108
for i, alert in enumerate(alerts):
103-
self._addstr(i+3, 1, '{0:<4} {1} {2:5d} {3:<12} {4:<12} {5:<12.12} {6:<12.12} {7:<6.6}'.format(
109+
row = i + 3
110+
if row >= self.lines - 2: # leave room for footer
111+
break
112+
113+
text = u'{:<4} {} {:5d} {:8.8} {:<12} {:<12} {:<12.12} {:5.5} {:<12.12} {:<5.5} {:.{width}}'.format(
104114
short_sev(alert.severity),
105-
alert.last_receive_time.strftime('%H:%M:%S'),
115+
DateTime.localtime(alert.last_receive_time, self.timezone, fmt='%H:%M:%S'),
106116
alert.duplicate_count,
117+
alert.customer or "-",
107118
alert.environment,
108119
','.join(alert.service),
109120
alert.resource,
121+
alert.group,
110122
alert.event,
111-
alert.value
112-
), color(alert.severity))
123+
alert.value or "n/a",
124+
alert.text,
125+
width=text_width
126+
)
127+
# XXX - needed to support python2 and python3
128+
if not isinstance(text, str):
129+
text = text.encode('ascii', errors='replace')
130+
131+
self._addstr(row, 1, text, color(alert.severity))
113132

114133
# draw footer
115134
self._addstr(self.lines - 1, 0, 'Last Update: {}'.format(last_time.strftime('%H:%M:%S')), curses.A_BOLD)
@@ -118,13 +137,13 @@ def color(severity):
118137

119138
self.screen.refresh()
120139

121-
def _addstr(self, y, x, str, attr=0):
140+
def _addstr(self, y, x, line, attr=0):
122141
if x == self.ALIGN_RIGHT:
123-
x = self.cols - len(str) - 1
142+
x = self.cols - len(line) - 1
124143
if x == self.ALIGN_CENTRE:
125-
x = int((self.cols / 2) - len(str) / 2)
144+
x = int((self.cols / 2) - len(line) / 2)
126145

127-
self.screen.addstr(y, x, str, attr)
146+
self.screen.addstr(y, x, line, attr)
128147

129148
def _key_press(self, key):
130149
if key in 'qQ':

alertaclient/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def iso8601(dt):
3131
return dt.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + ".%03dZ" % (dt.microsecond // 1000)
3232

3333
@staticmethod
34-
def localtime(dt, timezone=None):
34+
def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
3535
tz = pytz.timezone(timezone)
3636
try:
37-
return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime('%Y/%m/%d %H:%M:%S')
37+
return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime(fmt)
3838
except AttributeError:
3939
return
4040

0 commit comments

Comments
 (0)