From aa0f51c603ece664637eff36200546587df16144 Mon Sep 17 00:00:00 2001 From: JuArce <52429267+JuArce@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:58:05 -0300 Subject: [PATCH 1/3] fix: handle failed json decodes fix: do not fetch operator metadata every time --- telemetry_api/lib/telemetry_api/operators.ex | 46 ++++++++++++++------ telemetry_api/lib/telemetry_api/utils.ex | 7 ++- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/telemetry_api/lib/telemetry_api/operators.ex b/telemetry_api/lib/telemetry_api/operators.ex index 336340ab7..9a5f6031f 100644 --- a/telemetry_api/lib/telemetry_api/operators.ex +++ b/telemetry_api/lib/telemetry_api/operators.ex @@ -72,19 +72,39 @@ defmodule TelemetryApi.Operators do """ def fetch_all_operators() do - with {:ok, operators} <- OperatorStateRetriever.get_operators() do - Enum.map(operators, fn op_data -> - with {:ok, full_operator_data} <- add_operator_metadata(op_data) do - case Repo.get(Operator, op_data.address) do - nil -> %Operator{} - operator -> operator - end - |> Operator.changeset(full_operator_data) - |> Repo.insert_or_update() - end - end) - |> TelemetryApi.Utils.check_list_status("Error fetching operators metadata") - end + {:ok, operators} = OperatorStateRetriever.get_operators() + + # Construct tuple {%Operator{}, op_data} + operators = Enum.map(operators, fn op_data -> + {Repo.get(Operator, op_data.address), op_data} + end) + + # Filter operators already stored on db and those that are new + new_operators = Enum.filter(operators, fn {op, _} -> is_nil(op) end) + |> Enum.map(fn {_, data} -> {%Operator{}, data} end) + old_operators = Enum.filter(operators, fn {op, _} -> not is_nil(op) end) + + # Fetch metadata for new operators + new_operators = Enum.map(new_operators, fn {op, op_data} -> + case add_operator_metadata(op_data) do + {:ok, data} -> {:ok, {op, data}} + {:error, msg} -> {:error, msg} + end + end) + # Filter status ok and map to {op, op_data} + |> Enum.filter(fn {status, _} -> status == :ok end) + |> Enum.map(fn {_, data} -> data end) + + # Merge both lists + IO.inspect(new_operators) + operators = (new_operators ++ old_operators) + + # # Insert in db + operators = Enum.map(operators, fn {op, op_data} -> + Operator.changeset(op, op_data) |> Repo.insert_or_update() + end) + + {:ok, operators} end # Adds operator metadata to received operator. diff --git a/telemetry_api/lib/telemetry_api/utils.ex b/telemetry_api/lib/telemetry_api/utils.ex index 073a7a7df..46443d774 100644 --- a/telemetry_api/lib/telemetry_api/utils.ex +++ b/telemetry_api/lib/telemetry_api/utils.ex @@ -15,10 +15,9 @@ defmodule TelemetryApi.Utils do {:error, message} """ def fetch_json_data(url) do - case HTTPoison.get(url) do - {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - {:ok, Jason.decode!(body)} - + with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- HTTPoison.get(url) do + Jason.decode(body) + else {:ok, %HTTPoison.Response{status_code: status_code}} -> {:error, "Request failed with status #{status_code}"} From 43530bd29e5f1d2b9fd4a361d6f6e2697e4e68c7 Mon Sep 17 00:00:00 2001 From: Julian Ventura Date: Wed, 16 Oct 2024 19:04:18 -0300 Subject: [PATCH 2/3] Remove inspect, add error handling and refactor message --- telemetry_api/lib/telemetry_api/operators.ex | 63 ++++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/telemetry_api/lib/telemetry_api/operators.ex b/telemetry_api/lib/telemetry_api/operators.ex index 9a5f6031f..7f33a4bc2 100644 --- a/telemetry_api/lib/telemetry_api/operators.ex +++ b/telemetry_api/lib/telemetry_api/operators.ex @@ -72,39 +72,38 @@ defmodule TelemetryApi.Operators do """ def fetch_all_operators() do - {:ok, operators} = OperatorStateRetriever.get_operators() - - # Construct tuple {%Operator{}, op_data} - operators = Enum.map(operators, fn op_data -> - {Repo.get(Operator, op_data.address), op_data} - end) - - # Filter operators already stored on db and those that are new - new_operators = Enum.filter(operators, fn {op, _} -> is_nil(op) end) - |> Enum.map(fn {_, data} -> {%Operator{}, data} end) - old_operators = Enum.filter(operators, fn {op, _} -> not is_nil(op) end) - - # Fetch metadata for new operators - new_operators = Enum.map(new_operators, fn {op, op_data} -> - case add_operator_metadata(op_data) do - {:ok, data} -> {:ok, {op, data}} - {:error, msg} -> {:error, msg} + with {:ok, operators} <- OperatorStateRetriever.get_operators() do + # Construct tuple {%Operator{}, op_data} + operators = Enum.map(operators, fn op_data -> + {Repo.get(Operator, op_data.address), op_data} + end) + + # Filter operators already stored on db and those that are new + #TODO: We actually don't need to add the %Operator{} here, we could do it just before the merge + new_operators = Enum.filter(operators, fn {op, _} -> is_nil(op) end) + |> Enum.map(fn {_, data} -> {%Operator{}, data} end) + old_operators = Enum.filter(operators, fn {op, _} -> not is_nil(op) end) + + # Fetch metadata for new operators + new_operators = Enum.map(new_operators, fn {op, op_data} -> + case add_operator_metadata(op_data) do + {:ok, data} -> {:ok, {op, data}} + {:error, msg} -> {:error, msg} + end + end) + # Filter status ok and map to {op, op_data} + |> Enum.filter(fn {status, _} -> status == :ok end) + |> Enum.map(fn {_, data} -> data end) + + # Merge both lists + operators = (new_operators ++ old_operators) + + # Insert in db + operators = Enum.map(operators, fn {op, op_data} -> + Operator.changeset(op, op_data) |> Repo.insert_or_update() + end) end - end) - # Filter status ok and map to {op, op_data} - |> Enum.filter(fn {status, _} -> status == :ok end) - |> Enum.map(fn {_, data} -> data end) - - # Merge both lists - IO.inspect(new_operators) - operators = (new_operators ++ old_operators) - - # # Insert in db - operators = Enum.map(operators, fn {op, op_data} -> - Operator.changeset(op, op_data) |> Repo.insert_or_update() - end) - - {:ok, operators} + :ok end # Adds operator metadata to received operator. From 659fe623fe56aa13253a8a3334ff8259c86f7a56 Mon Sep 17 00:00:00 2001 From: Julian Ventura Date: Wed, 16 Oct 2024 19:07:51 -0300 Subject: [PATCH 3/3] Fix ok branch on telemetry info fetcher --- telemetry_api/lib/telemetry_api/operators.ex | 2 +- telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/telemetry_api/lib/telemetry_api/operators.ex b/telemetry_api/lib/telemetry_api/operators.ex index 7f33a4bc2..019499d2d 100644 --- a/telemetry_api/lib/telemetry_api/operators.ex +++ b/telemetry_api/lib/telemetry_api/operators.ex @@ -99,7 +99,7 @@ defmodule TelemetryApi.Operators do operators = (new_operators ++ old_operators) # Insert in db - operators = Enum.map(operators, fn {op, op_data} -> + Enum.map(operators, fn {op, op_data} -> Operator.changeset(op, op_data) |> Repo.insert_or_update() end) end diff --git a/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex b/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex index 6522bde6d..23fd509fe 100644 --- a/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex +++ b/telemetry_api/lib/telemetry_api/periodic/operator_fetcher.ex @@ -41,7 +41,7 @@ defmodule TelemetryApi.Periodic.OperatorFetcher do defp fetch_operators_info() do case Operators.fetch_all_operators() do - {:ok, _} -> :ok + :ok -> :ok {:error, message} -> IO.inspect("Couldn't fetch operators: #{IO.inspect(message)}") end end