Skip to content

Commit ed44fa9

Browse files
committed
http instance api : show query, show transactions, show transaction
1 parent cf832fa commit ed44fa9

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed

docs/references/http_api_reference.mdx

+143
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,150 @@ A `500` HTTP status code indicates an error condition. The response includes a J
26352635
26362636
---
26372637
2638+
## Show transactions
26382639
2640+
**GET** `/instance/transactions`
2641+
2642+
Shows running transactions on database.
2643+
2644+
### Request
2645+
2646+
- Method: GET
2647+
- URL: `/instance/transactions`
2648+
- Headers: `accept: application/json`
2649+
2650+
#### Request example
2651+
2652+
```shell
2653+
curl --request GET \
2654+
--url http://localhost:23820/instance/transactions \
2655+
--header 'accept: application/json'
2656+
```
2657+
2658+
### Response
2659+
2660+
<Tabs
2661+
defaultValue="s200"
2662+
values={[
2663+
{label: 'Status code 200', value: 's200'},
2664+
{label: 'Status code 500', value: 's500'},
2665+
]}>
2666+
<TabItem value="s200">
2667+
2668+
The response includes a JSON object like the following:
2669+
2670+
```shell
2671+
{
2672+
"error_code":0,
2673+
"transactions": [
2674+
{
2675+
"transaction_id":"27275",
2676+
"transaction_text":""
2677+
},
2678+
{
2679+
"transaction_id":"27274",
2680+
"transaction_text":""
2681+
}
2682+
]
2683+
}
2684+
```
2685+
2686+
- `"error_code"`: `integer`
2687+
`0`: The operation succeeds.
2688+
2689+
</TabItem>
2690+
<TabItem value="s500">
2691+
2692+
A `500` HTTP status code indicates an error condition. The response includes a JSON object like the following:
2693+
2694+
```shell
2695+
{
2696+
"error_code": 2005,
2697+
"error_message": "Not support in maintenance mode"
2698+
}
2699+
```
2700+
2701+
- `"error_code"`: `integer`
2702+
A non-zero value indicates a specific error condition.
2703+
- `"error_message"`: `string`
2704+
When `error_code` is non-zero, `"error_message"` provides additional details about the error.
2705+
2706+
</TabItem>
2707+
</Tabs>
2708+
2709+
---
2710+
2711+
## Show transaction
2712+
2713+
**GET** `/instance/transactions/{trasaction_id}`
2714+
2715+
Shows running transactions on database by transaction_id.
2716+
2717+
### Request
2718+
2719+
- Method: GET
2720+
- URL: `/instance/transactions`
2721+
- Headers: `accept: application/json`
2722+
2723+
#### Request example
2724+
2725+
```shell
2726+
curl --request GET \
2727+
--url http://localhost:23820/instance/transactions/{transaction_id} \
2728+
--header 'accept: application/json'
2729+
```
2730+
2731+
#### Request parameters
2732+
2733+
- `transaction_id`: (*Path parameter*)
2734+
The id of the running transaction.
2735+
2736+
### Response
2737+
2738+
<Tabs
2739+
defaultValue="s200"
2740+
values={[
2741+
{label: 'Status code 200', value: 's200'},
2742+
{label: 'Status code 500', value: 's500'},
2743+
]}>
2744+
<TabItem value="s200">
2745+
2746+
The response includes a JSON object like the following:
2747+
2748+
```shell
2749+
{
2750+
"error_code":0,
2751+
"transaction": {
2752+
"transaction_id":"27275",
2753+
"transaction_text":""
2754+
}
2755+
}
2756+
```
2757+
2758+
- `"error_code"`: `integer`
2759+
`0`: The operation succeeds.
2760+
2761+
</TabItem>
2762+
<TabItem value="s500">
2763+
2764+
A `500` HTTP status code indicates an error condition. The response includes a JSON object like the following:
2765+
2766+
```shell
2767+
{
2768+
"error_code": 2005,
2769+
"error_message": "Not support in maintenance mode"
2770+
}
2771+
```
2772+
2773+
- `"error_code"`: `integer`
2774+
A non-zero value indicates a specific error condition.
2775+
- `"error_message"`: `string`
2776+
When `error_code` is non-zero, `"error_message"` provides additional details about the error.
2777+
2778+
</TabItem>
2779+
</Tabs>
2780+
2781+
---
26392782
26402783
## Admin set node role
26412784

src/network/http_server.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -3473,6 +3473,113 @@ class ShowQueriesHandler final : public HttpRequestHandler {
34733473
}
34743474
};
34753475

3476+
class ShowQueryHandler final : public HttpRequestHandler {
3477+
public:
3478+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3479+
auto infinity = Infinity::RemoteConnect();
3480+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3481+
3482+
nlohmann::json json_response;
3483+
nlohmann::json json_table;
3484+
HTTPStatus http_status;
3485+
String query_id = request->getPathVariable("query_id");
3486+
QueryResult result = infinity->Query(fmt::format("show query {}", query_id));
3487+
3488+
if (result.IsOk()) {
3489+
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
3490+
auto column_cnt = result.result_table_->ColumnCount();
3491+
for (SizeT col = 0; col < column_cnt; ++col) {
3492+
const String &column_name = result.result_table_->GetColumnNameById(col);
3493+
Value value = data_block->GetValue(col, 0);
3494+
const String &column_value = value.ToString();
3495+
json_table[column_name] = column_value;
3496+
}
3497+
json_response["query"] = json_table;
3498+
json_response["error_code"] = 0;
3499+
http_status = HTTPStatus::CODE_200;
3500+
} else {
3501+
json_response["error_code"] = result.ErrorCode();
3502+
json_response["error_message"] = result.ErrorMsg();
3503+
http_status = HTTPStatus::CODE_500;
3504+
}
3505+
3506+
return ResponseFactory::createResponse(http_status, json_response.dump());
3507+
}
3508+
};
3509+
3510+
class ShowTransactionsHandler final : public HttpRequestHandler {
3511+
public:
3512+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3513+
auto infinity = Infinity::RemoteConnect();
3514+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3515+
3516+
nlohmann::json json_response;
3517+
HTTPStatus http_status;
3518+
QueryResult result = infinity->Query("show transactions");
3519+
3520+
if (result.IsOk()) {
3521+
SizeT block_rows = result.result_table_->DataBlockCount();
3522+
for (SizeT block_id = 0; block_id < block_rows; ++block_id) {
3523+
DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get();
3524+
auto row_count = data_block->row_count();
3525+
auto column_cnt = result.result_table_->ColumnCount();
3526+
for (int row = 0; row < row_count; ++row) {
3527+
nlohmann::json json_table;
3528+
for (SizeT col = 0; col < column_cnt; ++col) {
3529+
const String &column_name = result.result_table_->GetColumnNameById(col);
3530+
Value value = data_block->GetValue(col, row);
3531+
const String &column_value = value.ToString();
3532+
json_table[column_name] = column_value;
3533+
}
3534+
json_response["transactions"].push_back(json_table);
3535+
}
3536+
}
3537+
json_response["error_code"] = 0;
3538+
http_status = HTTPStatus::CODE_200;
3539+
} else {
3540+
json_response["error_code"] = result.ErrorCode();
3541+
json_response["error_message"] = result.ErrorMsg();
3542+
http_status = HTTPStatus::CODE_500;
3543+
}
3544+
3545+
return ResponseFactory::createResponse(http_status, json_response.dump());
3546+
}
3547+
};
3548+
3549+
class ShowTransactionHandler final : public HttpRequestHandler {
3550+
public:
3551+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3552+
auto infinity = Infinity::RemoteConnect();
3553+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3554+
3555+
nlohmann::json json_response;
3556+
nlohmann::json json_table;
3557+
HTTPStatus http_status;
3558+
String transaction_id = request->getPathVariable("transaction_id");
3559+
QueryResult result = infinity->Query(fmt::format("show transaction {}", transaction_id));
3560+
3561+
if (result.IsOk()) {
3562+
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
3563+
auto column_cnt = result.result_table_->ColumnCount();
3564+
for (SizeT col = 0; col < column_cnt; ++col) {
3565+
const String &column_name = result.result_table_->GetColumnNameById(col);
3566+
Value value = data_block->GetValue(col, 0);
3567+
const String &column_value = value.ToString();
3568+
json_table[column_name] = column_value;
3569+
}
3570+
json_response["error_code"] = 0;
3571+
json_response["transaction"]= json_table;
3572+
http_status = HTTPStatus::CODE_200;
3573+
} else {
3574+
json_response["error_code"] = result.ErrorCode();
3575+
json_response["error_message"] = result.ErrorMsg();
3576+
http_status = HTTPStatus::CODE_500;
3577+
}
3578+
3579+
return ResponseFactory::createResponse(http_status, json_response.dump());
3580+
}
3581+
};
3582+
34763583
class ShowCurrentNodeHandler final : public HttpRequestHandler {
34773584
public:
34783585
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
@@ -3792,6 +3899,9 @@ void HTTPServer::Start(const String &ip_address, u16 port) {
37923899
router->route("GET", "/instance/profiles", MakeShared<ShowProfilesHandler>());
37933900
router->route("GET", "/instance/memindex", MakeShared<ShowMemIndexHandler>());
37943901
router->route("GET", "/instance/queries", MakeShared<ShowQueriesHandler>());
3902+
router->route("GET", "/instance/queries/{query_id}", MakeShared<ShowQueryHandler>());
3903+
router->route("GET", "/instance/transactions", MakeShared<ShowTransactionsHandler>());
3904+
router->route("GET", "/instance/transactions/{transaction_id}", MakeShared<ShowTransactionHandler>());
37953905

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

0 commit comments

Comments
 (0)