1
2
3
4
5
6
7
8
9
10
11 import logging
12 log = logging.getLogger('zen.thresholds')
13
15 "Class for holding multiple Thresholds, used in most collectors"
16
18 self.byKey = {}
19 self.byFilename = {}
20 self.byDevice = {}
21
23 d = self.byDevice.get(threshold.context().deviceName, None)
24 if d and threshold.key() in d:
25 del d[threshold.key()]
26 doomed = self.byKey.get(threshold.key(), None)
27 if doomed:
28 del self.byKey[doomed.key()]
29 ctx = doomed.context()
30 for dp in doomed.dataPoints():
31 lst = self.byFilename[ctx.fileKey(dp)]
32 if (doomed, dp) in lst:
33 lst.remove( (doomed, dp) )
34 if not lst:
35 del self.byFilename[ctx.fileKey(dp)]
36 return doomed
37
38 - def add(self, threshold):
39 self.byKey[threshold.key()] = threshold
40 d = self.byDevice.setdefault(threshold.context().deviceName, {})
41 d[threshold.key()] = threshold
42 ctx = threshold.context()
43 for dp in threshold.dataPoints():
44 self.byFilename.setdefault(ctx.fileKey(dp), []).append((threshold, dp))
45
47 "Store a threshold instance for future computation"
48 log.debug("Updating threshold %r", threshold.key())
49 doomed = self.remove(threshold)
50 if doomed:
51 threshold.count = doomed.count
52 self.add(threshold)
53
55 "Store a threshold instance for future computation"
56 for threshold in thresholds:
57 self.update(threshold)
58
61
71
72 - def check(self, filename, timeAt, value):
73 "Check a given threshold based on an updated value"
74 result = []
75 if filename in self.byFilename:
76 log.debug("Checking value %s on %s", value, filename)
77 for t, dp in self.byFilename[filename]:
78 result += t.checkRaw(dp, timeAt, value)
79 return result
80
83
84 if __name__ == '__main__':
85 test()
86