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

Side by Side Diff: chromecast/base/metrics/cast_metrics_helper.cc

Issue 786233003: Add UpdateCurrentAppInfo() interface to record info about current app, including (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromecast/base/metrics/cast_metrics_helper.h" 5 #include "chromecast/base/metrics/cast_metrics_helper.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/metrics/user_metrics.h" 12 #include "base/metrics/user_metrics.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
13 #include "chromecast/base/metrics/cast_histograms.h" 15 #include "chromecast/base/metrics/cast_histograms.h"
14 #include "chromecast/base/metrics/grouped_histogram.h" 16 #include "chromecast/base/metrics/grouped_histogram.h"
15 17
16 namespace chromecast { 18 namespace chromecast {
17 namespace metrics { 19 namespace metrics {
18 20
19 // A useful macro to make sure current member function runs on the valid thread. 21 // A useful macro to make sure current member function runs on the valid thread.
20 #define MAKE_SURE_THREAD(callback, ...) \ 22 #define MAKE_SURE_THREAD(callback, ...) \
21 if (!message_loop_proxy_->BelongsToCurrentThread()) { \ 23 if (!message_loop_proxy_->BelongsToCurrentThread()) { \
22 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( \ 24 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( \
23 &CastMetricsHelper::callback, \ 25 &CastMetricsHelper::callback, \
24 base::Unretained(this), ##__VA_ARGS__)); \ 26 base::Unretained(this), ##__VA_ARGS__)); \
25 return; \ 27 return; \
26 } 28 }
27 29
28 namespace { 30 namespace {
29 31
30 CastMetricsHelper* g_instance = NULL; 32 CastMetricsHelper* g_instance = NULL;
31 33
32 // Displayed frames are logged in frames per second (but sampling can be over 34 // Displayed frames are logged in frames per second (but sampling can be over
33 // a longer period of time, e.g. 5 seconds). 35 // a longer period of time, e.g. 5 seconds).
34 const int kDisplayedFramesPerSecondPeriod = 1000000; 36 const int kDisplayedFramesPerSecondPeriod = 1000000;
35 37
36 // Sample every 5 seconds, represented in microseconds. 38 // Sample every 5 seconds, represented in microseconds.
37 const int kNominalVideoSamplePeriod = 5000000; 39 const int kNominalVideoSamplePeriod = 5000000;
38 40
39 } // namespace 41 } // namespace
40 42
43 // static
44 const char CastMetricsHelper::kMetricsNameAppInfoDelimiter = '#';
45
46 // static
47 CastMetricsHelper::MetricsAppInfo CastMetricsHelper::GetAppInfoFromMetricsName(
48 const std::string& metrics_name) {
49 // TODO(gfhuang): This is a hacky way to encode/decode app infos into a
gunsch 2014/12/10 22:43:58 Got a planned approach for the TODO, or is this ju
gfhuang 2014/12/11 02:59:39 Change to a NOTE.
50 // string. Mainly because it's hard to add another metrics serialization type
51 // into components/metrics/serialization/.
52 std::vector<std::string> tokens;
53 base::SplitString(metrics_name, kMetricsNameAppInfoDelimiter, &tokens);
54 DCHECK_EQ(tokens.size(), 4u);
55 CastMetricsHelper::MetricsAppInfo metrics_app_info;
56 // The order of tokens should match GetMetricsNameWithAppInfo().
57 metrics_app_info.action_name = tokens[0];
58 metrics_app_info.app_id = tokens[1];
59 metrics_app_info.session_id = tokens[2];
60 metrics_app_info.sdk_version = tokens[3];
61 return metrics_app_info;
62 }
63
64 // static
41 CastMetricsHelper* CastMetricsHelper::GetInstance() { 65 CastMetricsHelper* CastMetricsHelper::GetInstance() {
42 DCHECK(g_instance); 66 DCHECK(g_instance);
43 return g_instance; 67 return g_instance;
44 } 68 }
45 69
70 CastMetricsHelper::MetricsAppInfo::MetricsAppInfo() {
71 }
72
73 CastMetricsHelper::MetricsAppInfo::~MetricsAppInfo() {
74 }
75
46 CastMetricsHelper::CastMetricsHelper( 76 CastMetricsHelper::CastMetricsHelper(
47 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) 77 scoped_refptr<base::MessageLoopProxy> message_loop_proxy)
48 : message_loop_proxy_(message_loop_proxy), 78 : message_loop_proxy_(message_loop_proxy),
49 metrics_sink_(NULL), 79 metrics_sink_(NULL),
50 record_action_callback_(base::Bind(&base::RecordComputedAction)) { 80 record_action_callback_(base::Bind(&base::RecordComputedAction)) {
51 DCHECK(message_loop_proxy_.get()); 81 DCHECK(message_loop_proxy_.get());
52 DCHECK(!g_instance); 82 DCHECK(!g_instance);
53 g_instance = this; 83 g_instance = this;
54 } 84 }
55 85
56 CastMetricsHelper::CastMetricsHelper() 86 CastMetricsHelper::CastMetricsHelper()
57 : metrics_sink_(NULL) { 87 : metrics_sink_(NULL) {
58 DCHECK(!g_instance); 88 DCHECK(!g_instance);
59 g_instance = this; 89 g_instance = this;
60 } 90 }
61 91
62 CastMetricsHelper::~CastMetricsHelper() { 92 CastMetricsHelper::~CastMetricsHelper() {
63 DCHECK_EQ(g_instance, this); 93 DCHECK_EQ(g_instance, this);
64 g_instance = NULL; 94 g_instance = NULL;
65 } 95 }
66 96
67 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) { 97 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) {
68 MAKE_SURE_THREAD(TagAppStart, arg_app_name); 98 MAKE_SURE_THREAD(TagAppStart, arg_app_name);
69 app_name_ = arg_app_name; 99 app_name_ = arg_app_name;
70 app_start_time_ = base::TimeTicks::Now(); 100 app_start_time_ = base::TimeTicks::Now();
71 new_startup_time_ = true; 101 new_startup_time_ = true;
72 102
73 TagAppStartForGroupedHistograms(app_name_); 103 TagAppStartForGroupedHistograms(app_name_);
104 // Clear app info
105 UpdateCurrentAppInfo("", "", "");
74 } 106 }
75 107
108 void CastMetricsHelper::UpdateCurrentAppInfo(const std::string& app_id,
109 const std::string& session_id,
110 const std::string& sdk_version) {
111 MAKE_SURE_THREAD(UpdateCurrentAppInfo, app_id, session_id, sdk_version);
112 app_id_ = app_id;
113 session_id_ = session_id;
114 sdk_version_ = sdk_version;
115 }
gunsch 2014/12/10 22:43:58 style nit: blank line between methods
gfhuang 2014/12/11 02:59:39 Done.
76 void CastMetricsHelper::LogMediaPlay() { 116 void CastMetricsHelper::LogMediaPlay() {
77 RecordSimpleAction(GetMetricsNameWithAppName("MediaPlay", "")); 117 RecordSimpleAction(GetMetricsNameWithAppInfo("MediaPlay"));
78 } 118 }
79 119
80 void CastMetricsHelper::LogMediaPause() { 120 void CastMetricsHelper::LogMediaPause() {
81 RecordSimpleAction(GetMetricsNameWithAppName("MediaPause", "")); 121 RecordSimpleAction(GetMetricsNameWithAppInfo("MediaPause"));
82 } 122 }
83 123
84 void CastMetricsHelper::LogTimeToDisplayVideo() { 124 void CastMetricsHelper::LogTimeToDisplayVideo() {
85 if (!new_startup_time_) { // For faster check. 125 if (!new_startup_time_) { // For faster check.
86 return; 126 return;
87 } 127 }
88 MAKE_SURE_THREAD(LogTimeToDisplayVideo); 128 MAKE_SURE_THREAD(LogTimeToDisplayVideo);
89 new_startup_time_ = false; 129 new_startup_time_ = false;
90 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_; 130 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_;
91 const std::string uma_name(GetMetricsNameWithAppName("Startup", 131 const std::string uma_name(GetMetricsNameWithAppName("Startup",
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 metrics_name.append(app_name_); 222 metrics_name.append(app_name_);
183 } 223 }
184 if (!suffix.empty()) { 224 if (!suffix.empty()) {
185 if (!metrics_name.empty()) 225 if (!metrics_name.empty())
186 metrics_name.push_back('.'); 226 metrics_name.push_back('.');
187 metrics_name.append(suffix); 227 metrics_name.append(suffix);
188 } 228 }
189 return metrics_name; 229 return metrics_name;
190 } 230 }
191 231
232 std::string CastMetricsHelper::GetMetricsNameWithAppInfo(
233 const std::string& action_name) const {
234 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
235 std::vector<std::string> parts;
236 parts.push_back(action_name);
237 parts.push_back(app_id_);
238 parts.push_back(session_id_);
239 parts.push_back(sdk_version_);
240 return JoinString(parts, kMetricsNameAppInfoDelimiter);
241 }
242
192 void CastMetricsHelper::SetMetricsSink(MetricsSink* delegate) { 243 void CastMetricsHelper::SetMetricsSink(MetricsSink* delegate) {
193 MAKE_SURE_THREAD(SetMetricsSink, delegate); 244 MAKE_SURE_THREAD(SetMetricsSink, delegate);
194 metrics_sink_ = delegate; 245 metrics_sink_ = delegate;
195 } 246 }
196 247
197 void CastMetricsHelper::SetRecordActionCallback( 248 void CastMetricsHelper::SetRecordActionCallback(
198 const RecordActionCallback& callback) { 249 const RecordActionCallback& callback) {
199 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); 250 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
200 record_action_callback_ = callback; 251 record_action_callback_ = callback;
201 } 252 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 const base::TimeDelta& value) { 291 const base::TimeDelta& value) {
241 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. 292 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition.
242 LogTimeHistogramEvent(name, value, 293 LogTimeHistogramEvent(name, value,
243 base::TimeDelta::FromMilliseconds(10), 294 base::TimeDelta::FromMilliseconds(10),
244 base::TimeDelta::FromMinutes(3), 295 base::TimeDelta::FromMinutes(3),
245 50); 296 50);
246 } 297 }
247 298
248 } // namespace metrics 299 } // namespace metrics
249 } // namespace chromecast 300 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698