add adapter_spec to adapters, basic db stuff
This commit is contained in:
parent
30da8f31b5
commit
323ebf2569
3 changed files with 51 additions and 25 deletions
|
@ -1,16 +1,10 @@
|
||||||
defmodule Elstat.Adapter.Elixire do
|
defmodule Elstat.Adapter.Elixire do
|
||||||
@behaviour Elstat.Adapter
|
@behaviour Elstat.Adapter
|
||||||
|
|
||||||
use GenServer
|
def adapter_spec do
|
||||||
require Logger
|
%{
|
||||||
|
db_columns: [:status, :latency]
|
||||||
def start_link(service_opts) do
|
}
|
||||||
GenServer.start_link(__MODULE__, service_opts, [])
|
|
||||||
end
|
|
||||||
|
|
||||||
def init(service_opts) do
|
|
||||||
Logger.info("Elixire adapter started with #{inspect service_opts}")
|
|
||||||
{:ok, %{}}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check(args) do
|
def check(args) do
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
defmodule Elstat.Adapter.Ping do
|
defmodule Elstat.Adapter.Ping do
|
||||||
@behaviour Elstat.Adapter
|
@behaviour Elstat.Adapter
|
||||||
|
|
||||||
use GenServer
|
def adapter_spec do
|
||||||
require Logger
|
%{
|
||||||
|
db_columns: [:status]
|
||||||
def start_link(service_opts) do
|
}
|
||||||
GenServer.start_link(__MODULE__, service_opts, [])
|
|
||||||
end
|
|
||||||
|
|
||||||
def init(service_opts) do
|
|
||||||
Logger.info("Ping started with #{inspect service_opts}")
|
|
||||||
{:ok, %{}}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check(args) do
|
def check(args) do
|
||||||
|
|
|
@ -73,8 +73,46 @@ defmodule Elstat.Manager do
|
||||||
services = Application.fetch_env!(:elstat, :services)
|
services = Application.fetch_env!(:elstat, :services)
|
||||||
|
|
||||||
Sqlitex.with_db('elstat.db', fn(db) ->
|
Sqlitex.with_db('elstat.db', fn(db) ->
|
||||||
Enum.each(services, fn service ->
|
services
|
||||||
|
|> Map.keys
|
||||||
|
|> Enum.each(fn service_id ->
|
||||||
|
service = services[service_id]
|
||||||
|
adapter = service.adapter
|
||||||
|
|
||||||
|
spec = adapter.adapter_spec
|
||||||
|
|
||||||
|
columns = %{
|
||||||
|
status: "status bool",
|
||||||
|
latency: "latency bigint",
|
||||||
|
}
|
||||||
|
|
||||||
|
column_res = spec.db_columns
|
||||||
|
|> Enum.map(fn column ->
|
||||||
|
columns[column]
|
||||||
|
end)
|
||||||
|
|> Enum.join(",\n")
|
||||||
|
|
||||||
|
timestamp_column = if Enum.count(spec.db_columns) == 0 do
|
||||||
|
"timestamp bigint"
|
||||||
|
else
|
||||||
|
"timestamp bigint,"
|
||||||
|
end
|
||||||
|
|
||||||
|
query = """
|
||||||
|
CREATE TABLE IF NOT EXISTS #{Atom.to_string service_id} (
|
||||||
|
#{timestamp_column}
|
||||||
|
#{column_res}
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
Logger.debug "query for #{inspect service_id}: #{query}"
|
||||||
|
|
||||||
|
case Sqlitex.query(db, query) do
|
||||||
|
{:ok, _} ->
|
||||||
|
Logger.info "created table for #{inspect service_id}"
|
||||||
|
{:error, _err} ->
|
||||||
|
Logger.error "error making table for #{inspect service_id}"
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -91,7 +129,7 @@ defmodule Elstat.Manager do
|
||||||
data = state.serv_state[key]
|
data = state.serv_state[key]
|
||||||
desc = state.services[key].description
|
desc = state.services[key].description
|
||||||
|
|
||||||
result = case data do
|
case data do
|
||||||
{:map, data_map} ->
|
{:map, data_map} ->
|
||||||
{key, Map.put(data_map, :description, desc)}
|
{key, Map.put(data_map, :description, desc)}
|
||||||
{:bool, data_bool} ->
|
{:bool, data_bool} ->
|
||||||
|
|
Loading…
Reference in a new issue