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.openecomp.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;
34 private HashMap<String, HistogramSampler> namedHistograms;
37 * Instantiates a new abstract statistics.
39 protected AbstractStatistics() {
40 namedCounters = new HashMap<String, AtomicInteger>();
41 namedHistograms = new HashMap<String, HistogramSampler>();
45 * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
48 * sync-lock the creation of counters during initialization, but run time should not use lock
49 * synchronization, only thread safe types
51 * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
54 public synchronized void addCounter(String key) {
56 AtomicInteger counter = namedCounters.get(key);
58 if (counter == null) {
59 counter = new AtomicInteger(0);
60 namedCounters.put(key, counter);
66 * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
69 public void pegCounter(String key) {
71 AtomicInteger counter = namedCounters.get(key);
73 if (counter != null) {
74 counter.incrementAndGet();
80 * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
83 public void incrementCounter(String key, int value) {
85 AtomicInteger counter = namedCounters.get(key);
87 if (counter != null) {
88 counter.addAndGet(value);
95 * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String, java.lang.String, long, int, int)
98 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
99 int numDecimalPoints) {
100 HistogramSampler histSampler = namedHistograms.get(key);
102 if (histSampler == null) {
103 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
104 namedHistograms.put(key, histSampler);
110 * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
113 public void updateHistogram(String key, long value) {
114 HistogramSampler histSampler = namedHistograms.get(key);
116 if (histSampler != null) {
117 histSampler.track(value);
122 * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
125 public void reset() {
127 for (HistogramSampler h : namedHistograms.values()) {
131 for (AtomicInteger c : namedCounters.values()) {
138 * Gets the counter value.
141 * @return the counter value
143 protected int getCounterValue(String key) {
145 AtomicInteger counter = namedCounters.get(key);
147 if (counter == null) {
151 return counter.get();
156 * Gets the histogram stats.
159 * @param verboseEnabled the verbose enabled
160 * @param indentPadding the indent padding
161 * @return the histogram stats
163 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
165 HistogramSampler histSampler = namedHistograms.get(key);
167 if (histSampler == null) {
171 return histSampler.getStats(verboseEnabled, indentPadding);