-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Chore: change Repo test port * Add commanded_ecto_projections to the mix * Add Host read model * Add and wire-up host projector * Add projectors subscription migration (auto-generated) * Add projectors test helper * Add host projector test * Add after update pub sub callback * Chore: add event_store.reset mix task
- Loading branch information
1 parent
ba7b1d1
commit 404365d
Showing
12 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Tronto.Monitoring.HostProjector do | ||
@moduledoc """ | ||
Host projector | ||
""" | ||
|
||
use Commanded.Projections.Ecto, | ||
application: Tronto.Commanded, | ||
repo: Tronto.Repo, | ||
name: "host_projector" | ||
|
||
alias Tronto.Monitoring.Domain.Events.HostRegistered | ||
alias Tronto.Monitoring.HostReadModel | ||
|
||
project( | ||
%HostRegistered{ | ||
id_host: id, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
}, | ||
fn multi -> | ||
changeset = | ||
%HostReadModel{} | ||
|> HostReadModel.changeset(%{ | ||
id: id, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
}) | ||
|
||
Ecto.Multi.insert(multi, :host, changeset) | ||
end | ||
) | ||
|
||
@impl true | ||
def after_update( | ||
%HostRegistered{ | ||
id_host: id, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
}, | ||
_, | ||
_ | ||
) do | ||
Phoenix.PubSub.broadcast( | ||
Tronto.PubSub, | ||
"hosts", | ||
{:host_registered, | ||
%{ | ||
id: id, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
}} | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Tronto.Monitoring.ProjectorsSupervisor do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
def start_link(init_arg) do | ||
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init(_init_arg) do | ||
children = [ | ||
Tronto.Monitoring.HostProjector | ||
] | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Tronto.Monitoring.HostReadModel do | ||
@moduledoc """ | ||
Host read model | ||
""" | ||
|
||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
@derive {Jason.Encoder, except: [:__meta__, :__struct__]} | ||
@primary_key {:id, :binary_id, autogenerate: false} | ||
schema "hosts" do | ||
field :hostname, :string | ||
field :ip_addresses, {:array, :string} | ||
field :agent_version, :string | ||
end | ||
|
||
@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t() | ||
def changeset(host, attrs) do | ||
cast(host, attrs, __MODULE__.__schema__(:fields)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Tronto.Repo.Migrations.CreateHosts do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:hosts, primary_key: false) do | ||
add :id, :uuid, primary_key: true | ||
add :hostname, :string | ||
add :ip_addresses, {:array, :string} | ||
add :agent_version, :string | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
priv/repo/migrations/20211228125531_create_projection_versions.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Tronto.Repo.Migrations.CreateProjectionVersions do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:projection_versions, primary_key: false) do | ||
add(:projection_name, :text, primary_key: true) | ||
add(:last_seen_event_number, :bigint) | ||
|
||
timestamps(type: :naive_datetime_usec) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Tronto.ProjectorTestHelper do | ||
@moduledoc """ | ||
This module contains helper functions for testing projectors | ||
""" | ||
|
||
def project(projector, event, projection_name) do | ||
:ok = | ||
projector.handle(event, %{ | ||
event_number: next_event_number(projector, projection_name), | ||
handler_name: projection_name | ||
}) | ||
end | ||
|
||
defp next_event_number(projector, projection_name), | ||
do: last_seen_event_number(projector, projection_name) + 1 | ||
|
||
defp last_seen_event_number(projector, projection_name) do | ||
projector | ||
|> Module.concat(ProjectionVersion) | ||
|> Tronto.Repo.get(projection_name) | ||
|> case do | ||
nil -> | ||
0 | ||
|
||
projection_version -> | ||
Map.get(projection_version, :last_seen_event_number) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Tronto.Monitoring.HostProjectorTest do | ||
use ExUnit.Case | ||
use Tronto.DataCase | ||
|
||
alias Tronto.Monitoring.{ | ||
HostProjector, | ||
HostReadModel | ||
} | ||
|
||
alias Tronto.Monitoring.Domain.Events.HostRegistered | ||
|
||
alias Tronto.ProjectorTestHelper | ||
alias Tronto.Repo | ||
|
||
@moduletag :integration | ||
|
||
test "should project a new host when HostRegistered event is received" do | ||
event = %HostRegistered{ | ||
id_host: Faker.UUID.v4(), | ||
hostname: Faker.StarWars.character(), | ||
ip_addresses: [Faker.Internet.ip_v4_address()], | ||
agent_version: Faker.StarWars.planet() | ||
} | ||
|
||
ProjectorTestHelper.project(HostProjector, event, "host_projector") | ||
host_projection = Repo.get!(HostReadModel, event.id_host) | ||
|
||
assert event.id_host == host_projection.id | ||
assert event.hostname == host_projection.hostname | ||
assert event.ip_addresses == host_projection.ip_addresses | ||
assert event.agent_version == host_projection.agent_version | ||
end | ||
end |