Skip to content

Commit

Permalink
feat: get the Postgres estimate of rows in notifications table (#2209)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald authored Sep 7, 2023
1 parent fec10ff commit d4f3940
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ defmodule Report do
"""
@type t :: module()

@report_modules [Report.UserSettings, Report.UserNamesAndUuids, Report.UserConfigurations]
@report_modules [
Report.UserSettings,
Report.UserNamesAndUuids,
Report.UserConfigurations,
Report.NotificationsCountEstimate
]

@callback run() :: {:ok, [map()]} | :error
@callback short_name() :: String.t()
Expand Down
24 changes: 24 additions & 0 deletions lib/report/notifications_count_estimate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Report.NotificationsCountEstimate do
@moduledoc """
Returns the Postgres estimated count of entries in the notifications table,
for evaluating options to delete old entries
"""
import Ecto.Query

@behaviour Report

@impl Report
def run() do
[count] =
from(p in "pg_class", where: p.relname == "notifications", select: p.reltuples)
|> Skate.Repo.all()

{:ok, [%{count: count}]}
end

@impl Report
def short_name(), do: "notifications_count_estimate"

@impl Report
def description(), do: "Estimated count of notifications"
end
25 changes: 25 additions & 0 deletions test/report/notifications_count_estimate_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Report.NotificationsCountEstimateTest do
use Skate.DataCase

describe "run/0" do
test "returns a single-row numeric value" do
{:ok, result} = Report.NotificationsCountEstimate.run()

assert [%{count: count}] = result

assert is_number(count)
end
end

describe "short_name/0" do
test "returns short name" do
assert Report.NotificationsCountEstimate.short_name() == "notifications_count_estimate"
end
end

describe "description/0" do
test "returns description" do
assert Report.NotificationsCountEstimate.description() == "Estimated count of notifications"
end
end
end
3 changes: 2 additions & 1 deletion test/report_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ defmodule ReportTest do
assert Report.all_reports() == %{
"user_configurations" => Report.UserConfigurations,
"user_settings" => Report.UserSettings,
"user_names_and_uuids" => Report.UserNamesAndUuids
"user_names_and_uuids" => Report.UserNamesAndUuids,
"notifications_count_estimate" => Report.NotificationsCountEstimate
}
end
end
Expand Down

0 comments on commit d4f3940

Please sign in to comment.