From 1a5be876b1108aa1e863b5bc200d2108b332092f Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 29 Nov 2024 10:20:37 +0800 Subject: [PATCH] add stat api --- apps/iot/src/endpoint/iot_zd_endpoint.erl | 20 ++++++++++++---- apps/iot/src/http_handler/api_handler.erl | 29 +++++++++++++++++++++++ apps/iot/src/iot_app.erl | 1 + apps/iot/src/iot_util.erl | 7 +++++- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 apps/iot/src/http_handler/api_handler.erl diff --git a/apps/iot/src/endpoint/iot_zd_endpoint.erl b/apps/iot/src/endpoint/iot_zd_endpoint.erl index a14ba81..b941ef6 100644 --- a/apps/iot/src/endpoint/iot_zd_endpoint.erl +++ b/apps/iot/src/endpoint/iot_zd_endpoint.erl @@ -14,7 +14,7 @@ %% API -export([start_link/0]). --export([get_pid/0, forward/4, get_stat/0]). +-export([get_pid/0, forward/4, get_stat/0, get_num/1]). %% gen_statem callbacks -export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]). @@ -38,8 +38,8 @@ %% 是否繁忙 is_busy = false :: boolean(), - %% 记录成功处理的消息数 - acc_num = 0 + %% 记录成功处理的消息数, 记日期和总数的映射关系 + acc_num = #{} }). %%%=================================================================== @@ -58,6 +58,10 @@ forward(LocationCode, DynamicLocationCode, Fields, Timestamp) when is_binary(Loc get_stat() -> gen_statem:call(?MODULE, get_stat, 5000). +-spec get_num(Date :: string()) -> {ok, Num :: integer()}. +get_num(Date) when is_list(Date) -> + gen_statem:call(?MODULE, {get_num, Date}, 5000). + %% @doc Creates a gen_statem process which calls Module:init/1 to %% initialize. To ensure a synchronized start-up procedure, this %% function does not return until Module:init/1 has returned. @@ -142,7 +146,11 @@ handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = Ti end, is_reference(TimerRef) andalso erlang:cancel_timer(TimerRef), - {keep_state, State#state{timer_ref = undefined, acc_num = AccNum + 1, is_busy = false}, Actions}; + Date = iot_util:date(), + Num = maps:get(Date, AccNum, 0), + NAccNum = AccNum#{Date => Num + 1}, + + {keep_state, State#state{timer_ref = undefined, acc_num = NAccNum, is_busy = false}, Actions}; %% 收到重发过期请求 handle_event(info, {timeout, _, {repost_ticker, Body}}, connected, State = #state{postman_pid = PostmanPid}) -> @@ -177,6 +185,10 @@ handle_event({call, From}, get_stat, StateName, State = #state{acc_num = AccNum, }, {keep_state, State, [{reply, From, Stat}]}; +%% 获取当前统计信息 +handle_event({call, From}, {get_num, Date}, _StateName, State = #state{acc_num = AccNum}) -> + {keep_state, State, [{reply, From, maps:get(Date, AccNum, 0)}]}; + %% postman进程挂掉时,重新建立新的 handle_event(info, {'EXIT', PostmanPid, Reason}, connected, State = #state{timer_ref = TimerRef, postman_pid = PostmanPid}) -> lager:warning("[iot_zd_endpoint] postman exited with reason: ~p", [Reason]), diff --git a/apps/iot/src/http_handler/api_handler.erl b/apps/iot/src/http_handler/api_handler.erl new file mode 100644 index 0000000..041eadd --- /dev/null +++ b/apps/iot/src/http_handler/api_handler.erl @@ -0,0 +1,29 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2020, +%%% @doc +%%% +%%% @end +%%% Created : 26. 4月 2020 3:36 下午 +%%%------------------------------------------------------------------- +-module(api_handler). +-author("licheng5"). +-include("iot.hrl"). + +%% API +-export([handle_request/4]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% helper methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +handle_request("GET", "/api/stat", _, _) -> + Date = iot_util:date(), + ZDNum = iot_zd_endpoint:get_num(Date), + {ok, 200, iot_util:json_data(#{ + <<"zd_num">> => ZDNum + })}; + +handle_request(_, Path, _, _) -> + Path1 = list_to_binary(Path), + {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. \ No newline at end of file diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index d758072..5319cc4 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -46,6 +46,7 @@ start_http_server() -> Dispatcher = cowboy_router:compile([ {'_', [ + {"/api/[...]", http_protocol, [api_handler]}, {"/host/[...]", http_protocol, [host_handler]}, {"/device/[...]", http_protocol, [device_handler]}, {"/totalizator/[...]", http_protocol, [totalizator_handler]}, diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 63e7196..9661347 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -15,6 +15,7 @@ -export([json_data/1, json_error/2]). -export([queue_limited_in/3, assert_call/2, assert/2]). -export([format_timestamp/1]). +-export([date/0]). %% 格式化时间 -spec format_timestamp(Timestamp :: integer()) -> integer(). @@ -163,4 +164,8 @@ file_uri(Filename) when is_binary(Filename) -> {ok, <<"https://lgsiot.njau.edu.cn/upload/", Year/binary, $/, Month/binary, $/, Day/binary, $/, Filename/binary>>}; _ -> error - end. \ No newline at end of file + end. + +date() -> + {{Y, M, D}, _} = calendar:local_time(), + io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]). \ No newline at end of file