add stat api

This commit is contained in:
anlicheng 2024-11-29 10:20:37 +08:00
parent da4bd51e46
commit 1a5be876b1
4 changed files with 52 additions and 5 deletions

View File

@ -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]),

View 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">>)}.

View File

@ -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]},

View File

@ -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.
end.
date() ->
{{Y, M, D}, _} = calendar:local_time(),
io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]).