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