Skip to content

Commit 82839df

Browse files
author
delong1
committed
added: dict comparing
1 parent 475b8e4 commit 82839df

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

graphite_beacon/alerts.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
parse_rule,
1515
)
1616
import math
17+
import time
1718
from collections import deque, defaultdict
1819
from itertools import islice
1920

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

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

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

209211
if rvalue == COMPARISON:
210-
rvalue = self.get_graph_comparison()
212+
current_time = int(time.time())
213+
if self.comparing.has_key(target) and self.comparing[target]['time'] == current_time:
214+
rvalue = self.comparing[target]['value']
215+
else:
216+
rvalue = self.get_graph_comparison()
217+
if rvalue != -1:
218+
if not self.comparing.has_key(target):
219+
self.comparing[target] = {}
220+
self.comparing[target]['value'] = rvalue
221+
self.comparing[target]['time'] = current_time
211222

212223
rvalue = expr['mod'](rvalue)
213224
return rvalue
@@ -283,6 +294,7 @@ def load(self):
283294
self.check(data)
284295
self.notify('normal', 'Metrics are loaded', target='loading', ntype='common')
285296
except Exception as e:
297+
LOGGER.debug("ee: %s", str(e))
286298
self.notify(
287299
self.loading_error, 'Loading error: %s' % e, target='loading', ntype='common')
288300
self.waiting = False
@@ -323,7 +335,7 @@ def get_graph_comparison(self):
323335
return value
324336
except Exception as e:
325337
LOGGER.error('No data to compare: %s', str(e))
326-
return 0
338+
return -1
327339

328340

329341
class URLAlert(BaseAlert):

0 commit comments

Comments
 (0)