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