From be9072326e41526ed7709fca494976fdeb9a8e08 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Tue, 2 Jul 2024 18:13:08 +0200 Subject: [PATCH 01/13] Implements context function --- lib/trento/activity_log.ex | 9 +++++++-- lib/trento/activity_logging/activity_log.ex | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/trento/activity_log.ex b/lib/trento/activity_log.ex index ac3927dbf1..2527e7f98f 100644 --- a/lib/trento/activity_log.ex +++ b/lib/trento/activity_log.ex @@ -6,9 +6,8 @@ defmodule Trento.ActivityLog do require Logger require Trento.ActivityLog.RetentionPeriodUnit, as: RetentionPeriodUnit - + alias Trento.ActivityLog.ActivityLog alias Trento.ActivityLog.Settings - alias Trento.Repo @spec get_settings() :: @@ -38,6 +37,12 @@ defmodule Trento.ActivityLog do end end + @spec list_activity_log() :: list(ActivityLog) + def list_activity_log() do + # This will be made filterable/paginatable in a later PR + Repo.all(ActivityLog) + end + defp log_error({:error, _} = error, message) do Logger.error("#{message}: #{inspect(error)}") error diff --git a/lib/trento/activity_logging/activity_log.ex b/lib/trento/activity_logging/activity_log.ex index 054fd887be..7d34782940 100644 --- a/lib/trento/activity_logging/activity_log.ex +++ b/lib/trento/activity_logging/activity_log.ex @@ -5,6 +5,8 @@ defmodule Trento.ActivityLog.ActivityLog do use Ecto.Schema import Ecto.Changeset + @type t() :: %__MODULE__{} + @primary_key {:id, :binary_id, autogenerate: true} schema "activity_logs" do field :type, :string From edde408c6afa97cb651e38074b3aa86137669282 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Tue, 2 Jul 2024 18:15:16 +0200 Subject: [PATCH 02/13] Implements the view --- lib/trento_web/views/v1/activity_log_entry_view.ex | 12 ++++++++++++ lib/trento_web/views/v1/activity_log_view.ex | 7 +++++++ 2 files changed, 19 insertions(+) create mode 100644 lib/trento_web/views/v1/activity_log_entry_view.ex create mode 100644 lib/trento_web/views/v1/activity_log_view.ex diff --git a/lib/trento_web/views/v1/activity_log_entry_view.ex b/lib/trento_web/views/v1/activity_log_entry_view.ex new file mode 100644 index 0000000000..0714b33110 --- /dev/null +++ b/lib/trento_web/views/v1/activity_log_entry_view.ex @@ -0,0 +1,12 @@ +defmodule TrentoWeb.V1.ActivityLogEntryView do + use TrentoWeb, :view + + def render("activity_log_entry.json", %{activity_log_entry: entry}) do + %{ + type: entry.type, + actor: entry.type, + metadata: entry.metadata, + inserted_at: entry.inserted_at + } + end +end diff --git a/lib/trento_web/views/v1/activity_log_view.ex b/lib/trento_web/views/v1/activity_log_view.ex new file mode 100644 index 0000000000..9c9c19b301 --- /dev/null +++ b/lib/trento_web/views/v1/activity_log_view.ex @@ -0,0 +1,7 @@ +defmodule TrentoWeb.V1.ActivityLogView do + use TrentoWeb, :view + + def render("activity_log.json", %{activity_log: entries}) do + render_many(entries, TrentoWeb.V1.ActivityLogEntryView, "activity_log_entry.json") + end +end From bc0b5a5234c91cb98bb94830b7cf7ee8505d67ca Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Tue, 2 Jul 2024 18:15:45 +0200 Subject: [PATCH 03/13] Implements the controller --- .../controllers/v1/activity_log_controller.ex | 25 ++++++++++++++ .../openapi/v1/schema/activity_log.ex | 34 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/trento_web/controllers/v1/activity_log_controller.ex create mode 100644 lib/trento_web/openapi/v1/schema/activity_log.ex diff --git a/lib/trento_web/controllers/v1/activity_log_controller.ex b/lib/trento_web/controllers/v1/activity_log_controller.ex new file mode 100644 index 0000000000..7c7c7679ca --- /dev/null +++ b/lib/trento_web/controllers/v1/activity_log_controller.ex @@ -0,0 +1,25 @@ +defmodule TrentoWeb.V1.ActivityLogController do + use TrentoWeb, :controller + use OpenApiSpex.ControllerSpecs + + alias Trento.ActivityLog + alias TrentoWeb.OpenApi.V1.Schema + + plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true + action_fallback TrentoWeb.FallbackController + + operation :get_activity_log, + summary: "Fetches the Activity Log entries.", + tags: ["Activity Logging"], + responses: [ + ok: {"Activity Log settings fetched successfully", "application/json", Schema.ActivityLog} + ] + + def get_activity_log(conn, _) do + with activity_log_entries <- ActivityLog.list_activity_log() do + render(conn, "activity_log.json", %{ + activity_log: activity_log_entries + }) + end + end +end diff --git a/lib/trento_web/openapi/v1/schema/activity_log.ex b/lib/trento_web/openapi/v1/schema/activity_log.ex new file mode 100644 index 0000000000..b9c593d31f --- /dev/null +++ b/lib/trento_web/openapi/v1/schema/activity_log.ex @@ -0,0 +1,34 @@ +defmodule TrentoWeb.OpenApi.V1.Schema.ActivityLog do + @moduledoc false + + require OpenApiSpex + alias OpenApiSpex.Schema + + OpenApiSpex.schema(%{ + title: "ActivityLog", + description: "Activity Log for the current installation.", + type: :array, + items: %Schema{ + type: :object, + additionalProperties: false, + properties: %{ + type: %Schema{ + type: :string, + description: "Type of Activity Log entry." + }, + actor: %Schema{ + type: :string, + description: "Actor causing an Activity Log entry. E.g. System or a specific user." + }, + metadata: %Schema{ + type: :object + }, + inserted_at: %Schema{ + type: :string, + description: "Timestamp upon Activity Log entry insertion." + } + }, + required: [:type, :actor, :metadata, :inserted_at] + } + }) +end From 2c5b002b2d254ebabb212f009ecf3a7660da261b Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Tue, 2 Jul 2024 18:17:04 +0200 Subject: [PATCH 04/13] Adds an entry to the router --- lib/trento_web/router.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/trento_web/router.ex b/lib/trento_web/router.ex index 9ce485eb6c..874829a874 100644 --- a/lib/trento_web/router.ex +++ b/lib/trento_web/router.ex @@ -88,6 +88,8 @@ defmodule TrentoWeb.Router do get "/about", AboutController, :info + get "/activity_log", ActivityLogController, :get_activity_log + get "/installation/api-key", InstallationController, :get_api_key get "/hosts", HostController, :list From e25edddf8388e94f26355abf6bda22c13ac82b6e Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Tue, 2 Jul 2024 18:19:02 +0200 Subject: [PATCH 05/13] Adds tests --- test/support/factory.ex | 9 +++++ .../v1/activity_log_controller_test.exs | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/trento_web/controllers/v1/activity_log_controller_test.exs diff --git a/test/support/factory.ex b/test/support/factory.ex index 9528e42f5d..14d0b04ec7 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -127,6 +127,7 @@ defmodule Trento.Factory do alias Trento.ActivityLog.RetentionTime alias Trento.ActivityLog.Settings, as: ActivityLogSettings + alias Trento.ActivityLog.ActivityLog, as: ActivityLogEntry alias Trento.Abilities.{ Ability, @@ -1025,6 +1026,14 @@ defmodule Trento.Factory do } end + def activity_log_entry_factory do + %ActivityLogEntry{ + type: Faker.Pokemon.name(), + actor: Enum.random(["user", "system"]), + metadata: %{} + } + end + def user_factory do password = Faker.Pokemon.name() diff --git a/test/trento_web/controllers/v1/activity_log_controller_test.exs b/test/trento_web/controllers/v1/activity_log_controller_test.exs new file mode 100644 index 0000000000..2388b5f96c --- /dev/null +++ b/test/trento_web/controllers/v1/activity_log_controller_test.exs @@ -0,0 +1,36 @@ +defmodule TrentoWeb.V1.ActivityLogControllerTest do + use TrentoWeb.ConnCase, async: true + + import Trento.Factory + import OpenApiSpex.TestAssertions + + alias TrentoWeb.OpenApi.V1.ApiSpec + + describe "ActivityLogController" do + setup do + %{api_spec: ApiSpec.spec()} + end + + test "should return activity retention settings after setting up", %{ + conn: conn, + api_spec: api_spec + } do + insert(:activity_log_entry) + + conn + |> get("/api/v1/activity_log") + |> json_response(200) + |> assert_schema("ActivityLog", api_spec) + end + + test "should return valid response (empty list) if no activity logs entries exist", %{ + conn: conn, + api_spec: api_spec + } do + conn + |> get("/api/v1/activity_log") + |> json_response(200) + |> assert_schema("ActivityLog", api_spec) + end + end +end From 5ceac11ed13e8da5165433693f0d82ab217ab3b7 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Wed, 3 Jul 2024 13:31:46 +0200 Subject: [PATCH 06/13] Fixes field value in view --- lib/trento_web/views/v1/activity_log_entry_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trento_web/views/v1/activity_log_entry_view.ex b/lib/trento_web/views/v1/activity_log_entry_view.ex index 0714b33110..494ecf829b 100644 --- a/lib/trento_web/views/v1/activity_log_entry_view.ex +++ b/lib/trento_web/views/v1/activity_log_entry_view.ex @@ -4,7 +4,7 @@ defmodule TrentoWeb.V1.ActivityLogEntryView do def render("activity_log_entry.json", %{activity_log_entry: entry}) do %{ type: entry.type, - actor: entry.type, + actor: entry.actor, metadata: entry.metadata, inserted_at: entry.inserted_at } From dfd573cb1a052e99d2fd63d2c220c18b299add0c Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Wed, 3 Jul 2024 14:25:02 +0200 Subject: [PATCH 07/13] Fixes type signature --- lib/trento/activity_log.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trento/activity_log.ex b/lib/trento/activity_log.ex index 2527e7f98f..93eb3fac77 100644 --- a/lib/trento/activity_log.ex +++ b/lib/trento/activity_log.ex @@ -37,7 +37,7 @@ defmodule Trento.ActivityLog do end end - @spec list_activity_log() :: list(ActivityLog) + @spec list_activity_log() :: list(ActivityLog.t()) def list_activity_log() do # This will be made filterable/paginatable in a later PR Repo.all(ActivityLog) From 22130d56aaee4b65660bfdd4d6f2d5100826a126 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Wed, 3 Jul 2024 14:40:48 +0200 Subject: [PATCH 08/13] Make credo happy Small fixes about parentheses of zero arity functions and ordering of aliases in alphabetical order. --- lib/trento/activity_log.ex | 2 +- test/support/factory.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trento/activity_log.ex b/lib/trento/activity_log.ex index 93eb3fac77..fa017aa3f3 100644 --- a/lib/trento/activity_log.ex +++ b/lib/trento/activity_log.ex @@ -38,7 +38,7 @@ defmodule Trento.ActivityLog do end @spec list_activity_log() :: list(ActivityLog.t()) - def list_activity_log() do + def list_activity_log do # This will be made filterable/paginatable in a later PR Repo.all(ActivityLog) end diff --git a/test/support/factory.ex b/test/support/factory.ex index 14d0b04ec7..d52dd47f7e 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -125,9 +125,9 @@ defmodule Trento.Factory do InstallationSettings } + alias Trento.ActivityLog.ActivityLog, as: ActivityLogEntry alias Trento.ActivityLog.RetentionTime alias Trento.ActivityLog.Settings, as: ActivityLogSettings - alias Trento.ActivityLog.ActivityLog, as: ActivityLogEntry alias Trento.Abilities.{ Ability, From 3fc42f98b514addd2bf957737bd401d37b25c82a Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Wed, 3 Jul 2024 16:21:21 +0200 Subject: [PATCH 09/13] Fixes tests; renames openapi spec tags --- .../controllers/v1/activity_log_controller.ex | 2 +- .../v1/activity_log_controller_test.exs | 25 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/trento_web/controllers/v1/activity_log_controller.ex b/lib/trento_web/controllers/v1/activity_log_controller.ex index 7c7c7679ca..0725512690 100644 --- a/lib/trento_web/controllers/v1/activity_log_controller.ex +++ b/lib/trento_web/controllers/v1/activity_log_controller.ex @@ -10,7 +10,7 @@ defmodule TrentoWeb.V1.ActivityLogController do operation :get_activity_log, summary: "Fetches the Activity Log entries.", - tags: ["Activity Logging"], + tags: ["Platform"], responses: [ ok: {"Activity Log settings fetched successfully", "application/json", Schema.ActivityLog} ] diff --git a/test/trento_web/controllers/v1/activity_log_controller_test.exs b/test/trento_web/controllers/v1/activity_log_controller_test.exs index 2388b5f96c..9672edbd84 100644 --- a/test/trento_web/controllers/v1/activity_log_controller_test.exs +++ b/test/trento_web/controllers/v1/activity_log_controller_test.exs @@ -11,26 +11,33 @@ defmodule TrentoWeb.V1.ActivityLogControllerTest do %{api_spec: ApiSpec.spec()} end - test "should return activity retention settings after setting up", %{ + test "should return activity logs after inserting a few entries.", %{ conn: conn, api_spec: api_spec } do insert(:activity_log_entry) + insert(:activity_log_entry) + + resp = + conn + |> get("/api/v1/activity_log") + |> json_response(200) - conn - |> get("/api/v1/activity_log") - |> json_response(200) - |> assert_schema("ActivityLog", api_spec) + assert length(resp) == 2 + assert_schema(resp, "ActivityLog", api_spec) end test "should return valid response (empty list) if no activity logs entries exist", %{ conn: conn, api_spec: api_spec } do - conn - |> get("/api/v1/activity_log") - |> json_response(200) - |> assert_schema("ActivityLog", api_spec) + resp = + conn + |> get("/api/v1/activity_log") + |> json_response(200) + + assert resp == [] + assert_schema(resp, "ActivityLog", api_spec) end end end From b8f903b3b4d9be90407b0d3b0e197a83e96c6926 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Thu, 4 Jul 2024 11:51:16 +0200 Subject: [PATCH 10/13] Addresses PR comments - Single view file, instead of two - Adds title field in spec --- lib/trento_web/openapi/v1/schema/activity_log.ex | 1 + lib/trento_web/views/v1/activity_log_entry_view.ex | 12 ------------ lib/trento_web/views/v1/activity_log_view.ex | 11 ++++++++++- 3 files changed, 11 insertions(+), 13 deletions(-) delete mode 100644 lib/trento_web/views/v1/activity_log_entry_view.ex diff --git a/lib/trento_web/openapi/v1/schema/activity_log.ex b/lib/trento_web/openapi/v1/schema/activity_log.ex index b9c593d31f..06b566538e 100644 --- a/lib/trento_web/openapi/v1/schema/activity_log.ex +++ b/lib/trento_web/openapi/v1/schema/activity_log.ex @@ -9,6 +9,7 @@ defmodule TrentoWeb.OpenApi.V1.Schema.ActivityLog do description: "Activity Log for the current installation.", type: :array, items: %Schema{ + title: "ActivityLogEntry", type: :object, additionalProperties: false, properties: %{ diff --git a/lib/trento_web/views/v1/activity_log_entry_view.ex b/lib/trento_web/views/v1/activity_log_entry_view.ex deleted file mode 100644 index 494ecf829b..0000000000 --- a/lib/trento_web/views/v1/activity_log_entry_view.ex +++ /dev/null @@ -1,12 +0,0 @@ -defmodule TrentoWeb.V1.ActivityLogEntryView do - use TrentoWeb, :view - - def render("activity_log_entry.json", %{activity_log_entry: entry}) do - %{ - type: entry.type, - actor: entry.actor, - metadata: entry.metadata, - inserted_at: entry.inserted_at - } - end -end diff --git a/lib/trento_web/views/v1/activity_log_view.ex b/lib/trento_web/views/v1/activity_log_view.ex index 9c9c19b301..e4c5eccd2b 100644 --- a/lib/trento_web/views/v1/activity_log_view.ex +++ b/lib/trento_web/views/v1/activity_log_view.ex @@ -2,6 +2,15 @@ defmodule TrentoWeb.V1.ActivityLogView do use TrentoWeb, :view def render("activity_log.json", %{activity_log: entries}) do - render_many(entries, TrentoWeb.V1.ActivityLogEntryView, "activity_log_entry.json") + render_many(entries, __MODULE__, "activity_log_entry.json", as: :entries) + end + + def render("activity_log_entry.json", %{entries: entry}) do + %{ + type: entry.type, + actor: entry.actor, + metadata: entry.metadata, + inserted_at: entry.inserted_at + } end end From 9acb3bbc917cfa0500f38c40f2294cdaa8693101 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Thu, 4 Jul 2024 11:58:57 +0200 Subject: [PATCH 11/13] Renames field in schema According to PR comment --- lib/trento_web/openapi/v1/schema/activity_log.ex | 4 ++-- lib/trento_web/views/v1/activity_log_view.ex | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/trento_web/openapi/v1/schema/activity_log.ex b/lib/trento_web/openapi/v1/schema/activity_log.ex index 06b566538e..fb7515df52 100644 --- a/lib/trento_web/openapi/v1/schema/activity_log.ex +++ b/lib/trento_web/openapi/v1/schema/activity_log.ex @@ -24,12 +24,12 @@ defmodule TrentoWeb.OpenApi.V1.Schema.ActivityLog do metadata: %Schema{ type: :object }, - inserted_at: %Schema{ + occured_on: %Schema{ type: :string, description: "Timestamp upon Activity Log entry insertion." } }, - required: [:type, :actor, :metadata, :inserted_at] + required: [:type, :actor, :metadata, :occured_on] } }) end diff --git a/lib/trento_web/views/v1/activity_log_view.ex b/lib/trento_web/views/v1/activity_log_view.ex index e4c5eccd2b..774793209f 100644 --- a/lib/trento_web/views/v1/activity_log_view.ex +++ b/lib/trento_web/views/v1/activity_log_view.ex @@ -10,7 +10,8 @@ defmodule TrentoWeb.V1.ActivityLogView do type: entry.type, actor: entry.actor, metadata: entry.metadata, - inserted_at: entry.inserted_at + # Time of occurence approximated by time of insertion in DB. + occured_on: entry.inserted_at } end end From 87b7260ccc91f8730b58e5f29bdba6b1971cae33 Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Thu, 4 Jul 2024 12:09:18 +0200 Subject: [PATCH 12/13] Fixes spelling --- lib/trento_web/views/v1/activity_log_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trento_web/views/v1/activity_log_view.ex b/lib/trento_web/views/v1/activity_log_view.ex index 774793209f..b7f6799ec6 100644 --- a/lib/trento_web/views/v1/activity_log_view.ex +++ b/lib/trento_web/views/v1/activity_log_view.ex @@ -10,7 +10,7 @@ defmodule TrentoWeb.V1.ActivityLogView do type: entry.type, actor: entry.actor, metadata: entry.metadata, - # Time of occurence approximated by time of insertion in DB. + # Time of occurrence approximated by time of insertion in DB. occured_on: entry.inserted_at } end From 2b44a07475d2440dc2dd4f67750315a933e9ec2c Mon Sep 17 00:00:00 2001 From: Gagandeep Bhatia Date: Thu, 4 Jul 2024 12:12:41 +0200 Subject: [PATCH 13/13] Fixes more spellings --- lib/trento_web/openapi/v1/schema/activity_log.ex | 4 ++-- lib/trento_web/views/v1/activity_log_view.ex | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/trento_web/openapi/v1/schema/activity_log.ex b/lib/trento_web/openapi/v1/schema/activity_log.ex index fb7515df52..5e44160a31 100644 --- a/lib/trento_web/openapi/v1/schema/activity_log.ex +++ b/lib/trento_web/openapi/v1/schema/activity_log.ex @@ -24,12 +24,12 @@ defmodule TrentoWeb.OpenApi.V1.Schema.ActivityLog do metadata: %Schema{ type: :object }, - occured_on: %Schema{ + occurred_on: %Schema{ type: :string, description: "Timestamp upon Activity Log entry insertion." } }, - required: [:type, :actor, :metadata, :occured_on] + required: [:type, :actor, :metadata, :occurred_on] } }) end diff --git a/lib/trento_web/views/v1/activity_log_view.ex b/lib/trento_web/views/v1/activity_log_view.ex index b7f6799ec6..63aff847c3 100644 --- a/lib/trento_web/views/v1/activity_log_view.ex +++ b/lib/trento_web/views/v1/activity_log_view.ex @@ -11,7 +11,7 @@ defmodule TrentoWeb.V1.ActivityLogView do actor: entry.actor, metadata: entry.metadata, # Time of occurrence approximated by time of insertion in DB. - occured_on: entry.inserted_at + occurred_on: entry.inserted_at } end end