2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.analytics;
25 import java.util.HashMap;
26 import java.util.concurrent.atomic.AtomicInteger;
29 * The Class AbstractStatistics.
31 public class AbstractStatistics implements ComponentStatistics {
33 private HashMap<String, AtomicInteger> namedCounters;
36 * @return the namedCounters
38 public HashMap<String, AtomicInteger> getNamedCounters() {
43 * @param namedCounters the namedCounters to set
45 public void setNamedCounters(HashMap<String, AtomicInteger> namedCounters) {
46 this.namedCounters = namedCounters;
50 * @return the namedHistograms
52 public HashMap<String, HistogramSampler> getNamedHistograms() {
53 return namedHistograms;
57 * @param namedHistograms the namedHistograms to set
59 public void setNamedHistograms(HashMap<String, HistogramSampler> namedHistograms) {
60 this.namedHistograms = namedHistograms;
63 private HashMap<String, HistogramSampler> namedHistograms;
66 * Instantiates a new abstract statistics.
68 protected AbstractStatistics() {
69 namedCounters = new HashMap<String, AtomicInteger>();
70 namedHistograms = new HashMap<String, HistogramSampler>();
76 * @see org.onap.aai.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
79 * sync-lock the creation of counters during initialization, but run time should not use lock
80 * synchronization, only thread safe types
82 * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
85 public synchronized void addCounter(String key) {
87 AtomicInteger counter = namedCounters.get(key);
89 if (counter == null) {
90 counter = new AtomicInteger(0);
91 namedCounters.put(key, counter);
99 * @see org.onap.aai.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
102 public void pegCounter(String key) {
104 AtomicInteger counter = namedCounters.get(key);
106 if (counter != null) {
107 counter.incrementAndGet();
115 * @see org.onap.aai.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
118 public void incrementCounter(String key, int value) {
120 AtomicInteger counter = namedCounters.get(key);
122 if (counter != null) {
123 counter.addAndGet(value);
132 * @see org.onap.aai.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String,
133 * java.lang.String, long, int, int)
136 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
137 int numDecimalPoints) {
138 HistogramSampler histSampler = namedHistograms.get(key);
140 if (histSampler == null) {
141 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
142 namedHistograms.put(key, histSampler);
150 * @see org.onap.aai.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
153 public void updateHistogram(String key, long value) {
154 HistogramSampler histSampler = namedHistograms.get(key);
156 if (histSampler != null) {
157 histSampler.track(value);
164 * @see org.onap.aai.sparky.analytics.ComponentStatistics#reset()
167 public void reset() {
169 for (HistogramSampler h : namedHistograms.values()) {
173 for (AtomicInteger c : namedCounters.values()) {
180 * Gets the counter value.
183 * @return the counter value
185 protected int getCounterValue(String key) {
187 AtomicInteger counter = namedCounters.get(key);
189 if (counter == null) {
193 return counter.get();
198 * Gets the histogram stats.
201 * @param verboseEnabled the verbose enabled
202 * @param indentPadding the indent padding
203 * @return the histogram stats
205 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
207 HistogramSampler histSampler = namedHistograms.get(key);
209 if (histSampler == null) {
213 return histSampler.getStats(verboseEnabled, indentPadding);