|
14 | 14 | parse_rule, |
15 | 15 | ) |
16 | 16 | import math |
| 17 | +import time |
17 | 18 | from collections import deque, defaultdict |
18 | 19 | from itertools import islice |
19 | 20 |
|
@@ -81,6 +82,7 @@ def __init__(self, reactor, **options): |
81 | 82 | self.waiting = False |
82 | 83 | self.state = {None: "normal", "waiting": "normal", "loading": "normal"} |
83 | 84 | self.history = defaultdict(lambda: sliceable_deque([], self.history_size)) |
| 85 | + self.comparing = {} |
84 | 86 |
|
85 | 87 | LOGGER.info("Alert '%s': has inited", self) |
86 | 88 |
|
@@ -207,7 +209,16 @@ def get_value_for_expr(self, expr, target): |
207 | 209 | rvalue = sum(history) / float(len(history)) |
208 | 210 |
|
209 | 211 | 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 |
211 | 222 |
|
212 | 223 | rvalue = expr['mod'](rvalue) |
213 | 224 | return rvalue |
@@ -283,6 +294,7 @@ def load(self): |
283 | 294 | self.check(data) |
284 | 295 | self.notify('normal', 'Metrics are loaded', target='loading', ntype='common') |
285 | 296 | except Exception as e: |
| 297 | + LOGGER.debug("ee: %s", str(e)) |
286 | 298 | self.notify( |
287 | 299 | self.loading_error, 'Loading error: %s' % e, target='loading', ntype='common') |
288 | 300 | self.waiting = False |
@@ -323,7 +335,7 @@ def get_graph_comparison(self): |
323 | 335 | return value |
324 | 336 | except Exception as e: |
325 | 337 | LOGGER.error('No data to compare: %s', str(e)) |
326 | | - return 0 |
| 338 | + return -1 |
327 | 339 |
|
328 | 340 |
|
329 | 341 | class URLAlert(BaseAlert): |
|
0 commit comments