artdaq_utilities  v1_05_05
MetricData.hh
1 #ifndef ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
2 #define ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
3 
4 #include "fhiclcpp/fwd.h"
5 #include "messagefacility/MessageLogger/MessageLogger.h"
6 
7 #include <atomic>
8 #include <condition_variable>
9 #include <list>
10 #include <sstream>
11 
12 namespace artdaq {
16 enum class MetricType
17 {
19  StringMetric,
20  IntMetric,
21  DoubleMetric,
22  FloatMetric,
24 };
25 
29 enum class MetricMode : uint32_t
30 {
31  None = 0x0,
32  LastPoint = 0x1,
33  Accumulate = 0x2,
34  Average = 0x4,
35  Rate = 0x8,
36  Minimum = 0x10,
38  Maximum = 0x20,
39 };
47 {
48  return static_cast<MetricMode>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
49 }
57 {
58  return static_cast<MetricMode>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
59 }
60 
64 struct MetricData
65 {
70  MetricData(const MetricData& r) = default;
71 
76  MetricData(MetricData&& r) noexcept = default;
77 
83  MetricData& operator=(const MetricData& r) = default;
84 
90  MetricData& operator=(MetricData&& r) noexcept = default;
91 
95  std::string Name;
99  std::string StringValue;
100 
105  {
106  int i;
107  double d;
108  float f;
109  long unsigned int u;
110 
115  : i(0) {}
121  : i(v) {}
126  MetricDataValue(double v)
127  : d(v) {}
133  : f(v) {}
138  MetricDataValue(long unsigned int v)
139  : u(v) {}
140  };
141 
149 
157  std::string Unit;
161  int Level;
169  std::string MetricPrefix;
178 
189  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, MetricMode mode,
190  std::string const& metricPrefix, bool useNameOverride)
191  : Name(name), StringValue(value), Type(MetricType::StringMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
192 
203  MetricData(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
204  std::string const& metricPrefix, bool useNameOverride)
205  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::IntMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
206 
217  MetricData(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
218  std::string const& metricPrefix, bool useNameOverride)
219  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::DoubleMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
220 
231  MetricData(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
232  std::string const& metricPrefix, bool useNameOverride)
233  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::FloatMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
234 
245  MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level,
246  MetricMode mode, std::string const& metricPrefix, bool useNameOverride)
247  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::UnsignedMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
248 
254 
260  bool Add(MetricData other)
261  {
262  if (other.Name == Name && other.Type == Type && other.Unit == Unit && other.Level == Level)
263  {
264  if (other.DataPointCount == 0) return true;
265  if (DataPointCount == 0)
266  {
267  switch (Type)
268  {
270  StringValue = other.StringValue;
271  break;
273  Value.i = other.Value.i;
274  Last.i = other.Last.i;
275  Min.i = other.Min.i;
276  Max.i = other.Max.i;
277  break;
279  Value.d = other.Value.d;
280  Last.d = other.Last.d;
281  Min.d = other.Min.d;
282  Max.d = other.Max.d;
283  break;
285  Value.f = other.Value.f;
286  Last.f = other.Last.f;
287  Min.f = other.Min.f;
288  Max.f = other.Max.f;
289  break;
291  Value.u = other.Value.u;
292  Last.u = other.Last.u;
293  Min.u = other.Min.u;
294  Max.u = other.Max.u;
295  break;
297  break;
298  }
300  return true;
301  }
302  else
303  {
304  switch (Type)
305  {
307  StringValue += " " + other.StringValue;
308  break;
310  Value.i += other.Value.i;
311  Last.i = other.Last.i;
312  if (other.Min.i < Min.i) Min.i = other.Min.i;
313  if (other.Max.i > Max.i) Max.i = other.Max.i;
314  break;
316  Value.d += other.Value.d;
317  Last.d = other.Last.d;
318  if (other.Min.d < Min.d) Min.d = other.Min.d;
319  if (other.Max.d > Max.d) Max.d = other.Max.d;
320  break;
322  Value.f += other.Value.f;
323  Last.f = other.Last.f;
324  if (other.Min.f < Min.f) Min.f = other.Min.f;
325  if (other.Max.f > Max.f) Max.f = other.Max.f;
326  break;
328  Value.u += other.Value.u;
329  Last.u = other.Last.u;
330  if (other.Min.u < Min.u) Min.u = other.Min.u;
331  if (other.Max.u > Max.u) Max.u = other.Max.u;
332  break;
334  break;
335  }
337  return true;
338  }
339  }
340  return false;
341  }
342 
347  void AddPoint(int point)
348  {
349  Last.i = point;
350  Value.i += point;
351  DataPointCount++;
352  if (point > Max.i) Max.i = point;
353  if (point < Min.i) Min.i = point;
354  }
359  void AddPoint(double point)
360  {
361  Last.d = point;
362  Value.d += point;
363  DataPointCount++;
364  if (point > Max.d) Max.d = point;
365  if (point < Min.d) Min.d = point;
366  }
371  void AddPoint(float point)
372  {
373  Last.f = point;
374  Value.f += point;
375  DataPointCount++;
376  if (point > Max.f) Max.f = point;
377  if (point < Min.f) Min.f = point;
378  }
383  void AddPoint(unsigned long int point)
384  {
385  Last.u = point;
386  Value.u += point;
387  DataPointCount++;
388  if (point > Max.u) Max.u = point;
389  if (point < Min.u) Min.u = point;
390  }
391 
397  void Reset()
398  {
399  switch (Type)
400  {
402  StringValue = "";
403  break;
405  Value.i = 0;
406  Last.i = 0;
407  Min.i = std::numeric_limits<int>::max();
408  Max.i = std::numeric_limits<int>::min();
409  break;
411  Value.d = 0;
412  Last.d = 0;
413  Min.d = std::numeric_limits<double>::max();
414  Max.d = std::numeric_limits<double>::min();
415  break;
417  Value.f = 0;
418  Last.f = 0;
419  Min.f = std::numeric_limits<float>::max();
420  Max.f = std::numeric_limits<float>::min();
421  break;
423  Value.u = 0;
424  Last.u = 0;
425  Min.u = std::numeric_limits<unsigned>::max();
426  Max.u = std::numeric_limits<unsigned>::min();
427  break;
429  break;
430  }
431  DataPointCount = 0;
432  }
433 };
434 } // namespace artdaq
435 
436 #endif /* ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH */
std::string StringValue
Value of the metric, if it is a MetricType::StringMetric
Definition: MetricData.hh:99
std::string MetricPrefix
Name prefix for the metric
Definition: MetricData.hh:169
MetricDataValue(double v)
Construct a MetricDataValue as double
Definition: MetricData.hh:126
MetricDataValue(float v)
Construct a MetricDataValue as fload
Definition: MetricData.hh:132
size_t DataPointCount
Number of data points accumulated in this MetricData
Definition: MetricData.hh:177
Report the sum of all values. Use for counters to report accurate results.
std::string Unit
Units of the metric
Definition: MetricData.hh:157
float f
Value of the metric, if it is a MetricType::FloatMetric.
Definition: MetricData.hh:108
MetricDataValue()
Construct a MetricDataValue
Definition: MetricData.hh:114
Metric is a std::string (not in union)
std::string Name
Name of the metric
Definition: MetricData.hh:95
void AddPoint(unsigned long int point)
Add an unsigned int point to this MetricData
Definition: MetricData.hh:383
MetricData(std::string const &name, std::string const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a string value
Definition: MetricData.hh:189
MetricDataValue(long unsigned int v)
Construct a MetricDataValue as unsigned int
Definition: MetricData.hh:138
bool UseNameOverride
Whether to override the default naming convention for this metric
Definition: MetricData.hh:173
MetricMode Mode
Accumulation mode of the metric
Definition: MetricData.hh:165
bool Add(MetricData other)
Add two MetricData instances together
Definition: MetricData.hh:260
constexpr MetricMode operator&(MetricMode a, MetricMode b)
Bitwise AND operator for MetricMode
Definition: MetricData.hh:56
MetricType
This enumeration is used to identify the type of the metric instance (which value should be extraced ...
Definition: MetricData.hh:16
MetricDataValue Max
Maximum recorded vaule of this MetricData.
Definition: MetricData.hh:148
MetricData(std::string const &name, double const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a double value
Definition: MetricData.hh:217
Reports the minimum value recorded.
Metric is a long unsigned int.
over. Use to create rates from counters.
void AddPoint(int point)
Add an integer point to this MetricData
Definition: MetricData.hh:347
Report only the last value recorded. Useful for event counters, run numbers, etc. ...
Repots the maximum value recorded.
MetricData(std::string const &name, float const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a float value
Definition: MetricData.hh:231
MetricDataValue Value
Accumulated value of this MetricData
Definition: MetricData.hh:145
int i
Value of the metric, if it is a MetricType::IntMetric.
Definition: MetricData.hh:106
constexpr MetricMode operator|(MetricMode a, MetricMode b)
Bitwise OR operator for MetricMode
Definition: MetricData.hh:46
MetricType Type
Type of the metric
Definition: MetricData.hh:153
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:29
void AddPoint(double point)
Add a double point to this MetricData
Definition: MetricData.hh:359
MetricDataValue(int v)
Construct a MetricDataValue as integer
Definition: MetricData.hh:120
long unsigned int u
Value of the metric, if it is a MetricType::UnsignedMetric.
Definition: MetricData.hh:109
This union holds the values for all other metric types
Definition: MetricData.hh:104
MetricDataValue Last
Last value of this MetricData.
Definition: MetricData.hh:146
void Reset()
Reset this MetricData instance to the initial state
Definition: MetricData.hh:397
void AddPoint(float point)
Add a float point to this MetricData
Definition: MetricData.hh:371
MetricData()
Default constructor, constructs an MetricType::InvalidMetric
Definition: MetricData.hh:252
MetricData(std::string const &name, long unsigned int const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a unsigned long int value
Definition: MetricData.hh:245
MetricData & operator=(const MetricData &r)=default
Default copy assignment operator
Small structure used to hold a metric data point before sending to the metric plugins ...
Definition: MetricData.hh:64
MetricData(std::string const &name, int const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a int value
Definition: MetricData.hh:203
Default, invalid value.
Report the average of all values. Use for rates to report accurate results.
MetricDataValue Min
Minimum recorded value of this MetricData.
Definition: MetricData.hh:147
double d
Value of the metric, if it is a MetricType::DoubleMetric.
Definition: MetricData.hh:107
int Level
Reporting level of the metric
Definition: MetricData.hh:161