Skip to content

Commit

Permalink
Fix Hosts query without the soft deleted hosts exclusion (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco authored and rtorrero committed Mar 31, 2023
1 parent 595a8a6 commit c925c76
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 2 additions & 4 deletions lib/trento/application/integration/prometheus/prometheus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ defmodule Trento.Integration.Prometheus do
Prometheus integration service
"""

alias Trento.Repo

alias Trento.HostReadModel
alias Trento.Hosts

@spec get_targets :: [map]
def get_targets do
Repo.all(HostReadModel)
Hosts.get_all_hosts()
end

@spec get_exporters_status(String.t()) :: {:ok, map} | {:error, any}
Expand Down
2 changes: 1 addition & 1 deletion lib/trento/application/usecases/clusters/clusters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule Trento.Clusters do
Repo.all(
from h in HostReadModel,
select: %{host_id: h.id},
where: h.cluster_id == ^cluster_id
where: h.cluster_id == ^cluster_id and is_nil(h.deregistered_at)
)

Checks.request_execution(
Expand Down
3 changes: 2 additions & 1 deletion test/trento/application/usecases/clusters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ defmodule Trento.ClustersTest do
describe "checks execution with wanda adapter" do
test "should start a checks execution on demand if checks are selected" do
%{id: cluster_id} = insert(:cluster)
insert(:host, deregistered_at: DateTime.utc_now(), cluster_id: cluster_id)
insert_list(2, :host, cluster_id: cluster_id)

expect(Trento.Infrastructure.Messaging.Adapter.Mock, :publish, fn "executions", message ->
assert message.group_id == cluster_id

assert length(message.targets) == 2
:ok
end)

Expand Down
15 changes: 11 additions & 4 deletions test/trento_web/controllers/v1/prometheus_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ defmodule TrentoWeb.V1.PrometheusControllerTest do
import Trento.Factory

test "should return the expected targets", %{conn: conn} do
insert(:host, deregistered_at: DateTime.utc_now())
insert_list(2, :host)

api_spec = ApiSpec.spec()

conn
|> get("/api/v1/prometheus/targets")
|> json_response(200)
|> assert_schema("HttpSTDTargetList", api_spec)
response =
conn
|> get("/api/v1/prometheus/targets")
|> json_response(200)

targets_ids = Enum.map(response, &Map.get(&1, "labels")["agentID"])

assert length(targets_ids) == 2
assert_schema(response, "HttpSTDTargetList", api_spec)
end

test "should return the expected targets when some host does not have any IP address", %{
Expand Down

0 comments on commit c925c76

Please sign in to comment.