diff --git a/lib/trento/application/projectors/cluster_projector.ex b/lib/trento/application/projectors/cluster_projector.ex index 1ce45c1f88..f0659eba8f 100644 --- a/lib/trento/application/projectors/cluster_projector.ex +++ b/lib/trento/application/projectors/cluster_projector.ex @@ -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 }) diff --git a/lib/trento/application/projectors/host_projector.ex b/lib/trento/application/projectors/host_projector.ex index 1a070cc517..76a3e10849 100644 --- a/lib/trento/application/projectors/host_projector.ex +++ b/lib/trento/application/projectors/host_projector.ex @@ -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 }) @@ -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 diff --git a/lib/trento_web/views/v1/host_view.ex b/lib/trento_web/views/v1/host_view.ex index 49c638b746..7bdab56a1d 100644 --- a/lib/trento_web/views/v1/host_view.ex +++ b/lib/trento_web/views/v1/host_view.ex @@ -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) diff --git a/test/trento/application/projectors/host_projector_test.exs b/test/trento/application/projectors/host_projector_test.exs index c3e363cb94..3e9f92bec1 100644 --- a/test/trento/application/projectors/host_projector_test.exs +++ b/test/trento/application/projectors/host_projector_test.exs @@ -24,6 +24,7 @@ defmodule Trento.HostProjectorTest do HeartbeatFailed, HeartbeatSucceded, HostAddedToCluster, + HostChecksSelected, HostDetailsUpdated, ProviderUpdated } @@ -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{