| OLD | NEW |
| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 const char kMetricsNameAppInfoDelimiter = '#'; | 41 const char kMetricsNameAppInfoDelimiter = '#'; |
| 42 | 42 |
| 43 std::unique_ptr<std::string> SerializeToJson(const base::Value& value) { | 43 std::unique_ptr<std::string> SerializeToJson(const base::Value& value) { |
| 44 std::unique_ptr<std::string> json_str(new std::string()); | 44 std::unique_ptr<std::string> json_str(new std::string()); |
| 45 JSONStringValueSerializer serializer(json_str.get()); | 45 JSONStringValueSerializer serializer(json_str.get()); |
| 46 if (!serializer.Serialize(value)) | 46 if (!serializer.Serialize(value)) |
| 47 json_str.reset(nullptr); | 47 json_str.reset(nullptr); |
| 48 return json_str; | 48 return json_str; |
| 49 } | 49 } |
| 50 | 50 |
| 51 std::unique_ptr<base::DictionaryValue> CreateEventBase( |
| 52 const std::string& name) { |
| 53 std::unique_ptr<base::DictionaryValue> cast_event( |
| 54 new base::DictionaryValue()); |
| 55 cast_event->SetString("name", name); |
| 56 cast_event->SetDouble("time", base::TimeTicks::Now().ToInternalValue()); |
| 57 |
| 58 return cast_event; |
| 59 } |
| 60 |
| 51 } // namespace | 61 } // namespace |
| 52 | 62 |
| 53 // static | 63 // static |
| 54 | 64 |
| 55 // NOTE(gfhuang): This is a hacky way to encode/decode app infos into a | 65 // NOTE(gfhuang): This is a hacky way to encode/decode app infos into a |
| 56 // string. Mainly because it's hard to add another metrics serialization type | 66 // string. Mainly because it's hard to add another metrics serialization type |
| 57 // into components/metrics/serialization/. | 67 // into components/metrics/serialization/. |
| 58 // static | 68 // static |
| 59 bool CastMetricsHelper::DecodeAppInfoFromMetricsName( | 69 bool CastMetricsHelper::DecodeAppInfoFromMetricsName( |
| 60 const std::string& metrics_name, | 70 const std::string& metrics_name, |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 void CastMetricsHelper::LogMediumTimeHistogramEvent( | 294 void CastMetricsHelper::LogMediumTimeHistogramEvent( |
| 285 const std::string& name, | 295 const std::string& name, |
| 286 const base::TimeDelta& value) { | 296 const base::TimeDelta& value) { |
| 287 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. | 297 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. |
| 288 LogTimeHistogramEvent(name, value, | 298 LogTimeHistogramEvent(name, value, |
| 289 base::TimeDelta::FromMilliseconds(10), | 299 base::TimeDelta::FromMilliseconds(10), |
| 290 base::TimeDelta::FromMinutes(3), | 300 base::TimeDelta::FromMinutes(3), |
| 291 50); | 301 50); |
| 292 } | 302 } |
| 293 | 303 |
| 304 void CastMetricsHelper::RecordEventWithValue(const std::string& event, |
| 305 int value) { |
| 306 std::unique_ptr<base::DictionaryValue> cast_event(CreateEventBase(event)); |
| 307 cast_event->SetInteger("value", value); |
| 308 const std::string message = *SerializeToJson(*cast_event); |
| 309 RecordSimpleAction(message); |
| 310 } |
| 311 |
| 294 void CastMetricsHelper::RecordApplicationEvent(const std::string& event) { | 312 void CastMetricsHelper::RecordApplicationEvent(const std::string& event) { |
| 295 std::unique_ptr<base::DictionaryValue> cast_event( | 313 std::unique_ptr<base::DictionaryValue> cast_event(CreateEventBase(event)); |
| 296 new base::DictionaryValue()); | |
| 297 cast_event->SetString("name", event); | |
| 298 base::TimeTicks now = base::TimeTicks::Now(); | |
| 299 cast_event->SetDouble("time", now.ToInternalValue()); | |
| 300 cast_event->SetString("app_id", app_id_); | 314 cast_event->SetString("app_id", app_id_); |
| 301 cast_event->SetString("session_id", session_id_); | 315 cast_event->SetString("session_id", session_id_); |
| 302 cast_event->SetString("sdk_version", sdk_version_); | 316 cast_event->SetString("sdk_version", sdk_version_); |
| 303 const std::string message = *SerializeToJson(*cast_event.get()).get(); | 317 const std::string message = *SerializeToJson(*cast_event); |
| 304 RecordSimpleAction(message); | 318 RecordSimpleAction(message); |
| 305 } | 319 } |
| 306 | 320 |
| 307 void CastMetricsHelper::RecordApplicationEventWithValue( | 321 void CastMetricsHelper::RecordApplicationEventWithValue( |
| 308 const std::string& event, | 322 const std::string& event, |
| 309 int value) { | 323 int value) { |
| 310 std::unique_ptr<base::DictionaryValue> cast_event( | 324 std::unique_ptr<base::DictionaryValue> cast_event(CreateEventBase(event)); |
| 311 new base::DictionaryValue()); | |
| 312 cast_event->SetString("name", event); | |
| 313 base::TimeTicks now = base::TimeTicks::Now(); | |
| 314 cast_event->SetDouble("time", now.ToInternalValue()); | |
| 315 cast_event->SetString("app_id", app_id_); | 325 cast_event->SetString("app_id", app_id_); |
| 316 cast_event->SetString("session_id", session_id_); | 326 cast_event->SetString("session_id", session_id_); |
| 317 cast_event->SetString("sdk_version", sdk_version_); | 327 cast_event->SetString("sdk_version", sdk_version_); |
| 318 cast_event->SetInteger("value", value); | 328 cast_event->SetInteger("value", value); |
| 319 const std::string message = *SerializeToJson(*cast_event.get()).get(); | 329 const std::string message = *SerializeToJson(*cast_event); |
| 320 RecordSimpleAction(message); | 330 RecordSimpleAction(message); |
| 321 } | 331 } |
| 322 | 332 |
| 323 } // namespace metrics | 333 } // namespace metrics |
| 324 } // namespace chromecast | 334 } // namespace chromecast |
| OLD | NEW |