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 | |
9 #include "base/logging.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/strings/stringprintf.h" | |
12 | |
13 namespace gcm { | |
14 | |
15 const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; | |
16 | |
17 namespace { | |
18 | |
19 template <typename T> | |
20 void InsertCircularBuffer(std::deque<T>* q, const T& item) { | |
21 DCHECK(q); | |
22 q->push_front(item); | |
23 if (q->size() > MAX_LOGGED_ACTIVITY_COUNT) { | |
24 q->pop_back(); | |
25 } | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 GCMStatsRecorder::Activity::Activity() | |
31 : time(base::Time::Now()), type(GCMStatsRecorder::Initiated) { | |
32 } | |
33 | |
34 GCMStatsRecorder::Activity::~Activity() { | |
35 } | |
36 | |
37 std::string GCMStatsRecorder::Activity::GetTypeString() const { | |
38 switch(type) { | |
39 case GCMStatsRecorder::Initiated: | |
40 return "Initiated"; | |
41 case GCMStatsRecorder::RetryRequested: | |
42 return "RetryRequested"; | |
43 case GCMStatsRecorder::Retry: | |
44 return "Retry"; | |
45 case GCMStatsRecorder::Success: | |
46 return "Success"; | |
47 case GCMStatsRecorder::Intermediate: | |
48 return "Intermediate"; | |
49 case GCMStatsRecorder::Failure: | |
50 return "Failure"; | |
51 case GCMStatsRecorder::Error: | |
52 return "Error"; | |
53 default: | |
54 return "Unknown"; | |
55 } | |
56 } | |
57 | |
58 GCMStatsRecorder::CheckinActivity::CheckinActivity() | |
59 : android_id(0) { | |
60 } | |
61 | |
62 GCMStatsRecorder::CheckinActivity::~CheckinActivity() { | |
63 } | |
64 | |
65 GCMStatsRecorder::RegisterActivity::RegisterActivity() { | |
66 } | |
67 | |
68 GCMStatsRecorder::RegisterActivity::~RegisterActivity() { | |
69 } | |
70 | |
71 GCMStatsRecorder::SendMessageActivity::SendMessageActivity() | |
72 : message_len(0) { | |
73 } | |
74 | |
75 GCMStatsRecorder::SendMessageActivity::~SendMessageActivity() { | |
76 } | |
77 | |
78 GCMStatsRecorder::ReceiveMessageActivity::ReceiveMessageActivity() | |
79 : message_size(0) { | |
80 } | |
81 | |
82 GCMStatsRecorder::ReceiveMessageActivity::~ReceiveMessageActivity() { | |
83 } | |
84 | |
85 GCMStatsRecorder::GCMStatsRecorder() : is_recording_(false) { | |
86 } | |
87 | |
88 GCMStatsRecorder::~GCMStatsRecorder() { | |
89 } | |
90 | |
91 void GCMStatsRecorder::SetRecording(bool recording) { | |
92 is_recording_ = recording; | |
93 } | |
94 | |
95 void GCMStatsRecorder::Clear() { | |
96 checkin_activities_.clear(); | |
97 register_activities_.clear(); | |
98 send_activities_.clear(); | |
99 receive_activities_.clear(); | |
100 } | |
101 | |
102 void GCMStatsRecorder::RecordCheckin(uint64 android_id, ActivityType type){ | |
103 if (!is_recording_) | |
104 return; | |
105 CheckinActivity data; | |
106 data.type = type; | |
107 data.android_id = android_id; | |
108 InsertCircularBuffer(&checkin_activities_, data); | |
jianli
2014/03/18 23:53:23
It might be a bit more efficient by doing somethin
juyik
2014/03/20 01:09:53
Done.
| |
109 } | |
110 | |
111 void GCMStatsRecorder::RecordRegister( | |
112 const std::string& app_id, | |
113 ActivityType type, | |
114 const std::vector<std::string>& sender_id) { | |
115 if (!is_recording_) | |
116 return; | |
117 RegisterActivity data; | |
118 data.type = type; | |
119 data.app_id = app_id; | |
120 data.sender_id = JoinString(sender_id, ','); | |
121 InsertCircularBuffer(®ister_activities_, data); | |
122 } | |
123 | |
124 void GCMStatsRecorder::RecordRegisterWithStatus( | |
125 const std::string& app_id, | |
126 ActivityType type, | |
127 const std::vector<std::string>& sender_id, | |
128 std::string status) { | |
129 if (!is_recording_) | |
130 return; | |
131 RegisterActivity data; | |
132 data.type = type; | |
133 data.app_id = app_id; | |
134 data.sender_id = JoinString(sender_id, ','); | |
135 data.details = status; | |
136 InsertCircularBuffer(®ister_activities_, data); | |
137 } | |
138 | |
139 void GCMStatsRecorder::RecordSend(const std::string& app_id, | |
140 ActivityType type, | |
141 const std::string& receiver_id, | |
142 const std::string& message_id, | |
143 uint64 msg_len) { | |
144 if (!is_recording_) | |
145 return; | |
146 RecordSendWithDetails(app_id, | |
147 type, | |
148 receiver_id, | |
149 message_id, | |
150 msg_len, | |
151 std::string()); | |
152 } | |
153 | |
154 void GCMStatsRecorder::RecordSendWithDetails(const std::string& app_id, | |
155 ActivityType type, | |
156 const std::string& receiver_id, | |
157 const std::string& message_id, | |
158 uint64 msg_len, | |
fgorski
2014/03/18 21:28:37
you are not using msg_len
juyik
2014/03/20 01:09:53
Done.
| |
159 const std::string& details) { | |
160 if (!is_recording_) | |
161 return; | |
162 SendMessageActivity data; | |
163 data.type = type; | |
164 data.app_id = app_id; | |
165 data.receiver_id = receiver_id; | |
166 data.message_id = message_id; | |
167 data.details = details; | |
168 InsertCircularBuffer(&send_activities_, data); | |
169 } | |
170 | |
171 void GCMStatsRecorder::RecordReceiveDataMessage(const std::string& sender_id, | |
172 const std::string& app_id, | |
173 int message_size) { | |
174 if (!is_recording_) | |
175 return; | |
176 ReceiveMessageActivity data; | |
177 data.type = GCMStatsRecorder::Success; | |
178 data.app_id = app_id; | |
179 data.sender_id = sender_id; | |
180 data.message_size = message_size; | |
181 InsertCircularBuffer(&receive_activities_, data); | |
182 } | |
183 | |
184 void GCMStatsRecorder::RecordReceiveWithDetails(ActivityType type, | |
185 const std::string& sender_id, | |
186 const std::string& app_id, | |
187 const std::string& details) { | |
188 if (!is_recording_) | |
189 return; | |
190 ReceiveMessageActivity data; | |
191 data.type = type; | |
192 data.app_id = app_id; | |
193 data.sender_id = sender_id; | |
194 data.details = details; | |
195 InsertCircularBuffer(&receive_activities_, data); | |
196 } | |
197 | |
198 } // namespace gcm | |
OLD | NEW |