Kea: context.cc Source File

| Kea 3.2.0-git

context.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2025 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
13using namespace std;
14using namespace isc::data;
15using namespace isc::util;
16
17namespace isc {
18 namespace stats {
19
20 ObservationPtr
21StatContext::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
29void
30StatContext::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
40bool
41StatContext::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
50size_t
51StatContext::size() {
52 return (stats_.size());
53}
54
55void
56StatContext::clear() {
57 stats_.clear();
58}
59
60void
61StatContext::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
70StatContext::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 ConstElementPtr
81StatContext::getAllGlobal() const {
82 ElementPtr map = Element::createMap(); // a map
83 // Let's iterate over all stored statistics...
84 for (auto const& s : stats_) {
85 // ... and add each of them to the map omitting
86 // subnet statistics.
87 if (s.first.find("subnet") != 0) {
88 map->set(s.first, s.second->getJSON());
89 }
90 }
91 return (map);
92}
93
94void
95StatContext::setMaxSampleCountAll(uint32_t max_samples) {
96 // Let's iterate over all stored statistics...
97 for (auto const& s : stats_) {
98 // ... and set count limit for each statistic.
99 s.second->setMaxSampleCount(max_samples);
100 }
101}
102
103void
104StatContext::setMaxSampleAgeAll(const StatsDuration& duration) {
105 // Let's iterate over all stored statistics...
106 for (auto const& s : stats_) {
107 // ... and set duration limit for each statistic.
108 s.second->setMaxSampleAge(duration);
109 }
110}
111
112} // namespace stats
113} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
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:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
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 check_exists_add.cc:18
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:104
isc::stats::StatContext::getAllGlobal
isc::data::ConstElementPtr getAllGlobal() const
Returns a map with all observations omitting those related to subnet statistics.
Definition context.cc:81
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:95
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