add /api/current_status

This commit is contained in:
Luna Mendes 2018-06-11 17:42:40 -03:00
parent 62e636cfe7
commit e6b06612c3
8 changed files with 80 additions and 18 deletions

View File

@ -6,10 +6,9 @@ config :elstat,
# which services will be managed by elstat?
# this follows a service spec
# the example here is for elixire
services: [
%{
id: :elixire,
human_id: "elixi.re",
services: %{
elixire: %{
description: "the backend of elixi.re",
adapter: Elstat.Adapter.Elixire,
adapter_opts: %{
base_url: "https://elixi.re",
@ -17,13 +16,12 @@ config :elstat,
# every 10 seconds
poll: 10,
},
%{
id: :genserver,
human_id: "genserver, elixire main server",
genserver: %{
description: "genserver, elixire main server",
adapter: Elstat.Adapter.Ping,
adapter_opts: %{
address: "192.168.1.1",
},
poll: 15,
},
]
}

View File

@ -24,7 +24,7 @@ defmodule Elstat.Adapter.Elixire do
{:ok, {:map, %{
status: resp.status_code == 200,
latency: {:millisecond, delta},
latency: delta,
}}}
end

17
lib/api.ex Normal file
View File

@ -0,0 +1,17 @@
defmodule Elstat.API.Status do
require Logger
def init(req0, state) do
data = Elstat.Manager.get_current_state()
Logger.debug "#{inspect data}"
req = :cowboy_req.reply(200,
%{"content-type" => "text/json"},
Poison.encode!(data),
req0
)
{:ok, req, state}
end
end

View File

@ -30,8 +30,10 @@ defmodule Elstat.Cowboy do
def build_dispatch_config do
:cowboy_router.compile([
{:_, [
{"/", Elstat.Cowboy.DefaultHandler, []},
{"/api", Elstat.Cowboy.APIHandler, []},
{"/", :cowboy_static, {:priv_file, :my_app, "static/index.html"}},
{"/hewwo", Elstat.Cowboy.DefaultHandler, []},
{"/api/current_status", Elstat.API.Status, []},
]}
])
end

View File

@ -44,8 +44,9 @@ defmodule Elstat.Manager do
services = Application.fetch_env!(:elstat, :services)
workers = services
|> Enum.map(fn service ->
service_id = service.id
|> Map.keys
|> Enum.map(fn service_id ->
service = Map.put(services[service_id], :id, service_id)
# each service worker will enter an infinite loop
# polling the service (via an adapter) and giving
@ -61,20 +62,59 @@ defmodule Elstat.Manager do
[ Elstat.Manager | workers]
end
def get_current_state() do
man_pid = :global.whereis_name Elstat.Manager
GenServer.call(man_pid, {:get_state})
end
# server callbacks
def init(:ok) do
services = Application.fetch_env!(:elstat, :services)
# TODO : send data to db
Sqlitex.with_db('elstat.db', fn(db) ->
Enum.each(services, fn service ->
end)
end)
{:ok, %{
services: services,
serv_state: %{},
}}
end
def handle_call({:get_state}, _from, state) do
reply = state.serv_state
|> Map.keys
|> Enum.map(fn key ->
data = state.serv_state[key]
desc = state.services[key].description
result = case data do
{:map, data_map} ->
{key, Map.put(data_map, :description, desc)}
{:bool, data_bool} ->
{key, %{
status: data_bool,
description: desc,
}}
end
end)
|> Map.new
{:reply, reply, state}
end
def handle_info({:service_info, data}, state) do
Logger.debug "got service data! #{inspect data}"
{:noreply, state}
def handle_info({:service_info, {sid, sdata}}, state) do
case sdata do
{:ok, actual_data} ->
new_serv_state = Map.put(state.serv_state, sid, actual_data)
{:noreply, %{state | serv_state: new_serv_state}}
{:error, err} ->
Logger.warn "error on #{inspect sid}: #{inspect err}"
end
end
end

View File

@ -14,7 +14,10 @@ defmodule Elstat.Supervisor do
id: Elstat.Cowboy,
start: {Elstat.Cowboy, :start_link, []},
},
{Sqlitex.Server, ['elstat.db', [name: Sqlitex.Server]]}
%{
id: Sqlitex.Server,
start: {Sqlitex.Server, :start_link, ['elstat.db', [name: Sqlitex.Server]]}
}
]
service_children = Elstat.Manager.build_services()

View File

@ -23,6 +23,7 @@ defmodule Elstat.MixProject do
defp deps do
[
{:httpoison, "~> 1.1.1"},
{:poison, "~> 3.1"},
{:sqlitex, "~> 1.4.2"},
{:cowboy, "~> 2.4.0"}
]

View File

@ -10,6 +10,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.5.0", "f04166f456790fee2ac1aa05a02745cc75783c2bfb26d39faf6aefc9a3d3a58a", [:rebar3], [], "hexpm"},
"sqlitex": {:hex, :sqlitex, "1.4.2", "b18f2b53cefbc9cca0bd17d51386f9caa7cf341144cb314e5cd9fd2a1f9b0845", [:mix], [{:decimal, "~> 1.1", [hex: :decimal, repo: "hexpm", optional: false]}, {:esqlite, "~> 0.2.4", [hex: :esqlite, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},