Skip to content

Commit

Permalink
Adds download stats
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Feb 20, 2024
1 parent e25606b commit f89ed43
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 13 deletions.
51 changes: 41 additions & 10 deletions libraries/lib-cloud-audiocom/CloudSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,16 @@ bool HasAutosave(const std::string& path)

bool DropAutosave(const std::string& path)
{
auto connection = sqlite::Connection::Open(path, sqlite::OpenMode::ReadWrite);
auto connection =
sqlite::Connection::Open(path, sqlite::OpenMode::ReadWrite);

if (!connection)
return false;

if (!connection->CheckTableExists("autosave"))
return false;

auto statement =
connection->CreateStatement("DELETE FROM autosave");
auto statement = connection->CreateStatement("DELETE FROM autosave");

if (!statement)
return false;
Expand Down Expand Up @@ -451,7 +451,10 @@ void CloudSyncService::CompleteSync(std::string path)
void CloudSyncService::CompleteSync(sync::ProjectSyncResult result)
{
if (mRemoteSnapshot)
{
result.Stats = mRemoteSnapshot->GetTransferStats();
ReportUploadStats(mRemoteSnapshot->GetProjectId(), result.Stats);
}

mSyncPromise.set_value(std::move(result));
mRemoteSnapshot.reset();
Expand All @@ -469,10 +472,10 @@ void CloudSyncService::SyncCloudSnapshot(
const auto createNew = !localProjectInfo || mode == SyncMode::ForceNew;

const auto wxPath = createNew ?
sync::MakeSafeProjectPath(
CloudProjectsSavePath.Read(),
audacity::ToWXString(projectInfo.Name)) :
audacity::ToWXString(localProjectInfo->LocalPath);
sync::MakeSafeProjectPath(
CloudProjectsSavePath.Read(),
audacity::ToWXString(projectInfo.Name)) :
audacity::ToWXString(localProjectInfo->LocalPath);

const auto utf8Path = audacity::ToUTF8(wxPath);

Expand All @@ -499,8 +502,7 @@ void CloudSyncService::SyncCloudSnapshot(

mRemoteSnapshot = sync::RemoteProjectSnapshot::Sync(
projectInfo, snapshotInfo, utf8Path,
[this, createNew, path = utf8Path,
projectId = projectInfo.Id,
[this, createNew, path = utf8Path, projectId = projectInfo.Id,
snapshotId = snapshotInfo.Id](sync::RemoteProjectSnapshotState state)
{
UpdateDowloadProgress(
Expand All @@ -517,7 +519,8 @@ void CloudSyncService::SyncCloudSnapshot(
sync::ProjectSyncResult::StatusCode::Failed,
std::move(state.Result), std::move(path) });
}
}, mode == SyncMode::ForceNew);
},
mode == SyncMode::ForceNew);
}

void CloudSyncService::UpdateDowloadProgress(double downloadProgress)
Expand All @@ -539,4 +542,32 @@ void CloudSyncService::UpdateDowloadProgress(double downloadProgress)
});
}

void CloudSyncService::ReportUploadStats(
std::string_view projectId, const TransferStats& stats)
{
sync::NetworkStats networkStats;

networkStats.Bytes = stats.BytesTransferred;
networkStats.Blocks = stats.BlocksTransferred;
networkStats.Files = stats.ProjectFilesTransferred;
networkStats.Mixes = 0;
networkStats.IsDownload = true;

using namespace audacity::network_manager;

auto request = Request(GetServiceConfig().GetNetworkStatsUrl(projectId));

request.setHeader(
common_headers::ContentType, common_content_types::ApplicationJson);

SetCommonHeaders(request);

auto body = Serialize(networkStats);

auto response =
NetworkManager::GetInstance().doPost(request, body.data(), body.size());
// Keep response alive
response->setRequestFinishedCallback([response](auto) {});
}

} // namespace cloud::audiocom
3 changes: 3 additions & 0 deletions libraries/lib-cloud-audiocom/CloudSyncService.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class CLOUD_AUDIOCOM_API CloudSyncService final

void UpdateDowloadProgress(double downloadProgress);

void
ReportUploadStats(std::string_view projectId, const TransferStats& stats);

std::vector<std::shared_ptr<sync::LocalProjectSnapshot>> mLocalSnapshots;
std::shared_ptr<sync::RemoteProjectSnapshot> mRemoteSnapshot;

Expand Down
10 changes: 10 additions & 0 deletions libraries/lib-cloud-audiocom/ServiceConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ std::string ServiceConfig::GetSnapshotInfoUrl(
});
}

std::string ServiceConfig::GetNetworkStatsUrl(std::string_view projectId) const
{
return Substitute(
"{api_url}/project/{project_id}/network-stats",
{
{ "api_url", mApiEndpoint },
{ "project_id", projectId },
});
}

const ServiceConfig& GetServiceConfig()
{
static ServiceConfig config;
Expand Down
1 change: 1 addition & 0 deletions libraries/lib-cloud-audiocom/ServiceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CLOUD_AUDIOCOM_API ServiceConfig final
std::string GetSnapshotInfoUrl(
std::string_view projectId, std::string_view snapshotId) const;

std::string GetNetworkStatsUrl(std::string_view projectId) const;
private:
std::string mApiEndpoint;
std::string mOAuthClientID;
Expand Down
22 changes: 21 additions & 1 deletion libraries/lib-cloud-audiocom/sync/CloudSyncUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ template<typename T> bool Deserialize (std::string_view data, T& result)

} // namespace

std::string SerializeProjectForm(const ProjectForm& form)
std::string Serialize(const ProjectForm& form)
{
using namespace rapidjson;

Expand Down Expand Up @@ -472,4 +472,24 @@ MakeSafeProjectPath(const wxString& rootDir, const wxString& projectName)
return path.GetFullPath();
}

std::string Serialize(NetworkStats stats)
{
using namespace rapidjson;

Document document;
document.SetObject();

document.AddMember("is_download", stats.IsDownload, document.GetAllocator());
document.AddMember("bytes", stats.Bytes, document.GetAllocator());
document.AddMember("blocks", stats.Blocks, document.GetAllocator());
document.AddMember("mixes", stats.Mixes, document.GetAllocator());
document.AddMember("files", stats.Files, document.GetAllocator());

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
document.Accept(writer);

return buffer.GetString();
}

} // namespace cloud::audiocom::sync
13 changes: 12 additions & 1 deletion libraries/lib-cloud-audiocom/sync/CloudSyncUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct ProjectForm final
};


std::string SerializeProjectForm(const ProjectForm& form);
std::string Serialize(const ProjectForm& form);

struct UploadUrls final
{
Expand Down Expand Up @@ -135,6 +135,15 @@ struct PaginatedProjectsResponse final
PaginationInfo Pagination;
};

struct NetworkStats final
{
int64_t Files {};
int64_t Blocks {};
int64_t Bytes {};
int64_t Mixes {};
bool IsDownload {};
};

std::optional<CreateSnapshotResponse>
DeserializeCreateSnapshotResponse(const std::string& data);
std::optional<PaginatedProjectsResponse>
Expand All @@ -143,6 +152,8 @@ DeserializePaginatedProjectsResponse(const std::string& data);
std::optional<ProjectInfo> DeserializeProjectInfo(const std::string& data);
std::optional<SnapshotInfo> DeserializeSnapshotInfo(const std::string& data);

std::string Serialize(NetworkStats stats);

CLOUD_AUDIOCOM_API wxString
MakeSafeProjectPath(const wxString& rootDir, const wxString& projectName);

Expand Down
2 changes: 1 addition & 1 deletion libraries/lib-cloud-audiocom/sync/LocalProjectSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void LocalProjectSnapshot::UpdateProjectSnapshot()
request.setHeader(
common_headers::Authorization, mOAuthService.GetAccessToken());

auto serializedForm = SerializeProjectForm(projectForm);
auto serializedForm = Serialize(projectForm);

auto response = NetworkManager::GetInstance().doPost(
request, serializedForm.data(), serializedForm.size());
Expand Down
5 changes: 5 additions & 0 deletions libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ TransferStats RemoteProjectSnapshot::GetTransferStats() const
std::chrono::duration_cast<TransferStats::Duration>(duration));
}

std::string_view RemoteProjectSnapshot::GetProjectId() const
{
return mProjectInfo.Id;
}

std::string RemoteProjectSnapshot::AttachOriginalDB()
{
const std::string dbName = "o_" + mProjectInfo.Id;
Expand Down
2 changes: 2 additions & 0 deletions libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class RemoteProjectSnapshot final

TransferStats GetTransferStats() const;

std::string_view GetProjectId() const;

private:
enum class State
{
Expand Down

0 comments on commit f89ed43

Please sign in to comment.