1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jumpmind.symmetric.statistic;
21
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.management.Notification;
28
29 import org.jumpmind.symmetric.common.ParameterConstants;
30 import org.jumpmind.symmetric.model.Node;
31 import org.jumpmind.symmetric.model.StatisticAlertThresholds;
32 import org.jumpmind.symmetric.service.INodeService;
33 import org.jumpmind.symmetric.service.INotificationService;
34 import org.jumpmind.symmetric.service.IParameterService;
35 import org.jumpmind.symmetric.service.IStatisticService;
36
37 public class StatisticManager implements IStatisticManager {
38
39 Map<StatisticName, Statistic> statistics;
40
41 INodeService nodeService;
42
43 IStatisticService statisticService;
44
45 INotificationService notificationService;
46
47 IParameterService parameterService;
48
49 synchronized public void init() {
50 if (statistics == null) {
51 refresh(new Date());
52 }
53 }
54
55 synchronized public void flush() {
56 Date captureEndTime = new Date();
57 if (statistics != null) {
58 statisticService.save(statistics.values(), captureEndTime);
59 publishAlerts();
60 }
61 refresh(captureEndTime);
62 }
63
64 /***
65 * Compare the statistics we have cached against the configured statistic
66 * alert thresholds and publish alerts if we fall outside the range.
67 */
68 synchronized protected void publishAlerts() {
69 if (parameterService.is(ParameterConstants.STATISTIC_THRESHOLD_ALERTS_ENABLED) && statistics != null) {
70 List<StatisticAlertThresholds> thresholds = statisticService.getAlertThresholds();
71 if (thresholds != null) {
72 for (StatisticAlertThresholds statisticAlertThresholds : thresholds) {
73 StatisticName name = StatisticName.valueOf(statisticAlertThresholds.getStatisticName());
74 if (name != null) {
75 Notification event = statisticAlertThresholds.outsideOfBoundsNotification(statistics.get(name));
76 if (event != null) {
77 notificationService.sendNotification(event);
78 }
79 }
80 }
81 }
82 }
83 }
84
85 synchronized protected void refresh(Date lastCaptureEndTime) {
86 if (statistics == null) {
87 statistics = new HashMap<StatisticName, Statistic>();
88 }
89
90 statistics.clear();
91
92 Node node = nodeService.findIdentity();
93 String nodeId = "Unknown";
94 if (node != null) {
95 nodeId = node.getNodeId();
96 }
97
98 StatisticName[] all = StatisticName.values();
99 for (StatisticName statisticName : all) {
100 statistics.put(statisticName, new Statistic(statisticName, nodeId, lastCaptureEndTime == null ? new Date()
101 : lastCaptureEndTime));
102 }
103
104 }
105
106 public Statistic getStatistic(StatisticName statisticName) {
107 this.init();
108 return statistics.get(statisticName);
109 }
110
111 public void setNodeService(INodeService nodeService) {
112 this.nodeService = nodeService;
113 }
114
115 public void setStatisticService(IStatisticService statisticService) {
116 this.statisticService = statisticService;
117 }
118
119 public void setNotificationService(INotificationService notificationService) {
120 this.notificationService = notificationService;
121 }
122
123 public void setParameterService(IParameterService parameterService) {
124 this.parameterService = parameterService;
125 }
126 }