Kea 2.5.8
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>
11#include <map>
12
13using namespace std;
14using namespace isc::data;
15using namespace isc::util;
16
17namespace isc {
18namespace stats {
19
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
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
52 return (stats_.size());
53}
54
55void
57 stats_.clear();
58}
59
60void
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
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
80void
81StatContext::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
89void
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
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
Exception indicating that a given statistic is duplicated.
Definition: context.h:19
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:479
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition: observation.h:43
Defines the logger used by the top-level component of kea-lfc.
bool del(const std::string &name)
Attempts to delete an observation.
Definition: context.cc:41
void clear()
Removes all observations.
Definition: context.cc:56
void setMaxSampleAgeAll(const StatsDuration &duration)
Sets duration for all observations.
Definition: context.cc:90
void resetAll()
Resets all observations.
Definition: context.cc:61
isc::data::ConstElementPtr getAll() const
Returns a map with all observations.
Definition: context.cc:70
void add(const ObservationPtr &obs)
Adds a new observation.
Definition: context.cc:30
void setMaxSampleCountAll(uint32_t max_samples)
Sets max sample count for all observations.
Definition: context.cc:81
ObservationPtr get(const std::string &name) const
Attempts to get an observation.
Definition: context.cc:21
size_t size()
Returns the number of observations.
Definition: context.cc:51