Skip to content

Commit

Permalink
http instance apis : logs, delta_checkpoint, global_checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vsian committed Oct 10, 2024
1 parent 628a1af commit a70fa5a
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/network/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3473,6 +3473,123 @@ class ShowQueriesHandler final : public HttpRequestHandler {
}
};

class ShowLogsHandler final : public HttpRequestHandler {
public:
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &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<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &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<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &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<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
Expand Down Expand Up @@ -3806,6 +3923,9 @@ void HTTPServer::Start(const String &ip_address, u16 port) {
router->route("GET", "/instance/profiles", MakeShared<ShowProfilesHandler>());
router->route("GET", "/instance/memindex", MakeShared<ShowMemIndexHandler>());
router->route("GET", "/instance/queries", MakeShared<ShowQueriesHandler>());
router->route("GET", "/instance/logs", MakeShared<ShowLogsHandler>());
router->route("GET", "/instance/delta_checkpoint", MakeShared<ShowDeltaCheckpointHandler>());
router->route("GET", "/instance/global_checkpoint", MakeShared<ShowFullCheckpointHandler>());

// variable
router->route("GET", "/variables/global", MakeShared<ShowGlobalVariablesHandler>());
Expand Down

0 comments on commit a70fa5a

Please sign in to comment.