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 "google_apis/gcm/gcm_stats_recorder.h" | |
6 | |
7 #include <deque> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/metrics/histogram.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/stringprintf.h" | |
14 | |
15 namespace gcm { | |
16 | |
17 const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; | |
18 | |
19 namespace { | |
20 | |
21 // Insert an itme to the front of deque while maintaining the size of the deque. | |
22 // Overflow item is discarded. | |
23 template <typename T> | |
24 T* InsertCircularBuffer(std::deque<T>* q, const T& item) { | |
25 DCHECK(q); | |
26 q->push_front(item); | |
27 if (q->size() > MAX_LOGGED_ACTIVITY_COUNT) { | |
28 q->pop_back(); | |
29 } | |
30 return &q->front(); | |
31 } | |
32 | |
33 // Helper for getting string representation of the MessageSendStatus enum. | |
34 std::string GetMessageSendStatusString( | |
35 gcm::MCSClient::MessageSendStatus status) { | |
36 switch (status) { | |
37 case gcm::MCSClient::QUEUED: | |
38 return "QUEUED"; | |
39 case gcm::MCSClient::SENT: | |
40 return "SENT"; | |
41 case gcm::MCSClient::QUEUE_SIZE_LIMIT_REACHED: | |
42 return "QUEUE_SIZE_LIMIT_REACHED"; | |
43 case gcm::MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: | |
44 return "APP_QUEUE_SIZE_LIMIT_REACHED"; | |
45 case gcm::MCSClient::MESSAGE_TOO_LARGE: | |
46 return "MESSAGE_TOO_LARGE"; | |
47 case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: | |
48 return "NO_CONNECTION_ON_ZERO_TTL"; | |
49 case gcm::MCSClient::TTL_EXCEEDED: | |
50 return "TTL_EXCEEDED"; | |
51 default: | |
52 NOTREACHED(); | |
53 return "UNKNOWN"; | |
54 } | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 GCMStatsRecorder::Activity::Activity() | |
60 : time(base::Time::Now()) { | |
61 } | |
62 | |
63 GCMStatsRecorder::Activity::~Activity() { | |
64 } | |
65 | |
66 GCMStatsRecorder::SendingActivity::SendingActivity() { | |
67 } | |
68 | |
69 GCMStatsRecorder::SendingActivity::~SendingActivity() { | |
70 } | |
71 | |
72 GCMStatsRecorder::GCMStatsRecorder() : is_recording_(false) { | |
73 } | |
74 | |
75 GCMStatsRecorder::~GCMStatsRecorder() { | |
76 } | |
77 | |
78 void GCMStatsRecorder::SetRecording(bool recording) { | |
79 is_recording_ = recording; | |
80 } | |
81 | |
82 void GCMStatsRecorder::Clear() { | |
83 sending_activities_.clear(); | |
84 } | |
85 | |
86 void GCMStatsRecorder::CollectSendingActivities( | |
87 std::vector<SendingActivity>* activities) const { | |
88 for (std::deque<GCMStatsRecorder::SendingActivity>::const_iterator it = | |
fgorski
2014/04/01 03:36:29
nit: you could still use:
activities->insert(
juyik
2014/04/01 22:40:36
Done.
| |
89 sending_activities_.begin(); | |
fgorski
2014/04/01 03:36:29
nit: indentation
juyik
2014/04/01 22:40:36
Done.
| |
90 it != sending_activities_.end(); | |
91 ++it) { | |
92 activities->push_back(*it); | |
93 } | |
94 } | |
95 | |
96 void GCMStatsRecorder::RecordSending(const std::string& app_id, | |
97 const std::string& receiver_id, | |
98 const std::string& message_id, | |
99 const std::string& event, | |
100 const std::string& details) { | |
101 SendingActivity data; | |
102 SendingActivity* inserted_data = InsertCircularBuffer( | |
103 &sending_activities_, data); | |
104 inserted_data->app_id = app_id; | |
105 inserted_data->receiver_id = receiver_id; | |
106 inserted_data->message_id = message_id; | |
107 inserted_data->event = event; | |
108 inserted_data->details = details; | |
109 } | |
110 | |
111 void GCMStatsRecorder::RecordDataSentToWire( | |
112 const std::string& app_id, | |
113 const std::string& receiver_id, | |
114 const std::string& message_id, | |
115 int queued) { | |
116 if (is_recording_) { | |
117 RecordSending(app_id, receiver_id, message_id, "Data msg sent to wire", | |
118 base::StringPrintf("Msg queued for %d seconds", queued)); | |
119 } | |
120 } | |
121 | |
122 void GCMStatsRecorder::RecordNotifySendStatus( | |
123 const std::string& app_id, | |
124 const std::string& receiver_id, | |
125 const std::string& message_id, | |
126 gcm::MCSClient::MessageSendStatus status, | |
127 int byte_size, | |
128 int ttl) { | |
129 UMA_HISTOGRAM_ENUMERATION("GCM.SendMessageStatus", status, | |
130 gcm::MCSClient::SEND_STATUS_COUNT); | |
131 if (is_recording_) { | |
132 RecordSending( | |
133 app_id, | |
134 receiver_id, | |
135 message_id, | |
136 base::StringPrintf("SEND status: %s", | |
137 GetMessageSendStatusString(status).c_str()), | |
138 base::StringPrintf("Msg size: %d bytes, TTL: %d", byte_size, ttl)); | |
139 } | |
140 } | |
141 | |
142 void GCMStatsRecorder::RecordIncomingSendError( | |
143 const std::string& app_id, | |
144 const std::string& receiver_id, | |
145 const std::string& message_id) { | |
146 UMA_HISTOGRAM_COUNTS("GCM.IncomingSendErrors", 1); | |
147 if (is_recording_) { | |
148 RecordSending(app_id, receiver_id, message_id, "Received 'send error' msg", | |
149 std::string()); | |
150 } | |
151 } | |
152 | |
153 } // namespace gcm | |
OLD | NEW |