add adapter.transform_val
also add storage of gathered stats
This commit is contained in:
parent
323ebf2569
commit
740059a4b4
3 changed files with 69 additions and 16 deletions
|
@ -3,7 +3,7 @@ defmodule Elstat.Adapter.Elixire do
|
||||||
|
|
||||||
def adapter_spec do
|
def adapter_spec do
|
||||||
%{
|
%{
|
||||||
db_columns: [:status, :latency]
|
db_columns: [:timestamp, :status, :latency]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,4 +22,8 @@ defmodule Elstat.Adapter.Elixire do
|
||||||
}}}
|
}}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform_val({:map, %{status: status, latency: latency}}) do
|
||||||
|
[status, latency]
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ defmodule Elstat.Adapter.Ping do
|
||||||
|
|
||||||
def adapter_spec do
|
def adapter_spec do
|
||||||
%{
|
%{
|
||||||
db_columns: [:status]
|
db_columns: [:timestamp, :status]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,4 +13,8 @@ defmodule Elstat.Adapter.Ping do
|
||||||
{:ok, {:bool, alive?}}
|
{:ok, {:bool, alive?}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform_val({:bool, alive?}) do
|
||||||
|
[alive?]
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -64,11 +64,28 @@ defmodule Elstat.Manager do
|
||||||
|
|
||||||
def get_current_state() do
|
def get_current_state() do
|
||||||
man_pid = :global.whereis_name Elstat.Manager
|
man_pid = :global.whereis_name Elstat.Manager
|
||||||
GenServer.call(man_pid, {:get_state})
|
GenServer.call(man_pid, {:get_current})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# server callbacks
|
# server callbacks
|
||||||
|
|
||||||
|
def get_columns_def do
|
||||||
|
%{
|
||||||
|
timestamp: "timestamp bigint",
|
||||||
|
status: "status bool",
|
||||||
|
latency: "latency bigint",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_columns_name do
|
||||||
|
%{
|
||||||
|
timestamp: "timestamp",
|
||||||
|
status: "status",
|
||||||
|
latency: "latency",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def init(:ok) do
|
def init(:ok) do
|
||||||
services = Application.fetch_env!(:elstat, :services)
|
services = Application.fetch_env!(:elstat, :services)
|
||||||
|
|
||||||
|
@ -81,10 +98,7 @@ defmodule Elstat.Manager do
|
||||||
|
|
||||||
spec = adapter.adapter_spec
|
spec = adapter.adapter_spec
|
||||||
|
|
||||||
columns = %{
|
columns = get_columns_def()
|
||||||
status: "status bool",
|
|
||||||
latency: "latency bigint",
|
|
||||||
}
|
|
||||||
|
|
||||||
column_res = spec.db_columns
|
column_res = spec.db_columns
|
||||||
|> Enum.map(fn column ->
|
|> Enum.map(fn column ->
|
||||||
|
@ -92,15 +106,8 @@ defmodule Elstat.Manager do
|
||||||
end)
|
end)
|
||||||
|> Enum.join(",\n")
|
|> Enum.join(",\n")
|
||||||
|
|
||||||
timestamp_column = if Enum.count(spec.db_columns) == 0 do
|
|
||||||
"timestamp bigint"
|
|
||||||
else
|
|
||||||
"timestamp bigint,"
|
|
||||||
end
|
|
||||||
|
|
||||||
query = """
|
query = """
|
||||||
CREATE TABLE IF NOT EXISTS #{Atom.to_string service_id} (
|
CREATE TABLE IF NOT EXISTS #{Atom.to_string service_id} (
|
||||||
#{timestamp_column}
|
|
||||||
#{column_res}
|
#{column_res}
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
|
@ -122,7 +129,7 @@ defmodule Elstat.Manager do
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_call({:get_state}, _from, state) do
|
def handle_call({:get_current}, _from, state) do
|
||||||
reply = state.serv_state
|
reply = state.serv_state
|
||||||
|> Map.keys
|
|> Map.keys
|
||||||
|> Enum.map(fn key ->
|
|> Enum.map(fn key ->
|
||||||
|
@ -144,10 +151,48 @@ defmodule Elstat.Manager do
|
||||||
{:reply, reply, state}
|
{:reply, reply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_insert_query(service_id, state) do
|
||||||
|
services = state.services
|
||||||
|
service = services[service_id]
|
||||||
|
spec = service.adapter.adapter_spec
|
||||||
|
|
||||||
|
columns_str = spec.db_columns
|
||||||
|
|> Enum.map(fn atom ->
|
||||||
|
Atom.to_string atom
|
||||||
|
end)
|
||||||
|
|> Enum.join(",")
|
||||||
|
|
||||||
|
query_args_str = 1..Enum.count(spec.db_columns)
|
||||||
|
|> Enum.map(fn num ->
|
||||||
|
"$#{num}"
|
||||||
|
end)
|
||||||
|
|> Enum.join(",")
|
||||||
|
|
||||||
|
"""
|
||||||
|
INSERT INTO #{Atom.to_string service_id} (#{columns_str})
|
||||||
|
VALUES (#{query_args_str})
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
def handle_info({:service_info, {sid, sdata}}, state) do
|
def handle_info({:service_info, {sid, sdata}}, state) do
|
||||||
case sdata do
|
case sdata do
|
||||||
{:ok, actual_data} ->
|
{:ok, actual_data} ->
|
||||||
new_serv_state = Map.put(state.serv_state, sid, actual_data)
|
new_serv_state = Map.put(state.serv_state, sid, actual_data)
|
||||||
|
|
||||||
|
query = build_insert_query(sid, state)
|
||||||
|
|
||||||
|
adapter = state.services[sid].adapter
|
||||||
|
adp_args = adapter.transform_val(actual_data)
|
||||||
|
|
||||||
|
timestamp = :erlang.system_time(:millisecond)
|
||||||
|
|
||||||
|
Logger.debug "query: #{query}, timestamp: #{timestamp}, adp args: #{inspect adp_args}"
|
||||||
|
|
||||||
|
Sqlitex.with_db('elstat.db', fn(db) ->
|
||||||
|
res = Sqlitex.query(db, query, bind: [timestamp | adp_args])
|
||||||
|
IO.puts "#{inspect res}"
|
||||||
|
end)
|
||||||
|
|
||||||
{:noreply, %{state | serv_state: new_serv_state}}
|
{:noreply, %{state | serv_state: new_serv_state}}
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
Logger.warn "error on #{inspect sid}: #{inspect err}"
|
Logger.warn "error on #{inspect sid}: #{inspect err}"
|
||||||
|
|
Loading…
Reference in a new issue