summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/sparky/analytics/HistogramSampler.java
blob: 029ed9593d73f3c4153dd2a689c065358e09b3ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.openecomp.sparky.analytics;

/**
 * A class that models a histogram for reporting and tracking long values with variable steps, bins,
 * and floating point accuracy.
 * 
 * @author davea.
 */
public final class HistogramSampler {

  private String label;

  private long binMaxValue;

  private int numBins;

  private double stepSize;

  private long sampleValueTotal;

  private long minValue = -1;

  private long maxValue = 0;

  private long numSamples = 0;

  private long decimalPointAccuracy = 0;

  private static String FORMAT_FLOAT_TEMPLATE = "%%.%df";

  private String floatFormatStr;

  private long[] histogramBins;

  /**
   * Instantiates a new histogram sampler.
   *
   * @param label the label
   * @param maxValue the max value
   * @param numBins the num bins
   * @param decimalPointAccuracy the decimal point accuracy
   */
  public HistogramSampler(String label, long maxValue, int numBins, int decimalPointAccuracy) {
    this.label = label;
    this.binMaxValue = maxValue;
    this.numBins = numBins;
    this.stepSize = ((double) binMaxValue / (double) numBins);
    this.decimalPointAccuracy = decimalPointAccuracy;
    this.floatFormatStr = String.format(FORMAT_FLOAT_TEMPLATE, this.decimalPointAccuracy);

    /*
     * [numBins + 1] => last bin is catch-all for outliers
     */

    initializeHistogramBins(numBins + 1);

  }

  /**
   * Initialize histogram bins.
   *
   * @param numBins the num bins
   */
  private void initializeHistogramBins(int numBins) {

    histogramBins = new long[numBins];
    int counter = 0;
    while (counter < numBins) {
      histogramBins[counter] = 0;
      counter++;
    }

  }

  /*
   * Is it really necessary to synchronize the collection, or should we simply switch the underlying
   * data type to an AtomicLong
   */

  /**
   * Track.
   *
   * @param value the value
   */
  public synchronized void track(long value) {

    if (value < 0) {
      return;
    }

    sampleValueTotal += value;
    numSamples++;

    if (minValue == -1) {
      minValue = value;
    }

    if (value < minValue) {
      minValue = value;
    }

    if (value > maxValue) {
      maxValue = value;
    }

    /*
     * One step bin determination
     */

    if (value < (numBins * stepSize)) {

      int index = (int) (value / stepSize);
      histogramBins[index]++;

    } else {
      // peg the metric in the outlier bin
      histogramBins[numBins - 1]++;
    }

  }

  /**
   * Clear.
   */
  public void clear() {

    int counter = 0;
    while (counter < numBins) {
      histogramBins[counter] = 0;
      counter++;
    }

    minValue = -1;
    maxValue = 0;
    numSamples = 0;
    sampleValueTotal = 0;

  }

  /**
   * Re initialize bins.
   *
   * @param label the label
   * @param numBins the num bins
   * @param maxValue the max value
   * @param decimalPointAccuracy the decimal point accuracy
   */
  public void reInitializeBins(String label, int numBins, long maxValue, int decimalPointAccuracy) {
    this.label = label;
    this.decimalPointAccuracy = decimalPointAccuracy;
    this.floatFormatStr = String.format(FORMAT_FLOAT_TEMPLATE, this.decimalPointAccuracy);
    this.numBins = numBins;
    this.minValue = -1;
    this.maxValue = 0;
    initializeHistogramBins(numBins);
    this.stepSize = (maxValue / numBins);
    clear();
  }

  public long getNumberOfSamples() {
    return numSamples;
  }

  public long getTotalValueSum() {
    return sampleValueTotal;
  }

  /**
   * Gets the stats.
   *
   * @param formatted the formatted
   * @param indentPadding the indent padding
   * @return the stats
   */
  public String getStats(boolean formatted, String indentPadding) {

    StringBuilder sb = new StringBuilder(128);


    if (!formatted) {
      // generate CSV in the following format

      /*
       * label,minValue,maxValue,avgValue,numSamples,stepSize,numSteps,stepCounters
       */
      sb.append(indentPadding);
      sb.append(label).append(",");
      sb.append(minValue).append(",");
      sb.append(maxValue).append(",");
      if (numSamples == 0) {
        sb.append(0).append(",");
      } else {
        sb.append((sampleValueTotal / numSamples)).append(",");
      }
      sb.append(numSamples).append(",");
      sb.append(numBins).append(",");
      sb.append(String.format(floatFormatStr, stepSize));

      int counter = 0;
      while (counter < numBins) {

        if (counter != (numBins)) {
          sb.append(",");
        }

        sb.append(histogramBins[counter]);

        counter++;

      }

      return sb.toString();

    }

    sb.append("\n");
    sb.append(indentPadding).append("Label = ").append(label).append("\n");
    sb.append(indentPadding).append("Min = ").append(minValue).append("\n");
    sb.append(indentPadding).append("Max = ").append(maxValue).append("\n");
    sb.append(indentPadding).append("numSamples = ").append(numSamples).append("\n");

    if (numSamples == 0) {
      sb.append(indentPadding).append("Avg = ").append(0).append("\n");
    } else {
      sb.append(indentPadding).append("Avg = ").append((sampleValueTotal / numSamples))
          .append("\n");
    }

    sb.append(indentPadding).append("StepSize = ").append(String.format(floatFormatStr, stepSize))
        .append("\n");

    sb.append(indentPadding).append("Sample Histogram:").append("\n");

    int counter = 0;
    while (counter < numBins) {

      if (counter == (numBins - 1)) {
        // outlier bin
        double leftBound = (stepSize * counter);
        sb.append(indentPadding).append("\t")
            .append(" x >= " + String.format(floatFormatStr, leftBound) + " : " 
                + histogramBins[counter])
            .append("\n");

      } else {
        double leftBound = (stepSize * counter);
        double rightBound = ((stepSize) * (counter + 1));
        sb.append(indentPadding).append("\t")
            .append((String.format(floatFormatStr, leftBound) + " < x < "
                + String.format(floatFormatStr, rightBound) + " : " + histogramBins[counter]))
            .append("\n");
      }

      counter++;

    }

    return sb.toString();

  }

}