summaryrefslogtreecommitdiffstats
path: root/src/lib/stats/context.cc
blob: 5826b35f11681ebe0c7bcf64553e2f3f1d6b1d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <stats/context.h>
#include <util/multi_threading_mgr.h>
#include <map>

using namespace std;
using namespace isc::data;
using namespace isc::util;

namespace isc {
namespace stats {

ObservationPtr
StatContext::get(const std::string& name) const {
    auto obs = stats_.find(name);
    if (obs != stats_.end()) {
        return (obs->second);
    }
    return (ObservationPtr());
}

void
StatContext::add(const ObservationPtr& obs) {
    auto existing = stats_.find(obs->getName());
    if (existing == stats_.end()) {
        stats_.insert(make_pair(obs->getName() ,obs));
    } else {
        isc_throw(DuplicateStat, "Statistic named " << obs->getName()
                  << " already exists.");
    }
}

bool
StatContext::del(const std::string& name) {
    auto obs = stats_.find(name);
    if (obs != stats_.end()) {
        stats_.erase(obs);
        return (true);
    }
    return (false);
}

size_t
StatContext::size() {
    return (stats_.size());
}

void
StatContext::clear() {
    stats_.clear();
}

void
StatContext::resetAll() {
    // Let's iterate over all stored statistics...
    for (auto const& s : stats_) {
        // ... and reset each statistic.
        s.second->reset();
    }
}

ConstElementPtr
StatContext::getAll() const {
    ElementPtr map = Element::createMap(); // a map
    // Let's iterate over all stored statistics...
    for (auto const& s : stats_) {
        // ... and add each of them to the map.
        map->set(s.first, s.second->getJSON());
    }
    return (map);
}

void
StatContext::setMaxSampleCountAll(uint32_t max_samples) {
    // Let's iterate over all stored statistics...
    for (auto const& s : stats_) {
        // ... and set count limit for each statistic.
        s.second->setMaxSampleCount(max_samples);
    }
}

void
StatContext::setMaxSampleAgeAll(const StatsDuration& duration) {
    // Let's iterate over all stored statistics...
    for (auto const& s : stats_) {
        // ... and set duration limit for each statistic.
        s.second->setMaxSampleAge(duration);
    }
}

}  // namespace stats
}  // namespace isc