Kea  1.9.9-git
context.cc
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 #include <config.h>
8 
9 #include <stats/context.h>
11 #include <map>
12 
13 using namespace std;
14 using namespace isc::data;
15 using namespace isc::util;
16 
17 namespace isc {
18 namespace stats {
19 
21 StatContext::get(const std::string& name) const {
22  auto obs = stats_.find(name);
23  if (obs != stats_.end()) {
24  return (obs->second);
25  }
26  return (ObservationPtr());
27 }
28 
29 void
30 StatContext::add(const ObservationPtr& obs) {
31  auto existing = stats_.find(obs->getName());
32  if (existing == stats_.end()) {
33  stats_.insert(make_pair(obs->getName() ,obs));
34  } else {
35  isc_throw(DuplicateStat, "Statistic named " << obs->getName()
36  << " already exists.");
37  }
38 }
39 
40 bool
41 StatContext::del(const std::string& name) {
42  auto obs = stats_.find(name);
43  if (obs != stats_.end()) {
44  stats_.erase(obs);
45  return (true);
46  }
47  return (false);
48 }
49 
50 size_t
51 StatContext::size() {
52  return (stats_.size());
53 }
54 
55 void
56 StatContext::clear() {
57  stats_.clear();
58 }
59 
60 void
61 StatContext::resetAll() {
62  // Let's iterate over all stored statistics...
63  for (auto s : stats_) {
64  // ... and reset each statistic.
65  s.second->reset();
66  }
67 }
68 
70 StatContext::getAll() const {
71  ElementPtr map = Element::createMap(); // a map
72  // Let's iterate over all stored statistics...
73  for (auto s : stats_) {
74  // ... and add each of them to the map.
75  map->set(s.first, s.second->getJSON());
76  }
77  return (map);
78 }
79 
80 void
81 StatContext::setMaxSampleCountAll(uint32_t max_samples) {
82  // Let's iterate over all stored statistics...
83  for (auto s : stats_) {
84  // ... and set count limit for each statistic.
85  s.second->setMaxSampleCount(max_samples);
86  }
87 }
88 
89 void
90 StatContext::setMaxSampleAgeAll(const StatsDuration& duration) {
91  // Let's iterate over all stored statistics...
92  for (auto s : stats_) {
93  // ... and set duration limit for each statistic.
94  s.second->setMaxSampleAge(duration);
95  }
96 }
97 
98 } // namespace stats
99 } // namespace isc
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
STL namespace.
Exception indicating that a given statistic is duplicated.
Definition: context.h:19
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition: observation.h:39
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: edns.h:19
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:440