-
Notifications
You must be signed in to change notification settings - Fork 15
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
Activity log endpoint #2739
Merged
Merged
Activity log endpoint #2739
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
be90723
Implements context function
gagandeepb edde408
Implements the view
gagandeepb bc0b5a5
Implements the controller
gagandeepb 2c5b002
Adds an entry to the router
gagandeepb e25eddd
Adds tests
gagandeepb 5ceac11
Fixes field value in view
gagandeepb dfd573c
Fixes type signature
gagandeepb 22130d5
Make credo happy
gagandeepb 3fc42f9
Fixes tests; renames openapi spec tags
gagandeepb b8f903b
Addresses PR comments
gagandeepb 9acb3bb
Renames field in schema
gagandeepb 87b7260
Fixes spelling
gagandeepb 2b44a07
Fixes more spellings
gagandeepb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ["Platform"], | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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{ | ||
title: "ActivityLogEntry", | ||
type: :object, | ||
gagandeepb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
}, | ||
occurred_on: %Schema{ | ||
type: :string, | ||
description: "Timestamp upon Activity Log entry insertion." | ||
} | ||
}, | ||
required: [:type, :actor, :metadata, :occurred_on] | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule TrentoWeb.V1.ActivityLogView do | ||
use TrentoWeb, :view | ||
|
||
def render("activity_log.json", %{activity_log: entries}) do | ||
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, | ||
# Time of occurrence approximated by time of insertion in DB. | ||
occurred_on: entry.inserted_at | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/trento_web/controllers/v1/activity_log_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 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) | ||
|
||
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 | ||
resp = | ||
conn | ||
|> get("/api/v1/activity_log") | ||
|> json_response(200) | ||
|
||
assert resp == [] | ||
assert_schema(resp, "ActivityLog", api_spec) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Avoid comments on future implementations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention here was to motivate/justify the use of
Repo.all
in the following line, which is not very usual. In a way this implementation is partial and I was only drawing attention to this fact.