Skip to content

Commit

Permalink
added: dict comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
delong1 committed Jun 27, 2016
1 parent 475b8e4 commit 82839df
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions graphite_beacon/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
parse_rule,
)
import math
import time
from collections import deque, defaultdict
from itertools import islice

Expand Down Expand Up @@ -81,6 +82,7 @@ def __init__(self, reactor, **options):
self.waiting = False
self.state = {None: "normal", "waiting": "normal", "loading": "normal"}
self.history = defaultdict(lambda: sliceable_deque([], self.history_size))
self.comparing = {}

LOGGER.info("Alert '%s': has inited", self)

Expand Down Expand Up @@ -207,7 +209,16 @@ def get_value_for_expr(self, expr, target):
rvalue = sum(history) / float(len(history))

if rvalue == COMPARISON:
rvalue = self.get_graph_comparison()
current_time = int(time.time())
if self.comparing.has_key(target) and self.comparing[target]['time'] == current_time:
rvalue = self.comparing[target]['value']
else:
rvalue = self.get_graph_comparison()
if rvalue != -1:
if not self.comparing.has_key(target):
self.comparing[target] = {}
self.comparing[target]['value'] = rvalue
self.comparing[target]['time'] = current_time

rvalue = expr['mod'](rvalue)
return rvalue
Expand Down Expand Up @@ -283,6 +294,7 @@ def load(self):
self.check(data)
self.notify('normal', 'Metrics are loaded', target='loading', ntype='common')
except Exception as e:
LOGGER.debug("ee: %s", str(e))
self.notify(
self.loading_error, 'Loading error: %s' % e, target='loading', ntype='common')
self.waiting = False
Expand Down Expand Up @@ -323,7 +335,7 @@ def get_graph_comparison(self):
return value
except Exception as e:
LOGGER.error('No data to compare: %s', str(e))
return 0
return -1


class URLAlert(BaseAlert):
Expand Down

0 comments on commit 82839df

Please sign in to comment.