Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move database projections #2454

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/trento/databases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.Databases do

import Ecto.Query

alias Trento.SapSystems.Projections.{
alias Trento.Databases.Projections.{
DatabaseInstanceReadModel,
DatabaseReadModel
}
Expand Down Expand Up @@ -44,7 +44,7 @@ defmodule Trento.Databases do
date_service \\ DateService
) do
case Repo.get_by(DatabaseInstanceReadModel,
sap_system_id: database_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number
) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Trento.SapSystems.Projections.DatabaseInstanceReadModel do
defmodule Trento.Databases.Projections.DatabaseInstanceReadModel do
@moduledoc """
Database instance read model
"""
Expand All @@ -16,7 +16,7 @@ defmodule Trento.SapSystems.Projections.DatabaseInstanceReadModel do
@derive {Jason.Encoder, except: [:__meta__, :__struct__]}
@primary_key false
schema "database_instances" do
field :sap_system_id, Ecto.UUID, primary_key: true
field :database_id, Ecto.UUID, primary_key: true
field :sid, :string
field :tenant, :string
field :instance_number, :string, primary_key: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Trento.SapSystems.Projections.DatabaseProjector do
defmodule Trento.Databases.Projections.DatabaseProjector do
@moduledoc """
Database projector
"""
Expand All @@ -8,7 +8,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
repo: Trento.Repo,
name: "database_projector"

alias Trento.SapSystems.Projections.{
alias Trento.Databases.Projections.{
DatabaseInstanceReadModel,
DatabaseReadModel
}
Expand All @@ -33,11 +33,11 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
@databases_topic "monitoring:databases"

project(
%DatabaseRegistered{database_id: sap_system_id, sid: sid, health: health},
%DatabaseRegistered{database_id: database_id, sid: sid, health: health},
fn multi ->
changeset =
DatabaseReadModel.changeset(%DatabaseReadModel{}, %{
id: sap_system_id,
id: database_id,
sid: sid,
health: health
})
Expand All @@ -48,13 +48,13 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseHealthChanged{
database_id: sap_system_id,
database_id: database_id,
health: health
},
fn multi ->
changeset =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.get!(database_id)
|> DatabaseReadModel.changeset(%{health: health})

Ecto.Multi.update(multi, :database, changeset)
Expand All @@ -63,7 +63,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseInstanceRegistered{
database_id: sap_system_id,
database_id: database_id,
sid: sid,
instance_number: instance_number,
instance_hostname: instance_hostname,
Expand All @@ -80,7 +80,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
fn multi ->
database_instance_changeset =
DatabaseInstanceReadModel.changeset(%DatabaseInstanceReadModel{}, %{
sap_system_id: sap_system_id,
database_id: database_id,
sid: sid,
instance_number: instance_number,
instance_hostname: instance_hostname,
Expand All @@ -101,7 +101,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseInstanceHealthChanged{
database_id: sap_system_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
health: health
Expand All @@ -110,7 +110,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
changeset =
DatabaseInstanceReadModel
|> Repo.get_by(
sap_system_id: sap_system_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id
)
Expand All @@ -122,7 +122,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseInstanceSystemReplicationChanged{
database_id: sap_system_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
system_replication: system_replication,
Expand All @@ -132,7 +132,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
changeset =
DatabaseInstanceReadModel
|> Repo.get_by(
sap_system_id: sap_system_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id
)
Expand All @@ -149,14 +149,14 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceMarkedAbsent{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id,
database_id: database_id,
absent_at: absent_at
},
fn multi ->
changeset =
DatabaseInstanceReadModel
|> Repo.get_by(
sap_system_id: sap_system_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id
)
Expand All @@ -172,13 +172,13 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceMarkedPresent{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id
database_id: database_id
},
fn multi ->
changeset =
DatabaseInstanceReadModel
|> Repo.get_by(
sap_system_id: sap_system_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id
)
Expand All @@ -192,13 +192,13 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseDeregistered{
database_id: sap_system_id,
database_id: database_id,
deregistered_at: deregistered_at
},
fn multi ->
changeset =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.get!(database_id)
|> DatabaseReadModel.changeset(%{deregistered_at: deregistered_at})

Ecto.Multi.update(multi, :database, changeset)
Expand All @@ -207,13 +207,13 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

project(
%DatabaseRestored{
database_id: sap_system_id,
database_id: database_id,
health: health
},
fn multi ->
changeset =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.get!(database_id)
|> DatabaseReadModel.changeset(%{deregistered_at: nil, health: health})

Ecto.Multi.update(multi, :database, changeset)
Expand All @@ -224,12 +224,12 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceDeregistered{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id
database_id: database_id
},
fn multi ->
deregistered_instance =
Repo.get_by(DatabaseInstanceReadModel,
sap_system_id: sap_system_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id
)
Expand Down Expand Up @@ -273,12 +273,12 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
def after_update(
%DatabaseInstanceRegistered{},
_,
%{database_instance: %DatabaseInstanceReadModel{sap_system_id: sap_system_id} = instance}
%{database_instance: %DatabaseInstanceReadModel{database_id: database_id} = instance}
) do
# All database instances are required to compute the system replication status in the current instance
database_instances =
DatabaseInstanceReadModel
|> where([i], i.sap_system_id == ^sap_system_id)
|> where([i], i.database_id == ^database_id)
|> Repo.all()

TrentoWeb.Endpoint.broadcast(
Expand All @@ -300,7 +300,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
_,
%{
database_instance: %DatabaseInstanceReadModel{
sap_system_id: sap_system_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
health: health
Expand All @@ -312,7 +312,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
"database_instance_health_changed",
SapSystemView.render("database_instance_health_changed.json",
instance: %{
sap_system_id: sap_system_id,
sap_system_id: database_id,
host_id: host_id,
instance_number: instance_number,
health: health
Expand All @@ -327,7 +327,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
_,
%{
database_instance: %DatabaseInstanceReadModel{
sap_system_id: sap_system_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
system_replication: system_replication,
Expand All @@ -340,7 +340,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
"database_instance_system_replication_changed",
SapSystemView.render("database_instance_system_replication_changed.json",
instance: %{
sap_system_id: sap_system_id,
sap_system_id: database_id,
host_id: host_id,
instance_number: instance_number,
system_replication: system_replication,
Expand All @@ -355,7 +355,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceMarkedAbsent{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id,
database_id: database_id,
absent_at: absent_at
},
_,
Expand All @@ -368,7 +368,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
instance: %{
instance_number: instance_number,
host_id: host_id,
sap_system_id: sap_system_id,
sap_system_id: database_id,
sid: sid,
absent_at: absent_at
}
Expand All @@ -381,7 +381,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceMarkedPresent{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id
database_id: database_id
},
_,
%{database_instance: %DatabaseInstanceReadModel{sid: sid}}
Expand All @@ -393,7 +393,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
instance: %{
instance_number: instance_number,
host_id: host_id,
sap_system_id: sap_system_id,
sap_system_id: database_id,
sid: sid,
absent_at: nil
}
Expand All @@ -403,13 +403,13 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do

@impl true
def after_update(
%DatabaseRestored{database_id: sap_system_id},
%DatabaseRestored{database_id: database_id},
_,
_
) do
database =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.get!(database_id)
|> Repo.preload([:tags, :database_instances])

TrentoWeb.Endpoint.broadcast(
Expand All @@ -422,7 +422,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
@impl true
def after_update(
%DatabaseDeregistered{
database_id: sap_system_id
database_id: database_id
},
_,
%{
Expand All @@ -435,7 +435,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
@databases_topic,
"database_deregistered",
SapSystemView.render("database_deregistered.json",
id: sap_system_id,
id: database_id,
sid: sid
)
)
Expand All @@ -446,7 +446,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
%DatabaseInstanceDeregistered{
instance_number: instance_number,
host_id: host_id,
database_id: sap_system_id
database_id: database_id
},
_,
%{
Expand All @@ -459,7 +459,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do
@databases_topic,
"database_instance_deregistered",
SapSystemView.render("instance_deregistered.json",
sap_system_id: sap_system_id,
sap_system_id: database_id,
instance_number: instance_number,
host_id: host_id,
sid: sid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Trento.SapSystems.Projections.DatabaseReadModel do
defmodule Trento.Databases.Projections.DatabaseReadModel do
@moduledoc """
Database read model
"""
Expand All @@ -9,10 +9,8 @@ defmodule Trento.SapSystems.Projections.DatabaseReadModel do

require Trento.Enums.Health, as: Health

alias Trento.SapSystems.Projections.{
DatabaseInstanceReadModel,
SapSystemReadModel
}
alias Trento.Databases.Projections.DatabaseInstanceReadModel
alias Trento.SapSystems.Projections.SapSystemReadModel

alias Trento.Tags.Tag

Expand All @@ -30,7 +28,7 @@ defmodule Trento.SapSystems.Projections.DatabaseReadModel do

has_many :database_instances, DatabaseInstanceReadModel,
references: :id,
foreign_key: :sap_system_id,
foreign_key: :database_id,
preload_order: [asc: :instance_number, asc: :host_id]

field :deregistered_at, :utc_datetime_usec
Expand Down
6 changes: 4 additions & 2 deletions lib/trento/discovery/policies/sap_system_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ defmodule Trento.Discovery.Policies.SapSystemPolicy do
RegisterApplicationInstance
}

alias Trento.SapSystems.Projections.{ApplicationInstanceReadModel, DatabaseInstanceReadModel}
alias Trento.Databases.Projections.DatabaseInstanceReadModel

alias Trento.SapSystems.Projections.ApplicationInstanceReadModel

alias Trento.Discovery.Payloads.SapSystemDiscoveryPayload

Expand Down Expand Up @@ -170,7 +172,7 @@ defmodule Trento.Discovery.Policies.SapSystemPolicy do
MarkDatabaseInstanceAbsent.new!(%{
host_id: instance.host_id,
instance_number: instance.instance_number,
database_id: instance.sap_system_id,
database_id: instance.database_id,
absent_at: DateTime.utc_now()
})
end)
Expand Down
Loading
Loading