add /api/current_status
This commit is contained in:
parent
62e636cfe7
commit
e6b06612c3
8 changed files with 80 additions and 18 deletions
|
@ -6,10 +6,9 @@ config :elstat,
|
||||||
# which services will be managed by elstat?
|
# which services will be managed by elstat?
|
||||||
# this follows a service spec
|
# this follows a service spec
|
||||||
# the example here is for elixire
|
# the example here is for elixire
|
||||||
services: [
|
services: %{
|
||||||
%{
|
elixire: %{
|
||||||
id: :elixire,
|
description: "the backend of elixi.re",
|
||||||
human_id: "elixi.re",
|
|
||||||
adapter: Elstat.Adapter.Elixire,
|
adapter: Elstat.Adapter.Elixire,
|
||||||
adapter_opts: %{
|
adapter_opts: %{
|
||||||
base_url: "https://elixi.re",
|
base_url: "https://elixi.re",
|
||||||
|
@ -17,13 +16,12 @@ config :elstat,
|
||||||
# every 10 seconds
|
# every 10 seconds
|
||||||
poll: 10,
|
poll: 10,
|
||||||
},
|
},
|
||||||
%{
|
genserver: %{
|
||||||
id: :genserver,
|
description: "genserver, elixire main server",
|
||||||
human_id: "genserver, elixire main server",
|
|
||||||
adapter: Elstat.Adapter.Ping,
|
adapter: Elstat.Adapter.Ping,
|
||||||
adapter_opts: %{
|
adapter_opts: %{
|
||||||
address: "192.168.1.1",
|
address: "192.168.1.1",
|
||||||
},
|
},
|
||||||
poll: 15,
|
poll: 15,
|
||||||
},
|
},
|
||||||
]
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ defmodule Elstat.Adapter.Elixire do
|
||||||
|
|
||||||
{:ok, {:map, %{
|
{:ok, {:map, %{
|
||||||
status: resp.status_code == 200,
|
status: resp.status_code == 200,
|
||||||
latency: {:millisecond, delta},
|
latency: delta,
|
||||||
}}}
|
}}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
17
lib/api.ex
Normal file
17
lib/api.ex
Normal 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
|
|
@ -30,8 +30,10 @@ defmodule Elstat.Cowboy do
|
||||||
def build_dispatch_config do
|
def build_dispatch_config do
|
||||||
:cowboy_router.compile([
|
:cowboy_router.compile([
|
||||||
{:_, [
|
{:_, [
|
||||||
{"/", Elstat.Cowboy.DefaultHandler, []},
|
{"/", :cowboy_static, {:priv_file, :my_app, "static/index.html"}},
|
||||||
{"/api", Elstat.Cowboy.APIHandler, []},
|
{"/hewwo", Elstat.Cowboy.DefaultHandler, []},
|
||||||
|
{"/api/current_status", Elstat.API.Status, []},
|
||||||
|
|
||||||
]}
|
]}
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,8 +44,9 @@ defmodule Elstat.Manager do
|
||||||
services = Application.fetch_env!(:elstat, :services)
|
services = Application.fetch_env!(:elstat, :services)
|
||||||
|
|
||||||
workers = services
|
workers = services
|
||||||
|> Enum.map(fn service ->
|
|> Map.keys
|
||||||
service_id = service.id
|
|> Enum.map(fn service_id ->
|
||||||
|
service = Map.put(services[service_id], :id, service_id)
|
||||||
|
|
||||||
# each service worker will enter an infinite loop
|
# each service worker will enter an infinite loop
|
||||||
# polling the service (via an adapter) and giving
|
# polling the service (via an adapter) and giving
|
||||||
|
@ -61,20 +62,59 @@ defmodule Elstat.Manager do
|
||||||
[ Elstat.Manager | workers]
|
[ Elstat.Manager | workers]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_current_state() do
|
||||||
|
man_pid = :global.whereis_name Elstat.Manager
|
||||||
|
GenServer.call(man_pid, {:get_state})
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# server callbacks
|
# server callbacks
|
||||||
def init(:ok) do
|
def init(:ok) do
|
||||||
services = Application.fetch_env!(:elstat, :services)
|
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, %{
|
{:ok, %{
|
||||||
services: services,
|
services: services,
|
||||||
|
serv_state: %{},
|
||||||
}}
|
}}
|
||||||
end
|
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
|
def handle_info({:service_info, {sid, sdata}}, state) do
|
||||||
Logger.debug "got service data! #{inspect data}"
|
case sdata do
|
||||||
{:noreply, state}
|
{: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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,10 @@ defmodule Elstat.Supervisor do
|
||||||
id: Elstat.Cowboy,
|
id: Elstat.Cowboy,
|
||||||
start: {Elstat.Cowboy, :start_link, []},
|
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()
|
service_children = Elstat.Manager.build_services()
|
||||||
|
|
1
mix.exs
1
mix.exs
|
@ -23,6 +23,7 @@ defmodule Elstat.MixProject do
|
||||||
defp deps do
|
defp deps do
|
||||||
[
|
[
|
||||||
{:httpoison, "~> 1.1.1"},
|
{:httpoison, "~> 1.1.1"},
|
||||||
|
{:poison, "~> 3.1"},
|
||||||
{:sqlitex, "~> 1.4.2"},
|
{:sqlitex, "~> 1.4.2"},
|
||||||
{:cowboy, "~> 2.4.0"}
|
{:cowboy, "~> 2.4.0"}
|
||||||
]
|
]
|
||||||
|
|
1
mix.lock
1
mix.lock
|
@ -10,6 +10,7 @@
|
||||||
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
|
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
|
||||||
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
|
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
|
||||||
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [: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"},
|
"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"},
|
"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"},
|
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
|
||||||
|
|
Loading…
Reference in a new issue