Skip to content

Commit 04e7662

Browse files
authored
Avoid sys.exit where possible and fix watch cmd (#116)
1 parent c4ecfe0 commit 04e7662

9 files changed

Lines changed: 169 additions & 181 deletions

File tree

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -16,19 +15,18 @@ def cli(obj, purge):
1615
if obj['output'] == 'json':
1716
r = client.http.get('/blackouts')
1817
click.echo(json.dumps(r['blackouts'], sort_keys=True, indent=4, ensure_ascii=False))
19-
sys.exit(0)
18+
else:
19+
timezone = obj['timezone']
20+
headers = {
21+
'id': 'ID', 'priority': 'P', 'environment': 'ENVIRONMENT', 'service': 'SERVICE', 'resource': 'RESOURCE',
22+
'event': 'EVENT', 'group': 'GROUP', 'tags': 'TAGS', 'startTime': 'START', 'endTime': 'END',
23+
'duration': 'DURATION', 'status': 'STATUS', 'remaining': 'REMAINING', 'customer': 'CUSTOMER'
24+
}
25+
blackouts = client.get_blackouts()
26+
click.echo(tabulate([b.tabular(timezone) for b in blackouts], headers=headers, tablefmt=obj['output']))
2027

21-
timezone = obj['timezone']
22-
headers = {
23-
'id': 'ID', 'priority': 'P', 'environment': 'ENVIRONMENT', 'service': 'SERVICE', 'resource': 'RESOURCE',
24-
'event': 'EVENT', 'group': 'GROUP', 'tags': 'TAGS', 'startTime': 'START', 'endTime': 'END',
25-
'duration': 'DURATION', 'status': 'STATUS', 'remaining': 'REMAINING', 'customer': 'CUSTOMER'
26-
}
27-
blackouts = client.get_blackouts()
28-
click.echo(tabulate([b.tabular(timezone) for b in blackouts], headers=headers, tablefmt=obj['output']))
29-
30-
expired = [b for b in blackouts if b.status == 'expired']
31-
if purge:
32-
with click.progressbar(expired, label='Purging {} blackouts'.format(len(expired))) as bar:
33-
for b in bar:
34-
client.delete_blackout(b.id)
28+
expired = [b for b in blackouts if b.status == 'expired']
29+
if purge:
30+
with click.progressbar(expired, label='Purging {} blackouts'.format(len(expired))) as bar:
31+
for b in bar:
32+
client.delete_blackout(b.id)

alertaclient/commands/cmd_customers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -15,7 +14,6 @@ def cli(obj):
1514
if obj['output'] == 'json':
1615
r = client.http.get('/customers')
1716
click.echo(json.dumps(r['customers'], sort_keys=True, indent=4, ensure_ascii=False))
18-
sys.exit(0)
19-
20-
headers = {'id': 'ID', 'customer': 'CUSTOMER', 'match': 'GROUP'}
21-
click.echo(tabulate([c.tabular() for c in client.get_customers()], headers=headers, tablefmt=obj['output']))
17+
else:
18+
headers = {'id': 'ID', 'customer': 'CUSTOMER', 'match': 'GROUP'}
19+
click.echo(tabulate([c.tabular() for c in client.get_customers()], headers=headers, tablefmt=obj['output']))
Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -19,69 +18,68 @@ def cli(obj, alert, severity, purge):
1918
if obj['output'] == 'json':
2019
r = client.http.get('/heartbeats')
2120
click.echo(json.dumps(r['heartbeats'], sort_keys=True, indent=4, ensure_ascii=False))
22-
sys.exit(0)
21+
else:
22+
timezone = obj['timezone']
23+
headers = {
24+
'id': 'ID', 'origin': 'ORIGIN', 'customer': 'CUSTOMER', 'tags': 'TAGS', 'createTime': 'CREATED',
25+
'receiveTime': 'RECEIVED', 'latency': 'LATENCY', 'timeout': 'TIMEOUT', 'since': 'SINCE', 'status': 'STATUS'
26+
}
27+
heartbeats = client.get_heartbeats()
28+
click.echo(tabulate([h.tabular(timezone) for h in heartbeats], headers=headers, tablefmt=obj['output']))
2329

24-
timezone = obj['timezone']
25-
headers = {
26-
'id': 'ID', 'origin': 'ORIGIN', 'customer': 'CUSTOMER', 'tags': 'TAGS', 'createTime': 'CREATED',
27-
'receiveTime': 'RECEIVED', 'latency': 'LATENCY', 'timeout': 'TIMEOUT', 'since': 'SINCE', 'status': 'STATUS'
28-
}
29-
heartbeats = client.get_heartbeats()
30-
click.echo(tabulate([h.tabular(timezone) for h in heartbeats], headers=headers, tablefmt=obj['output']))
30+
not_ok = [hb for hb in heartbeats if hb.status != 'ok']
31+
if purge:
32+
with click.progressbar(not_ok, label='Purging {} heartbeats'.format(len(not_ok))) as bar:
33+
for b in bar:
34+
client.delete_heartbeat(b.id)
3135

32-
not_ok = [hb for hb in heartbeats if hb.status != 'ok']
33-
if purge:
34-
with click.progressbar(not_ok, label='Purging {} heartbeats'.format(len(not_ok))) as bar:
35-
for b in bar:
36-
client.delete_heartbeat(b.id)
36+
elif alert:
37+
with click.progressbar(heartbeats, label='Alerting {} heartbeats'.format(len(heartbeats))) as bar:
38+
for b in bar:
39+
params = dict(filter(lambda a: len(a) == 2, map(lambda a: a.split(':'), b.tags)))
40+
environment = params.get('environment', 'Production')
41+
group = params.get('group', 'System')
42+
tags = list(filter(lambda a: not a.startswith('environment:') and not a.startswith('group:'), b.tags))
3743

38-
elif alert:
39-
with click.progressbar(heartbeats, label='Alerting {} heartbeats'.format(len(heartbeats))) as bar:
40-
for b in bar:
41-
params = dict(filter(lambda a: len(a) == 2, map(lambda a: a.split(':'), b.tags)))
42-
environment = params.get('environment', 'Production')
43-
group = params.get('group', 'System')
44-
tags = list(filter(lambda a: not a.startswith('environment:') and not a.startswith('group:'), b.tags))
45-
46-
if b.status == 'expired': # aka. "stale"
47-
client.send_alert(
48-
resource=b.origin,
49-
event='HeartbeatFail',
50-
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
51-
group=group,
52-
environment=environment,
53-
service=['Alerta'],
54-
severity=severity,
55-
value='{}'.format(b.since),
56-
text='Heartbeat not received in {} seconds'.format(b.timeout),
57-
tags=tags,
58-
type='heartbeatAlert'
59-
)
60-
elif b.status == 'slow':
61-
client.send_alert(
62-
resource=b.origin,
63-
event='HeartbeatSlow',
64-
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
65-
group=group,
66-
environment=environment,
67-
service=['Alerta'],
68-
severity=severity,
69-
value='{}ms'.format(b.latency),
70-
text='Heartbeat took more than {}ms to be processed'.format(MAX_LATENCY),
71-
tags=tags,
72-
type='heartbeatAlert'
73-
)
74-
else:
75-
client.send_alert(
76-
resource=b.origin,
77-
event='HeartbeatOK',
78-
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
79-
group=group,
80-
environment=environment,
81-
service=['Alerta'],
82-
severity='normal',
83-
value='',
84-
text='Heartbeat OK',
85-
tags=tags,
86-
type='heartbeatAlert'
87-
)
44+
if b.status == 'expired': # aka. "stale"
45+
client.send_alert(
46+
resource=b.origin,
47+
event='HeartbeatFail',
48+
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
49+
group=group,
50+
environment=environment,
51+
service=['Alerta'],
52+
severity=severity,
53+
value='{}'.format(b.since),
54+
text='Heartbeat not received in {} seconds'.format(b.timeout),
55+
tags=tags,
56+
type='heartbeatAlert'
57+
)
58+
elif b.status == 'slow':
59+
client.send_alert(
60+
resource=b.origin,
61+
event='HeartbeatSlow',
62+
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
63+
group=group,
64+
environment=environment,
65+
service=['Alerta'],
66+
severity=severity,
67+
value='{}ms'.format(b.latency),
68+
text='Heartbeat took more than {}ms to be processed'.format(MAX_LATENCY),
69+
tags=tags,
70+
type='heartbeatAlert'
71+
)
72+
else:
73+
client.send_alert(
74+
resource=b.origin,
75+
event='HeartbeatOK',
76+
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
77+
group=group,
78+
environment=environment,
79+
service=['Alerta'],
80+
severity='normal',
81+
value='',
82+
text='Heartbeat OK',
83+
tags=tags,
84+
type='heartbeatAlert'
85+
)
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -19,17 +18,16 @@ def cli(obj, ids, filters):
1918
if obj['output'] == 'json':
2019
r = client.http.get('/alerts/history')
2120
click.echo(json.dumps(r['history'], sort_keys=True, indent=4, ensure_ascii=False))
22-
sys.exit(0)
23-
24-
timezone = obj['timezone']
25-
if ids:
26-
query = [('id', x) for x in ids]
2721
else:
28-
query = build_query(filters)
29-
alerts = client.get_history(query)
30-
31-
headers = {'id': 'ID', 'updateTime': 'LAST UPDATED', 'severity': 'SEVERITY', 'status': 'STATUS',
32-
'type': 'TYPE', 'customer': 'CUSTOMER', 'environment': 'ENVIRONMENT', 'service': 'SERVICE',
33-
'resource': 'RESOURCE', 'group': 'GROUP', 'event': 'EVENT', 'value': 'VALUE', 'text': 'TEXT'}
34-
click.echo(
35-
tabulate([a.tabular(timezone) for a in alerts], headers=headers, tablefmt=obj['output']))
22+
timezone = obj['timezone']
23+
if ids:
24+
query = [('id', x) for x in ids]
25+
else:
26+
query = build_query(filters)
27+
alerts = client.get_history(query)
28+
29+
headers = {'id': 'ID', 'updateTime': 'LAST UPDATED', 'severity': 'SEVERITY', 'status': 'STATUS',
30+
'type': 'TYPE', 'customer': 'CUSTOMER', 'environment': 'ENVIRONMENT', 'service': 'SERVICE',
31+
'resource': 'RESOURCE', 'group': 'GROUP', 'event': 'EVENT', 'value': 'VALUE', 'text': 'TEXT'}
32+
click.echo(
33+
tabulate([a.tabular(timezone) for a in alerts], headers=headers, tablefmt=obj['output']))

alertaclient/commands/cmd_keys.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -15,11 +14,10 @@ def cli(obj):
1514
if obj['output'] == 'json':
1615
r = client.http.get('/keys')
1716
click.echo(json.dumps(r['keys'], sort_keys=True, indent=4, ensure_ascii=False))
18-
sys.exit(0)
19-
20-
timezone = obj['timezone']
21-
headers = {
22-
'id': 'ID', 'key': 'API KEY', 'user': 'USER', 'scopes': 'SCOPES', 'text': 'TEXT',
23-
'expireTime': 'EXPIRES', 'count': 'COUNT', 'lastUsedTime': 'LAST USED', 'customer': 'CUSTOMER'
24-
}
25-
click.echo(tabulate([k.tabular(timezone) for k in client.get_keys()], headers=headers, tablefmt=obj['output']))
17+
else:
18+
timezone = obj['timezone']
19+
headers = {
20+
'id': 'ID', 'key': 'API KEY', 'user': 'USER', 'scopes': 'SCOPES', 'text': 'TEXT',
21+
'expireTime': 'EXPIRES', 'count': 'COUNT', 'lastUsedTime': 'LAST USED', 'customer': 'CUSTOMER'
22+
}
23+
click.echo(tabulate([k.tabular(timezone) for k in client.get_keys()], headers=headers, tablefmt=obj['output']))

alertaclient/commands/cmd_perms.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import sys
32
import click
43
import json
54

@@ -15,7 +14,6 @@ def cli(obj):
1514
if obj['output'] == 'json':
1615
r = client.http.get('/perms')
1716
click.echo(json.dumps(r['permissions'], sort_keys=True, indent=4, ensure_ascii=False))
18-
sys.exit(0)
19-
20-
headers = {'id': 'ID', 'scopes': 'SCOPES', 'match': 'ROLE'}
21-
click.echo(tabulate([p.tabular() for p in client.get_perms()], headers=headers, tablefmt=obj['output']))
17+
else:
18+
headers = {'id': 'ID', 'scopes': 'SCOPES', 'match': 'ROLE'}
19+
click.echo(tabulate([p.tabular() for p in client.get_perms()], headers=headers, tablefmt=obj['output']))

0 commit comments

Comments
 (0)