ihistogram.h
5.93 KB
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
#ifndef OSDEV_COMPONENTS_MQTT_MEASUREMENT_IHISTOGRAM_H
#define OSDEV_COMPONENTS_MQTT_MEASUREMENT_IHISTOGRAM_H
// std
#include <ostream>
#include <string>
#include <vector>
namespace osdev {
namespace components {
namespace mqtt {
namespace measurement {
/**
* @brief Class that holds the dynamic part of a histogram.
* Used to take a snapshot of the histogram.
*/
class HistogramData
{
public:
/**
* @brief Construct dynamic histrogram data.
* @param smallestValue A stringified version of the smallest recorded value since the last update.
* @param largestValue A stringified version of the largest recorded value since the last update.
* @param smallestValueSinceLastClear A stringified version of the smallest value as string since last clear.
* @param largestValueSinceLastClear A stringified version of the largest value as string since last clear.
* @param averageValueSinceLastClear A stringified version of the average value as string since last clear.
* @param nrOfValuesSinceLastClear Number of values since last clear.
* @param histCounts The histogram counts.
*/
HistogramData(const std::string& smallestValue, const std::string& largestValue,
const std::string& smallestValueSinceLastClear,
const std::string& largestValueSinceLastClear,
const std::string& averageValueSinceLastClear,
std::size_t nrOfValuesSinceLastClear,
const std::vector<std::size_t>& histCounts)
: m_smallestValueString(smallestValue)
, m_largestValueString(largestValue)
, m_smallestValueSinceLastClearString(smallestValueSinceLastClear)
, m_largestValueSinceLastClearString(largestValueSinceLastClear)
, m_averageValueSinceLastClearString(averageValueSinceLastClear)
, m_numberOfValuesSinceLastClear(nrOfValuesSinceLastClear)
, m_data(histCounts)
{
}
/**
* @return The smallest recorded value as a string.
*/
const std::string& smallestValueString() const
{
return m_smallestValueString;
}
/**
* @return The largest recorded value as a string.
*/
const std::string& largestValueString() const
{
return m_largestValueString;
}
/**
* @return The number of value since last clear.
*/
std::size_t numberOfValuesSinceLastClear() const
{
return m_numberOfValuesSinceLastClear;
}
/**
* @return The smallest value since last clear as a string.
*/
const std::string& smallestValueSinceLastClearString() const
{
return m_smallestValueSinceLastClearString;
}
/**
* @return The largest value since last clear as a string.
*/
const std::string& largestValueSinceLastClearString() const
{
return m_largestValueSinceLastClearString;
}
/**
* @return The average value since last clear as a string.
*/
const std::string& averageValueSinceLastClearString() const
{
return m_averageValueSinceLastClearString;
}
/**
* @return The histogram counts.
*/
const std::vector<std::size_t>& data() const
{
return m_data;
}
private:
std::string m_smallestValueString;
std::string m_largestValueString;
std::string m_smallestValueSinceLastClearString;
std::string m_largestValueSinceLastClearString;
std::string m_averageValueSinceLastClearString;
std::size_t m_numberOfValuesSinceLastClear;
std::vector<std::size_t> m_data;
};
/**
* @brief Interface for histogram classes
* Can be used to store histograms in a container and visualize the histogram.
*/
class IHistogram
{
public:
virtual ~IHistogram();
/**
* @return The histogram identification (title).
*/
virtual const std::string& id() const = 0;
/**
* @return The number of buckets.
*/
virtual std::size_t numBuckets() const = 0;
/**
* @return The bucket width as a floating point value.
*/
virtual double bucketWidth() const = 0;
/**
* @return The minimum value of the histogram definition as a string.
*/
virtual std::string minValueString() const = 0;
/**
* @return The maximum value of the histogram definition as a string.
*/
virtual std::string maxValueString() const = 0;
/**
* @return The value unit as a a string.
*/
virtual const std::string& unit() const = 0;
/**
* @return The histogram data.
* This takes a snapshot of the histogram data.
*/
virtual HistogramData histogramData() const = 0;
/**
* @return The number of value since last clear.
*/
virtual std::size_t numberOfValuesSinceLastClear() const = 0;
/**
* @return The smallest value since last clear as a string.
*/
virtual std::string smallestValueSinceLastClear() const = 0;
/**
* @return The largest value since last clear as a string.
*/
virtual std::string largestValueSinceLastClear() const = 0;
/**
* @return The average value since last clear as a string.
*/
virtual std::string averageValueSinceLastClear() const = 0;
/**
* @brief Clears the values that are kept between successive clearRunningValues calls.
* These are:
* smallestValueSinceLastClear
* largestValueSinceLastClear
* averageValueSinceLastClear
*/
virtual void clearRunningValues() = 0;
/**
* @return The ascii representation of the histogram.
*/
virtual std::string toString() const = 0;
};
/**
* @brief Make an ascii visualisation of the given histogram data.
* @param histogram The histogram to visualize.
*/
std::string visualizeHistogram(const IHistogram& histogram);
/**
* @brief Stream operator for IHistogram instances.
*/
std::ostream& operator<<(std::ostream& os, const IHistogram& rhs);
} // End namespace measurement
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_MEASUREMENT_IHISTOGRAM_H