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.
26 package org.openecomp.sparky.analytics;
28 import java.util.HashMap;
29 import java.util.concurrent.atomic.AtomicInteger;
32 * The Class AbstractStatistics.
34 public class AbstractStatistics implements ComponentStatistics {
36 private HashMap<String, AtomicInteger> namedCounters;
37 private HashMap<String, HistogramSampler> namedHistograms;
40 * Instantiates a new abstract statistics.
42 protected AbstractStatistics() {
43 namedCounters = new HashMap<String, AtomicInteger>();
44 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);
69 * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
72 public void pegCounter(String key) {
74 AtomicInteger counter = namedCounters.get(key);
76 if (counter != null) {
77 counter.incrementAndGet();
83 * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
86 public void incrementCounter(String key, int value) {
88 AtomicInteger counter = namedCounters.get(key);
90 if (counter != null) {
91 counter.addAndGet(value);
98 * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String, java.lang.String, long, int, int)
101 public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
102 int numDecimalPoints) {
103 HistogramSampler histSampler = namedHistograms.get(key);
105 if (histSampler == null) {
106 histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
107 namedHistograms.put(key, histSampler);
113 * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
116 public void updateHistogram(String key, long value) {
117 HistogramSampler histSampler = namedHistograms.get(key);
119 if (histSampler != null) {
120 histSampler.track(value);
125 * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
128 public void reset() {
130 for (HistogramSampler h : namedHistograms.values()) {
134 for (AtomicInteger c : namedCounters.values()) {
141 * Gets the counter value.
144 * @return the counter value
146 protected int getCounterValue(String key) {
148 AtomicInteger counter = namedCounters.get(key);
150 if (counter == null) {
154 return counter.get();
159 * Gets the histogram stats.
162 * @param verboseEnabled the verbose enabled
163 * @param indentPadding the indent padding
164 * @return the histogram stats
166 protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
168 HistogramSampler histSampler = namedHistograms.get(key);
170 if (histSampler == null) {
174 return histSampler.getStats(verboseEnabled, indentPadding);