Kea  2.1.7-git
observation.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef OBSERVATION_H
8 #define OBSERVATION_H
9 
10 #include <cc/data.h>
11 #include <exceptions/exceptions.h>
12 #include <boost/shared_ptr.hpp>
13 #include <chrono>
14 #include <list>
15 #include <stdint.h>
16 
17 namespace isc {
18 namespace stats {
19 
24 class InvalidStatType : public Exception {
25 public:
26  InvalidStatType(const char* file, size_t line, const char* what) :
27  isc::Exception(file, line, what) {}
28 };
29 
34 typedef std::chrono::system_clock SampleClock;
35 
39 typedef std::chrono::system_clock::duration StatsDuration;
40 
45 inline long toSeconds(const StatsDuration& dur) {
46  return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
47 }
48 
54 
56 typedef std::pair<int64_t, SampleClock::time_point> IntegerSample;
57 
59 typedef std::pair<double, SampleClock::time_point> FloatSample;
60 
62 typedef std::pair<StatsDuration, SampleClock::time_point> DurationSample;
63 
65 typedef std::pair<std::string, SampleClock::time_point> StringSample;
66 
68 
84 class Observation {
85 public:
86 
95  enum Type {
99  STAT_STRING
100  };
101 
106  Observation(const std::string& name, const int64_t value);
107 
112  Observation(const std::string& name, const double value);
113 
118  Observation(const std::string& name, const StatsDuration& value);
119 
124  Observation(const std::string& name, const std::string& value);
125 
141  void setMaxSampleAge(const StatsDuration& duration);
142 
155  void setMaxSampleCount(uint32_t max_samples);
156 
160  static void setMaxSampleAgeDefault(const StatsDuration& duration);
161 
166  static void setMaxSampleCountDefault(uint32_t max_samples);
167 
171  static const StatsDuration& getMaxSampleAgeDefault();
172 
177  static uint32_t getMaxSampleCountDefault();
178 
180 
185  void setValue(const int64_t value);
186 
191  void setValue(const double value);
192 
197  void setValue(const StatsDuration& value);
198 
203  void setValue(const std::string& value);
204 
209  void addValue(const int64_t value);
210 
215  void addValue(const double value);
216 
221  void addValue(const StatsDuration& value);
222 
227  void addValue(const std::string& value);
228 
232  size_t getSize() const;
233 
237  std::pair<bool, StatsDuration> getMaxSampleAge() const;
238 
242  std::pair<bool, uint32_t> getMaxSampleCount() const;
243 
248  void reset();
249 
252  Type getType() const {
253  return (type_);
254  }
255 
259  IntegerSample getInteger() const;
260 
264  FloatSample getFloat() const;
265 
269  DurationSample getDuration() const;
270 
274  StringSample getString() const;
275 
279  std::list<IntegerSample> getIntegers() const;
280 
284  std::list<FloatSample> getFloats() const;
285 
289  std::list<DurationSample> getDurations() const;
290 
294  std::list<StringSample> getStrings() const;
295 
298  isc::data::ConstElementPtr getJSON() const;
299 
302  static std::string typeToText(Type type);
303 
305  std::string getName() const {
306  return (name_);
307  }
308 
309 private:
310 
320  template<typename StorageType>
321  size_t getSizeInternal(StorageType& storage, Type exp_type) const;
322 
335  template<typename SampleType, typename StorageType>
336  void setValueInternal(SampleType value, StorageType& storage,
337  Type exp_type);
338 
347  template<typename SampleType, typename Storage>
348  SampleType getValueInternal(Storage& storage, Type exp_type) const;
349 
358  template<typename SampleType, typename Storage>
359  std::list<SampleType> getValuesInternal(Storage& storage,
360  Type exp_type) const;
361 
368  template<typename StorageType>
369  void setMaxSampleAgeInternal(StorageType& storage,
370  const StatsDuration& duration, Type exp_type);
371 
378  template<typename StorageType>
379  void setMaxSampleCountInternal(StorageType& storage,
380  uint32_t max_samples, Type exp_type);
381 
383  std::string name_;
384 
386  Type type_;
387 
395  std::pair<bool, uint32_t> max_sample_count_;
396 
401  static std::pair<bool, uint32_t> default_max_sample_count_;
402 
410  std::pair<bool, StatsDuration> max_sample_age_;
411 
416  static std::pair<bool, StatsDuration> default_max_sample_age_;
417 
424 
426  std::list<IntegerSample> integer_samples_;
427 
429  std::list<FloatSample> float_samples_;
430 
432  std::list<DurationSample> duration_samples_;
433 
435  std::list<StringSample> string_samples_;
437 };
438 
440 typedef boost::shared_ptr<Observation> ObservationPtr;
441 
442 }
443 }
444 
445 #endif // OBSERVATION_H
this statistic is unsigned 64-bit integer value
Definition: observation.h:96
std::pair< double, SampleClock::time_point > FloatSample
Float (implemented as double precision)
Definition: observation.h:59
InvalidStatType(const char *file, size_t line, const char *what)
Definition: observation.h:26
std::chrono::system_clock SampleClock
Define clock type.
Definition: observation.h:34
this statistic is a floating point value
Definition: observation.h:97
Type getType() const
Returns statistic type.
Definition: observation.h:252
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition: observation.h:39
std::pair< std::string, SampleClock::time_point > StringSample
String.
Definition: observation.h:65
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::pair< int64_t, SampleClock::time_point > IntegerSample
Integer (implemented as signed 64-bit integer)
Definition: observation.h:56
this statistic represents time duration
Definition: observation.h:98
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::string getName() const
Returns observation name.
Definition: observation.h:305
const Name & name_
Definition: dns/message.cc:693
Represents a single observable characteristic (a &#39;statistic&#39;)
Definition: observation.h:84
Type
Type of available statistics.
Definition: observation.h:95
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:440
Exception thrown if invalid statistic type is used.
Definition: observation.h:24
std::pair< StatsDuration, SampleClock::time_point > DurationSample
Time Duration.
Definition: observation.h:62
long toSeconds(const StatsDuration &dur)
Returns the number of seconds in a duration.
Definition: observation.h:45