@@ -82,7 +82,7 @@ void Stats::Enable() {
82
82
enabled_ = true ;
83
83
}
84
84
85
- void Stats::GzipAndArchiveFileInTheQueue (const std::string & in_file, const std::string & out_archive) {
85
+ void Stats::GzipAndArchiveFileInTheQueue (const std::string & in_file, const std::string & out_archive) const {
86
86
std::string encoded_unique_client_id;
87
87
if (unique_client_id_.empty ()) {
88
88
LOG_IF_DEBUG (
@@ -91,11 +91,7 @@ void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std:
91
91
} else {
92
92
// Pre-calculation for special ID event.
93
93
// We do it for every archived file to have a fresh timestamp.
94
- AlohalyticsIdEvent event;
95
- event.id = unique_client_id_;
96
- std::ostringstream ostream;
97
- { cereal::BinaryOutputArchive (ostream) << std::unique_ptr<AlohalyticsBaseEvent, NoOpDeleter>(&event); }
98
- encoded_unique_client_id = ostream.str ();
94
+ encoded_unique_client_id = SerializeUniqueClientId ();
99
95
}
100
96
LOG_IF_DEBUG (" Archiving" , in_file, " to" , out_archive);
101
97
// Append unique installation id in the beginning of each archived file.
@@ -132,6 +128,22 @@ void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std:
132
128
LOG_IF_DEBUG (" CRITICAL ERROR: std::remove" , in_file, " has failed with error" , result, " and errno" , errno);
133
129
}
134
130
}
131
+
132
+ std::string Stats::GzipInMemoryBuffer (const std::string & buffer) const {
133
+ if (unique_client_id_.empty ()) {
134
+ LOG_IF_DEBUG (" Warning: unique client id was not set in GzipInMemoryBuffer,"
135
+ " statistics will be completely anonymous and hard to process on the server." );
136
+ }
137
+ return alohalytics::Gzip (SerializeUniqueClientId () + buffer);
138
+ }
139
+
140
+ std::string Stats::SerializeUniqueClientId () const {
141
+ AlohalyticsIdEvent event;
142
+ event.id = unique_client_id_;
143
+ std::ostringstream ostream;
144
+ { cereal::BinaryOutputArchive (ostream) << std::unique_ptr<AlohalyticsBaseEvent, NoOpDeleter>(&event); }
145
+ return ostream.str ();
146
+ }
135
147
136
148
Stats & Stats::Instance () {
137
149
static Stats alohalytics;
@@ -282,7 +294,7 @@ void Stats::Upload(const TFileProcessingFinishedCallback & upload_finished_callb
282
294
r = *upload_result;
283
295
}
284
296
}
285
- if (need_notify)
297
+ if (upload_finished_callback && need_notify)
286
298
upload_finished_callback (r);
287
299
};
288
300
for (auto & c : channels_) {
@@ -308,7 +320,7 @@ bool Stats::UploadFileImpl(const std::string & upload_url, bool file_name_in_con
308
320
if (file_name_in_content) {
309
321
request.set_body_file (content, kAlohalyticsHTTPContentType , " POST" , " gzip" );
310
322
} else {
311
- request.set_body_data (alohalytics::Gzip (content), kAlohalyticsHTTPContentType , " POST" , " gzip" );
323
+ request.set_body_data (GzipInMemoryBuffer (content), kAlohalyticsHTTPContentType , " POST" , " gzip" );
312
324
}
313
325
const bool uploadSucceeded = request.RunHTTPRequest () && 200 == request.error_code () && !request.was_redirected ();
314
326
LOG_IF_DEBUG (" RunHTTPRequest has returned code" , request.error_code (),
@@ -351,7 +363,7 @@ void Stats::CollectBlobsToUpload(bool delete_files, TGetBlobResultCallback resul
351
363
result->push_back (std::move (buffer));
352
364
} else {
353
365
std::lock_guard<std::mutex> lock (collect_mutex_);
354
- result->push_back (alohalytics::Gzip (content));
366
+ result->push_back (GzipInMemoryBuffer (content));
355
367
}
356
368
return true ;
357
369
} catch (const std::exception & ex) {
0 commit comments