6e7d85473993e990d386ce036587f986e9d0d285
[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
24 package org.onap.aai.sparky.analytics;
25
26 import java.util.HashMap;
27 import java.util.concurrent.atomic.AtomicInteger;
28
29 /**
30  * The Class AbstractStatistics.
31  */
32 public class AbstractStatistics implements ComponentStatistics {
33
34   private HashMap<String, AtomicInteger> namedCounters;
35   private HashMap<String, HistogramSampler> namedHistograms;
36
37   /**
38    * Instantiates a new abstract statistics.
39    */
40   protected AbstractStatistics() {
41     namedCounters = new HashMap<String, AtomicInteger>();
42     namedHistograms = new HashMap<String, HistogramSampler>();
43   }
44
45   /*
46    * (non-Javadoc)
47    * 
48    * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
49    */
50   /*
51    * sync-lock the creation of counters during initialization, but run time should not use lock
52    * synchronization, only thread safe types
53    * 
54    * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
55    */
56   @Override
57   public synchronized void addCounter(String key) {
58
59     AtomicInteger counter = namedCounters.get(key);
60
61     if (counter == null) {
62       counter = new AtomicInteger(0);
63       namedCounters.put(key, counter);
64     }
65
66   }
67
68   /*
69    * (non-Javadoc)
70    * 
71    * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
72    */
73   @Override
74   public void pegCounter(String key) {
75
76     AtomicInteger counter = namedCounters.get(key);
77
78     if (counter != null) {
79       counter.incrementAndGet();
80     }
81
82   }
83
84   /*
85    * (non-Javadoc)
86    * 
87    * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
88    */
89   @Override
90   public void incrementCounter(String key, int value) {
91
92     AtomicInteger counter = namedCounters.get(key);
93
94     if (counter != null) {
95       counter.addAndGet(value);
96     }
97
98   }
99
100
101   /*
102    * (non-Javadoc)
103    * 
104    * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String,
105    * java.lang.String, long, int, int)
106    */
107   @Override
108   public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
109       int numDecimalPoints) {
110     HistogramSampler histSampler = namedHistograms.get(key);
111
112     if (histSampler == null) {
113       histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
114       namedHistograms.put(key, histSampler);
115     }
116
117   }
118
119   /*
120    * (non-Javadoc)
121    * 
122    * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
123    */
124   @Override
125   public void updateHistogram(String key, long value) {
126     HistogramSampler histSampler = namedHistograms.get(key);
127
128     if (histSampler != null) {
129       histSampler.track(value);
130     }
131   }
132
133   /*
134    * (non-Javadoc)
135    * 
136    * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
137    */
138   @Override
139   public void reset() {
140
141     for (HistogramSampler h : namedHistograms.values()) {
142       h.clear();
143     }
144
145     for (AtomicInteger c : namedCounters.values()) {
146       c.set(0);
147     }
148
149   }
150
151   /**
152    * Gets the counter value.
153    *
154    * @param key the key
155    * @return the counter value
156    */
157   protected int getCounterValue(String key) {
158
159     AtomicInteger counter = namedCounters.get(key);
160
161     if (counter == null) {
162       return -1;
163     }
164
165     return counter.get();
166
167   }
168
169   /**
170    * Gets the histogram stats.
171    *
172    * @param key the key
173    * @param verboseEnabled the verbose enabled
174    * @param indentPadding the indent padding
175    * @return the histogram stats
176    */
177   protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
178
179     HistogramSampler histSampler = namedHistograms.get(key);
180
181     if (histSampler == null) {
182       return null;
183     }
184
185     return histSampler.getStats(verboseEnabled, indentPadding);
186
187   }
188
189
190
191 }