Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / analytics / AbstractStatistics.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 package org.onap.aai.sparky.analytics;
26
27 import java.util.HashMap;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 /**
31  * The Class AbstractStatistics.
32  */
33 public class AbstractStatistics implements ComponentStatistics {
34
35   private HashMap<String, AtomicInteger> namedCounters;
36   private HashMap<String, HistogramSampler> namedHistograms;
37
38   /**
39    * Instantiates a new abstract statistics.
40    */
41   protected AbstractStatistics() {
42     namedCounters = new HashMap<String, AtomicInteger>();
43     namedHistograms = new HashMap<String, HistogramSampler>();
44   }
45
46   /* (non-Javadoc)
47    * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
48    */
49   /*
50    * sync-lock the creation of counters during initialization, but run time should not use lock
51    * synchronization, only thread safe types
52    * 
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 }