Skip to content

Commit

Permalink
Fixed uploading bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
r.kuznetsov authored and mpimenov committed Sep 9, 2019
1 parent 5a90d77 commit 4edde70
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/alohalytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ class Stats final {
// - Gzipped.
// - Saved as out_archive for easier post-processing (e.g. uploading).
// - Deleted.
void GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive);
void GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive) const;
std::string GzipInMemoryBuffer(const std::string & buffer) const;
std::string SerializeUniqueClientId() const;

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

Expand Down
1 change: 1 addition & 0 deletions src/android/java/org/alohalytics/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public static void setup(String[] serverUrls, final Context context) {
storagePath = context.getFilesDir().getAbsolutePath() + "/Alohalytics" + i + "/";
// Native code expects valid existing writable dir.
(new File(storagePath)).mkdirs();
paths[i] = storagePath;
}

String[] urls = new String[serverUrls.length];
Expand Down
30 changes: 21 additions & 9 deletions src/cpp/alohalytics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void Stats::Enable() {
enabled_ = true;
}

void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive) {
void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std::string & out_archive) const {
std::string encoded_unique_client_id;
if (unique_client_id_.empty()) {
LOG_IF_DEBUG(
Expand All @@ -91,11 +91,7 @@ void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std:
} else {
// Pre-calculation for special ID event.
// We do it for every archived file to have a fresh timestamp.
AlohalyticsIdEvent event;
event.id = unique_client_id_;
std::ostringstream ostream;
{ cereal::BinaryOutputArchive(ostream) << std::unique_ptr<AlohalyticsBaseEvent, NoOpDeleter>(&event); }
encoded_unique_client_id = ostream.str();
encoded_unique_client_id = SerializeUniqueClientId();
}
LOG_IF_DEBUG("Archiving", in_file, "to", out_archive);
// Append unique installation id in the beginning of each archived file.
Expand Down Expand Up @@ -132,6 +128,22 @@ void Stats::GzipAndArchiveFileInTheQueue(const std::string & in_file, const std:
LOG_IF_DEBUG("CRITICAL ERROR: std::remove", in_file, "has failed with error", result, "and errno", errno);
}
}

std::string Stats::GzipInMemoryBuffer(const std::string & buffer) const {
if (unique_client_id_.empty()) {
LOG_IF_DEBUG("Warning: unique client id was not set in GzipInMemoryBuffer,"
"statistics will be completely anonymous and hard to process on the server.");
}
return alohalytics::Gzip(SerializeUniqueClientId() + buffer);
}

std::string Stats::SerializeUniqueClientId() const {
AlohalyticsIdEvent event;
event.id = unique_client_id_;
std::ostringstream ostream;
{ cereal::BinaryOutputArchive(ostream) << std::unique_ptr<AlohalyticsBaseEvent, NoOpDeleter>(&event); }
return ostream.str();
}

Stats & Stats::Instance() {
static Stats alohalytics;
Expand Down Expand Up @@ -282,7 +294,7 @@ void Stats::Upload(const TFileProcessingFinishedCallback & upload_finished_callb
r = *upload_result;
}
}
if (need_notify)
if (upload_finished_callback && need_notify)
upload_finished_callback(r);
};
for (auto & c : channels_) {
Expand All @@ -308,7 +320,7 @@ bool Stats::UploadFileImpl(const std::string & upload_url, bool file_name_in_con
if (file_name_in_content) {
request.set_body_file(content, kAlohalyticsHTTPContentType, "POST", "gzip");
} else {
request.set_body_data(alohalytics::Gzip(content), kAlohalyticsHTTPContentType, "POST", "gzip");
request.set_body_data(GzipInMemoryBuffer(content), kAlohalyticsHTTPContentType, "POST", "gzip");
}
const bool uploadSucceeded = request.RunHTTPRequest() && 200 == request.error_code() && !request.was_redirected();
LOG_IF_DEBUG("RunHTTPRequest has returned code", request.error_code(),
Expand Down Expand Up @@ -351,7 +363,7 @@ void Stats::CollectBlobsToUpload(bool delete_files, TGetBlobResultCallback resul
result->push_back(std::move(buffer));
} else {
std::lock_guard<std::mutex> lock(collect_mutex_);
result->push_back(alohalytics::Gzip(content));
result->push_back(GzipInMemoryBuffer(content));
}
return true;
} catch (const std::exception & ex) {
Expand Down

0 comments on commit 4edde70

Please sign in to comment.