Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(telemetry): handle json decoding errors #1280

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions telemetry_api/lib/telemetry_api/operators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,37 @@ 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()
# 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)
|> TelemetryApi.Utils.check_list_status("Error fetching operators metadata")
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
Enum.map(operators, fn {op, op_data} ->
Operator.changeset(op, op_data) |> Repo.insert_or_update()
end)
end
:ok
end

# Adds operator metadata to received operator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions telemetry_api/lib/telemetry_api/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}

Expand Down
Loading