Blame view

src/ihistogram.cpp 5.41 KB
51becbde   Peter M. Groen   Committed the ent...
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
  /* Copyright (C) 2019
   *
   * This file is part of the osdev components suite
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; either version 2, or (at your option) any
   * later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
   */
  #include "ihistogram.h"
  
  // std
  #include <algorithm>
  #include <iomanip>
  #include <numeric>
  #include <sstream>
  
  namespace osdev {
  namespace components {
  namespace mqtt {
  namespace measurement {
  
  IHistogram::~IHistogram() = default;
  
  namespace {
  
  std::pair<int, std::size_t> largestValueUnderThreshold(const std::vector<std::size_t>& hist, double thresholdValue)
  {
      int bin = -1;
      std::size_t maxValue = 0;
      // do not use the outlier bins for this calculation.
      for (std::size_t i = 1; i < hist.size() - 1; ++i) {
          if (hist[i] > maxValue && hist[i] < thresholdValue) {
              bin = static_cast<int>(i);
              maxValue = hist[i];
          }
      }
      return { bin, maxValue };
  }
  
  } // namespace
  
  std::string visualizeHistogram(const IHistogram& histogram)
  {
      const std::size_t diagramHeight = 10;
      auto histData = histogram.histogramData(); // take a snapshot of the histogram data.
      const auto& hist = histData.data();
  
      std::ostringstream oss;
      oss << "Histogram for " << histogram.id() << "\\n";
      auto minmax = std::minmax_element(hist.begin(), hist.end());
  
      auto minCountValueString = std::to_string(*minmax.first);
      auto maxCountValueString = std::to_string(*minmax.second);
  
      auto countValueFieldWidth = static_cast<std::int32_t>(std::max(minCountValueString.size(), maxCountValueString.size()));
      auto verticalSize = (*minmax.second - *minmax.first) / static_cast<double>(diagramHeight);
  
      auto totalMeasurements = std::accumulate(hist.begin(), hist.end(), 0);
      oss << "Total nr of measurements: " << totalMeasurements << ", Nr of buckets: " << histogram.numBuckets() << ", bucket width: " << histogram.bucketWidth() << ", vertical resolution: " << verticalSize << "\\n";
      auto binValuePair = largestValueUnderThreshold(hist, verticalSize);
      if (binValuePair.first >= 0) {
          oss << "Largest value under threshold: " << binValuePair.second << " in bin " << binValuePair.first << "\\n";
      }
      if (hist.front() > 0) {
          oss << "Outliers to the left: " << hist.front() << ", smallest value: " << histData.smallestValueString() << histogram.unit() << "\\n";
      }
      if (hist.back() > 0) {
          oss << "Outliers to the right: " << hist.back() << ", largest value: " << histData.largestValueString() << histogram.unit() << "\\n";
      }
  
      oss << "\\nSince last clear:\\n"
          << "  number of values : " << histData.numberOfValuesSinceLastClear() << "\\n"
          << "  smallest value   : " << histData.smallestValueSinceLastClearString() << histogram.unit() << "\\n"
          << "  largest value    : " << histData.largestValueSinceLastClearString() << histogram.unit() << "\\n"
          << "  average value    : " << histData.averageValueSinceLastClearString() << histogram.unit() << "\\n\\n";
  
      oss << std::setw(countValueFieldWidth) << maxCountValueString << " |";
      for (std::size_t line = 0; line < diagramHeight; ++line) {
          for (const auto& cnt : hist) {
              if ((cnt - *minmax.first) > (diagramHeight - line - 1) * verticalSize) {
                  oss << '*';
              }
              else {
                  oss << ' ';
              }
          }
          oss << "|\\n";
          if (line + 1 < diagramHeight) {
              oss << std::setw(countValueFieldWidth + 2) << '|';
          }
      }
      oss << std::setw(countValueFieldWidth) << minCountValueString << " |/" << std::string(hist.size() - 2, '-') << "\\| " << histogram.unit() << "\\n";
  
      auto minValueString = histogram.minValueString();
      auto maxValueString = histogram.maxValueString();
      auto valueFieldWidth = std::max(minValueString.size(), maxValueString.size());
  
      for (std::size_t line = 0; line < valueFieldWidth; ++line) {
          oss << std::setw(countValueFieldWidth + 3 + static_cast<int>(line)) << "";
          oss << (line < minValueString.size() ? minValueString[line] : ' ');
  
          if (line < maxValueString.size()) {
              oss << std::string(histogram.numBuckets() - 1, ' ') << maxValueString[line];
          }
          oss << "\\n";
      }
  
      // Add the non zero data to the log for later analysis
      // format: binnumber:value;binnumber:value;....
      bool first = true;
      for (std::size_t idx = 0; idx < hist.size(); ++idx) {
          if (hist[idx] > 0) {
              if (!first) {
                  oss << ';';
              }
              else {
                  oss << "\\n";
                  first = false;
              }
              oss << idx << ':' << hist[idx];
          }
      }
  
      return oss.str();
  }
  
  std::ostream& operator<<(std::ostream& os, const IHistogram& rhs)
  {
      return os << rhs.toString();
  }
  
  }       // End namespace measurement
  }       // End namespace mqtt
  }       // End namespace components
  }       // End namespace osdev