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>();
46 * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
49 * sync-lock the creation of counters during initialization, but run time should not use lock
50 * synchronization, only thread safe types
52 * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
55 public synchronized void addCounter(String key) {
57 AtomicInteger counter = namedCounters.get(key);
59 if (counter == null) {
60 counter = new AtomicInteger(0);
61 namedCounters.put(key, counter);
67 * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
70 public void pegCounter(String key) {
72 AtomicInteger counter = namedCounters.get(key);
74 if (counter != null) {
75 counter.incrementAndGet();
81 * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
84 public void incrementCounter(String key, int value) {
86 AtomicInteger counter = namedCounters.get(key);
88 if (counter != null) {
89 counter.addAndGet(value);
96 * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String, java.lang.String, long, int, int)
99 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
100 int numDecimalPoints) {
101 HistogramSampler histSampler = namedHistograms.get(key);
103 if (histSampler == null) {
104 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
105 namedHistograms.put(key, histSampler);
111 * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
114 public void updateHistogram(String key, long value) {
115 HistogramSampler histSampler = namedHistograms.get(key);
117 if (histSampler != null) {
118 histSampler.track(value);
123 * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
126 public void reset() {
128 for (HistogramSampler h : namedHistograms.values()) {
132 for (AtomicInteger c : namedCounters.values()) {
139 * Gets the counter value.
142 * @return the counter value
144 protected int getCounterValue(String key) {
146 AtomicInteger counter = namedCounters.get(key);
148 if (counter == null) {
152 return counter.get();
157 * Gets the histogram stats.
160 * @param verboseEnabled the verbose enabled
161 * @param indentPadding the indent padding
162 * @return the histogram stats
164 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
166 HistogramSampler histSampler = namedHistograms.get(key);
168 if (histSampler == null) {
172 return histSampler.getStats(verboseEnabled, indentPadding);