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

Commit f80edba

Browse files
Merge branch 'dev' into feat/api-docs
2 parents 07caf8e + 601437d commit f80edba

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

engine/cli/commands/engine_install_cmd.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "engine_install_cmd.h"
2+
#include <future>
23
#include "server_start_cmd.h"
34
#include "utils/download_progress.h"
45
#include "utils/engine_constants.h"
@@ -31,9 +32,16 @@ bool EngineInstallCmd::Exec(const std::string& engine,
3132
}
3233
}
3334

35+
DownloadProgress dp;
36+
dp.Connect(host_, port_);
37+
// engine can be small, so need to start ws first
38+
auto dp_res = std::async(std::launch::deferred,
39+
[&dp, &engine] { return dp.Handle(engine); });
40+
CLI_LOG("Validating download items, please wait..")
41+
3442
httplib::Client cli(host_ + ":" + std::to_string(port_));
3543
Json::Value json_data;
36-
json_data["version"] = version.empty() ? "latest" : version;
44+
json_data["version"] = version.empty() ? "latest" : version;
3745
auto data_str = json_data.toStyledString();
3846
cli.set_read_timeout(std::chrono::seconds(60));
3947
auto res = cli.Post("/v1/engines/install/" + engine, httplib::Headers(),
@@ -43,18 +51,19 @@ bool EngineInstallCmd::Exec(const std::string& engine,
4351
if (res->status != httplib::StatusCode::OK_200) {
4452
auto root = json_helper::ParseJsonString(res->body);
4553
CLI_LOG(root["message"].asString());
54+
dp.ForceStop();
4655
return false;
56+
} else {
57+
CLI_LOG("Start downloading..");
4758
}
4859
} else {
4960
auto err = res.error();
5061
CTL_ERR("HTTP error: " << httplib::to_string(err));
62+
dp.ForceStop();
5163
return false;
5264
}
5365

54-
CLI_LOG("Start downloading ...")
55-
DownloadProgress dp;
56-
dp.Connect(host_, port_);
57-
if (!dp.Handle(engine))
66+
if (!dp_res.get())
5867
return false;
5968

6069
bool check_cuda_download = !system_info_utils::GetCudaVersion().empty();

engine/cli/commands/model_pull_cmd.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ std::optional<std::string> ModelPullCmd::Exec(const std::string& host, int port,
103103
CTL_INF("model: " << model << ", model_id: " << model_id);
104104

105105
// Send request download model to server
106+
CLI_LOG("Validating download items, please wait..")
106107
Json::Value json_data;
107108
json_data["model"] = model;
108109
auto data_str = json_data.toStyledString();
@@ -122,7 +123,7 @@ std::optional<std::string> ModelPullCmd::Exec(const std::string& host, int port,
122123
return std::nullopt;
123124
}
124125

125-
CLI_LOG("Start downloading ...")
126+
CLI_LOG("Start downloading..")
126127
DownloadProgress dp;
127128
bool force_stop = false;
128129

engine/cli/utils/download_progress.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ bool DownloadProgress::Connect(const std::string& host, int port) {
2323

2424
bool DownloadProgress::Handle(const std::string& id) {
2525
assert(!!ws_);
26+
std::unordered_map<std::string, uint64_t> totals;
2627
status_ = DownloadStatus::DownloadStarted;
2728
std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;
2829

2930
std::vector<std::unique_ptr<indicators::ProgressBar>> items;
3031
indicators::show_console_cursor(false);
31-
auto handle_message = [this, &bars, &items, id](const std::string& message) {
32+
auto handle_message = [this, &bars, &items, &totals,
33+
id](const std::string& message) {
3234
CTL_INF(message);
3335

3436
auto pad_string = [](const std::string& str,
@@ -70,20 +72,26 @@ bool DownloadProgress::Handle(const std::string& id) {
7072
for (int i = 0; i < ev.download_task_.items.size(); i++) {
7173
auto& it = ev.download_task_.items[i];
7274
uint64_t downloaded = it.downloadedBytes.value_or(0);
73-
uint64_t total = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
74-
if (ev.type_ == DownloadStatus::DownloadUpdated) {
75+
if (totals.find(it.id) == totals.end()) {
76+
totals[it.id] = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
77+
CTL_INF("Updated " << it.id << " - total: " << totals[it.id]);
78+
}
79+
80+
if (ev.type_ == DownloadStatus::DownloadStarted ||
81+
ev.type_ == DownloadStatus::DownloadUpdated) {
7582
(*bars)[i].set_option(indicators::option::PrefixText{
7683
pad_string(it.id) +
77-
std::to_string(int(static_cast<double>(downloaded) / total * 100)) +
84+
std::to_string(
85+
int(static_cast<double>(downloaded) / totals[it.id] * 100)) +
7886
'%'});
7987
(*bars)[i].set_progress(
80-
int(static_cast<double>(downloaded) / total * 100));
88+
int(static_cast<double>(downloaded) / totals[it.id] * 100));
8189
(*bars)[i].set_option(indicators::option::PostfixText{
8290
format_utils::BytesToHumanReadable(downloaded) + "/" +
83-
format_utils::BytesToHumanReadable(total)});
91+
format_utils::BytesToHumanReadable(totals[it.id])});
8492
} else if (ev.type_ == DownloadStatus::DownloadSuccess) {
8593
(*bars)[i].set_progress(100);
86-
auto total_str = format_utils::BytesToHumanReadable(total);
94+
auto total_str = format_utils::BytesToHumanReadable(totals[it.id]);
8795
(*bars)[i].set_option(
8896
indicators::option::PostfixText{total_str + "/" + total_str});
8997
(*bars)[i].set_option(

0 commit comments

Comments
 (0)