| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "base/bind.h" | |
| 6 #include "mojo/services/test_service/test_request_tracker_impl.h" | |
| 7 | |
| 8 namespace mojo { | |
| 9 namespace test { | |
| 10 | |
| 11 TrackingContext::TrackingContext() : next_id(1) {} | |
| 12 TrackingContext::~TrackingContext() {} | |
| 13 | |
| 14 TestRequestTrackerImpl::TestRequestTrackerImpl(TrackingContext* context) | |
| 15 : context_(context), weak_factory_(this) { | |
| 16 } | |
| 17 | |
| 18 TestRequestTrackerImpl::~TestRequestTrackerImpl() { | |
| 19 } | |
| 20 | |
| 21 void TestRequestTrackerImpl::RecordStats( | |
| 22 uint64_t client_id, | |
| 23 ServiceStatsPtr stats) { | |
| 24 assert(context_->ids_to_names.find(client_id) != | |
| 25 context_->ids_to_names.end()); | |
| 26 context_->records[client_id].push_back(*stats); | |
| 27 } | |
| 28 | |
| 29 void TestRequestTrackerImpl::OnConnectionEstablished() { | |
| 30 uint64_t id = context_->next_id++; | |
| 31 client()->SetIdAndReturnName(id, | |
| 32 base::Bind(&TestRequestTrackerImpl::UploaderNameCallback, | |
| 33 weak_factory_.GetWeakPtr(), | |
| 34 id)); | |
| 35 } | |
| 36 | |
| 37 void TestRequestTrackerImpl::UploaderNameCallback( | |
| 38 uint64_t id, const mojo::String& name) { | |
| 39 DCHECK(context_->ids_to_names.find(id) == context_->ids_to_names.end()); | |
| 40 context_->ids_to_names[id] = name; | |
| 41 } | |
| 42 | |
| 43 TestTrackedRequestServiceImpl::TestTrackedRequestServiceImpl( | |
| 44 TrackingContext* context) | |
| 45 : context_(context) { | |
| 46 } | |
| 47 | |
| 48 TestTrackedRequestServiceImpl::~TestTrackedRequestServiceImpl() { | |
| 49 } | |
| 50 | |
| 51 void TestTrackedRequestServiceImpl::GetReport( | |
| 52 const mojo::Callback<void(mojo::Array<ServiceReportPtr>)>& callback) { | |
| 53 mojo::Array<ServiceReportPtr> reports; | |
| 54 for (AllRecordsMap::const_iterator it1 = context_->records.begin(); | |
| 55 it1 != context_->records.end(); ++it1) { | |
| 56 ServiceReportPtr report(ServiceReport::New()); | |
| 57 report->service_name = context_->ids_to_names[it1->first]; | |
| 58 double mean_health_numerator = 0; | |
| 59 size_t num_samples = it1->second.size(); | |
| 60 if (num_samples == 0) | |
| 61 continue; | |
| 62 | |
| 63 for (std::vector<ServiceStats>::const_iterator it2 = it1->second.begin(); | |
| 64 it2 != it1->second.end(); ++it2) { | |
| 65 report->total_requests += it2->num_new_requests; | |
| 66 mean_health_numerator += it2->health; | |
| 67 } | |
| 68 report->mean_health = mean_health_numerator / num_samples; | |
| 69 reports.push_back(report.Pass()); | |
| 70 } | |
| 71 callback.Run(reports.Pass()); | |
| 72 } | |
| 73 | |
| 74 } // namespace test | |
| 75 } // namespace mojo | |
| OLD | NEW |