From 56ffd5c1df70b9b9325a8c629deb018f33c26433 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Mon, 8 Jul 2024 16:27:00 +0200 Subject: [PATCH 1/2] Add activity log default ordering by occurrence date --- lib/trento/activity_log.ex | 8 +++++++- test/trento/activity_log_test.exs | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/trento/activity_log.ex b/lib/trento/activity_log.ex index fa017aa3f3..89bf4a16f2 100644 --- a/lib/trento/activity_log.ex +++ b/lib/trento/activity_log.ex @@ -3,6 +3,8 @@ defmodule Trento.ActivityLog do Activity Log module provides functionality to manage activity log settings and track activity. """ + import Ecto.Query + require Logger require Trento.ActivityLog.RetentionPeriodUnit, as: RetentionPeriodUnit @@ -40,7 +42,11 @@ defmodule Trento.ActivityLog do @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) + query = + from activity in ActivityLog, + order_by: [desc: activity.inserted_at] + + Repo.all(query) end defp log_error({:error, _} = error, message) do diff --git a/test/trento/activity_log_test.exs b/test/trento/activity_log_test.exs index ccbdfb9d9b..b3c8fdf2d7 100644 --- a/test/trento/activity_log_test.exs +++ b/test/trento/activity_log_test.exs @@ -10,6 +10,7 @@ defmodule Trento.ActivityLogTest do require Trento.ActivityLog.RetentionPeriodUnit, as: RetentionPeriodUnit alias Trento.ActivityLog + alias Trento.ActivityLog.ActivityLog, as: ActivityLogEntry alias Trento.ActivityLog.RetentionTime alias Trento.ActivityLog.Settings @@ -177,4 +178,27 @@ defmodule Trento.ActivityLogTest do ) end end + + describe "retrieving logged activity" do + test "should return an emtpty list" do + assert [] == ActivityLog.list_activity_log() + end + + test "should return entries ordered by occurrence date" do + older_occurrence = Faker.DateTime.backward(1) + newer_occurrence = Faker.DateTime.forward(2) + + insert(:activity_log_entry, inserted_at: newer_occurrence) + insert(:activity_log_entry, inserted_at: older_occurrence) + + assert [ + %ActivityLogEntry{ + inserted_at: ^newer_occurrence + }, + %ActivityLogEntry{ + inserted_at: ^older_occurrence + } + ] = ActivityLog.list_activity_log() + end + end end From 536061eaba1593e1f02303d0218ab1a69a8a1341 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Wed, 10 Jul 2024 08:33:16 +0200 Subject: [PATCH 2/2] Add index on inserted at for ActivityLog --- .../migrations/20240710062924_add_activity_log_intex.exs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 priv/repo/migrations/20240710062924_add_activity_log_intex.exs diff --git a/priv/repo/migrations/20240710062924_add_activity_log_intex.exs b/priv/repo/migrations/20240710062924_add_activity_log_intex.exs new file mode 100644 index 0000000000..0055a9a659 --- /dev/null +++ b/priv/repo/migrations/20240710062924_add_activity_log_intex.exs @@ -0,0 +1,7 @@ +defmodule Trento.Repo.Migrations.AddActivityLogIntex do + use Ecto.Migration + + def change do + create index(:activity_logs, [:inserted_at]) + end +end