From b4008f8012f2c2ddee40c3b10b1817dcbec006f0 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 10 Mar 2020 17:27:40 +0300 Subject: [PATCH] Extract host name for splitter indexer and web app --- CHANGELOG.md | 1 + apps/explorer/lib/explorer/chain.ex | 13 +++++++++++++ .../lib/explorer/chain/events/listener.ex | 3 ++- apps/explorer/test/explorer/chain_test.exs | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d97bf72d1b..1f513aeca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- [#3043](https://github.com/poanetwork/blockscout/pull/3043) - Extract host name for split couple of indexer and web app - [#3042](https://github.com/poanetwork/blockscout/pull/3042) - Speedup pending txs list query - [#2944](https://github.com/poanetwork/blockscout/pull/2944) - Split js logic into multiple files diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index dd7d4c8e5f..2da792cdfb 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -4073,6 +4073,19 @@ defmodule Explorer.Chain do end end + def extract_db_host(db_url) do + if db_url == nil do + "" + else + db_url + |> String.split("@") + |> Enum.take(-1) + |> Enum.at(0) + |> String.split(":") + |> Enum.at(0) + end + end + @doc """ Fetches the first trace from the Parity trace URL. """ diff --git a/apps/explorer/lib/explorer/chain/events/listener.ex b/apps/explorer/lib/explorer/chain/events/listener.ex index ebb637c30e..7040a3bd32 100644 --- a/apps/explorer/lib/explorer/chain/events/listener.ex +++ b/apps/explorer/lib/explorer/chain/events/listener.ex @@ -6,7 +6,7 @@ defmodule Explorer.Chain.Events.Listener do use GenServer alias Postgrex.Notifications - import Explorer.Chain, only: [extract_db_name: 1] + import Explorer.Chain, only: [extract_db_name: 1, extract_db_host: 1] def start_link(_) do GenServer.start_link(__MODULE__, "chain_event", name: __MODULE__) @@ -22,6 +22,7 @@ defmodule Explorer.Chain.Events.Listener do {:ok, pid} = explorer_repo |> Keyword.put(:database, extract_db_name(db_url)) + |> Keyword.put(:hostname, extract_db_host(db_url)) |> Notifications.start_link() ref = Notifications.listen!(pid, channel) diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index b299306573..70023b13bf 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -4887,6 +4887,23 @@ defmodule Explorer.ChainTest do end end + describe "extract_db_host/1" do + test "extracts correct db host" do + db_url = "postgresql://viktor:@localhost:5432/blockscout-dev-1" + assert Chain.extract_db_host(db_url) == "localhost" + end + + test "returns empty db name" do + db_url = "" + assert Chain.extract_db_host(db_url) == "" + end + + test "returns nil db name" do + db_url = nil + assert Chain.extract_db_host(db_url) == "" + end + end + describe "fetch_first_trace/2" do test "fetched first trace", %{ json_rpc_named_arguments: json_rpc_named_arguments