diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index c5abe99b23bbdb9cba0ffa12894adc0eba0f3937..746328f1487a6f518e1f4e6613777eedd20c145d 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -1680,13 +1680,51 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) case SUBCMD_EVENT_LIST: { json_object * json_obj_event_list = json_object_new_object(); json_object * json_arr_obj_event = json_object_new_array(); - const char *l_limit_str = NULL, *l_offset_str = NULL, *l_head_str = NULL; + const char *l_limit_str = NULL, *l_offset_str = NULL, *l_head_str = NULL, *l_from_date_str = NULL, *l_to_date_str = NULL, + *l_from_hash_str = NULL, l_to_hash_str = NULL; + dap_hash_fast_t l_from_hash = {}, l_to_hash = {}; + dap_time_t l_from_time = 0, l_to_time = 0; dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-limit", &l_limit_str); dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-offset", &l_offset_str); + dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-from_date", &l_from_date_str); + dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-to_date", &l_to_date_str); + dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-from_hash", &l_from_hash_str); + dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-to_hash", &l_to_hash_str); bool l_head = dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-head", &l_head_str) ? true : false; char *ptr; - size_t l_limit = l_limit_str ? strtoull(l_limit_str, &ptr, 10) : 0; - size_t l_offset = l_offset_str ? strtoull(l_offset_str, &ptr, 10) : 0; + size_t l_limit = l_limit_str ? strtoull(l_limit_str, &ptr, 10) : 1000; + size_t l_offset = l_offset_str ? strtoull(l_offset_str, &ptr, 10) : 0; + + if (l_from_hash_str) { + if (dap_chain_hash_fast_from_str(l_from_hash_str, &l_from_hash)) { + dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR, "Can't convert \"%s\" to hash", l_from_hash_str); + return DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR; + } + } + if (l_to_hash_str) { + if (dap_chain_hash_fast_from_str(l_to_hash_str, &l_to_hash)) { + dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR, "Can't convert \"%s\" to hash", l_to_hash_str); + return DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR; + } + } + + if (l_from_date_str) { + l_from_time = dap_time_from_str_simplified(l_from_date_str); + if (!l_from_time) { + dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR, "Can't convert \"%s\" to date", l_from_date_str); + return DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR; + } + } + if (l_to_date_str) { + l_to_time = dap_time_from_str_simplified(l_to_date_str); + if (!l_to_time) { + dap_json_rpc_error_add(*a_json_arr_reply, DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR, "Can't convert \"%s\" to date", l_to_date_str); + return DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR; + } + struct tm *l_localtime = localtime((time_t *)&l_to_time); + l_localtime->tm_mday += 1; // + 1 day to end date, got it inclusive + l_to_time = mktime(l_localtime); + } if (l_from_events_str && strcmp(l_from_events_str,"round.new") == 0) { char * l_gdb_group_events = DAP_CHAIN_CS_DAG(l_chain)->gdb_group_events_round_new; @@ -1727,7 +1765,9 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) dap_chain_cs_dag_event_item_t * l_event_item = NULL,*l_event_item_tmp = NULL; if (l_head){ HASH_ITER(hh, PVT(l_dag)->events, l_event_item, l_event_item_tmp) { - if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + dap_time_t l_ts = l_event_item->event->header.ts_created; + if (i_tmp < l_arr_start || i_tmp >= l_arr_end || + (l_from_time && l_ts < l_from_time) || (l_to_time && l_ts >= l_to_time)) { i_tmp++; } else { i_tmp++; @@ -1738,7 +1778,9 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) else { l_event_item = HASH_LAST(PVT(l_dag)->events); for(; l_event_item; l_event_item = l_event_item->hh.prev){ - if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + dap_time_t l_ts = l_event_item->event->header.ts_created; + if (i_tmp < l_arr_start || i_tmp >= l_arr_end || + (l_from_time && l_ts < l_from_time) || (l_to_time && l_ts >= l_to_time)) { i_tmp++; } else { i_tmp++; @@ -1766,7 +1808,9 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) size_t i_tmp = 0; if (l_head){ HASH_ITER(hh, PVT(l_dag)->events_treshold, l_event_item, l_event_item_tmp) { - if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + dap_time_t l_ts = l_event_item->event->header.ts_created; + if (i_tmp < l_arr_start || i_tmp >= l_arr_end || + (l_from_time && l_ts < l_from_time) || (l_to_time && l_ts >= l_to_time)) { i_tmp++; } else { i_tmp++; @@ -1777,7 +1821,9 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply) else { l_event_item = HASH_LAST(PVT(l_dag)->events); for(; l_event_item; l_event_item = l_event_item->hh.prev){ - if (i_tmp < l_arr_start || i_tmp >= l_arr_end) { + dap_time_t l_ts = l_event_item->event->header.ts_created; + if (i_tmp < l_arr_start || i_tmp >= l_arr_end || + (l_from_time && l_ts < l_from_time) || (l_to_time && l_ts >= l_to_time)) { i_tmp++; } else { i_tmp++; diff --git a/modules/type/dag/include/dap_chain_cs_dag.h b/modules/type/dag/include/dap_chain_cs_dag.h index f28f7baf3c6e91287fe49be29591cd1d4a99bde8..66b876b02efdb8de0d5fd70826ded361623d655a 100644 --- a/modules/type/dag/include/dap_chain_cs_dag.h +++ b/modules/type/dag/include/dap_chain_cs_dag.h @@ -85,6 +85,7 @@ typedef enum s_com_dag_err{ DAP_CHAIN_NODE_CLI_COM_DAG_CERT_ERR, DAP_CHAIN_NODE_CLI_COM_DAG_FIND_EVENT_ERR, DAP_CHAIN_NODE_CLI_COM_DAG_UNDEF_SUB_ERR, + DAP_CHAIN_NODE_CLI_COM_DAG_CONVERT_ERR, /* add custom codes here */