Adding back-end support for UI filters
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / analytics / AbstractStatistics.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.analytics;
24
25 import java.util.HashMap;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 /**
29  * The Class AbstractStatistics.
30  */
31 public class AbstractStatistics implements ComponentStatistics {
32
33   private HashMap<String, AtomicInteger> namedCounters;
34
35   /**
36    * @return the namedCounters
37    */
38   public HashMap<String, AtomicInteger> getNamedCounters() {
39     return namedCounters;
40   }
41
42   /**
43    * @param namedCounters the namedCounters to set
44    */
45   public void setNamedCounters(HashMap<String, AtomicInteger> namedCounters) {
46     this.namedCounters = namedCounters;
47   }
48
49   /**
50    * @return the namedHistograms
51    */
52   public HashMap<String, HistogramSampler> getNamedHistograms() {
53     return namedHistograms;
54   }
55
56   /**
57    * @param namedHistograms the namedHistograms to set
58    */
59   public void setNamedHistograms(HashMap<String, HistogramSampler> namedHistograms) {
60     this.namedHistograms = namedHistograms;
61   }
62
63   private HashMap<String, HistogramSampler> namedHistograms;
64
65   /**
66    * Instantiates a new abstract statistics.
67    */
68   protected AbstractStatistics() {
69     namedCounters = new HashMap<String, AtomicInteger>();
70     namedHistograms = new HashMap<String, HistogramSampler>();
71   }
72
73   /*
74    * (non-Javadoc)
75    * 
76    * @see org.onap.aai.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
77    */
78   /*
79    * sync-lock the creation of counters during initialization, but run time should not use lock
80    * synchronization, only thread safe types
81    * 
82    * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
83    */
84   @Override
85   public synchronized void addCounter(String key) {
86
87     AtomicInteger counter = namedCounters.get(key);
88
89     if (counter == null) {
90       counter = new AtomicInteger(0);
91       namedCounters.put(key, counter);
92     }
93
94   }
95
96   /*
97    * (non-Javadoc)
98    * 
99    * @see org.onap.aai.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
100    */
101   @Override
102   public void pegCounter(String key) {
103
104     AtomicInteger counter = namedCounters.get(key);
105
106     if (counter != null) {
107       counter.incrementAndGet();
108     }
109
110   }
111
112   /*
113    * (non-Javadoc)
114    * 
115    * @see org.onap.aai.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
116    */
117   @Override
118   public void incrementCounter(String key, int value) {
119
120     AtomicInteger counter = namedCounters.get(key);
121
122     if (counter != null) {
123       counter.addAndGet(value);
124     }
125
126   }
127
128
129   /*
130    * (non-Javadoc)
131    * 
132    * @see org.onap.aai.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String,
133    * java.lang.String, long, int, int)
134    */
135   @Override
136   public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
137       int numDecimalPoints) {
138     HistogramSampler histSampler = namedHistograms.get(key);
139
140     if (histSampler == null) {
141       histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
142       namedHistograms.put(key, histSampler);
143     }
144
145   }
146
147   /*
148    * (non-Javadoc)
149    * 
150    * @see org.onap.aai.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
151    */
152   @Override
153   public void updateHistogram(String key, long value) {
154     HistogramSampler histSampler = namedHistograms.get(key);
155
156     if (histSampler != null) {
157       histSampler.track(value);
158     }
159   }
160
161   /*
162    * (non-Javadoc)
163    * 
164    * @see org.onap.aai.sparky.analytics.ComponentStatistics#reset()
165    */
166   @Override
167   public void reset() {
168
169     for (HistogramSampler h : namedHistograms.values()) {
170       h.clear();
171     }
172
173     for (AtomicInteger c : namedCounters.values()) {
174       c.set(0);
175     }
176
177   }
178
179   /**
180    * Gets the counter value.
181    *
182    * @param key the key
183    * @return the counter value
184    */
185   protected int getCounterValue(String key) {
186
187     AtomicInteger counter = namedCounters.get(key);
188
189     if (counter == null) {
190       return -1;
191     }
192
193     return counter.get();
194
195   }
196
197   /**
198    * Gets the histogram stats.
199    *
200    * @param key the key
201    * @param verboseEnabled the verbose enabled
202    * @param indentPadding the indent padding
203    * @return the histogram stats
204    */
205   protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
206
207     HistogramSampler histSampler = namedHistograms.get(key);
208
209     if (histSampler == null) {
210       return null;
211     }
212
213     return histSampler.getStats(verboseEnabled, indentPadding);
214
215   }
216
217
218
219 }