2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.analytics;
27 import java.util.HashMap;
28 import java.util.concurrent.atomic.AtomicInteger;
31 * The Class AbstractStatistics.
33 public class AbstractStatistics implements ComponentStatistics {
35 private HashMap<String, AtomicInteger> namedCounters;
36 private HashMap<String, HistogramSampler> namedHistograms;
39 * Instantiates a new abstract statistics.
41 protected AbstractStatistics() {
42 namedCounters = new HashMap<String, AtomicInteger>();
43 namedHistograms = new HashMap<String, HistogramSampler>();
47 * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
50 * sync-lock the creation of counters during initialization, but run time should not use lock
51 * synchronization, only thread safe types
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);