Chromium Code Reviews| Index: mojo/services/test_service/toy_monitoring_service_impl.cc |
| diff --git a/mojo/services/test_service/toy_monitoring_service_impl.cc b/mojo/services/test_service/toy_monitoring_service_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4f994c1d83bda2cee089f50458e8aa8c731980dd |
| --- /dev/null |
| +++ b/mojo/services/test_service/toy_monitoring_service_impl.cc |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2014 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 "base/bind.h" |
| +#include "mojo/services/test_service/toy_monitoring_service_impl.h" |
| + |
| +namespace mojo { |
| +namespace test { |
| + |
| + MonitoringContext::MonitoringContext() : next_id(1) {} |
| + MonitoringContext::~MonitoringContext() {} |
| + |
| + ToyMonitoringServiceRecorderImpl::ToyMonitoringServiceRecorderImpl( |
| + ApplicationConnection* connection, |
| + MonitoringContext* context) : context_(context), weak_factory_(this) { |
| + } |
| + |
| + ToyMonitoringServiceRecorderImpl::~ToyMonitoringServiceRecorderImpl() { |
| + } |
| + |
| +void ToyMonitoringServiceRecorderImpl::RecordStats( |
| + uint64_t client_id, |
| + ServiceStatsPtr stats) { |
| + assert(context_->ids_to_names.find(client_id) != |
|
viettrungluu
2014/06/30 17:08:24
You can use base/logging.h (CHECK/DCHECK) here.
tim (not reviewing)
2014/06/30 22:20:54
Done.
|
| + context_->ids_to_names.end()); |
| + context_->records[client_id].push_back(*stats); |
| +} |
| + |
| +void ToyMonitoringServiceRecorderImpl::OnConnectionEstablished() { |
| + uint64_t id = context_->next_id++; |
| + client()->SetIdAndReturnName(id, |
| + base::Bind(&ToyMonitoringServiceRecorderImpl::UploaderNameCallback, |
| + weak_factory_.GetWeakPtr(), |
| + id)); |
| +} |
| + |
| +void ToyMonitoringServiceRecorderImpl::UploaderNameCallback( |
| + uint64_t id, const mojo::String& name) { |
| + assert(context_->ids_to_names.find(id) == context_->ids_to_names.end()); |
| + context_->ids_to_names[id] = name; |
| +} |
| + |
| +ToyMonitoringServiceImpl::ToyMonitoringServiceImpl( |
| + ApplicationConnection* connection, |
| + MonitoringContext* context) : context_(context) { |
| +} |
| + |
| +ToyMonitoringServiceImpl::~ToyMonitoringServiceImpl() { |
| +} |
| + |
| +void ToyMonitoringServiceImpl::GetReport( |
| + const mojo::Callback<void(mojo::Array<ServiceReportPtr>)>& callback) { |
| + mojo::Array<ServiceReportPtr> reports; |
| + for (AllRecordsMap::const_iterator it1 = context_->records.begin(); |
| + it1 != context_->records.end(); ++it1) { |
| + ServiceReportPtr report(ServiceReport::New()); |
| + report->service_name = context_->ids_to_names[it1->first]; |
| + double mean_health_numerator = 0; |
| + int num_samples = it1->second.size(); |
| + if (num_samples == 0) |
| + continue; |
| + |
| + for (std::vector<ServiceStats>::const_iterator it2 = it1->second.begin(); |
| + it2 != it1->second.end(); ++it2) { |
| + report->total_requests += it2->num_new_requests; |
| + mean_health_numerator += it2->health; |
| + } |
| + report->mean_health = mean_health_numerator / num_samples; |
| + reports.push_back(report.Pass()); |
| + } |
| + callback.Run(reports.Pass()); |
| +} |
| + |
| +} // namespace test |
| +} // namespace mojo |