Skip to content

Commit

Permalink
Adress comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Jun 22, 2023
1 parent fd868a2 commit eb2364d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
3 changes: 0 additions & 3 deletions lib/trento/application/projectors/cluster_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ defmodule Trento.ClusterProjector do
changeset =
ClusterReadModel
|> Repo.get(id)
# TODO: couldn't make it work with Ecto.Multi
# With following line when we receive an empty list of selected checks, the readmodel does not get updated
# %ClusterReadModel{id: id}
|> ClusterReadModel.changeset(%{
selected_checks: checks
})
Expand Down
15 changes: 7 additions & 8 deletions lib/trento/application/projectors/host_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ defmodule Trento.HostProjector do
changeset =
HostReadModel
|> Repo.get(id)
# TODO: couldn't make it work with Ecto.Multi
# With following line when we receive an empty list of selected checks, the readmodel does not get updated
# %ClusterReadModel{id: id}
|> HostReadModel.changeset(%{
selected_checks: checks
})
Expand Down Expand Up @@ -250,17 +247,19 @@ defmodule Trento.HostProjector do
end

def after_update(
%HostChecksSelected{checks: checks},
%HostChecksSelected{host_id: host_id, checks: checks},
_,
%{host: %HostReadModel{id: id}}
_
) do
host = %HostReadModel{id: host_id, selected_checks: checks}

message =
HostView.render(
"host_checks_updated.json",
%{host: %HostReadModel{id: id, selected_checks: checks}}
"host_details_updated.json",
%{host: host}
)

TrentoWeb.Endpoint.broadcast("monitoring:hosts", "host_checks_updated", message)
TrentoWeb.Endpoint.broadcast("monitoring:hosts", "host_details_updated", message)
end

def after_update(_, _, _), do: :ok
Expand Down
4 changes: 0 additions & 4 deletions lib/trento_web/views/v1/host_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ defmodule TrentoWeb.V1.HostView do
|> Map.delete(:provider)
end

def render("host_checks_updated.json", %{host: %{id: id, selected_checks: checks}}) do
%{id: id, selected_checks: checks}
end

def render("host_registered.json", %{host: host}) do
render("host.json", %{host: host})
|> Map.delete(:sles_subscriptions)
Expand Down
8 changes: 4 additions & 4 deletions test/trento/application/projectors/host_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ defmodule Trento.HostProjectorTest do
end

test "should update the selected_checks field when event is received" do
%{id: host_id} = insert(:host, id: UUID.uuid4())
%{id: host_id} = insert(:host)

test_checks_data = [
cases = [
%{checks: [Faker.StarWars.character(), Faker.StarWars.character()]},
%{checks: []}
]

Enum.each(test_checks_data, fn %{checks: checks} ->
Enum.each(cases, fn %{checks: checks} ->
event = %HostChecksSelected{host_id: host_id, checks: checks}

ProjectorTestHelper.project(HostProjector, event, "host_projector")
host_projection = Repo.get!(HostReadModel, event.host_id)

assert event.checks == host_projection.selected_checks

assert_broadcast "host_checks_updated",
assert_broadcast "host_details_updated",
%{selected_checks: ^checks, id: ^host_id},
1000
end)
Expand Down

0 comments on commit eb2364d

Please sign in to comment.