Commit bc0c3063 authored by kaiyou's avatar kaiyou
Browse files

Add a hook for normalizing error messages and one for displaying proper status

parent a69e019f
Pipeline #162 passed with stage
in 1 minute and 36 seconds
......@@ -30,13 +30,25 @@ def populate(context, args, key, table, replace=True):
return (context, args)
def recurrence(context, result, fields=["up"], dest="stable", dest_prev=None):
def normalize(context, result, success="message", error="message", up="up"):
""" This hook is quasi-mandatory, as it normalizes the result to a dictionary
for further use.
if isinstance(result, Exception):
result = {up: False, error: str(result)}
elif not type(result) is dict:
result = {up: True, message: result}
return context, result
def recurrence(context, result, fields={"up"}, dest="stable", dest_prev=None):
""" Compute the recurrence of a the value of given fields
recur = {}
prev = {}
fields = set(fields)
if dest in context:
fields = fields + list(context[dest])
fields |= set(context[dest])
if type(context[dest]) is dict:
for field in fields:
......@@ -46,9 +58,23 @@ def recurrence(context, result, fields=["up"], dest="stable", dest_prev=None):
else 0
prev[field] = context.get(field, None)
context[dest] = recur
if prev is not None:
context[dest_prev] = prev
result[dest] = recur
if dest_prev is not None:
result[dest_prev] = prev
return context, result
def status(context, result, up, down, recur="stable", field="up", dest="status"):
""" Converts the recurrence of the given field to a status based on thresholds
The simplest example will set the status to 'up' or 'down', more complex examples
might detect flapping or transient states.
recurrence = result.get(recur, {}).get(field, 0)
scale = up if result.get(field, False) else down
for level in reversed(sorted(scale.keys())):
if recurrence >= level:
result[dest] = scale[level]
return context, result
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment