Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 09599ec

Browse files
authored
Merge pull request #1658 from janhq/j/update-download-event
chore: update download event
2 parents 505cac8 + aa71b87 commit 09599ec

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

engine/cli/utils/download_progress.cc

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
5050
}
5151
}
5252
#endif
53-
std::unordered_map<std::string, uint64_t> totals;
5453
status_ = DownloadStatus::DownloadStarted;
5554
std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;
5655

5756
std::vector<std::unique_ptr<indicators::ProgressBar>> items;
5857
indicators::show_console_cursor(false);
59-
auto handle_message = [this, &bars, &items, &totals,
58+
auto handle_message = [this, &bars, &items,
6059
event_type](const std::string& message) {
6160
CTL_INF(message);
6261

@@ -98,27 +97,24 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
9897
}
9998
for (int i = 0; i < ev.download_task_.items.size(); i++) {
10099
auto& it = ev.download_task_.items[i];
101-
uint64_t downloaded = it.downloadedBytes.value_or(0);
102-
if (totals.find(it.id) == totals.end()) {
103-
totals[it.id] = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
104-
CTL_INF("Updated " << it.id << " - total: " << totals[it.id]);
105-
}
106-
107-
if (ev.type_ == DownloadStatus::DownloadStarted ||
108-
ev.type_ == DownloadStatus::DownloadUpdated) {
100+
if (ev.type_ == DownloadStatus::DownloadUpdated) {
101+
uint64_t downloaded = it.downloadedBytes.value_or(0u);
102+
uint64_t total =
103+
it.bytes.value_or(std::numeric_limits<uint64_t>::max());
109104
(*bars)[i].set_option(indicators::option::PrefixText{
110105
pad_string(Repo2Engine(it.id)) +
111-
std::to_string(
112-
int(static_cast<double>(downloaded) / totals[it.id] * 100)) +
106+
std::to_string(int(static_cast<double>(downloaded) / total * 100)) +
113107
'%'});
114108
(*bars)[i].set_progress(
115-
int(static_cast<double>(downloaded) / totals[it.id] * 100));
109+
int(static_cast<double>(downloaded) / total * 100));
116110
(*bars)[i].set_option(indicators::option::PostfixText{
117111
format_utils::BytesToHumanReadable(downloaded) + "/" +
118-
format_utils::BytesToHumanReadable(totals[it.id])});
112+
format_utils::BytesToHumanReadable(total)});
119113
} else if (ev.type_ == DownloadStatus::DownloadSuccess) {
114+
uint64_t total =
115+
it.bytes.value_or(std::numeric_limits<uint64_t>::max());
120116
(*bars)[i].set_progress(100);
121-
auto total_str = format_utils::BytesToHumanReadable(totals[it.id]);
117+
auto total_str = format_utils::BytesToHumanReadable(total);
122118
(*bars)[i].set_option(
123119
indicators::option::PostfixText{total_str + "/" + total_str});
124120
(*bars)[i].set_option(indicators::option::PrefixText{

engine/services/download_service.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
288288
fclose(pair.second);
289289
}
290290
downloading_data_map_.clear();
291+
auto copied_task = *active_task_;
291292
active_task_.reset();
292293

293294
RemoveTaskFromStopList(task.id);
@@ -298,15 +299,20 @@ void DownloadService::ProcessTask(DownloadTask& task) {
298299
event_queue_->enqueue(
299300
EventType::DownloadEvent,
300301
DownloadEvent{.type_ = DownloadEventType::DownloadStopped,
301-
.download_task_ = task});
302+
.download_task_ = copied_task});
302303
} else {
303304
CTL_INF("Executing callback..");
304305
ExecuteCallback(task);
305306

307+
// set all items to done
308+
for (auto& item : copied_task.items) {
309+
item.downloadedBytes = item.bytes;
310+
}
311+
306312
event_queue_->enqueue(
307313
EventType::DownloadEvent,
308314
DownloadEvent{.type_ = DownloadEventType::DownloadSuccess,
309-
.download_task_ = task});
315+
.download_task_ = copied_task});
310316
}
311317
}
312318

engine/services/download_service.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ class DownloadService {
149149
}
150150
}
151151

152+
auto all_items_bytes_greater_than_zero =
153+
std::all_of(active_task->items.begin(), active_task->items.end(),
154+
[](const DownloadItem& item) { return item.bytes > 0; });
155+
if (!all_items_bytes_greater_than_zero) {
156+
return 0;
157+
}
158+
152159
// Check if one second has passed since the last event
153160
static auto last_event_time = std::chrono::steady_clock::now();
154161
auto current_time = std::chrono::steady_clock::now();

0 commit comments

Comments
 (0)