From a583abfa326892618bc81000069a9fd6ce64cb18 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Thu, 8 Sep 2022 10:48:06 +0800 Subject: [PATCH] feat(restful): add node statistics api --- plugins/restful/adapter_handle.c | 44 ++++++++++++++++++++++++++++++++ plugins/restful/adapter_handle.h | 2 ++ plugins/restful/handle.c | 6 +++++ plugins/restful/rest.c | 4 +++ src/parser/neu_json_node.c | 28 ++++++++++++++++++++ src/parser/neu_json_node.h | 7 +++++ 6 files changed, 91 insertions(+) diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index fb52a515..bb4a0276 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -224,6 +224,50 @@ void handle_node_ctl(nng_aio *aio) }) } +void handle_get_node_stat(nng_aio *aio) +{ + neu_plugin_t * plugin = neu_rest_get_plugin(); + char node_name[NEU_NODE_NAME_LEN] = { 0 }; + int ret = 0; + neu_reqresp_head_t header = { 0 }; + neu_req_get_node_state_t cmd = { 0 }; + + VALIDATE_JWT(aio); + + header.ctx = aio; + + if (http_get_param_str(aio, "node", node_name, sizeof(node_name)) <= 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { + http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error); + }); + return; + } else { + header.type = NEU_REQ_GET_NODE_STAT; + strcpy(cmd.node, node_name); + } + + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } +} + +void handle_get_node_stat_resp(nng_aio *aio, neu_resp_get_node_stat_t *stat) +{ + neu_json_get_node_stat_resp_t res = { 0 }; + char * result = NULL; + + res.type = stat->type; + res.data = stat->data; + + neu_json_encode_by_fn(&res, neu_json_encode_get_node_stat_resp, &result); + + http_ok(aio, result); + free(result); +} + void handle_get_node_state(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); diff --git a/plugins/restful/adapter_handle.h b/plugins/restful/adapter_handle.h index 693263d5..206cf10e 100644 --- a/plugins/restful/adapter_handle.h +++ b/plugins/restful/adapter_handle.h @@ -34,7 +34,9 @@ void handle_get_node_setting_resp(nng_aio * aio, neu_resp_get_node_setting_t *setting); void handle_node_ctl(nng_aio *aio); +void handle_get_node_stat(nng_aio *aio); void handle_get_node_state(nng_aio *aio); +void handle_get_node_stat_resp(nng_aio *aio, neu_resp_get_node_stat_t *stat); void handle_get_node_state_resp(nng_aio *aio, neu_resp_get_node_state_t *state); void handle_get_nodes_state_resp(nng_aio * aio, neu_resp_get_nodes_state_t *states); diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 426c4654..a2b17341 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -271,6 +271,12 @@ struct neu_rest_handler api_handlers[] = { .url = "/api/v2/node/ctl", .value.handler = handle_node_ctl, }, + { + .method = NEU_REST_METHOD_GET, + .type = NEU_REST_HANDLER_FUNCTION, + .url = "/api/v2/node/stat", + .value.handler = handle_get_node_stat, + }, { .method = NEU_REST_METHOD_GET, .type = NEU_REST_HANDLER_FUNCTION, diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 984573c8..7f79a3e7 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -294,6 +294,10 @@ static int dashb_plugin_request(neu_plugin_t * plugin, handle_get_nodes_state_resp(header->ctx, (neu_resp_get_nodes_state_t *) data); break; + case NEU_RESP_GET_NODE_STAT: + handle_get_node_stat_resp(header->ctx, + (neu_resp_get_node_stat_t *) data); + break; case NEU_RESP_GET_NODE_STATE: handle_get_node_state_resp(header->ctx, (neu_resp_get_node_state_t *) data); diff --git a/src/parser/neu_json_node.c b/src/parser/neu_json_node.c index c2b2f486..14def345 100644 --- a/src/parser/neu_json_node.c +++ b/src/parser/neu_json_node.c @@ -27,6 +27,7 @@ #include "json/json.h" +#include "adapter.h" #include "neu_json_node.h" int neu_json_decode_add_node_req(char *buf, neu_json_add_node_req_t **result) @@ -123,6 +124,33 @@ void neu_json_decode_del_node_req_free(neu_json_del_node_req_t *req) free(req); } +int neu_json_encode_get_node_stat_resp(void *json_object, void *param) +{ + int ret = 0; + neu_json_get_node_stat_resp_t *resp = + (neu_json_get_node_stat_resp_t *) param; + + for (int i = 0; i < NEU_NODE_STAT_MAX; ++i) { + if (i >= NEU_NODE_STAT_TAG_TOT_CNT && + NEU_NA_TYPE_DRIVER != resp->type) { + // not a driver node, filter out driver specific counters + break; + } + + neu_json_elem_t resp_elems[] = { + { + .name = (char *) neu_node_stat_string(i), + .t = NEU_JSON_INT, + .v.val_int = resp->data[i], + }, + }; + ret = neu_json_encode_field(json_object, resp_elems, + NEU_JSON_ELEM_SIZE(resp_elems)); + } + + return ret; +} + int neu_json_encode_get_node_state_resp(void *json_object, void *param) { int ret = 0; diff --git a/src/parser/neu_json_node.h b/src/parser/neu_json_node.h index 9a549726..c6d60d49 100644 --- a/src/parser/neu_json_node.h +++ b/src/parser/neu_json_node.h @@ -46,6 +46,13 @@ typedef struct { int neu_json_decode_del_node_req(char *buf, neu_json_del_node_req_t **result); void neu_json_decode_del_node_req_free(neu_json_del_node_req_t *req); +typedef struct { + uint64_t type; + uint64_t *data; +} neu_json_get_node_stat_resp_t; + +int neu_json_encode_get_node_stat_resp(void *json_object, void *param); + typedef struct { int64_t running; int64_t link;