2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.analytics;
23 import java.util.HashMap;
24 import java.util.concurrent.atomic.AtomicInteger;
27 * The Class AbstractStatistics.
29 public class AbstractStatistics implements ComponentStatistics {
31 private HashMap<String, AtomicInteger> namedCounters;
32 private HashMap<String, HistogramSampler> namedHistograms;
35 * Instantiates a new abstract statistics.
37 protected AbstractStatistics() {
38 namedCounters = new HashMap<String, AtomicInteger>();
39 namedHistograms = new HashMap<String, HistogramSampler>();
43 * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
46 * sync-lock the creation of counters during initialization, but run time should not use lock
47 * synchronization, only thread safe types
51 public synchronized void addCounter(String key) {
53 AtomicInteger counter = namedCounters.get(key);
55 if (counter == null) {
56 counter = new AtomicInteger(0);
57 namedCounters.put(key, counter);
63 * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
66 public void pegCounter(String key) {
68 AtomicInteger counter = namedCounters.get(key);
70 if (counter != null) {
71 counter.incrementAndGet();
77 * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
80 public void incrementCounter(String key, int value) {
82 AtomicInteger counter = namedCounters.get(key);
84 if (counter != null) {
85 counter.addAndGet(value);
92 * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String, java.lang.String, long, int, int)
95 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
96 int numDecimalPoints) {
97 HistogramSampler histSampler = namedHistograms.get(key);
99 if (histSampler == null) {
100 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
101 namedHistograms.put(key, histSampler);
107 * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
110 public void updateHistogram(String key, long value) {
111 HistogramSampler histSampler = namedHistograms.get(key);
113 if (histSampler != null) {
114 histSampler.track(value);
119 * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
122 public void reset() {
124 for (HistogramSampler h : namedHistograms.values()) {
128 for (AtomicInteger c : namedCounters.values()) {
135 * Gets the counter value.
138 * @return the counter value
140 protected int getCounterValue(String key) {
142 AtomicInteger counter = namedCounters.get(key);
144 if (counter == null) {
148 return counter.get();
153 * Gets the histogram stats.
156 * @param verboseEnabled the verbose enabled
157 * @param indentPadding the indent padding
158 * @return the histogram stats
160 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
162 HistogramSampler histSampler = namedHistograms.get(key);
164 if (histSampler == null) {
168 return histSampler.getStats(verboseEnabled, indentPadding);