| Kea 3.0.2-git

context.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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>
10#include <util/multi_threading_mgr.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
20 ObservationPtr
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 const& s : stats_) {
64 // ... and reset each statistic.
65 s.second->reset();
66 }
67}
68
69 ConstElementPtr
70 StatContext::getAll() const {
71 ElementPtr map = Element::createMap(); // a map
72 // Let's iterate over all stored statistics...
73 for (auto const& 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 const& 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 const& s : stats_) {
93 // ... and set duration limit for each statistic.
94 s.second->setMaxSampleAge(duration);
95 }
96}
97
98} // namespace stats
99} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::stats::DuplicateStat
Exception indicating that a given statistic is duplicated.
Definition context.h:19
context.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
multi_threading_mgr.h
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::stats
Definition context.cc:18
isc::stats::ObservationPtr
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition observation.h:479
isc::stats::StatsDuration
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition observation.h:43
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::stats::StatContext::del
bool del(const std::string &name)
Attempts to delete an observation.
Definition context.cc:41
isc::stats::StatContext::clear
void clear()
Removes all observations.
Definition context.cc:56
isc::stats::StatContext::setMaxSampleAgeAll
void setMaxSampleAgeAll(const StatsDuration &duration)
Sets duration for all observations.
Definition context.cc:90
isc::stats::StatContext::resetAll
void resetAll()
Resets all observations.
Definition context.cc:61
isc::stats::StatContext::getAll
isc::data::ConstElementPtr getAll() const
Returns a map with all observations.
Definition context.cc:70
isc::stats::StatContext::add
void add(const ObservationPtr &obs)
Adds a new observation.
Definition context.cc:30
isc::stats::StatContext::setMaxSampleCountAll
void setMaxSampleCountAll(uint32_t max_samples)
Sets max sample count for all observations.
Definition context.cc:81
isc::stats::StatContext::get
ObservationPtr get(const std::string &name) const
Attempts to get an observation.
Definition context.cc:21
isc::stats::StatContext::size
size_t size()
Returns the number of observations.
Definition context.cc:51