From 64daaf148cd59bf19942014bc754992b6bc6d86d Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Thu, 2 Mar 2017 12:52:16 +0100 Subject: Rename to statusserver --- statusserver/src/statusserver.erl | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 statusserver/src/statusserver.erl (limited to 'statusserver/src/statusserver.erl') diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl new file mode 100644 index 0000000..323b28d --- /dev/null +++ b/statusserver/src/statusserver.erl @@ -0,0 +1,71 @@ +%%% Copyright (c) 2014-2016, NORDUnet A/S. +%%% See LICENSE for licensing information. + +%%% @doc Frontend node API + +-module(statusserver). +-export([init_module/0]). +%% API (URL) +-export([request/4]). + +-define(APPURL_PLOP_STATUS, "plop/v1/status"). + +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), + Variable = proplists:get_value(<<"key">>, PropList), + Status = proplists:get_value(<<"value">>, PropList), + set_status(Service, Target, Variable, Status) + end, Entries) + end, + success({[{result, <<"ok">>}]}); +request(get, "", "status", Input) -> + Now = erlang:monotonic_time(millisecond), + Variables = [{struct, [ + {service, list_to_binary(Service)}, + {target, Target}, + {variable, Variable}, + {status, Status}, + {age, (Now - Timestamp) / 1000} + ]} || {{Service, Target, Variable}, Status, Timestamp} <- get_status()], + 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). + +init_module() -> + create_statusdb(). + +create_statusdb() -> + case ets:info(?STATUSDB_TABLE) of + undefined -> + ok; + _ -> + ets:delete(?STATUSDB_TABLE) + end, + ets:new(?STATUSDB_TABLE, [set, public, named_table]). + +get_status() -> + [E || [E] <- ets:match(?STATUSDB_TABLE, '$1')]. + +set_status(Service, Target, Variable, Status) -> + lager:info("status: ~p ~p ~p ~p", [Service, Target, Variable, Status]), + Timestamp = erlang:monotonic_time(millisecond), + true = ets:insert(?STATUSDB_TABLE, + {{Service, Target, Variable}, Status, Timestamp}), + ok. -- cgit v1.1 From 27b809c9525a876ecde0a5346e0264643197d934 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Wed, 8 Mar 2017 23:20:36 +0100 Subject: Added heartbeat service. Add source. Send better messages. --- statusserver/src/statusserver.erl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'statusserver/src/statusserver.erl') diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl index 323b28d..f38bf69 100644 --- a/statusserver/src/statusserver.erl +++ b/statusserver/src/statusserver.erl @@ -17,9 +17,10 @@ request(post, ?APPURL_PLOP_STATUS, Service, Input) -> 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, Target, Variable, Status) + set_status(Service, Source, Target, Variable, Status) end, Entries) end, success({[{result, <<"ok">>}]}); @@ -27,11 +28,12 @@ request(get, "", "status", Input) -> Now = erlang:monotonic_time(millisecond), Variables = [{struct, [ {service, list_to_binary(Service)}, + {source, Source}, {target, Target}, {variable, Variable}, {status, Status}, {age, (Now - Timestamp) / 1000} - ]} || {{Service, Target, Variable}, Status, Timestamp} <- get_status()], + ]} || {{Service, Source, Target, Variable}, Status, Timestamp} <- get_status()], success({[{result, Variables}]}). @@ -63,9 +65,9 @@ create_statusdb() -> get_status() -> [E || [E] <- ets:match(?STATUSDB_TABLE, '$1')]. -set_status(Service, Target, Variable, Status) -> - lager:info("status: ~p ~p ~p ~p", [Service, Target, Variable, Status]), +set_status(Service, Source, Target, Variable, Status) -> + lager:info("status: ~p ~p ~p ~p ~p", [Service, Source, Target, Variable, Status]), Timestamp = erlang:monotonic_time(millisecond), true = ets:insert(?STATUSDB_TABLE, - {{Service, Target, Variable}, Status, Timestamp}), + {{Service, Source, Target, Variable}, Status, Timestamp}), ok. -- cgit v1.1 From 9c05acc9ee261d29374cfcbe74844013ec06ad85 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Tue, 14 Mar 2017 13:19:22 +0100 Subject: Fix copyright year. --- statusserver/src/statusserver.erl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'statusserver/src/statusserver.erl') diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl index f38bf69..e4f38d5 100644 --- a/statusserver/src/statusserver.erl +++ b/statusserver/src/statusserver.erl @@ -1,8 +1,6 @@ -%%% Copyright (c) 2014-2016, NORDUnet A/S. +%%% Copyright (c) 2017, NORDUnet A/S. %%% See LICENSE for licensing information. -%%% @doc Frontend node API - -module(statusserver). -export([init_module/0]). %% API (URL) -- cgit v1.1 From 39cc6c19e79c19000d5e2174aa3b5f5c2ed2545b Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 15 Mar 2017 17:16:06 +0100 Subject: Add compat functions for timing functionality missing in R17. --- statusserver/src/statusserver.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'statusserver/src/statusserver.erl') diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl index e4f38d5..81fcd7a 100644 --- a/statusserver/src/statusserver.erl +++ b/statusserver/src/statusserver.erl @@ -23,7 +23,7 @@ request(post, ?APPURL_PLOP_STATUS, Service, Input) -> end, success({[{result, <<"ok">>}]}); request(get, "", "status", Input) -> - Now = erlang:monotonic_time(millisecond), + Now = plop_compat:monotonic_time(millisecond), Variables = [{struct, [ {service, list_to_binary(Service)}, {source, Source}, @@ -65,7 +65,7 @@ get_status() -> set_status(Service, Source, Target, Variable, Status) -> lager:info("status: ~p ~p ~p ~p ~p", [Service, Source, Target, Variable, Status]), - Timestamp = erlang:monotonic_time(millisecond), + Timestamp = plop_compat:monotonic_time(millisecond), true = ets:insert(?STATUSDB_TABLE, {{Service, Source, Target, Variable}, Status, Timestamp}), ok. -- cgit v1.1