|
| 1 | +#include "ps_cmd.h" |
| 2 | +#include <drogon/drogon.h> |
| 3 | +#include <httplib.h> |
| 4 | +#include <string> |
| 5 | +#include <tabulate/table.hpp> |
| 6 | +#include "nlohmann/json.hpp" |
| 7 | +#include "utils/logging_utils.h" |
| 8 | +#include "utils/string_utils.h" |
| 9 | + |
| 10 | +namespace commands { |
| 11 | + |
| 12 | +void PsCmd::Exec(const std::string& host, int port) { |
| 13 | + auto host_and_port{host + ":" + std::to_string(port)}; |
| 14 | + httplib::Client cli(host_and_port); |
| 15 | + |
| 16 | + auto res = cli.Get("/inferences/server/models"); |
| 17 | + if (!res || res->status != httplib::StatusCode::OK_200) { |
| 18 | + CLI_LOG("No model loaded!"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + auto body = nlohmann::json::parse(res->body); |
| 23 | + auto data = body["data"]; |
| 24 | + std::vector<ModelLoadedStatus> model_status_list; |
| 25 | + try { |
| 26 | + for (const auto& item : data) { |
| 27 | + ModelLoadedStatus model_status; |
| 28 | + model_status.engine = item["engine"]; |
| 29 | + model_status.model = item["id"]; |
| 30 | + model_status.ram = item["ram"]; |
| 31 | + model_status.start_time = item["start_time"]; |
| 32 | + model_status.vram = item["vram"]; |
| 33 | + model_status_list.push_back(model_status); |
| 34 | + } |
| 35 | + } catch (const std::exception& e) { |
| 36 | + CLI_LOG("Fail to get list model information: " + std::string(e.what())); |
| 37 | + } |
| 38 | + |
| 39 | + PrintModelStatusList(model_status_list); |
| 40 | +} |
| 41 | + |
| 42 | +void PsCmd::PrintModelStatusList( |
| 43 | + const std::vector<ModelLoadedStatus>& model_status_list) const { |
| 44 | + if (model_status_list.empty()) { |
| 45 | + CLI_LOG("No model loaded!"); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + tabulate::Table table; |
| 50 | + table.add_row({"Model", "Engine", "RAM", "VRAM", "Up time"}); |
| 51 | + for (const auto& model_status : model_status_list) { |
| 52 | + table.add_row({ |
| 53 | + model_status.model, |
| 54 | + model_status.engine, |
| 55 | + model_status.ram, |
| 56 | + model_status.vram, |
| 57 | + string_utils::FormatTimeElapsed(model_status.start_time), |
| 58 | + }); |
| 59 | + } |
| 60 | + std::cout << table << std::endl; |
| 61 | +} |
| 62 | + |
| 63 | +}; // namespace commands |
0 commit comments