forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload_progress.cc
More file actions
189 lines (175 loc) · 6.52 KB
/
download_progress.cc
File metadata and controls
189 lines (175 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "download_progress.h"
#include <chrono>
#include <limits>
#include "common/event.h"
#include "indicators/dynamic_progress.hpp"
#include "indicators/progress_bar.hpp"
#include "utils/engine_constants.h"
#include "utils/format_utils.h"
#include "utils/json_helper.h"
#include "utils/logging_utils.h"
#if !defined(WIN32) && !defined(WIN64)
#include <sys/ioctl.h>
#include <unistd.h>
#endif
namespace {
std::string Repo2Engine(const std::string& r) {
if (r == kLlamaRepo) {
return kLlamaEngine;
}
return r;
};
int GetColumns() {
#if defined(WIN32) || defined(WIN64)
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
return columns;
#else
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
#endif
}
} // namespace
bool DownloadProgress::Connect(const std::string& host, int port) {
if (ws_) {
CTL_INF("Already connected!");
return true;
}
ws_.reset(easywsclient::WebSocket::from_url(
"ws://" + host + ":" + std::to_string(port) + "/events"));
if (!!ws_)
return false;
return true;
}
bool DownloadProgress::Handle(
const std::unordered_set<DownloadType>& event_type) {
assert(!!ws_);
#if defined(_WIN32)
HANDLE h_out = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dw_original_out_mode = 0;
if (h_out != INVALID_HANDLE_VALUE) {
GetConsoleMode(h_out, &dw_original_out_mode);
// Enable ANSI escape code processing
DWORD dw_requested_out_mode =
dw_original_out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(h_out, dw_requested_out_mode)) {
SetConsoleMode(h_out, dw_original_out_mode);
}
}
#endif
for (auto et : event_type) {
status_[et] = DownloadStatus::DownloadStarted;
}
std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;
std::unordered_map<std::string,
std::pair<int, std::unique_ptr<indicators::ProgressBar>>>
items;
indicators::show_console_cursor(false);
auto start = std::chrono::steady_clock::now();
auto handle_message = [this, &bars, &items, event_type,
start](const std::string& message) {
CTL_INF(message);
auto pad_string = [](const std::string& str,
size_t max_length = 20) -> std::string {
// Check the length of the input string
if (str.length() >= max_length) {
return str.substr(0, max_length - 3) +
".. "; // Return truncated string if it's too long
}
// Calculate the number of spaces needed
size_t padding_size = max_length - str.length();
// Create a new string with the original string followed by spaces
return str + std::string(padding_size, ' ');
};
auto ev = cortex::event::GetDownloadEventFromJson(
json_helper::ParseJsonString(message));
// Ignore other task type
if (event_type.find(ev.download_task_.type) == event_type.end()) {
return;
}
auto now = std::chrono::steady_clock::now();
if (!bars) {
bars = std::make_unique<
indicators::DynamicProgress<indicators::ProgressBar>>();
}
for (auto& i : ev.download_task_.items) {
if (items.find(i.id) == items.end()) {
auto idx = items.size();
items[i.id] = std::pair(
idx,
std::make_unique<indicators::ProgressBar>(
indicators::option::BarWidth{GetColumns() / 6},
indicators::option::Start{"["}, indicators::option::Fill{"="},
indicators::option::Lead{">"}, indicators::option::End{"]"},
indicators::option::PrefixText{pad_string(Repo2Engine(i.id))},
indicators::option::ForegroundColor{indicators::Color::white},
indicators::option::ShowRemainingTime{false}));
bars->push_back(*(items.at(i.id).second));
}
}
for (int i = 0; i < (int) ev.download_task_.items.size(); i++) {
auto& it = ev.download_task_.items[i];
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());
auto d = std::chrono::duration_cast<std::chrono::seconds>(now - start)
.count();
uint64_t bytes_per_sec = downloaded / (d + 1);
std::string time_remaining;
if (downloaded == total || bytes_per_sec == 0) {
time_remaining = "00m:00s";
} else {
time_remaining = format_utils::TimeDownloadFormat(
(total - downloaded) / bytes_per_sec);
}
(*bars)[items.at(it.id).first].set_option(
indicators::option::PrefixText{
pad_string(Repo2Engine(it.id)) +
std::to_string(
int(static_cast<double>(downloaded) / total * 100)) +
'%'});
(*bars)[items.at(it.id).first].set_progress(
int(static_cast<double>(downloaded) / total * 100));
(*bars)[items.at(it.id).first].set_option(
indicators::option::PostfixText{
time_remaining + " " +
format_utils::BytesToHumanReadable(downloaded) + "/" +
format_utils::BytesToHumanReadable(total)});
} else if (ev.type_ == DownloadStatus::DownloadSuccess) {
uint64_t total =
it.bytes.value_or(std::numeric_limits<uint64_t>::max());
(*bars)[items.at(it.id).first].set_progress(100);
auto total_str = format_utils::BytesToHumanReadable(total);
(*bars)[items.at(it.id).first].set_option(
indicators::option::PostfixText{"00m:00s " + total_str + "/" +
total_str});
(*bars)[items.at(it.id).first].set_option(
indicators::option::PrefixText{pad_string(Repo2Engine(it.id)) +
"100%"});
(*bars)[items.at(it.id).first].set_progress(100);
CTL_INF("Download success");
}
status_[ev.download_task_.type] = ev.type_;
}
};
while (ws_->getReadyState() != easywsclient::WebSocket::CLOSED &&
!should_stop()) {
ws_->poll();
ws_->dispatch(handle_message);
}
indicators::show_console_cursor(true);
#if defined(_WIN32)
if (dw_original_out_mode != 0 && h_out != INVALID_HANDLE_VALUE) {
SetConsoleMode(h_out, dw_original_out_mode);
}
#endif
for (auto const& [_, v] : status_) {
if (v == DownloadStatus::DownloadError)
return false;
}
return true;
}