From cf50bcf5adacca9fdb204eec346e5994ca08ad4b Mon Sep 17 00:00:00 2001 From: kballou Date: Tue, 22 Mar 2016 15:54:49 -0600 Subject: Refactor: internally pass tuples This removes the need for 3 different method heads for `process/4`, the internal query functions will handle building a correct url for processing --- lib/ex_prometheus_io.ex | 6 +++--- lib/ex_prometheus_io/query.ex | 26 ++++++-------------------- test/ex_prometheus_io_query_test.exs | 4 ++-- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/ex_prometheus_io.ex b/lib/ex_prometheus_io.ex index 43604ea..9eb7c1c 100644 --- a/lib/ex_prometheus_io.ex +++ b/lib/ex_prometheus_io.ex @@ -11,17 +11,17 @@ defmodule ExPrometheusIo do end def range(query, start_ts, end_ts, step, _opts \\ []) do - query_opts = [query, start_ts, end_ts, step] + query_opts = {query, start_ts, end_ts, step} spawn_query(:range, query_opts) end def series(matches, _opts \\ []) when is_list(matches) do - spawn_query(:series, [matches]) + spawn_query(:series, {matches}) end defp spawn_query(query, query_opts, _opts \\ []) do query_ref = make_ref() - query_opts = [query | query_opts] ++ [query_ref, self()] + query_opts = [query, query_opts, query_ref, self()] {:ok, pid} = Task.Supervisor.start_child( ExPrometheusIo.QuerySupervisor, ExPrometheusIo.Query, diff --git a/lib/ex_prometheus_io/query.ex b/lib/ex_prometheus_io/query.ex index 140d0a9..20b01a9 100644 --- a/lib/ex_prometheus_io/query.ex +++ b/lib/ex_prometheus_io/query.ex @@ -1,24 +1,10 @@ defmodule ExPrometheusIo.Query do - def process(:query, query, query_ref, owner) do - build_url(:query, query) - |> fetch_json() - |> Poison.decode - |> send_results(query_ref, owner) - end - - def process(:range, query, start_ts, end_ts, step, query_ref, owner) do - build_url(:range, {query, start_ts, end_ts, step}) - |> fetch_json() - |> Poison.decode - |> send_results(query_ref, owner) - end - - def process(:series, matches, query_ref, owner) do - build_url(:series, matches) - |> fetch_json() - |> Poison.decode - |> send_results(query_ref, owner) + def process(query, query_opts, query_ref, owner) do + build_url(query, query_opts) + |> fetch_json() + |> Poison.decode + |> send_results(query_ref, owner) end defp fetch_json(uri) do @@ -48,7 +34,7 @@ defmodule ExPrometheusIo.Query do <> "&step=#{step}" end - def query_params(:series, matches) when is_list(matches) do + def query_params(:series, {matches}) when is_list(matches) do matches |> Enum.map(fn(match) -> "match[]=#{match}" end) |> Enum.join("&") diff --git a/test/ex_prometheus_io_query_test.exs b/test/ex_prometheus_io_query_test.exs index 6e480a9..856cb06 100644 --- a/test/ex_prometheus_io_query_test.exs +++ b/test/ex_prometheus_io_query_test.exs @@ -17,7 +17,7 @@ defmodule ExPrometheusIo.QueryTest do end test "query_params builds correct series query" do - assert "match[]=up" == query_params(:series, ["up"]) + assert "match[]=up" == query_params(:series, {["up"]}) end test "query endpoint" do @@ -42,7 +42,7 @@ defmodule ExPrometheusIo.QueryTest do <> "&end=#{cur_time}" <> "&step=1" == build_url(:range, {"up", cur_time - 5, cur_time, 1}) - assert base_url <> "series?match[]=up" == build_url(:series, ["up"]) + assert base_url <> "series?match[]=up" == build_url(:series, {["up"]}) end defp prom_host, do: Application.fetch_env!(:ex_prometheus_io, :hostname) -- cgit v1.2.1