feat(restful): add node statistics api

This commit is contained in:
eeff
2022-09-08 14:51:21 +08:00
parent 717a4eed0c
commit a583abfa32
6 changed files with 91 additions and 0 deletions
+44
View File
@@ -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();
+2
View File
@@ -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);
+6
View File
@@ -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,
+4
View File
@@ -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);
+28
View File
@@ -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;
+7
View File
@@ -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;