Skip to content

Commit

Permalink
Use host_details_updated view and event
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Jun 22, 2023
1 parent 610d756 commit 29bc275
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 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
13 changes: 0 additions & 13 deletions lib/trento_web/views/v1/host_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ defmodule TrentoWeb.V1.HostView do
|> Map.delete(:provider)
end

def render("host_checks_updated.json", %{host: host}) do
render("host.json", %{host: host})
|> Map.delete(:sles_subscriptions)
|> Map.delete(:tags)
|> Map.delete(:cluster_id)
|> Map.delete(:heartbeat)
|> Map.delete(:provider)
|> Map.delete(:agent_version)
|> Map.delete(:hostname)
|> Map.delete(:ip_addresses)
|> Map.delete(:provider_data)
end

def render("host_registered.json", %{host: host}) do
render("host.json", %{host: host})
|> Map.delete(:sles_subscriptions)
Expand Down
23 changes: 23 additions & 0 deletions test/trento/application/projectors/host_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Trento.HostProjectorTest do
HeartbeatFailed,
HeartbeatSucceded,
HostAddedToCluster,
HostChecksSelected,
HostDetailsUpdated,
ProviderUpdated
}
Expand Down Expand Up @@ -166,6 +167,28 @@ defmodule Trento.HostProjectorTest do
1000
end

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

cases = [
%{checks: [Faker.StarWars.character(), Faker.StarWars.character()]},
%{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_details_updated",
%{selected_checks: ^checks, id: ^host_id},
1000
end)
end

test "should update the heartbeat field to passing status when HeartbeatSucceded is received",
%{host_id: host_id} do
event = %HeartbeatSucceded{
Expand Down

0 comments on commit 29bc275

Please sign in to comment.