Skip to content

Commit 3cf79fb

Browse files
authored
Skip alerts on failed actions (#192)
1 parent 7c40b30 commit 3cf79fb

7 files changed

Lines changed: 32 additions & 25 deletions

File tree

alertaclient/commands/cmd_ack.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22

3-
from alertaclient.utils import build_query
3+
from alertaclient.utils import action_progressbar, build_query
44

55

66
@click.command('ack', short_help='Acknowledge alerts')
@@ -22,6 +22,4 @@ def cli(obj, ids, query, filters, text):
2222
total, _, _ = client.get_count(query)
2323
ids = [a.id for a in client.get_alerts(query)]
2424

25-
with click.progressbar(ids, label='Acking {} alerts'.format(total)) as bar:
26-
for id in bar:
27-
client.action(id, action='ack', text=text or 'status changed using CLI')
25+
action_progressbar(client, action='ack', ids=ids, label='Acking {} alerts'.format(total), text=text)

alertaclient/commands/cmd_action.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import click
44

5-
from alertaclient.utils import build_query
5+
from alertaclient.utils import action_progressbar, build_query
66

77

88
@click.command('action', short_help='Action alerts')
@@ -26,6 +26,6 @@ def cli(obj, action, ids, query, filters, text):
2626
ids = [a.id for a in client.get_alerts(query)]
2727

2828
action_text = re.sub('([A-Z])', r' \1', action).title() # 'createIssue' => 'Create Issue'
29-
with click.progressbar(ids, label='Action {} {} alerts'.format(action, total)) as bar:
30-
for id in bar:
31-
client.action(id, action=action, text=text or '{} using CLI'.format(action_text))
29+
label = 'Action {} {} alerts'.format(action, total)
30+
text = text or '{} using CLI'.format(action_text)
31+
action_progressbar(client, action=action, ids=ids, label=label, text=text)

alertaclient/commands/cmd_close.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22

3-
from alertaclient.utils import build_query
3+
from alertaclient.utils import action_progressbar, build_query
44

55

66
@click.command('ack', short_help='Close alerts')
@@ -22,6 +22,4 @@ def cli(obj, ids, query, filters, text):
2222
total, _, _ = client.get_count(query)
2323
ids = [a.id for a in client.get_alerts(query)]
2424

25-
with click.progressbar(ids, label='Closing {} alerts'.format(total)) as bar:
26-
for id in bar:
27-
client.action(id, action='close', text=text or 'status changed using CLI')
25+
action_progressbar(client, action='close', ids=ids, label='Closing {} alerts'.format(total), text=text)

alertaclient/commands/cmd_shelve.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import click
22

3-
from alertaclient.utils import build_query
3+
from alertaclient.utils import action_progressbar, build_query
44

55

66
@click.command('shelve', short_help='Shelve alerts')
77
@click.option('--ids', '-i', metavar='UUID', multiple=True, help='List of alert IDs (can use short 8-char id)')
88
@click.option('--query', '-q', 'query', metavar='QUERY', help='severity:"warning" AND resource:web')
99
@click.option('--filter', '-f', 'filters', metavar='FILTER', multiple=True, help='KEY=VALUE eg. serverity=warning resource=web')
10-
@click.option('--timeout', metavar='SECONDS', type=int, help='Seconds before alert auto-unshelved.')
10+
@click.option('--timeout', metavar='SECONDS', type=int, help='Seconds before alert auto-unshelved.', default=7200, show_default=True)
1111
@click.option('--text', help='Message associated with status change')
1212
@click.pass_obj
1313
def cli(obj, ids, query, filters, timeout, text):
@@ -23,6 +23,5 @@ def cli(obj, ids, query, filters, timeout, text):
2323
total, _, _ = client.get_count(query)
2424
ids = [a.id for a in client.get_alerts(query)]
2525

26-
with click.progressbar(ids, label='Shelving {} alerts'.format(total)) as bar:
27-
for id in bar:
28-
client.action(id, action='shelve', text=text or 'status changed using CLI', timeout=timeout)
26+
action_progressbar(client, action='shelve', ids=ids,
27+
label='Shelving {} alerts'.format(total), text=text, timeout=timeout)

alertaclient/commands/cmd_unack.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22

3-
from alertaclient.utils import build_query
3+
from alertaclient.utils import action_progressbar, build_query
44

55

66
@click.command('unack', short_help='Un-acknowledge alerts')
@@ -22,6 +22,4 @@ def cli(obj, ids, query, filters, text):
2222
total, _, _ = client.get_count(query)
2323
ids = [a.id for a in client.get_alerts(query)]
2424

25-
with click.progressbar(ids, label='Un-acking {} alerts'.format(total)) as bar:
26-
for id in bar:
27-
client.action(id, action='unack', text=text or 'status changed using CLI')
25+
action_progressbar(client, action='unack', ids=ids, label='Un-acking {} alerts'.format(total), text=text)

alertaclient/commands/cmd_unshelve.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22

3-
from alertaclient.utils import build_query
3+
from alertaclient.utils import action_progressbar, build_query
44

55

66
@click.command('unshelve', short_help='Un-shelve alerts')
@@ -22,6 +22,4 @@ def cli(obj, ids, query, filters, text):
2222
total, _, _ = client.get_count(query)
2323
ids = [a.id for a in client.get_alerts(query)]
2424

25-
with click.progressbar(ids, label='Un-shelving {} alerts'.format(total)) as bar:
26-
for id in bar:
27-
client.action(id, action='unshelve', text=text or 'status changed using CLI')
25+
action_progressbar(client, 'unshelve', ids, label='Un-shelving {} alerts'.format(total), text=text)

alertaclient/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import json
44

5+
import click
56
import pytz
67
import six
78

@@ -41,3 +42,18 @@ def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
4142

4243
def build_query(filters):
4344
return [tuple(f.split('=', 1)) for f in filters if '=' in f]
45+
46+
47+
def action_progressbar(client, action, ids, label, text=None, timeout=None):
48+
skipped = 0
49+
50+
def show_skipped(id):
51+
if not id and skipped:
52+
return '(skipped {})'.format(skipped)
53+
54+
with click.progressbar(ids, label=label, show_eta=True, item_show_func=show_skipped) as bar:
55+
for id in bar:
56+
try:
57+
client.action(id, action=action, text=text or 'status changed using CLI', timeout=timeout)
58+
except Exception as e:
59+
skipped += 1

0 commit comments

Comments
 (0)