From a70fa5aa8d7067cdf43b66c6285e0bac31c8cb4e Mon Sep 17 00:00:00 2001 From: vsian Date: Thu, 10 Oct 2024 19:49:40 +0800 Subject: [PATCH] http instance apis : logs, delta_checkpoint, global_checkpoint --- src/network/http_server.cpp | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/src/network/http_server.cpp b/src/network/http_server.cpp index 88ba30a06e..67c116dc4c 100644 --- a/src/network/http_server.cpp +++ b/src/network/http_server.cpp @@ -3473,6 +3473,123 @@ class ShowQueriesHandler final : public HttpRequestHandler { } }; +class ShowLogsHandler final : public HttpRequestHandler { +public: + SharedPtr handle(const SharedPtr &request) final { + auto infinity = Infinity::RemoteConnect(); + DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); }); + + nlohmann::json json_response; + HTTPStatus http_status; + QueryResult result = infinity->ShowLogs(); + + if (result.IsOk()) { + SizeT block_rows = result.result_table_->DataBlockCount(); + for (SizeT block_id = 0; block_id < block_rows; ++block_id) { + DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get(); + auto row_count = data_block->row_count(); + auto column_cnt = result.result_table_->ColumnCount(); + for (int row = 0; row < row_count; ++row) { + nlohmann::json json_table; + for (SizeT col = 0; col < column_cnt; ++col) { + const String &column_name = result.result_table_->GetColumnNameById(col); + Value value = data_block->GetValue(col, row); + const String &column_value = value.ToString(); + json_table[column_name] = column_value; + } + json_response["logs"].push_back(json_table); + } + } + json_response["error_code"] = 0; + http_status = HTTPStatus::CODE_200; + } else { + json_response["error_code"] = result.ErrorCode(); + json_response["error_message"] = result.ErrorMsg(); + http_status = HTTPStatus::CODE_500; + } + + return ResponseFactory::createResponse(http_status, json_response.dump()); + } +}; + +class ShowDeltaCheckpointHandler final : public HttpRequestHandler { +public: + SharedPtr handle(const SharedPtr &request) final { + auto infinity = Infinity::RemoteConnect(); + DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); }); + + nlohmann::json json_response; + HTTPStatus http_status; + QueryResult result = infinity->ShowDeltaCheckpoint(); + + if (result.IsOk()) { + SizeT block_rows = result.result_table_->DataBlockCount(); + for (SizeT block_id = 0; block_id < block_rows; ++block_id) { + DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get(); + auto row_count = data_block->row_count(); + auto column_cnt = result.result_table_->ColumnCount(); + for (int row = 0; row < row_count; ++row) { + nlohmann::json json_table; + for (SizeT col = 0; col < column_cnt; ++col) { + const String &column_name = result.result_table_->GetColumnNameById(col); + Value value = data_block->GetValue(col, row); + const String &column_value = value.ToString(); + json_table[column_name] = column_value; + } + json_response["delta_checkpoint"].push_back(json_table); + } + } + json_response["error_code"] = 0; + http_status = HTTPStatus::CODE_200; + } else { + json_response["error_code"] = result.ErrorCode(); + json_response["error_message"] = result.ErrorMsg(); + http_status = HTTPStatus::CODE_500; + } + + return ResponseFactory::createResponse(http_status, json_response.dump()); + } +}; + +class ShowFullCheckpointHandler final : public HttpRequestHandler { +public: + SharedPtr handle(const SharedPtr &request) final { + auto infinity = Infinity::RemoteConnect(); + DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); }); + + nlohmann::json json_response; + HTTPStatus http_status; + QueryResult result = infinity->ShowFullCheckpoint(); + + if (result.IsOk()) { + SizeT block_rows = result.result_table_->DataBlockCount(); + for (SizeT block_id = 0; block_id < block_rows; ++block_id) { + DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get(); + auto row_count = data_block->row_count(); + auto column_cnt = result.result_table_->ColumnCount(); + for (int row = 0; row < row_count; ++row) { + nlohmann::json json_table; + for (SizeT col = 0; col < column_cnt; ++col) { + const String &column_name = result.result_table_->GetColumnNameById(col); + Value value = data_block->GetValue(col, row); + const String &column_value = value.ToString(); + json_table[column_name] = column_value; + } + json_response["global_checkpoint"].push_back(json_table); + } + } + json_response["error_code"] = 0; + http_status = HTTPStatus::CODE_200; + } else { + json_response["error_code"] = result.ErrorCode(); + json_response["error_message"] = result.ErrorMsg(); + http_status = HTTPStatus::CODE_500; + } + + return ResponseFactory::createResponse(http_status, json_response.dump()); + } +}; + class AdminShowCurrentNodeHandler final : public HttpRequestHandler { public: SharedPtr handle(const SharedPtr &request) final { @@ -3806,6 +3923,9 @@ void HTTPServer::Start(const String &ip_address, u16 port) { router->route("GET", "/instance/profiles", MakeShared()); router->route("GET", "/instance/memindex", MakeShared()); router->route("GET", "/instance/queries", MakeShared()); + router->route("GET", "/instance/logs", MakeShared()); + router->route("GET", "/instance/delta_checkpoint", MakeShared()); + router->route("GET", "/instance/global_checkpoint", MakeShared()); // variable router->route("GET", "/variables/global", MakeShared());