diff --git a/lib/lattice_observer/observed/event_processor.ex b/lib/lattice_observer/observed/event_processor.ex index 0d1990f..6937a25 100644 --- a/lib/lattice_observer/observed/event_processor.ex +++ b/lib/lattice_observer/observed/event_processor.ex @@ -225,26 +225,6 @@ defmodule LatticeObserver.Observed.EventProcessor do } end - # New heartbeat format - # "actors": { - # "MB2ZQB6ROOMAYBO4ZCTFYWN7YIVBWA3MTKZYAQKJMTIHE2ELLRW2E3ZW": 10 - # }, - # "friendly_name": "wandering-meadow-5880", - # "labels": { - # "hostcore.arch": "aarch64", - # "hostcore.os": "macos", - # "hostcore.osfamily": "unix" - # }, - # "providers": [ - # { - # "link_name": "default", - # "public_key": "VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M" - # } - # ], - # "uptime_human": "1 minute, 32 seconds", - # "uptime_seconds": 92, - # "version": "0.60.0" - def record_heartbeat(l = %Lattice{}, source_host, stamp, data) do labels = Map.get(data, "labels", %{}) friendly_name = Map.get(data, "friendly_name", "") @@ -282,11 +262,10 @@ defmodule LatticeObserver.Observed.EventProcessor do l = record_host(l, source_host, labels, stamp, friendly_name, uptime_seconds, version) - # legacy heartbeat has a list for the actors field... - # default to "new format" if this field is missing + # new heartbeat has a list for the actors field with more information l = if is_list(Map.get(data, "actors", %{})) do - put_legacy_instances(l, source_host, stamp, data) + put_v82_instances(l, source_host, stamp, data) else actors_expanded = Enum.flat_map(Map.get(data, "actors", %{}), fn {public_key, count} -> @@ -324,25 +303,31 @@ defmodule LatticeObserver.Observed.EventProcessor do l end - defp put_legacy_instances(l = %Lattice{}, source_host, stamp, data) do + defp put_v82_instances(l = %Lattice{}, source_host, stamp, data) do l = - List.foldl(Map.get(data, "actors", []), l, fn x, acc -> - put_actor_instance( - acc, - source_host, - x["public_key"], - Map.get(x, "instance_id", "n/a"), - Map.get(x, "annotations", %{}), - stamp, - %{} - ) + List.foldl(Map.get(data, "actors", []), l, fn x, all_actors -> + id = x["id"] + + # Iterate over the instances and, using the annotations and scale, create + # a list of instances to add to the actor. + x["instances"] + |> Enum.reduce(all_actors, fn instance, actors -> + set_actor_instances( + actors, + source_host, + id, + Map.get(instance, "annotations", %{}), + %{}, + Map.get(instance, "max_instances", 1) + ) + end) end) List.foldl(Map.get(data, "providers", []), l, fn x, acc -> put_provider_instance( acc, source_host, - x["public_key"], + x["id"], x["link_name"], x["contract_id"], Map.get(x, "instance_id", "n/a"), diff --git a/test/observed/hosts_test.exs b/test/observed/hosts_test.exs index 4c1e01b..c17ce94 100644 --- a/test/observed/hosts_test.exs +++ b/test/observed/hosts_test.exs @@ -113,28 +113,8 @@ defmodule LatticeObserverTest.Observed.HostsTest do Lattice.new() end - # updated heartbeat shape - # "actors": { - # "MB2ZQB6ROOMAYBO4ZCTFYWN7YIVBWA3MTKZYAQKJMTIHE2ELLRW2E3ZW": 10 - # }, - # "friendly_name": "wandering-meadow-5880", - # "labels": { - # "hostcore.arch": "aarch64", - # "hostcore.os": "macos", - # "hostcore.osfamily": "unix" - # }, - # "providers": [ - # { - # "link_name": "default", - # "public_key": "VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M" - # } - # ], - # "uptime_human": "1 minute, 32 seconds", - # "uptime_seconds": 92, - # "version": "0.60.0" - - test "Propertly records LEGACY host heartbeat" do - hb = CloudEvents.host_heartbeat_old(@test_host, %{foo: "bar", baz: "biz"}) + test "Propertly records NEW host heartbeat" do + hb = CloudEvents.host_heartbeat_new(@test_host, %{foo: "bar", baz: "biz"}) stamp = EventProcessor.timestamp_from_iso8601(hb.time) l = Lattice.apply_event(Lattice.new(), hb) @@ -142,7 +122,7 @@ defmodule LatticeObserverTest.Observed.HostsTest do assert l.hosts[@test_host].status == :healthy assert l.hosts[@test_host].last_seen == stamp - hb2 = CloudEvents.host_heartbeat_old(@test_host, %{foo: "bar", baz: "biz"}) + hb2 = CloudEvents.host_heartbeat_new(@test_host, %{foo: "bar", baz: "biz"}) stamp2 = EventProcessor.timestamp_from_iso8601(hb2.time) l = Lattice.apply_event(l, hb2) @@ -156,17 +136,37 @@ defmodule LatticeObserverTest.Observed.HostsTest do %{foo: "bar"}, [ %{ - "public_key" => "Mxxxx", - "instance_id" => "iid1" + "id" => "Mxxxx", + "name" => "Test Actor", + "image_ref" => "wasmcloud.azurecr.io/actor:0.1.0", + "instances" => [ + %{ + "annotations" => %{"foo" => "bar"}, + "image_ref" => "wasmcloud.azurecr.io/actor:0.1.0", + "instance_id" => "iid1", + "revision" => 0, + "max_instances" => 1 + } + ] }, %{ - "public_key" => "Mxxxy", - "instance_id" => "iid2" + "id" => "Mxxxy", + "name" => "Test Actor Deux", + "image_ref" => "wasmcloud.azurecr.io/actortwo:0.1.0", + "instances" => [ + %{ + "annotations" => %{"bar" => "baz"}, + "image_ref" => "wasmcloud.azurecr.io/actortwo:0.1.0", + "instance_id" => "iid2", + "revision" => 0, + "max_instances" => 1 + } + ] } ], [ %{ - "public_key" => "Vxxxxx", + "id" => "Vxxxxx", "instance_id" => "iid3", "contract_id" => "wasmcloud:test", "link_name" => "default" @@ -182,16 +182,29 @@ defmodule LatticeObserverTest.Observed.HostsTest do %{foo: "bar"}, [ %{ - "public_key" => "Mxxxx", - "instance_id" => "iid1" + "id" => "Mxxxx", + "name" => "Test Actor", + "image_ref" => "wasmcloud.azurecr.io/actor:0.1.0", + "instances" => [ + %{ + "annotations" => %{"foo" => "bar"}, + "image_ref" => "wasmcloud.azurecr.io/actor:0.1.0", + "instance_id" => "iid1", + "revision" => 0, + "max_instances" => 1 + } + ] } ], [ %{ - "public_key" => "Vxxxxx", - "instance_id" => "iid3", + "id" => "Vxxxxx", "contract_id" => "wasmcloud:test", - "link_name" => "default" + "link_name" => "default", + "image_ref" => "wasmcloud.azurecr.io/provider:0.1.0", + "revision" => 0, + "annotations" => %{"foo" => "bar"}, + "name" => "Test Provider" } ] ) @@ -207,9 +220,9 @@ defmodule LatticeObserverTest.Observed.HostsTest do instances: [ %LatticeObserver.Observed.Instance{ host_id: "Nxxx", - id: "iid1", + id: "N/A", revision: 0, - annotations: %{}, + annotations: %{"foo" => "bar"}, version: "" } ], @@ -226,9 +239,9 @@ defmodule LatticeObserverTest.Observed.HostsTest do instances: [ %LatticeObserver.Observed.Instance{ host_id: "Nxxx", - id: "iid3", + id: "n/a", revision: 0, - annotations: %{}, + annotations: %{"foo" => "bar"}, version: "" } ], diff --git a/test/support/cloud_events.ex b/test/support/cloud_events.ex index 06061a0..6461370 100644 --- a/test/support/cloud_events.ex +++ b/test/support/cloud_events.ex @@ -172,7 +172,7 @@ defmodule TestSupport.CloudEvents do |> LatticeObserver.CloudEvent.new("invocation_failed", host) end - def host_heartbeat_old(host, labels, actors \\ [], providers \\ []) do + def host_heartbeat_new(host, labels, actors \\ [], providers \\ []) do %{ "actors" => actors, "providers" => providers,