Commit e47b6b5f authored by Patrick Godwin's avatar Patrick Godwin
Browse files

clean up some transforms in serve.py, move to transforms.py

parent 302080ca
......@@ -21,7 +21,7 @@ import bottle
import numpy
import yaml
from . import utils
from . import transforms, utils
from .io import hdf5, influx
#-------------------------------------------------
......@@ -403,7 +403,6 @@ def serve_nagios(check):
### alert tracking
alert_values = []
bad_status = 0
now = utils.gps_now()
### retrieve data
......@@ -414,18 +413,14 @@ def serve_nagios(check):
time, data = consumer.retrieve_timeseries(measurement, start, end, column, tags=retrieve_tags, aggregate=aggregate, dt=dt)
if alert_type == 'heartbeat':
if time:
alert_values.append(max(now - time[-1], 0))
else:
bad_status += 1
alert_values.append(transforms.latency(time, data, numpy.nan, t0=now))
elif alert_type == 'threshold':
if data:
max_data = max(data)
if max_data >= nagios_config['alert_settings']['threshold']:
bad_status += 1
threshold = nagios_config['alert_settings']['threshold']
alert_values.append(transforms.cutoff(time, data, numpy.nan, threshold))
### format nagios response
alert_values = numpy.array(alert_values)
bad_status = numpy.sum(numpy.isnan(alert_values))
if bad_status:
if alert_type == 'heartbeat':
text_status = "{num_tags} {alert_tag} more than {lookback} seconds behind".format(
......@@ -446,7 +441,7 @@ def serve_nagios(check):
else:
if alert_type == 'heartbeat':
text_status = "OK: Max delay: {delay} seconds".format(delay=max(alert_values))
text_status = "OK: Max delay: {delay} seconds".format(delay=numpy.nanmax(alert_values))
elif alert_type == 'threshold':
text_status = "OK: No {alert_tag}s above {column} threshold = {threshold} {units} from gps times: {start} - {end}".format(
alert_tag=schema['tag_key'],
......@@ -502,21 +497,13 @@ def parse_query(query):
return results
def transform_data(time_value, data_value, transform, default_value, now):
def transform_data(time, data, transform, default, now):
if transform == 'none':
if data_value:
return data_value
else:
return default_value
return transforms.identity(time, data, default)
elif transform == 'latency':
if isinstance(time_value, list):
return max(now - time_value[-1], 0)
elif time_value:
return max(now - time_value, 0)
else:
return default_value
return transforms.latency(time, data, default, t0=now)
else:
raise NotImplementedError('transform option not known/implemented, only "none" or "latency" are accepted right now')
raise NotImplementedError('transform {} not known/implemented'.format(transform))
def config_to_consumer(config):
......
#!/usr/bin/env python
__author__ = "Patrick Godwin (patrick.godwin@ligo.org)"
__description__ = "a module for transform utilities"
#-------------------------------------------------
### imports
from . import utils
#-------------------------------------------------
### transforms
def identity(time, data, default):
return data if data else default
def latency(time, data, default, t0=None):
if t0 is None:
t0 = utils.gps_now()
if time and isinstance(time, list):
return max(t0 - time[-1], 0)
elif time:
return max(t0 - time, 0)
else:
return default
def cutoff(time, data, default, cutoff):
if data:
max_data = max(data)
if max_data < cutoff:
return max_data
else:
return default
else:
return default
Markdown is supported
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