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