Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / analytics / AbstractStatistics.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.analytics;
27
28 import java.util.HashMap;
29 import java.util.concurrent.atomic.AtomicInteger;
30
31 /**
32  * The Class AbstractStatistics.
33  */
34 public class AbstractStatistics implements ComponentStatistics {
35
36   private HashMap<String, AtomicInteger> namedCounters;
37   private HashMap<String, HistogramSampler> namedHistograms;
38
39   /**
40    * Instantiates a new abstract statistics.
41    */
42   protected AbstractStatistics() {
43     namedCounters = new HashMap<String, AtomicInteger>();
44     namedHistograms = new HashMap<String, HistogramSampler>();
45   }
46
47   /* (non-Javadoc)
48    * @see org.openecomp.sparky.analytics.ComponentStatistics#addCounter(java.lang.String)
49    */
50   /*
51    * sync-lock the creation of counters during initialization, but run time should not use lock
52    * synchronization, only thread safe types
53    * 
54    * @see com.att.ecomp.uicommon.resolver.stat.ComponentStatistics#addCounter(java.lang.String)
55    */
56   @Override
57   public synchronized void addCounter(String key) {
58
59     AtomicInteger counter = namedCounters.get(key);
60
61     if (counter == null) {
62       counter = new AtomicInteger(0);
63       namedCounters.put(key, counter);
64     }
65
66   }
67
68   /* (non-Javadoc)
69    * @see org.openecomp.sparky.analytics.ComponentStatistics#pegCounter(java.lang.String)
70    */
71   @Override
72   public void pegCounter(String key) {
73
74     AtomicInteger counter = namedCounters.get(key);
75
76     if (counter != null) {
77       counter.incrementAndGet();
78     }
79
80   }
81
82   /* (non-Javadoc)
83    * @see org.openecomp.sparky.analytics.ComponentStatistics#incrementCounter(java.lang.String, int)
84    */
85   @Override
86   public void incrementCounter(String key, int value) {
87
88     AtomicInteger counter = namedCounters.get(key);
89
90     if (counter != null) {
91       counter.addAndGet(value);
92     }
93
94   }
95
96
97   /* (non-Javadoc)
98    * @see org.openecomp.sparky.analytics.ComponentStatistics#addHistogram(java.lang.String, java.lang.String, long, int, int)
99    */
100   @Override
101   public synchronized void addHistogram(String key, String histName, long maxYValue, int numBins,
102       int numDecimalPoints) {
103     HistogramSampler histSampler = namedHistograms.get(key);
104
105     if (histSampler == null) {
106       histSampler = new HistogramSampler(histName, maxYValue, numBins, numDecimalPoints);
107       namedHistograms.put(key, histSampler);
108     }
109
110   }
111
112   /* (non-Javadoc)
113    * @see org.openecomp.sparky.analytics.ComponentStatistics#updateHistogram(java.lang.String, long)
114    */
115   @Override
116   public void updateHistogram(String key, long value) {
117     HistogramSampler histSampler = namedHistograms.get(key);
118
119     if (histSampler != null) {
120       histSampler.track(value);
121     }
122   }
123
124   /* (non-Javadoc)
125    * @see org.openecomp.sparky.analytics.ComponentStatistics#reset()
126    */
127   @Override
128   public void reset() {
129
130     for (HistogramSampler h : namedHistograms.values()) {
131       h.clear();
132     }
133
134     for (AtomicInteger c : namedCounters.values()) {
135       c.set(0);
136     }
137
138   }
139
140   /**
141    * Gets the counter value.
142    *
143    * @param key the key
144    * @return the counter value
145    */
146   protected int getCounterValue(String key) {
147
148     AtomicInteger counter = namedCounters.get(key);
149
150     if (counter == null) {
151       return -1;
152     }
153
154     return counter.get();
155
156   }
157
158   /**
159    * Gets the histogram stats.
160    *
161    * @param key the key
162    * @param verboseEnabled the verbose enabled
163    * @param indentPadding the indent padding
164    * @return the histogram stats
165    */
166   protected String getHistogramStats(String key, boolean verboseEnabled, String indentPadding) {
167
168     HistogramSampler histSampler = namedHistograms.get(key);
169
170     if (histSampler == null) {
171       return null;
172     }
173
174     return histSampler.getStats(verboseEnabled, indentPadding);
175
176   }
177
178
179
180 }