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

Commit ffe22a4

Browse files
committed
clean up
1 parent 73cf1a3 commit ffe22a4

File tree

9 files changed

+52
-24631
lines changed

9 files changed

+52
-24631
lines changed

engine/common/download_event.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#pragma once
22

3+
#include <nlohmann/json.hpp>
34
#include "common/download_task.h"
45
#include "common/event.h"
56

67
namespace cortex::event {
8+
using namespace nlohmann;
79

810
enum class DownloadEventType {
9-
DownloadValidateStarted,
10-
DownloadValidateFailed,
1111
DownloadStarted,
12-
DownloadPaused,
12+
DownloadStopped,
1313
DownloadUpdated,
1414
DownloadSuccess,
1515
DownloadError,
@@ -18,13 +18,9 @@ enum class DownloadEventType {
1818
namespace {
1919
std::string DownloadEventTypeToString(DownloadEventType type) {
2020
switch (type) {
21-
case DownloadEventType::DownloadValidateStarted:
22-
return "DownloadValidateStarted";
23-
case DownloadEventType::DownloadValidateFailed:
24-
return "DownloadValidateFailed";
2521
case DownloadEventType::DownloadStarted:
2622
return "DownloadStarted";
27-
case DownloadEventType::DownloadPaused:
23+
case DownloadEventType::DownloadStopped:
2824
return "DownloadPaused";
2925
case DownloadEventType::DownloadUpdated:
3026
return "DownloadUpdated";
@@ -39,7 +35,11 @@ std::string DownloadEventTypeToString(DownloadEventType type) {
3935
} // namespace
4036

4137
struct DownloadEvent : public cortex::event::Event {
42-
std::string ToString() const { return download_task_.ToString(); }
38+
std::string ToJsonString() const {
39+
json json{{"type", DownloadEventTypeToString(type_)},
40+
{"task", download_task_.ToJson()}};
41+
return json.dump();
42+
}
4343

4444
DownloadEventType type_;
4545
DownloadTask download_task_;

engine/common/download_task.h

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22

33
#include <filesystem>
4+
#include <nlohmann/json.hpp>
45
#include <sstream>
56
#include <string>
67

78
enum class DownloadType { Model, Engine, Miscellaneous, CudaToolkit, Cortex };
9+
using namespace nlohmann;
810

911
struct DownloadItem {
1012
std::string id;
@@ -33,6 +35,23 @@ struct DownloadItem {
3335
}
3436
};
3537

38+
inline std::string DownloadTypeToString(DownloadType type) {
39+
switch (type) {
40+
case DownloadType::Model:
41+
return "Model";
42+
case DownloadType::Engine:
43+
return "Engine";
44+
case DownloadType::Miscellaneous:
45+
return "Miscellaneous";
46+
case DownloadType::CudaToolkit:
47+
return "CudaToolkit";
48+
case DownloadType::Cortex:
49+
return "Cortex";
50+
default:
51+
return "Unknown";
52+
}
53+
}
54+
3655
struct DownloadTask {
3756
std::string id;
3857

@@ -50,21 +69,21 @@ struct DownloadTask {
5069
output << "]}";
5170
return output.str();
5271
}
53-
};
5472

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";
73+
json ToJson() const {
74+
json dl_items = json::array();
75+
76+
for (const auto& item : items) {
77+
json dl_item{{"id", item.id},
78+
{"downloadUrl", item.downloadUrl},
79+
{"localPath", item.localPath},
80+
{"checksum", item.checksum.value_or("N/A")},
81+
{"bytes", item.bytes.value_or(0)},
82+
{"downloadedBytes", item.downloadedBytes.value_or(0)}};
83+
dl_items.push_back(dl_item);
84+
}
85+
86+
return json{
87+
{"id", id}, {"type", DownloadTypeToString(type)}, {"items", dl_items}};
6988
}
70-
}
89+
};

engine/controllers/events.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Events : public drogon::WebSocketController<Events, false> {
2323
: event_queue_{event_queue} {
2424
// TODO: namh make a list of event
2525
event_queue_->appendListener("download-update", [this](DownloadEvent e) {
26-
this->broadcast(e.ToString());
26+
this->broadcast(e.ToJsonString());
2727
});
2828
};
2929

engine/controllers/models.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ void Models::GetModel(const HttpRequestPtr& req,
137137
resp->setStatusCode(k200OK);
138138
callback(resp);
139139
} catch (const std::exception& e) {
140-
std::string message = "Fail to get model information with ID '" +
141-
model_id + "': " + e.what();
140+
std::string message =
141+
"Fail to get model information with ID '" + model_id + "': " + e.what();
142142
LOG_ERROR << message;
143143
ret["data"] = data;
144144
ret["result"] = "Fail to get model information";

engine/controllers/server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <nlohmann/json.hpp>
12
#include <string>
3+
24
#if defined(_WIN32)
35
#define NOMINMAX
46
#endif
@@ -20,7 +22,6 @@
2022
#include "cortex-common/EngineI.h"
2123
#include "cortex-common/cortexpythoni.h"
2224
#include "utils/dylib.h"
23-
#include "utils/json.hpp"
2425

2526
#ifndef SERVER_VERBOSE
2627
#define SERVER_VERBOSE 1

engine/services/engine_service.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "utils/archive_utils.h"
66
#include "utils/engine_matcher_utils.h"
77
#include "utils/file_manager_utils.h"
8-
#include "utils/json.hpp"
98
#include "utils/result.hpp"
109
#include "utils/semantic_version_utils.h"
1110
#include "utils/system_info_utils.h"

engine/utils/huggingface_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

33
#include <httplib.h>
4+
#include <nlohmann/json.hpp>
45
#include <optional>
56
#include <string>
67
#include <vector>
7-
#include "utils/json.hpp"
88
#include "utils/result.hpp"
99
#include "utils/url_parser.h"
1010

0 commit comments

Comments
 (0)