%%% Copyright (c) 2017, NORDUnet A/S. %%% See LICENSE for licensing information. -module(statusserver). -export([init_module/0]). %% API (URL) -export([request/4]). -define(APPURL_PLOP_STATUS, "plop/v1/status"). -define(APPURL_PLOP_BENCH, "plop/v1/bench"). request(post, ?APPURL_PLOP_STATUS, Service, Input) -> case (catch mochijson2:decode(Input)) of {error, E} -> html("bad input:", E); Entries when is_list(Entries) -> lists:foreach(fun ({struct, PropList}) -> Target = proplists:get_value(<<"target">>, PropList), Source = proplists:get_value(<<"source">>, PropList), Variable = proplists:get_value(<<"key">>, PropList), Status = proplists:get_value(<<"value">>, PropList), set_status(Service, Source, Target, Variable, Status) end, Entries) end, success({[{result, <<"ok">>}]}); request(post, ?APPURL_PLOP_BENCH, Service, Input) -> case (catch mochijson2:decode(Input)) of {error, E} -> html("bad input:", E); Entries when is_list(Entries) -> lists:foreach(fun ({struct, PropList}) -> Target = proplists:get_value(<<"target">>, PropList), Source = proplists:get_value(<<"source">>, PropList), Tag = proplists:get_value(<<"tag">>, PropList), Seq = proplists:get_value(<<"seq">>, PropList), Starttime = proplists:get_value(<<"starttime">>, PropList), Elapsed = proplists:get_value(<<"elapsed">>, PropList), add_bench(Service, Source, Target, Tag, Seq, Starttime, Elapsed) end, Entries) end, success({[{result, <<"ok">>}]}); request(get, "", "status", _Input) -> Now = plop_compat:monotonic_time(millisecond), Variables = [{struct, [ {service, list_to_binary(Service)}, {source, Source}, {target, Target}, {variable, Variable}, {status, Status}, {age, (Now - Timestamp) / 1000} ]} || {{Service, Source, Target, Variable}, Status, Timestamp} <- get_status()], success({[{result, Variables}]}); request(get, "", "bench", _Input) -> Variables = [{struct, [ {service, list_to_binary(Service)}, {source, Source}, {target, Target}, {tag, Tag}, {starttime, Starttime}, {elapsed, Elapsed} ]} || {{Service, Source, Target, Tag, _Seq, _Starttime}, Starttime, Elapsed} <- get_bench()], success({[{result, Variables}]}). success(Data) -> {200, [{"Content-Type", "text/json"}, {"Access-Control-Allow-Origin", "*"}], mochijson2:encode(Data)}. html(Text, Input) -> {400, [{"Content-Type", "text/html"}], io_lib:format( "

~n" ++ "~s~n" ++ "~p~n" ++ "~n", [Text, Input])}. -define(STATUSDB_TABLE, statusserver_statusdb). -define(BENCHDB_TABLE, statusserver_benchdb). init_module() -> create_table(?STATUSDB_TABLE), create_table(?BENCHDB_TABLE). create_table(Table) -> case ets:info(Table) of undefined -> ok; _ -> ets:delete(Table) end, ets:new(Table, [set, public, named_table]). get_status() -> [E || [E] <- ets:match(?STATUSDB_TABLE, '$1')]. get_bench() -> [E || [E] <- ets:match(?BENCHDB_TABLE, '$1')]. set_status(Service, Source, Target, Variable, Status) -> lager:info("status: ~p ~p ~p ~p ~p", [Service, Source, Target, Variable, Status]), Timestamp = plop_compat:monotonic_time(millisecond), true = ets:insert(?STATUSDB_TABLE, {{Service, Source, Target, Variable}, Status, Timestamp}), ok. add_bench(Service, Source, Target, Tag, Seq, Starttime, Elapsed) -> lager:info("bench: ~p ~p ~p ~p ~p ~p ~p", [Service, Source, Target, Tag, Seq, Starttime, Elapsed]), true = ets:insert(?BENCHDB_TABLE, {{Service, Source, Target, Tag, Seq, Starttime}, Starttime, Elapsed}), ok.