Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: net/tools/ct_mapper/metrics.cc

Issue 1238413004: Framework for iterating over certificates in CT database from Chromium code. (not for review) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make samples page work Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/tools/ct_mapper/metrics.h ('k') | net/tools/ct_mapper/my_visitor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/tools/ct_mapper/metrics.h"
6
7 #include "net/tools/ct_mapper/entry.h"
8
9 namespace net {
10
11 namespace {
12
13 size_t g_max_samples_per_bucket = 10;
14
15 } // namespace
16
17 Sample::Sample() = default;
18 Sample::Sample(const Sample& other) = default;
19 Sample::Sample(Sample&& other) = default;
20 Sample& Sample::operator=(const Sample& other) = default;
21 Sample& Sample::operator=(Sample&& other) = default;
22 Sample::~Sample() = default;
23
24 BucketValue::BucketValue() = default;
25 BucketValue::BucketValue(const BucketValue&) = default;
26 BucketValue::BucketValue(BucketValue&&) = default;
27 BucketValue& BucketValue::operator=(const BucketValue& other) = default;
28 BucketValue& BucketValue::operator=(BucketValue&& other) = default;
29 BucketValue::~BucketValue() = default;
30
31 void BucketValue::AddSampleChain(const Entry& entry) {
32 if (samples.size() >= g_max_samples_per_bucket)
33 return;
34
35 samples.push_back({});
36 Sample& sample = samples.back();
37
38 sample.certs.reserve(entry.extra_certs.size() + 1);
39 sample.certs.push_back(entry.cert);
40 for (const auto& extra_certs : entry.extra_certs)
41 sample.certs.push_back(extra_certs);
42 }
43
44 void BucketValue::AddSampleCert(const der::Input& cert) {
45 if (samples.size() >= g_max_samples_per_bucket)
46 return;
47
48 samples.push_back({});
49 Sample& sample = samples.back();
50
51 sample.certs.push_back(cert);
52 }
53
54 void BucketValue::Merge(const BucketValue& other) {
55 total += other.total;
56
57 // TODO(eroman): Should enforce the max samples constraint. Moreover for
58 // replayability, order the samples by index before truncating.
59 samples.insert(samples.end(), other.samples.begin(), other.samples.end());
60 }
61
62 void BucketValue::Finalize() {
63 if (samples.size() > g_max_samples_per_bucket)
64 samples.resize(g_max_samples_per_bucket);
65 }
66
67 void BucketValue::SetMaxSamples(size_t limit) {
68 g_max_samples_per_bucket = limit;
69 }
70
71 MetricsItem::MetricsItem() = default;
72 MetricsItem::MetricsItem(const MetricsItem&) = default;
73 MetricsItem::MetricsItem(MetricsItem&&) = default;
74 MetricsItem& MetricsItem::operator=(const MetricsItem& other) = default;
75 MetricsItem& MetricsItem::operator=(MetricsItem&& other) = default;
76 MetricsItem::~MetricsItem() = default;
77
78 BucketValue* MetricsItem::GetAndIncrementTotal(const std::string& bucket_name) {
79 BucketValue* result = &buckets_[bucket_name];
80 result->IncrementTotal();
81 return result;
82 }
83
84 void MetricsItem::Merge(const MetricsItem& other) {
85 total_ += other.total_;
86
87 for (const auto& it : other.buckets_) {
88 const std::string& key = it.first;
89 const BucketValue& other_value = it.second;
90
91 BucketValue& value = buckets_[key];
92 value.Merge(other_value);
93 }
94 }
95
96 void MetricsItem::Finalize() {
97 // Yuck. Because merging doesn't strictly adhere to the constraint, apply it
98 // at the end.
99 for (auto& it : buckets_) {
100 it.second.Finalize();
101 }
102 }
103
104 Metrics::Metrics() = default;
105 Metrics::~Metrics() = default;
106
107 MetricsItem* Metrics::GetAndIncrementTotal(const std::string& item_name) {
108 MetricsItem* result = &items_[item_name];
109 result->IncrementTotal();
110 return result;
111 }
112
113 void Metrics::Merge(const Metrics& other) {
114 for (const auto& it : other.items_) {
115 const std::string& key = it.first;
116 const MetricsItem& other_value = it.second;
117
118 MetricsItem& value = items_[key];
119 value.Merge(other_value);
120 }
121 }
122
123 void Metrics::Finalize() {
124 for (auto& it : items_) {
125 it.second.Finalize();
126 }
127 }
128
129 } // namespace net
OLDNEW
« no previous file with comments | « net/tools/ct_mapper/metrics.h ('k') | net/tools/ct_mapper/my_visitor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698