Kea 1.5.0
observation.cc
Go to the documentation of this file.
1// Copyright (C) 2015 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#include <config.h>
8
9#include <stats/observation.h>
11#include <cc/data.h>
12#include <boost/date_time/posix_time/posix_time.hpp>
13#include <boost/date_time/gregorian/gregorian.hpp>
14#include <utility>
15
16using namespace std;
17using namespace isc::data;
18using namespace boost::posix_time;
19
20namespace isc {
21namespace stats {
22
23Observation::Observation(const std::string& name, const int64_t value)
24 :name_(name), type_(STAT_INTEGER) {
25 setValue(value);
26}
27
28Observation::Observation(const std::string& name, const double value)
29 :name_(name), type_(STAT_FLOAT) {
30 setValue(value);
31}
32
33Observation::Observation(const std::string& name, const StatsDuration& value)
34 :name_(name), type_(STAT_DURATION) {
35 setValue(value);
36}
37
38Observation::Observation(const std::string& name, const std::string& value)
39 :name_(name), type_(STAT_STRING) {
40 setValue(value);
41}
42
43void Observation::addValue(const int64_t value) {
44 IntegerSample current = getInteger();
45 setValue(current.first + value);
46}
47
48void Observation::addValue(const double value) {
49 FloatSample current = getFloat();
50 setValue(current.first + value);
51}
52
54 DurationSample current = getDuration();
55 setValue(current.first + value);
56}
57
58void Observation::addValue(const std::string& value) {
59 StringSample current = getString();
60 setValue(current.first + value);
61}
62
63void Observation::setValue(const int64_t value) {
64 setValueInternal(value, integer_samples_, STAT_INTEGER);
65}
66
67void Observation::setValue(const double value) {
68 setValueInternal(value, float_samples_, STAT_FLOAT);
69}
70
72 setValueInternal(value, duration_samples_, STAT_DURATION);
73}
74
75void Observation::setValue(const std::string& value) {
76 setValueInternal(value, string_samples_, STAT_STRING);
77}
78
79template<typename SampleType, typename StorageType>
80void Observation::setValueInternal(SampleType value, StorageType& storage,
81 Type exp_type) {
82 if (type_ != exp_type) {
83 isc_throw(InvalidStatType, "Invalid statistic type requested: "
84 << typeToText(exp_type) << ", but the actual type is "
85 << typeToText(type_) );
86 }
87
88 if (storage.empty()) {
89 storage.push_back(make_pair(value, microsec_clock::local_time()));
90 } else {
91
93 *storage.begin() = make_pair(value, microsec_clock::local_time());
94 }
95}
96
98 return (getValueInternal<IntegerSample>(integer_samples_, STAT_INTEGER));
99}
100
102 return (getValueInternal<FloatSample>(float_samples_, STAT_FLOAT));
103}
104
106 return (getValueInternal<DurationSample>(duration_samples_, STAT_DURATION));
107}
108
110 return (getValueInternal<StringSample>(string_samples_, STAT_STRING));
111}
112
113template<typename SampleType, typename Storage>
114SampleType Observation::getValueInternal(Storage& storage, Type exp_type) const {
115 if (type_ != exp_type) {
116 isc_throw(InvalidStatType, "Invalid statistic type requested: "
117 << typeToText(exp_type) << ", but the actual type is "
118 << typeToText(type_) );
119 }
120
121 if (storage.empty()) {
122 // That should never happen. The first element is always initialized in
123 // the constructor. reset() sets its value to zero, but the element should
124 // still be there.
125 isc_throw(Unexpected, "Observation storage container empty");
126 }
127 return (*storage.begin());
128}
129
130std::string Observation::typeToText(Type type) {
131 std::stringstream tmp;
132 switch (type) {
133 case STAT_INTEGER:
134 tmp << "integer";
135 break;
136 case STAT_FLOAT:
137 tmp << "float";
138 break;
139 case STAT_DURATION:
140 tmp << "duration";
141 break;
142 case STAT_STRING:
143 tmp << "string";
144 break;
145 default:
146 tmp << "unknown";
147 break;
148 }
149 tmp << "(" << type << ")";
150 return (tmp.str());
151}
152
155
156 ElementPtr entry = isc::data::Element::createList(); // a single observation
157 ElementPtr value;
158 ElementPtr timestamp;
159
162
163 switch (type_) {
164 case STAT_INTEGER: {
166 value = isc::data::Element::create(static_cast<int64_t>(s.first));
168 break;
169 }
170 case STAT_FLOAT: {
171 FloatSample s = getFloat();
172 value = isc::data::Element::create(s.first);
174 break;
175 }
176 case STAT_DURATION: {
180 break;
181 }
182 case STAT_STRING: {
184 value = isc::data::Element::create(s.first);
186 break;
187 }
188 default:
189 isc_throw(InvalidStatType, "Unknown statistic type: "
190 << typeToText(type_));
191 };
192
193 entry->add(value);
194 entry->add(timestamp);
195
196 ElementPtr list = isc::data::Element::createList(); // a single observation
197 list->add(entry);
198
199 return (list);
200}
201
203 switch(type_) {
204 case STAT_INTEGER: {
205 setValue(static_cast<int64_t>(0));
206 return;
207 }
208 case STAT_FLOAT: {
209 setValue(0.0);
210 return;
211 }
212 case STAT_DURATION: {
213 setValue(time_duration(0,0,0,0));
214 return;
215 }
216 case STAT_STRING: {
217 setValue(string(""));
218 return;
219 }
220 default:
221 isc_throw(InvalidStatType, "Unknown statistic type: "
222 << typeToText(type_));
223 };
224}
225
226};
227};
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
Exception thrown if invalid statistic type is used.
Definition: observation.h:25
static std::string typeToText(Type type)
Converts statistic type to string.
Definition: observation.cc:130
void setValue(const int64_t value)
Records absolute integer observation.
Definition: observation.cc:63
void reset()
Resets statistic.
Definition: observation.cc:202
FloatSample getFloat() const
Returns observed float sample.
Definition: observation.cc:101
void addValue(const int64_t value)
Records incremental integer observation.
Definition: observation.cc:43
StringSample getString() const
Returns observed string sample.
Definition: observation.cc:109
Type
Type of available statistics.
Definition: observation.h:82
@ STAT_INTEGER
this statistic is unsinged 64-bit integer value
Definition: observation.h:83
@ STAT_STRING
this statistic represents a string
Definition: observation.h:86
@ STAT_FLOAT
this statistic is a floating point value
Definition: observation.h:84
@ STAT_DURATION
this statistic represents time duration
Definition: observation.h:85
isc::data::ConstElementPtr getJSON() const
Returns as a JSON structure.
Definition: observation.cc:154
IntegerSample getInteger() const
Returns observed integer sample.
Definition: observation.cc:97
Observation(const std::string &name, const int64_t value)
Constructor for integer observations.
Definition: observation.cc:23
DurationSample getDuration() const
Returns observed duration sample.
Definition: observation.cc:105
const Name & name_
Definition: dns/message.cc:693
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::pair< StatsDuration, boost::posix_time::ptime > DurationSample
Time Duration.
Definition: observation.h:48
std::pair< double, boost::posix_time::ptime > FloatSample
Float (implemented as double precision)
Definition: observation.h:45
std::pair< std::string, boost::posix_time::ptime > StringSample
String.
Definition: observation.h:51
std::pair< int64_t, boost::posix_time::ptime > IntegerSample
Integer (implemented as signed 64-bit integer)
Definition: observation.h:42
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
boost::posix_time::time_duration StatsDuration
Defines duration resolution.
Definition: observation.h:33
std::string ptimeToText(boost::posix_time::ptime t)
Converts ptime structure to text.
std::string durationToText(boost::posix_time::time_duration dur)
Converts StatsDuration to text.
Defines the logger used by the top-level component of kea-dhcp-ddns.