diff --git a/lib/report.ex b/lib/report.ex index 03f936870..176cb158f 100644 --- a/lib/report.ex +++ b/lib/report.ex @@ -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() diff --git a/lib/report/notifications_count_estimate.ex b/lib/report/notifications_count_estimate.ex new file mode 100644 index 000000000..3ec3ae943 --- /dev/null +++ b/lib/report/notifications_count_estimate.ex @@ -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 diff --git a/test/report/notifications_count_estimate_test.exs b/test/report/notifications_count_estimate_test.exs new file mode 100644 index 000000000..015254043 --- /dev/null +++ b/test/report/notifications_count_estimate_test.exs @@ -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 diff --git a/test/report_test.exs b/test/report_test.exs index 80a9b4f0e..1128eb6db 100644 --- a/test/report_test.exs +++ b/test/report_test.exs @@ -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