View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.model;
21  
22  import java.math.BigDecimal;
23  
24  import javax.management.Notification;
25  
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  import org.jumpmind.symmetric.statistic.Statistic;
29  
30  public class StatisticAlertThresholds {
31  
32      String statisticName;
33      BigDecimal thresholdTotalMax;
34      Long thresholdCountMax;
35      BigDecimal thresholdTotalMin;
36      Long thresholdCountMin;
37      BigDecimal thresholdAvgMax;
38      BigDecimal thresholdAvgMin;
39      static long sequenceNumber = System.currentTimeMillis();
40  
41      public StatisticAlertThresholds() {
42      }
43  
44      public StatisticAlertThresholds(String statisticName, BigDecimal threshholdTotalMax, Long threshholdCountMax,
45              BigDecimal threshholdTotalMin, Long threshholdCountMin, BigDecimal threshholdAvgMax,
46              BigDecimal threshholdAvgMin) {
47          super();
48          this.statisticName = statisticName;
49          this.thresholdTotalMax = threshholdTotalMax;
50          this.thresholdCountMax = threshholdCountMax;
51          this.thresholdTotalMin = threshholdTotalMin;
52          this.thresholdCountMin = threshholdCountMin;
53          this.thresholdAvgMax = threshholdAvgMax;
54          this.thresholdAvgMin = threshholdAvgMin;
55      }
56  
57      public String getStatisticName() {
58          return statisticName;
59      }
60  
61      public void setStatisticName(String statisticName) {
62          this.statisticName = statisticName;
63      }
64  
65      public BigDecimal getThresholdTotalMax() {
66          return thresholdTotalMax == null ? BigDecimal.ZERO : thresholdTotalMax;
67      }
68  
69      public void setThresholdTotalMax(BigDecimal threshholdTotalMax) {
70          this.thresholdTotalMax = threshholdTotalMax;
71      }
72  
73      public Long getThresholdCountMax() {
74          return thresholdCountMax == null ? 0l : thresholdCountMax;
75      }
76  
77      public void setThresholdCountMax(Long threshholdCountMax) {
78          this.thresholdCountMax = threshholdCountMax;
79      }
80  
81      public BigDecimal getThresholdTotalMin() {
82          return thresholdTotalMin == null ? BigDecimal.ZERO : thresholdTotalMin;
83      }
84  
85      public void setThresholdTotalMin(BigDecimal threshholdTotalMin) {
86          this.thresholdTotalMin = threshholdTotalMin;
87      }
88  
89      public Long getThresholdCountMin() {
90          return thresholdCountMin == null ? 0l : thresholdCountMin;
91      }
92  
93      public void setThresholdCountMin(Long threshholdCountMin) {
94          this.thresholdCountMin = threshholdCountMin;
95      }
96  
97      public Notification outsideOfBoundsNotification(Statistic stats) {
98          if (stats != null && stats.getName().name().equals(statisticName)) {
99              boolean createNotification = false;
100             StringBuilder msg = new StringBuilder(statisticName);
101             long count = stats.getCount();
102             BigDecimal total = stats.getTotal();
103             BigDecimal avg = stats.getAverageValue();
104             if ((thresholdCountMax != null && thresholdCountMax > 0 && count > thresholdCountMax)
105                     || (thresholdCountMin != null && thresholdCountMin > 0 && count < thresholdCountMin)) {
106                 msg.append(":count=");
107                 msg.append(count);
108                 createNotification = true;
109             }
110 
111             if ((thresholdTotalMax != null && thresholdTotalMax.compareTo(BigDecimal.ZERO) > 0 && total
112                     .compareTo(thresholdTotalMax) > 0)
113                     || (thresholdTotalMin != null && thresholdTotalMin.compareTo(BigDecimal.ZERO) > 0 && total
114                             .compareTo(thresholdTotalMin) < 0)) {
115                 msg.append(":total=");
116                 msg.append(total);
117                 createNotification = true;
118             }
119 
120             if ((thresholdAvgMax != null && thresholdAvgMax.compareTo(BigDecimal.ZERO) > 0 && avg
121                     .compareTo(thresholdAvgMax) > 0)
122                     || (thresholdAvgMin != null && thresholdAvgMin.compareTo(BigDecimal.ZERO) > 0 && avg
123                             .compareTo(thresholdAvgMin) < 0)) {
124                 msg.append(":avg=");
125                 msg.append(avg);
126                 createNotification = true;
127             }
128 
129             if (createNotification) {
130                 return new Notification("SymmetricDS:Alert", stats, sequenceNumber++, System.currentTimeMillis(), msg
131                         .toString());
132             }
133 
134         }
135         return null;
136     }
137 
138     public BigDecimal getThresholdAvgMax() {
139         return thresholdAvgMax == null ? BigDecimal.ZERO : thresholdAvgMax;
140     }
141 
142     public void setThresholdAvgMax(BigDecimal threshholdAvgMax) {
143         this.thresholdAvgMax = threshholdAvgMax;
144     }
145 
146     public BigDecimal getThresholdAvgMin() {
147         return thresholdAvgMin == null ? BigDecimal.ZERO : thresholdAvgMin;
148     }
149 
150     public void setThresholdAvgMin(BigDecimal threshholdAvgMin) {
151         this.thresholdAvgMin = threshholdAvgMin;
152     }
153 
154     @Override
155     public int hashCode() {
156         return new HashCodeBuilder(17, 37).append(statisticName).append(getThresholdTotalMax()).append(
157                 getThresholdCountMax()).append(getThresholdTotalMin()).append(getThresholdCountMin()).append(
158                 getThresholdAvgMax()).append(getThresholdAvgMin()).toHashCode();
159 
160     }
161 
162     @Override
163     public boolean equals(Object obj) {
164         if (obj instanceof StatisticAlertThresholds == false) {
165             return false;
166         }
167         if (this == obj) {
168             return true;
169         }
170         StatisticAlertThresholds rhs = (StatisticAlertThresholds) obj;
171         return new EqualsBuilder().append(statisticName, rhs.statisticName).append(getThresholdTotalMax(),
172                 rhs.getThresholdTotalMax()).append(getThresholdCountMax(), rhs.getThresholdCountMax()).append(
173                 getThresholdTotalMin(), rhs.getThresholdTotalMin()).append(getThresholdCountMin(),
174                 rhs.getThresholdCountMin()).append(getThresholdAvgMax(), rhs.getThresholdAvgMax()).append(
175                 getThresholdAvgMin(), rhs.getThresholdAvgMin()).isEquals();
176     }
177 
178 }