Skip to content

Commit 4edde70

Browse files
r.kuznetsovmpimenov
r.kuznetsov
authored andcommitted
Fixed uploading bugs
1 parent 5a90d77 commit 4edde70

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/alohalytics.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ class Stats final {
8080
// - Gzipped.
8181
// - Saved as out_archive for easier post-processing (e.g. uploading).
8282
// - Deleted.
83-
void GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive);
83+
void GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive) const;
84+
std::string GzipInMemoryBuffer(const std::string & buffer) const;
85+
std::string SerializeUniqueClientId() const;
8486

8587
void LogEventImpl(AlohalyticsBaseEvent const & event, uint32_t channels_mask);
8688

src/android/java/org/alohalytics/Statistics.java

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public static void setup(String[] serverUrls, final Context context) {
143143
storagePath = context.getFilesDir().getAbsolutePath() + "/Alohalytics" + i + "/";
144144
// Native code expects valid existing writable dir.
145145
(new File(storagePath)).mkdirs();
146+
paths[i] = storagePath;
146147
}
147148

148149
String[] urls = new String[serverUrls.length];

src/cpp/alohalytics.cc

+21-9
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void Stats::Enable() {
8282
enabled_ = true;
8383
}
8484

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 {
8686
std::string encoded_unique_client_id;
8787
if (unique_client_id_.empty()) {
8888
LOG_IF_DEBUG(
@@ -91,11 +91,7 @@ void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std:
9191
} else {
9292
// Pre-calculation for special ID event.
9393
// 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();
9995
}
10096
LOG_IF_DEBUG("Archiving", in_file, "to", out_archive);
10197
// 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:
132128
LOG_IF_DEBUG("CRITICAL ERROR: std::remove", in_file, "has failed with error", result, "and errno", errno);
133129
}
134130
}
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+
}
135147

136148
Stats & Stats::Instance() {
137149
static Stats alohalytics;
@@ -282,7 +294,7 @@ void Stats::Upload(const TFileProcessingFinishedCallback & upload_finished_callb
282294
r = *upload_result;
283295
}
284296
}
285-
if (need_notify)
297+
if (upload_finished_callback && need_notify)
286298
upload_finished_callback(r);
287299
};
288300
for (auto & c : channels_) {
@@ -308,7 +320,7 @@ bool Stats::UploadFileImpl(const std::string & upload_url, bool file_name_in_con
308320
if (file_name_in_content) {
309321
request.set_body_file(content, kAlohalyticsHTTPContentType, "POST", "gzip");
310322
} else {
311-
request.set_body_data(alohalytics::Gzip(content), kAlohalyticsHTTPContentType, "POST", "gzip");
323+
request.set_body_data(GzipInMemoryBuffer(content), kAlohalyticsHTTPContentType, "POST", "gzip");
312324
}
313325
const bool uploadSucceeded = request.RunHTTPRequest() && 200 == request.error_code() && !request.was_redirected();
314326
LOG_IF_DEBUG("RunHTTPRequest has returned code", request.error_code(),
@@ -351,7 +363,7 @@ void Stats::CollectBlobsToUpload(bool delete_files, TGetBlobResultCallback resul
351363
result->push_back(std::move(buffer));
352364
} else {
353365
std::lock_guard<std::mutex> lock(collect_mutex_);
354-
result->push_back(alohalytics::Gzip(content));
366+
result->push_back(GzipInMemoryBuffer(content));
355367
}
356368
return true;
357369
} catch (const std::exception & ex) {

0 commit comments

Comments
 (0)