def __init__(self, history_wrapper, weeks=4, snap_to_bin=True, bin_size='1h', dict_extractor_path=''): self.history_wrapper = history_wrapper self.weeks = weeks self.snap_to_bin = snap_to_bin self.bin_size = parse_timedelta(bin_size) self.dict_extractor_path = dict_extractor_path
def stale_active_alerts(self, multiplier=2, offset='5m'): ''' Returns a list of alerts that weren't executed in a given period of time. The period is calculated using multiplier and offset: check's interval * multiplier + offset. Parameters ---------- multiplier: int Multiplier for check's interval. offset: str Time offset, for details see parse_timedelta function in zmon-worker/src/function/time_.py. Returns ------- list A list of stale active alerts. ''' alert_entities = self.__redis.keys(self.ZMON_ALERTS_ENTITIES_PATTERN) # Load evaluated alerts and their entities from redis. p = self.__redis.pipeline() for key in alert_entities: p.hkeys(key) entities = p.execute() evaluated_alerts = dict((int(key.split(':')[2]), entities[i]) for (i, key) in enumerate(alert_entities)) # Load check results for previously loaded alerts and entities. check_ids = [] for alert in self.__alerts: if alert['id'] in evaluated_alerts: for entity in evaluated_alerts[alert['id']]: p.lindex('zmon:checks:{}:{}'.format(alert['check_id'], entity), 0) check_ids.append('{}:{}'.format(alert['check_id'], entity)) results = p.execute() check_results = dict((check_id, json.loads(results[i])['ts']) for (i, check_id) in enumerate(check_ids) if results[i]) return [{'id': alert['id'], 'team': alert['team'], 'responsible_team': alert['responsible_team']} for alert in self.__alerts if self.__is_alert_stale(alert, evaluated_alerts, check_results, multiplier, parse_timedelta(offset).total_seconds())]