Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / analytics / HistoricalCounter.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26
27 package org.openecomp.sparky.analytics;
28
29 /**
30  * A simple class to model a historical counter. A set of values will be tracked and basic
31  * statistics will be calculated in real time (n, min, max, avg).
32  * 
33  * @author davea
34  */
35 public class HistoricalCounter {
36
37   private double min;
38
39   private double max;
40
41   private double totalOfSamples;
42
43   private long numSamples;
44
45   private double value;
46
47   private boolean maintainSingleValue;
48
49   /**
50    * Instantiates a new historical counter.
51    *
52    * @param trackSingleValue the track single value
53    */
54   public HistoricalCounter(boolean trackSingleValue) {
55     min = -1;
56     max = 0;
57     totalOfSamples = 0;
58     value = 0.0;
59     numSamples = 0;
60     this.maintainSingleValue = trackSingleValue;
61   }
62
63   public boolean isSingleValue() {
64     return maintainSingleValue;
65   }
66
67   /**
68    * Update.
69    *
70    * @param value the value
71    */
72   public synchronized void update(double value) {
73
74     if (value < 0) {
75       return;
76     }
77
78     if (maintainSingleValue) {
79
80       this.value = value;
81
82     } else {
83
84       if (min == -1) {
85         min = value;
86       }
87
88       if (value < min) {
89         min = value;
90       }
91
92       if (value > max) {
93         max = value;
94       }
95
96       totalOfSamples += value;
97       numSamples++;
98     }
99   }
100
101   public double getValue() {
102     return value;
103   }
104
105   public double getMin() {
106     return min;
107   }
108
109   public double getMax() {
110     return max;
111   }
112
113   public long getNumSamples() {
114     return numSamples;
115   }
116
117   public double getAvg() {
118     if (numSamples == 0) {
119       return 0;
120     }
121
122     return (totalOfSamples / numSamples);
123   }
124
125   /**
126    * Reset.
127    */
128   public synchronized void reset() {
129     min = -1;
130     max = 0;
131     numSamples = 0;
132     totalOfSamples = 0;
133     value = 0.0;
134   }
135
136   /* (non-Javadoc)
137    * @see java.lang.Object#toString()
138    */
139   @Override
140   public String toString() {
141     StringBuilder sb = new StringBuilder(32);
142
143     if (maintainSingleValue) {
144       sb.append("[ Val=").append(value).append(" ]");
145     } else {
146       sb.append("[ NumSamples=").append(numSamples).append(",");
147       sb.append(" Min=").append(min).append(",");
148       sb.append(" Max=").append(max).append(",");
149       sb.append(" Avg=").append(getAvg()).append(" ]");
150     }
151
152     return sb.toString();
153   }
154
155 }