From 7596fc9a8e025a79171de1ee2b6b1d490e122c52 Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Thu, 6 Oct 2022 15:29:26 +0200 Subject: [PATCH 1/6] WIP on HealthSummaryDTO --- .../sap_systems/dto/health_summary_dto.ex | 2 ++ .../sap_systems/health_summary_service.ex | 18 +++++++++++++++++- .../usecases/health_summary_service_test.exs | 9 ++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex index a982aaf18c..88f220334f 100644 --- a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex +++ b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex @@ -15,6 +15,8 @@ defmodule Trento.Application.UseCases.SapSystems.HealthSummaryDto do field :sapsystem_health, Ecto.Enum, values: Health.values() field :database_health, Ecto.Enum, values: Health.values() field :clusters_health, Ecto.Enum, values: Health.values() + field :hana_cluster_id, :string + field :database_id, :string field :hosts_health, Ecto.Enum, values: [:passing, :critical, :unknown] end end diff --git a/lib/trento/application/usecases/sap_systems/health_summary_service.ex b/lib/trento/application/usecases/sap_systems/health_summary_service.ex index f28e5a57ed..c5424ae8ae 100644 --- a/lib/trento/application/usecases/sap_systems/health_summary_service.ex +++ b/lib/trento/application/usecases/sap_systems/health_summary_service.ex @@ -48,10 +48,26 @@ defmodule Trento.SapSystems.HealthSummaryService do sapsystem_health: health, database_health: compute_database_health(database_instances), clusters_health: compute_clusters_health(all_instances), - hosts_health: compute_hosts_health(all_instances) + hosts_health: compute_hosts_health(all_instances), + hana_cluster_id: extract_hana_cluster_id(database_instances), + database_id: extract_database_id(database_instances) }) end + @spec extract_database_id([DatabaseInstanceReadModel.t()]) :: binary() + defp extract_database_id(database_instances) do + database_instances + |> Enum.map(fn %{sap_system_id: database_id} -> database_id end) + |> List.first() + end + + @spec extract_hana_cluster_id([DatabaseInstanceReadModel.t()]) :: binary() + defp extract_hana_cluster_id(database_instances) do + database_instances + |> Enum.map(fn %{host: %{cluster_id: hana_cluster_id}} -> hana_cluster_id end) + |> List.first() + end + @spec compute_database_health([DatabaseInstanceReadModel.t()]) :: Health.t() defp compute_database_health(database_instances) do database_instances diff --git a/test/trento/application/usecases/health_summary_service_test.exs b/test/trento/application/usecases/health_summary_service_test.exs index 444050d776..77baa6cc25 100644 --- a/test/trento/application/usecases/health_summary_service_test.exs +++ b/test/trento/application/usecases/health_summary_service_test.exs @@ -49,8 +49,9 @@ defmodule Trento.HealthSummaryServiceTest do test "should determine health summary for a SAP System" do %{ sap_system_id: sap_system_id, - sid: sid - } = sap_system_with_cluster_and_hosts() + sid: sid, + cluster_id: cluster_id, + } = sap_system_with_database_hosts() assert [ %HealthSummaryDto{ @@ -59,7 +60,9 @@ defmodule Trento.HealthSummaryServiceTest do sapsystem_health: :passing, database_health: :critical, clusters_health: :warning, - hosts_health: :unknown + hosts_health: :unknown, + database_id: ^sap_system_id, + hana_cluster_id: ^cluster_id } ] = HealthSummaryService.get_health_summary() end From cc29ec90edd4cc11c3073c6c0f794871d7dcb165 Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Fri, 7 Oct 2022 11:07:44 +0200 Subject: [PATCH 2/6] Add test to the health summary service Co-Authored-By: Eugen Maksymenko --- test/support/factory.ex | 56 ------------------- .../usecases/health_summary_service_test.exs | 35 ++++++++++-- .../health_overview_controller_test.exs | 41 +++++++++++--- 3 files changed, 63 insertions(+), 69 deletions(-) diff --git a/test/support/factory.ex b/test/support/factory.ex index 3fe8838f05..ed6905b22f 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -434,60 +434,4 @@ defmodule Trento.Factory do } end - def sap_system_with_cluster_and_hosts do - %ClusterReadModel{id: cluster_id} = - insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.passing()) - - %ClusterReadModel{id: another_cluster_id} = - insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.warning()) - - %HostReadModel{id: host_1_id} = insert(:host, cluster_id: cluster_id, heartbeat: :unknown) - - %HostReadModel{id: host_2_id} = - insert(:host, cluster_id: another_cluster_id, heartbeat: :passing) - - database_sid = "HDD" - - %SapSystemReadModel{ - id: sap_system_id, - sid: sid - } = insert(:sap_system, health: Health.passing()) - - insert( - :database_instance_without_host, - sap_system_id: sap_system_id, - sid: database_sid, - host_id: host_1_id, - health: Health.warning() - ) - - insert( - :database_instance_without_host, - sap_system_id: sap_system_id, - sid: database_sid, - host_id: host_2_id, - health: Health.critical() - ) - - insert( - :application_instance_without_host, - sap_system_id: sap_system_id, - sid: sid, - host_id: host_1_id, - health: Health.passing() - ) - - insert( - :application_instance_without_host, - sap_system_id: sap_system_id, - sid: sid, - host_id: host_2_id, - health: Health.warning() - ) - - %{ - sap_system_id: sap_system_id, - sid: sid - } - end end diff --git a/test/trento/application/usecases/health_summary_service_test.exs b/test/trento/application/usecases/health_summary_service_test.exs index 77baa6cc25..2815535a6b 100644 --- a/test/trento/application/usecases/health_summary_service_test.exs +++ b/test/trento/application/usecases/health_summary_service_test.exs @@ -8,6 +8,9 @@ defmodule Trento.HealthSummaryServiceTest do alias Trento.Application.UseCases.SapSystems.HealthSummaryDto alias Trento.SapSystems.HealthSummaryService + require Trento.Domain.Enums.Health, as: Health + require Trento.Domain.Enums.ClusterType, as: ClusterType + alias Trento.{ HostReadModel, @@ -47,19 +50,39 @@ defmodule Trento.HealthSummaryServiceTest do end test "should determine health summary for a SAP System" do - %{ + %Trento.ClusterReadModel{id: cluster_id} = + insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.passing()) + + %Trento.HostReadModel{id: host_1_id} = insert(:host, cluster_id: cluster_id, heartbeat: :unknown) + + %Trento.SapSystemReadModel{ + id: sap_system_id, + sid: sid + } = insert(:sap_system, health: Health.critical()) + + insert( + :database_instance_without_host, + sap_system_id: sap_system_id, + sid: "HDD", + host_id: host_1_id, + health: Health.warning() + ) + + insert( + :application_instance_without_host, sap_system_id: sap_system_id, sid: sid, - cluster_id: cluster_id, - } = sap_system_with_database_hosts() + host_id: host_1_id, + health: Health.critical() + ) assert [ %HealthSummaryDto{ id: ^sap_system_id, sid: ^sid, - sapsystem_health: :passing, - database_health: :critical, - clusters_health: :warning, + sapsystem_health: :critical, + database_health: :warning, + clusters_health: :passing, hosts_health: :unknown, database_id: ^sap_system_id, hana_cluster_id: ^cluster_id diff --git a/test/trento_web/controllers/health_overview_controller_test.exs b/test/trento_web/controllers/health_overview_controller_test.exs index 81e7164578..d2dbdd0d56 100644 --- a/test/trento_web/controllers/health_overview_controller_test.exs +++ b/test/trento_web/controllers/health_overview_controller_test.exs @@ -2,12 +2,37 @@ defmodule TrentoWeb.HealthOverviewControllerTest do use TrentoWeb.ConnCase, async: true import Trento.Factory + require Trento.Domain.Enums.Health, as: Health + require Trento.Domain.Enums.ClusterType, as: ClusterType test "should return the expected overview", %{conn: conn} do - %{ - sap_system_id: sap_system_id, + + %Trento.ClusterReadModel{id: cluster_id} = + insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.passing()) + + %Trento.HostReadModel{id: host_1_id} = insert(:host, cluster_id: cluster_id, heartbeat: :unknown) + + %Trento.SapSystemReadModel{ + id: sap_system_id, sid: sid - } = sap_system_with_cluster_and_hosts() + } = insert(:sap_system, health: Health.critical()) + + insert( + :database_instance_without_host, + sap_system_id: sap_system_id, + sid: "HDD", + host_id: host_1_id, + health: Health.warning() + ) + + insert( + :application_instance_without_host, + sap_system_id: sap_system_id, + sid: sid, + host_id: host_1_id, + health: Health.critical() + ) + conn = get(conn, Routes.health_overview_path(conn, :overview)) @@ -17,10 +42,12 @@ defmodule TrentoWeb.HealthOverviewControllerTest do %{ "id" => "#{sap_system_id}", "sid" => "#{sid}", - "sapsystem_health" => "passing", - "database_health" => "critical", - "clusters_health" => "warning", - "hosts_health" => "unknown" + "sapsystem_health" => "critical", + "database_health" => "warning", + "clusters_health" => "passing", + "hosts_health" => "unknown", + "database_id" => "#{sap_system_id}", + "hana_cluster_id" => "#{cluster_id}" } ] == json_response(conn, 200) end From 5d400033b7346f9ca3e51f57e245c41bfccebea2 Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Fri, 7 Oct 2022 11:35:04 +0200 Subject: [PATCH 3/6] Formatting code --- test/support/factory.ex | 1 - .../application/usecases/health_summary_service_test.exs | 4 ++-- .../controllers/health_overview_controller_test.exs | 5 ++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/support/factory.ex b/test/support/factory.ex index ed6905b22f..d3fc886232 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -433,5 +433,4 @@ defmodule Trento.Factory do msg: Faker.StarWars.planet() } end - end diff --git a/test/trento/application/usecases/health_summary_service_test.exs b/test/trento/application/usecases/health_summary_service_test.exs index 2815535a6b..90835ae706 100644 --- a/test/trento/application/usecases/health_summary_service_test.exs +++ b/test/trento/application/usecases/health_summary_service_test.exs @@ -11,7 +11,6 @@ defmodule Trento.HealthSummaryServiceTest do require Trento.Domain.Enums.Health, as: Health require Trento.Domain.Enums.ClusterType, as: ClusterType - alias Trento.{ HostReadModel, SapSystemReadModel @@ -53,7 +52,8 @@ defmodule Trento.HealthSummaryServiceTest do %Trento.ClusterReadModel{id: cluster_id} = insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.passing()) - %Trento.HostReadModel{id: host_1_id} = insert(:host, cluster_id: cluster_id, heartbeat: :unknown) + %Trento.HostReadModel{id: host_1_id} = + insert(:host, cluster_id: cluster_id, heartbeat: :unknown) %Trento.SapSystemReadModel{ id: sap_system_id, diff --git a/test/trento_web/controllers/health_overview_controller_test.exs b/test/trento_web/controllers/health_overview_controller_test.exs index d2dbdd0d56..363546a780 100644 --- a/test/trento_web/controllers/health_overview_controller_test.exs +++ b/test/trento_web/controllers/health_overview_controller_test.exs @@ -6,11 +6,11 @@ defmodule TrentoWeb.HealthOverviewControllerTest do require Trento.Domain.Enums.ClusterType, as: ClusterType test "should return the expected overview", %{conn: conn} do - %Trento.ClusterReadModel{id: cluster_id} = insert(:cluster, type: ClusterType.hana_scale_up(), health: Health.passing()) - %Trento.HostReadModel{id: host_1_id} = insert(:host, cluster_id: cluster_id, heartbeat: :unknown) + %Trento.HostReadModel{id: host_1_id} = + insert(:host, cluster_id: cluster_id, heartbeat: :unknown) %Trento.SapSystemReadModel{ id: sap_system_id, @@ -33,7 +33,6 @@ defmodule TrentoWeb.HealthOverviewControllerTest do health: Health.critical() ) - conn = get(conn, Routes.health_overview_path(conn, :overview)) assert 200 == conn.status From da625601319f784266a827cd3290f770101b7229 Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Fri, 7 Oct 2022 11:59:18 +0200 Subject: [PATCH 4/6] Fix typespec in health summary service --- .../usecases/sap_systems/health_summary_service.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trento/application/usecases/sap_systems/health_summary_service.ex b/lib/trento/application/usecases/sap_systems/health_summary_service.ex index c5424ae8ae..8290fb3973 100644 --- a/lib/trento/application/usecases/sap_systems/health_summary_service.ex +++ b/lib/trento/application/usecases/sap_systems/health_summary_service.ex @@ -54,14 +54,14 @@ defmodule Trento.SapSystems.HealthSummaryService do }) end - @spec extract_database_id([DatabaseInstanceReadModel.t()]) :: binary() + @spec extract_database_id([DatabaseInstanceReadModel.t()]) :: String.t() defp extract_database_id(database_instances) do database_instances |> Enum.map(fn %{sap_system_id: database_id} -> database_id end) |> List.first() end - @spec extract_hana_cluster_id([DatabaseInstanceReadModel.t()]) :: binary() + @spec extract_hana_cluster_id([DatabaseInstanceReadModel.t()]) :: String.t() defp extract_hana_cluster_id(database_instances) do database_instances |> Enum.map(fn %{host: %{cluster_id: hana_cluster_id}} -> hana_cluster_id end) From ae9c27652d65551115a1abdf9cc1459b859e839b Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Fri, 7 Oct 2022 15:34:41 +0200 Subject: [PATCH 5/6] Health Summary DTO has database and cluster id as Ecto.UUID --- .../usecases/sap_systems/dto/health_summary_dto.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex index 88f220334f..d8faecb7a3 100644 --- a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex +++ b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex @@ -15,8 +15,8 @@ defmodule Trento.Application.UseCases.SapSystems.HealthSummaryDto do field :sapsystem_health, Ecto.Enum, values: Health.values() field :database_health, Ecto.Enum, values: Health.values() field :clusters_health, Ecto.Enum, values: Health.values() - field :hana_cluster_id, :string - field :database_id, :string + field :hana_cluster_id, Ecto.UUID + field :database_id, Ecto.UUID field :hosts_health, Ecto.Enum, values: [:passing, :critical, :unknown] end end From 3580e6e39ae1516a70752714e67e4cbdcd9fadf6 Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Fri, 7 Oct 2022 15:40:44 +0200 Subject: [PATCH 6/6] Refactored field extraction in Health Summary Service/DTO --- .../sap_systems/dto/health_summary_dto.ex | 2 +- .../sap_systems/health_summary_service.ex | 28 +++++++++---------- .../usecases/health_summary_service_test.exs | 2 +- .../health_overview_controller_test.exs | 2 +- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex index d8faecb7a3..1f2838b12e 100644 --- a/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex +++ b/lib/trento/application/usecases/sap_systems/dto/health_summary_dto.ex @@ -15,7 +15,7 @@ defmodule Trento.Application.UseCases.SapSystems.HealthSummaryDto do field :sapsystem_health, Ecto.Enum, values: Health.values() field :database_health, Ecto.Enum, values: Health.values() field :clusters_health, Ecto.Enum, values: Health.values() - field :hana_cluster_id, Ecto.UUID + field :cluster_id, Ecto.UUID field :database_id, Ecto.UUID field :hosts_health, Ecto.Enum, values: [:passing, :critical, :unknown] end diff --git a/lib/trento/application/usecases/sap_systems/health_summary_service.ex b/lib/trento/application/usecases/sap_systems/health_summary_service.ex index 8290fb3973..521ab46d7c 100644 --- a/lib/trento/application/usecases/sap_systems/health_summary_service.ex +++ b/lib/trento/application/usecases/sap_systems/health_summary_service.ex @@ -49,25 +49,11 @@ defmodule Trento.SapSystems.HealthSummaryService do database_health: compute_database_health(database_instances), clusters_health: compute_clusters_health(all_instances), hosts_health: compute_hosts_health(all_instances), - hana_cluster_id: extract_hana_cluster_id(database_instances), + cluster_id: extract_cluster_id(database_instances), database_id: extract_database_id(database_instances) }) end - @spec extract_database_id([DatabaseInstanceReadModel.t()]) :: String.t() - defp extract_database_id(database_instances) do - database_instances - |> Enum.map(fn %{sap_system_id: database_id} -> database_id end) - |> List.first() - end - - @spec extract_hana_cluster_id([DatabaseInstanceReadModel.t()]) :: String.t() - defp extract_hana_cluster_id(database_instances) do - database_instances - |> Enum.map(fn %{host: %{cluster_id: hana_cluster_id}} -> hana_cluster_id end) - |> List.first() - end - @spec compute_database_health([DatabaseInstanceReadModel.t()]) :: Health.t() defp compute_database_health(database_instances) do database_instances @@ -86,6 +72,18 @@ defmodule Trento.SapSystems.HealthSummaryService do |> HealthService.compute_aggregated_health() end + @spec extract_database_id([DatabaseInstanceReadModel.t()]) :: String.t() + defp extract_database_id([]), do: nil + + defp extract_database_id([%DatabaseInstanceReadModel{sap_system_id: sap_system_id} | _]), + do: sap_system_id + + @spec extract_cluster_id([DatabaseInstanceReadModel.t()]) :: String.t() + defp extract_cluster_id([]), do: nil + + defp extract_cluster_id([%DatabaseInstanceReadModel{host: %{cluster_id: cluster_id}} | _]), + do: cluster_id + @spec clusters_from_instance(instance_list) :: [ClusterReadModel.t()] defp clusters_from_instance(instances) do instances diff --git a/test/trento/application/usecases/health_summary_service_test.exs b/test/trento/application/usecases/health_summary_service_test.exs index 90835ae706..23f6ec6ba7 100644 --- a/test/trento/application/usecases/health_summary_service_test.exs +++ b/test/trento/application/usecases/health_summary_service_test.exs @@ -85,7 +85,7 @@ defmodule Trento.HealthSummaryServiceTest do clusters_health: :passing, hosts_health: :unknown, database_id: ^sap_system_id, - hana_cluster_id: ^cluster_id + cluster_id: ^cluster_id } ] = HealthSummaryService.get_health_summary() end diff --git a/test/trento_web/controllers/health_overview_controller_test.exs b/test/trento_web/controllers/health_overview_controller_test.exs index 363546a780..342cd78aaf 100644 --- a/test/trento_web/controllers/health_overview_controller_test.exs +++ b/test/trento_web/controllers/health_overview_controller_test.exs @@ -46,7 +46,7 @@ defmodule TrentoWeb.HealthOverviewControllerTest do "clusters_health" => "passing", "hosts_health" => "unknown", "database_id" => "#{sap_system_id}", - "hana_cluster_id" => "#{cluster_id}" + "cluster_id" => "#{cluster_id}" } ] == json_response(conn, 200) end