add stat api
This commit is contained in:
parent
da4bd51e46
commit
1a5be876b1
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/0]).
|
-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
|
%% gen_statem callbacks
|
||||||
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||||
@ -38,8 +38,8 @@
|
|||||||
%% 是否繁忙
|
%% 是否繁忙
|
||||||
is_busy = false :: boolean(),
|
is_busy = false :: boolean(),
|
||||||
|
|
||||||
%% 记录成功处理的消息数
|
%% 记录成功处理的消息数, 记日期和总数的映射关系
|
||||||
acc_num = 0
|
acc_num = #{}
|
||||||
}).
|
}).
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
@ -58,6 +58,10 @@ forward(LocationCode, DynamicLocationCode, Fields, Timestamp) when is_binary(Loc
|
|||||||
get_stat() ->
|
get_stat() ->
|
||||||
gen_statem:call(?MODULE, get_stat, 5000).
|
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
|
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||||
%% initialize. To ensure a synchronized start-up procedure, this
|
%% initialize. To ensure a synchronized start-up procedure, this
|
||||||
%% function does not return until Module:init/1 has returned.
|
%% 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,
|
end,
|
||||||
is_reference(TimerRef) andalso erlang:cancel_timer(TimerRef),
|
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}) ->
|
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}]};
|
{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进程挂掉时,重新建立新的
|
%% postman进程挂掉时,重新建立新的
|
||||||
handle_event(info, {'EXIT', PostmanPid, Reason}, connected, State = #state{timer_ref = TimerRef, postman_pid = PostmanPid}) ->
|
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]),
|
lager:warning("[iot_zd_endpoint] postman exited with reason: ~p", [Reason]),
|
||||||
|
|||||||
29
apps/iot/src/http_handler/api_handler.erl
Normal file
29
apps/iot/src/http_handler/api_handler.erl
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author licheng5
|
||||||
|
%%% @copyright (C) 2020, <COMPANY>
|
||||||
|
%%% @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">>)}.
|
||||||
@ -46,6 +46,7 @@ start_http_server() ->
|
|||||||
|
|
||||||
Dispatcher = cowboy_router:compile([
|
Dispatcher = cowboy_router:compile([
|
||||||
{'_', [
|
{'_', [
|
||||||
|
{"/api/[...]", http_protocol, [api_handler]},
|
||||||
{"/host/[...]", http_protocol, [host_handler]},
|
{"/host/[...]", http_protocol, [host_handler]},
|
||||||
{"/device/[...]", http_protocol, [device_handler]},
|
{"/device/[...]", http_protocol, [device_handler]},
|
||||||
{"/totalizator/[...]", http_protocol, [totalizator_handler]},
|
{"/totalizator/[...]", http_protocol, [totalizator_handler]},
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
-export([json_data/1, json_error/2]).
|
-export([json_data/1, json_error/2]).
|
||||||
-export([queue_limited_in/3, assert_call/2, assert/2]).
|
-export([queue_limited_in/3, assert_call/2, assert/2]).
|
||||||
-export([format_timestamp/1]).
|
-export([format_timestamp/1]).
|
||||||
|
-export([date/0]).
|
||||||
|
|
||||||
%% 格式化时间
|
%% 格式化时间
|
||||||
-spec format_timestamp(Timestamp :: integer()) -> integer().
|
-spec format_timestamp(Timestamp :: integer()) -> integer().
|
||||||
@ -164,3 +165,7 @@ file_uri(Filename) when is_binary(Filename) ->
|
|||||||
_ ->
|
_ ->
|
||||||
error
|
error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
date() ->
|
||||||
|
{{Y, M, D}, _} = calendar:local_time(),
|
||||||
|
io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]).
|
||||||
Loading…
x
Reference in New Issue
Block a user