Skip to content

Commit

Permalink
Merge pull request #1658 from janhq/j/update-download-event
Browse files Browse the repository at this point in the history
chore: update download event
  • Loading branch information
namchuai authored Nov 8, 2024
2 parents 505cac8 + aa71b87 commit 09599ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
26 changes: 11 additions & 15 deletions engine/cli/utils/download_progress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
}
}
#endif
std::unordered_map<std::string, uint64_t> totals;
status_ = DownloadStatus::DownloadStarted;
std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;

std::vector<std::unique_ptr<indicators::ProgressBar>> items;
indicators::show_console_cursor(false);
auto handle_message = [this, &bars, &items, &totals,
auto handle_message = [this, &bars, &items,
event_type](const std::string& message) {
CTL_INF(message);

Expand Down Expand Up @@ -98,27 +97,24 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
}
for (int i = 0; i < ev.download_task_.items.size(); i++) {
auto& it = ev.download_task_.items[i];
uint64_t downloaded = it.downloadedBytes.value_or(0);
if (totals.find(it.id) == totals.end()) {
totals[it.id] = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
CTL_INF("Updated " << it.id << " - total: " << totals[it.id]);
}

if (ev.type_ == DownloadStatus::DownloadStarted ||
ev.type_ == DownloadStatus::DownloadUpdated) {
if (ev.type_ == DownloadStatus::DownloadUpdated) {
uint64_t downloaded = it.downloadedBytes.value_or(0u);
uint64_t total =
it.bytes.value_or(std::numeric_limits<uint64_t>::max());
(*bars)[i].set_option(indicators::option::PrefixText{
pad_string(Repo2Engine(it.id)) +
std::to_string(
int(static_cast<double>(downloaded) / totals[it.id] * 100)) +
std::to_string(int(static_cast<double>(downloaded) / total * 100)) +
'%'});
(*bars)[i].set_progress(
int(static_cast<double>(downloaded) / totals[it.id] * 100));
int(static_cast<double>(downloaded) / total * 100));
(*bars)[i].set_option(indicators::option::PostfixText{
format_utils::BytesToHumanReadable(downloaded) + "/" +
format_utils::BytesToHumanReadable(totals[it.id])});
format_utils::BytesToHumanReadable(total)});
} else if (ev.type_ == DownloadStatus::DownloadSuccess) {
uint64_t total =
it.bytes.value_or(std::numeric_limits<uint64_t>::max());
(*bars)[i].set_progress(100);
auto total_str = format_utils::BytesToHumanReadable(totals[it.id]);
auto total_str = format_utils::BytesToHumanReadable(total);
(*bars)[i].set_option(
indicators::option::PostfixText{total_str + "/" + total_str});
(*bars)[i].set_option(indicators::option::PrefixText{
Expand Down
10 changes: 8 additions & 2 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
fclose(pair.second);
}
downloading_data_map_.clear();
auto copied_task = *active_task_;
active_task_.reset();

RemoveTaskFromStopList(task.id);
Expand All @@ -298,15 +299,20 @@ void DownloadService::ProcessTask(DownloadTask& task) {
event_queue_->enqueue(
EventType::DownloadEvent,
DownloadEvent{.type_ = DownloadEventType::DownloadStopped,
.download_task_ = task});
.download_task_ = copied_task});
} else {
CTL_INF("Executing callback..");
ExecuteCallback(task);

// set all items to done
for (auto& item : copied_task.items) {
item.downloadedBytes = item.bytes;
}

event_queue_->enqueue(
EventType::DownloadEvent,
DownloadEvent{.type_ = DownloadEventType::DownloadSuccess,
.download_task_ = task});
.download_task_ = copied_task});
}
}

Expand Down
7 changes: 7 additions & 0 deletions engine/services/download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ class DownloadService {
}
}

auto all_items_bytes_greater_than_zero =
std::all_of(active_task->items.begin(), active_task->items.end(),
[](const DownloadItem& item) { return item.bytes > 0; });
if (!all_items_bytes_greater_than_zero) {
return 0;
}

// Check if one second has passed since the last event
static auto last_event_time = std::chrono::steady_clock::now();
auto current_time = std::chrono::steady_clock::now();
Expand Down

0 comments on commit 09599ec

Please sign in to comment.