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

Commit b93a501

Browse files
committed
update
1 parent 229b092 commit b93a501

File tree

7 files changed

+48
-48
lines changed

7 files changed

+48
-48
lines changed

engine/common/download_event.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,9 @@ std::string DownloadEventTypeToString(DownloadEventType type) {
3838
}
3939
} // namespace
4040

41-
class DownloadEvent : public cortex::event::Event {
42-
public:
43-
explicit DownloadEvent(DownloadEventType type, const DownloadTask task)
44-
: type_{type}, download_task_{task} {}
41+
struct DownloadEvent : public cortex::event::Event {
42+
std::string ToString() const { return download_task_.ToString(); }
4543

46-
std::string ToString() const {
47-
return "DownloadEvent: " + DownloadEventTypeToString(type_) +
48-
" task: " + download_task_.ToString();
49-
}
50-
51-
private:
5244
DownloadEventType type_;
5345
DownloadTask download_task_;
5446
};

engine/common/download_task.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ struct DownloadItem {
2020

2121
std::optional<uint64_t> bytes;
2222

23+
std::optional<uint64_t> downloadedBytes;
24+
2325
std::string ToString() const {
2426
std::ostringstream output;
2527
output << "DownloadItem{id: " << id << ", downloadUrl: " << downloadUrl
2628
<< ", localContainerPath: " << localPath
29+
<< ", bytes: " << bytes.value_or(0)
30+
<< ", downloadedBytes: " << downloadedBytes.value_or(0)
2731
<< ", checksum: " << checksum.value_or("N/A") << "}";
2832
return output.str();
2933
}
@@ -46,6 +50,21 @@ struct DownloadTask {
4650
output << "]}";
4751
return output.str();
4852
}
49-
50-
// TODO: namh implement ToJson()
5153
};
54+
55+
inline std::string DownloadTypeToString(DownloadType type) {
56+
switch (type) {
57+
case DownloadType::Model:
58+
return "Model";
59+
case DownloadType::Engine:
60+
return "Engine";
61+
case DownloadType::Miscellaneous:
62+
return "Miscellaneous";
63+
case DownloadType::CudaToolkit:
64+
return "CudaToolkit";
65+
case DownloadType::Cortex:
66+
return "Cortex";
67+
default:
68+
return "Unknown";
69+
}
70+
}

engine/common/event.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33
namespace cortex::event {
44

5-
class Event {
6-
public:
7-
virtual ~Event() {}
8-
};
5+
struct Event {};
96
} // namespace cortex::event

engine/common/hardward_event.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

engine/common/model_event.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

engine/services/download_service.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ cpp::result<bool, std::string> DownloadService::Download(
232232
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
233233
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
234234

235-
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressCallback);
235+
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback);
236236
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
237237

238238
if (mode == "ab") {

engine/services/download_service.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,41 @@ class DownloadService {
5757
std::optional<std::string> active_download_task_id_;
5858
std::optional<std::string> active_download_item_id_;
5959

60+
/**
61+
* Invoked when download is completed (both failed or success)
62+
*/
6063
void CleanUp(const std::string& task_id);
6164

62-
static int progressCallback(void* ptr, double dltotal, double dlnow,
65+
static int ProgressCallback(void* ptr, double dltotal, double dlnow,
6366
double ultotal, double ulnow) {
6467
DownloadService* service = static_cast<DownloadService*>(ptr);
65-
double progress = (dltotal > 0) ? (dlnow / dltotal * 100.0) : 0.0;
66-
6768
auto event_queue = service->event_queue_.value();
6869
if (event_queue == nullptr) {
6970
return 0;
7071
}
7172

72-
if (service->active_download_task_id_.has_value() &&
73-
service->active_download_item_id_.has_value()) {
73+
auto active_task_id = service->active_download_task_id_;
74+
auto active_item_id = service->active_download_item_id_;
75+
if (!active_task_id.has_value() || !active_item_id.has_value()) {
76+
return 0;
77+
}
78+
79+
auto task = service->download_task_map_[active_task_id.value()];
7480

75-
auto task =
76-
service
77-
->download_task_map_[service->active_download_task_id_.value()];
78-
auto dl_evt = DownloadEvent(
79-
cortex::event::DownloadEventType::DownloadUpdated, task);
80-
event_queue->enqueue("download-update", dl_evt);
81+
// loop through download items, find the active one and update it
82+
for (auto& item : task.items) {
83+
if (item.id == active_item_id.value()) {
84+
item.downloadedBytes = dlnow;
85+
item.bytes = dltotal;
86+
break;
87+
}
8188
}
8289

90+
event_queue->enqueue(
91+
"download-update",
92+
DownloadEvent{
93+
.type_ = cortex::event::DownloadEventType::DownloadUpdated,
94+
.download_task_ = task});
8395
return 0;
8496
}
8597
};

0 commit comments

Comments
 (0)