Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity log default ordering by occurrence date #2758

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
balanza marked this conversation as resolved.
Show resolved Hide resolved
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