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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/tools/ct_mapper/metrics.cc
diff --git a/net/tools/ct_mapper/metrics.cc b/net/tools/ct_mapper/metrics.cc
new file mode 100644
index 0000000000000000000000000000000000000000..efa7296250cc69e4d534241f5549c9045f5523aa
--- /dev/null
+++ b/net/tools/ct_mapper/metrics.cc
@@ -0,0 +1,129 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/tools/ct_mapper/metrics.h"
+
+#include "net/tools/ct_mapper/entry.h"
+
+namespace net {
+
+namespace {
+
+size_t g_max_samples_per_bucket = 10;
+
+} // namespace
+
+Sample::Sample() = default;
+Sample::Sample(const Sample& other) = default;
+Sample::Sample(Sample&& other) = default;
+Sample& Sample::operator=(const Sample& other) = default;
+Sample& Sample::operator=(Sample&& other) = default;
+Sample::~Sample() = default;
+
+BucketValue::BucketValue() = default;
+BucketValue::BucketValue(const BucketValue&) = default;
+BucketValue::BucketValue(BucketValue&&) = default;
+BucketValue& BucketValue::operator=(const BucketValue& other) = default;
+BucketValue& BucketValue::operator=(BucketValue&& other) = default;
+BucketValue::~BucketValue() = default;
+
+void BucketValue::AddSampleChain(const Entry& entry) {
+ if (samples.size() >= g_max_samples_per_bucket)
+ return;
+
+ samples.push_back({});
+ Sample& sample = samples.back();
+
+ sample.certs.reserve(entry.extra_certs.size() + 1);
+ sample.certs.push_back(entry.cert);
+ for (const auto& extra_certs : entry.extra_certs)
+ sample.certs.push_back(extra_certs);
+}
+
+void BucketValue::AddSampleCert(const der::Input& cert) {
+ if (samples.size() >= g_max_samples_per_bucket)
+ return;
+
+ samples.push_back({});
+ Sample& sample = samples.back();
+
+ sample.certs.push_back(cert);
+}
+
+void BucketValue::Merge(const BucketValue& other) {
+ total += other.total;
+
+ // TODO(eroman): Should enforce the max samples constraint. Moreover for
+ // replayability, order the samples by index before truncating.
+ samples.insert(samples.end(), other.samples.begin(), other.samples.end());
+}
+
+void BucketValue::Finalize() {
+ if (samples.size() > g_max_samples_per_bucket)
+ samples.resize(g_max_samples_per_bucket);
+}
+
+void BucketValue::SetMaxSamples(size_t limit) {
+ g_max_samples_per_bucket = limit;
+}
+
+MetricsItem::MetricsItem() = default;
+MetricsItem::MetricsItem(const MetricsItem&) = default;
+MetricsItem::MetricsItem(MetricsItem&&) = default;
+MetricsItem& MetricsItem::operator=(const MetricsItem& other) = default;
+MetricsItem& MetricsItem::operator=(MetricsItem&& other) = default;
+MetricsItem::~MetricsItem() = default;
+
+BucketValue* MetricsItem::GetAndIncrementTotal(const std::string& bucket_name) {
+ BucketValue* result = &buckets_[bucket_name];
+ result->IncrementTotal();
+ return result;
+}
+
+void MetricsItem::Merge(const MetricsItem& other) {
+ total_ += other.total_;
+
+ for (const auto& it : other.buckets_) {
+ const std::string& key = it.first;
+ const BucketValue& other_value = it.second;
+
+ BucketValue& value = buckets_[key];
+ value.Merge(other_value);
+ }
+}
+
+void MetricsItem::Finalize() {
+ // Yuck. Because merging doesn't strictly adhere to the constraint, apply it
+ // at the end.
+ for (auto& it : buckets_) {
+ it.second.Finalize();
+ }
+}
+
+Metrics::Metrics() = default;
+Metrics::~Metrics() = default;
+
+MetricsItem* Metrics::GetAndIncrementTotal(const std::string& item_name) {
+ MetricsItem* result = &items_[item_name];
+ result->IncrementTotal();
+ return result;
+}
+
+void Metrics::Merge(const Metrics& other) {
+ for (const auto& it : other.items_) {
+ const std::string& key = it.first;
+ const MetricsItem& other_value = it.second;
+
+ MetricsItem& value = items_[key];
+ value.Merge(other_value);
+ }
+}
+
+void Metrics::Finalize() {
+ for (auto& it : items_) {
+ it.second.Finalize();
+ }
+}
+
+} // namespace net
« 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