Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

CRM Audit browser: implement basic pagination (#5592)

Adam Rutkowski1e54949241ab
diff --git a/extra/lib/plausible/audit.ex b/extra/lib/plausible/audit.ex
index be2a941af9..e29719e459 100644
--- a/extra/lib/plausible/audit.ex
+++ b/extra/lib/plausible/audit.ex
@@ -9,8 +9,20 @@ defmodule Plausible.Audit do
defdelegate set_context(term), to: Plausible.Audit.Entry
def list_entries(attrs) do
- Plausible.Repo.all(
- from ae in Plausible.Audit.Entry, where: ^attrs, order_by: [asc: :datetime]
- )
+ attrs
+ |> entries_query()
+ |> Plausible.Repo.all()
+ end
+
+ def list_entries_paginated(attrs, params \\ %{}) do
+ attrs
+ |> entries_query()
+ |> Plausible.Pagination.paginate(params, cursor_fields: [{:datetime, :asc}])
+ end
+
+ defp entries_query(attrs) do
+ from ae in Plausible.Audit.Entry,
+ where: ^attrs,
+ order_by: [asc: :datetime]
end
end
diff --git a/extra/lib/plausible_web/live/customer_support.ex b/extra/lib/plausible_web/live/customer_support.ex
index 79a528083a..50622f25c4 100644
--- a/extra/lib/plausible_web/live/customer_support.ex
+++ b/extra/lib/plausible_web/live/customer_support.ex
@@ -132,9 +132,12 @@ defmodule PlausibleWeb.Live.CustomerSupport do
id = String.to_integer(id)
+ tab_params = Map.drop(p, ["id", "resource", "tab"])
+
send_update(self(), mod.component(),
id: "#{mod.type()}-#{id}",
- tab: p["tab"]
+ tab: p["tab"],
+ tab_params: tab_params
)
{:noreply, assign(socket, type: type, current: mod, id: id)}
diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.ex
index 20a2b422e8..adb8be690a 100644
--- a/extra/lib/plausible_web/live/customer_support/live/team.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex
@@ -12,7 +12,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
require Plausible.Billing.Subscription.Status
- import Ecto.Query
+ import Ecto.Query, only: [from: 2]
def update(%{resource_id: resource_id}, socket) do
team = Resource.Team.get(resource_id)
@@ -43,11 +43,13 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
{:ok, assign(socket, tab: "sso")}
end
- def update(%{tab: "audit"}, %{assigns: %{team: team}} = socket) do
- audit_entries = Plausible.Audit.list_entries(team_id: team.id)
+ def update(%{tab: "audit", tab_params: tab_params}, %{assigns: %{team: team}} = socket) do
+ pagination_params = get_pagination_params(tab_params)
- audit_entries =
- Enum.map(audit_entries, fn entry ->
+ audit_page = Plausible.Audit.list_entries_paginated([team_id: team.id], pagination_params)
+
+ entries =
+ Enum.map(audit_page.entries, fn entry ->
meta = entry.meta
meta =
@@ -69,8 +71,16 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
Map.put(entry, :meta, meta)
end)
+ audit_page = %{audit_page | entries: entries}
+ current_limit = pagination_params["limit"]
+
{:ok,
- assign(socket, tab: "audit", audit_entries: audit_entries, revealed_audit_entry_id: nil)}
+ assign(socket,
+ tab: "audit",
+ audit_page: audit_page,
+ revealed_audit_entry_id: nil,
+ current_limit: current_limit
+ )}
end
def update(%{tab: "sites"}, %{assigns: %{team: team}} = socket) do
@@ -292,10 +302,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
</.button>
</div>
</div>
- <.table
- :if={not Enum.empty?(@sso_integration.sso_domains)}
- rows={@sso_integration.sso_domains}
- >
+ <.table rows={@sso_integration.sso_domains}>
<:thead>
<.th>Domain</.th>
<.th>Status</.th>
@@ -329,10 +336,15 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
</div>
<div :if={@tab == "audit"} class="mt-4 mb-4 text-gray-900 dark:text-gray-400 relative">
- <div :if={Enum.empty?(@audit_entries)} class="flex justify-center items-center">
+ <div :if={Enum.empty?(@audit_page.entries)} class="flex justify-center items-center">
No audit logs yet
</div>
- <div :if={@revealed_audit_entry_id}>
+ <div
+ :if={@revealed_audit_entry_id}
+ phx-target={@myself}
+ phx-window-keydown="reveal-audit-entry"
+ phx-key="escape"
+ >
<.input_with_clipboard
id="audit-entry-identifier"
name="audit-entry-identifier"
@@ -347,7 +359,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
name="audit-entry-change"
value={
Jason.encode!(
- Enum.find(@audit_entries, &(&1.id == @revealed_audit_entry_id)).change,
+ Enum.find(@audit_page.entries, &(&1.id == @revealed_audit_entry_id)).change,
pretty: true
)
}
@@ -369,10 +381,13 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
class="float-right pt-4 text-sm"
>
&larr; Return
+ <kbd class="rounded border border-gray-200 dark:border-gray-600 px-2 font-mono font-normal text-xs text-gray-400">
+ ESC
+ </kbd>
</.styled_link>
</div>
</div>
- <.table :if={is_nil(@revealed_audit_entry_id)} rows={@audit_entries}>
+ <.table :if={is_nil(@revealed_audit_entry_id)} rows={@audit_page.entries}>
<:thead>
<.th invisible></.th>
<.th invisible></.th>
@@ -407,8 +422,38 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
</.td>
</:tbody>
</.table>
+ <div
+ :if={
+ is_nil(@revealed_audit_entry_id) &&
+ (@audit_page.metadata.before || @audit_page.metadata.after)
+ }
+ class="flex justify-between items-center mt-4"
+ >
+ <.button
+ :if={@audit_page.metadata.before}
+ id="prev-page"
+ phx-click="paginate-audit"
+ phx-value-before={@audit_page.metadata.before}
+ phx-value-limit={@current_limit}
+ phx-target={@myself}
+ theme="bright"
+ >
+ &larr; Prev
+ </.button>
+ <div></div>
+ <.button
+ :if={@audit_page.metadata.after}
+ id="next-page"
+ phx-click="paginate-audit"
+ phx-value-after={@audit_page.metadata.after}
+ phx-value-limit={@current_limit}
+ phx-target={@myself}
+ theme="bright"
+ >
+ Next &rarr;
+ </.button>
+ </div>
</div>
-
<div :if={@tab == "billing"} class="mt-4 mb-4 text-gray-900 dark:text-gray-400">
<h1 class="text-xs font-semibold">Usage</h1>
<.table rows={monthly_pageviews_usage(@usage.monthly_pageviews, @limits.monthly_pageviews)}>
@@ -775,6 +820,18 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
{:noreply, assign(socket, revealed_audit_entry_id: nil)}
end
+ def handle_event("paginate-audit", params, socket) do
+ pagination_params = get_pagination_params(params)
+ team = socket.assigns.team
+
+ query_params = %{"tab" => "audit"} |> Map.merge(pagination_params)
+
+ {:noreply,
+ push_patch(socket,
+ to: "/cs/teams/team/#{team.id}?" <> URI.encode_query(query_params)
+ )}
+ end
+
def handle_event("show-plan-form", _, socket) do
{:noreply, assign(socket, show_plan_form?: true, editing_plan: nil)}
end
@@ -1256,4 +1313,10 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
</div>
"""
end
+
+ defp get_pagination_params(params) do
+ params
+ |> Map.take(["after", "before", "limit"])
+ |> Map.put_new("limit", 15)
+ end
end
diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exs
index 26119cc7e8..90dd6c03a3 100644
--- a/test/plausible_web/live/customer_support/teams_test.exs
+++ b/test/plausible_web/live/customer_support/teams_test.exs
@@ -679,6 +679,42 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
assert text =~ "(N/A) (N/A)"
end
+
+ test "paginates audit entries", %{conn: conn, user: user} do
+ team = team_of(user)
+
+ now = NaiveDateTime.utc_now()
+
+ for i <- 1..8 do
+ %Plausible.Audit.Entry{
+ name: "Entry (#{i})",
+ entity: "Plausible.Teams.Team",
+ entity_id: to_string(team.id),
+ team_id: team.id,
+ datetime: NaiveDateTime.shift(now, second: i)
+ }
+ |> Plausible.Repo.insert!()
+ end
+
+ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :audit, limit: 3))
+ text = lv |> render() |> text()
+
+ for i <- 4..8, do: refute(text =~ "Entry (#{i})")
+ for i <- 1..3, do: assert(text =~ "Entry (#{i})")
+
+ lv |> element("button#next-page") |> render_click()
+ text = lv |> render() |> text()
+
+ for i <- 4..6, do: assert(text =~ "Entry (#{i})")
+ for i <- 1..3, do: refute(text =~ "Entry (#{i})")
+ for i <- 7..8, do: refute(text =~ "Entry (#{i})")
+
+ lv |> element("button#prev-page") |> render_click()
+ text = lv |> render() |> text()
+
+ for i <- 4..8, do: refute(text =~ "Entry (#{i})")
+ for i <- 1..3, do: assert(text =~ "Entry (#{i})")
+ end
end
end
end