Skip to content

Commit

Permalink
Add activity log default ordering by occurrence date (#2758)
Browse files Browse the repository at this point in the history
* Add activity log default ordering by occurrence date

* Add index on inserted at for ActivityLog
  • Loading branch information
nelsonkopliku authored Jul 10, 2024
1 parent 19a2589 commit 90a98b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/trento/activity_log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Trento.Repo.Migrations.AddActivityLogIntex do
use Ecto.Migration

def change do
create index(:activity_logs, [:inserted_at])
end
end
24 changes: 24 additions & 0 deletions test/trento/activity_log_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 90a98b5

Please sign in to comment.