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.
24 package org.onap.aai.sparky.analytics;
26 import java.util.HashMap;
27 import java.util.concurrent.atomic.AtomicInteger;
30 * The Class AbstractStatistics.
32 public class AbstractStatistics implements ComponentStatistics {
34 private HashMap<String, AtomicInteger> namedCounters;
35 private HashMap<String, HistogramSampler> namedHistograms;
38 * Instantiates a new abstract statistics.
40 protected AbstractStatistics() {
41 namedCounters = new HashMap<String, AtomicInteger>();
42 namedHistograms = new HashMap<String, HistogramSampler>();
48 * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
51 * sync-lock the creation of counters during initialization, but run time should not use lock
52 * synchronization, only thread safe types
54 * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
57 public synchronized void addCounter(String key) {
59 AtomicInteger counter = namedCounters.get(key);
61 if (counter == null) {
62 counter = new AtomicInteger(0);
63 namedCounters.put(key, counter);
71 * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
74 public void pegCounter(String key) {
76 AtomicInteger counter = namedCounters.get(key);
78 if (counter != null) {
79 counter.incrementAndGet();
87 * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
90 public void incrementCounter(String key, int value) {
92 AtomicInteger counter = namedCounters.get(key);
94 if (counter != null) {
95 counter.addAndGet(value);
104 * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String,
105 * java.lang.String, long, int, int)
108 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
109 int numDecimalPoints) {
110 HistogramSampler histSampler = namedHistograms.get(key);
112 if (histSampler == null) {
113 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
114 namedHistograms.put(key, histSampler);
122 * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
125 public void updateHistogram(String key, long value) {
126 HistogramSampler histSampler = namedHistograms.get(key);
128 if (histSampler != null) {
129 histSampler.track(value);
136 * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
139 public void reset() {
141 for (HistogramSampler h : namedHistograms.values()) {
145 for (AtomicInteger c : namedCounters.values()) {
152 * Gets the counter value.
155 * @return the counter value
157 protected int getCounterValue(String key) {
159 AtomicInteger counter = namedCounters.get(key);
161 if (counter == null) {
165 return counter.get();
170 * Gets the histogram stats.
173 * @param verboseEnabled the verbose enabled
174 * @param indentPadding the indent padding
175 * @return the histogram stats
177 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
179 HistogramSampler histSampler = namedHistograms.get(key);
181 if (histSampler == null) {
185 return histSampler.getStats(verboseEnabled, indentPadding);