diff --git a/config/footer.json b/config/footer.json index 9126ac36..e164b7c2 100644 --- a/config/footer.json +++ b/config/footer.json @@ -82,7 +82,7 @@ }, { "title": "Statistics", - "url": "/stats" + "url": "/pages/stats" }, { "title": "About", diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index 23735961..a2725036 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -26,6 +26,7 @@ defmodule Philomena.Application do # Start the endpoint when the application starts PhilomenaWeb.Endpoint, + PhilomenaWeb.StatsUpdater, # Connection drainer for SIGTERM {RanchConnectionDrainer, ranch_ref: PhilomenaWeb.Endpoint.HTTP, shutdown: 30_000} diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index d444a95c..86013338 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -499,7 +499,6 @@ defmodule PhilomenaWeb.Router do resources "/dnp", DnpEntryController, only: [:index, :show] resources "/staff", StaffController, only: [:index] - resources "/stats", StatController, only: [:index] resources "/channels", ChannelController, only: [:index, :show] resources "/settings", SettingController, only: [:edit, :update], singleton: true resources "/duplicate_reports", DuplicateReportController, only: [:index, :show, :create] diff --git a/lib/philomena_web/controllers/stat_controller.ex b/lib/philomena_web/stats_updater.ex similarity index 69% rename from lib/philomena_web/controllers/stat_controller.ex rename to lib/philomena_web/stats_updater.ex index b6860f10..edeccc12 100644 --- a/lib/philomena_web/controllers/stat_controller.ex +++ b/lib/philomena_web/stats_updater.ex @@ -1,6 +1,4 @@ -defmodule PhilomenaWeb.StatController do - use PhilomenaWeb, :controller - +defmodule PhilomenaWeb.StatsUpdater do alias Philomena.Elasticsearch alias Philomena.Servers.Config alias Philomena.Images.Image @@ -14,10 +12,24 @@ defmodule PhilomenaWeb.StatController do alias Philomena.Commissions.Commission alias Philomena.Commissions.Item alias Philomena.Reports.Report + alias Philomena.StaticPages.StaticPage alias Philomena.Repo import Ecto.Query - def index(conn, _params) do + def child_spec([]) do + %{ + id: PhilomenaWeb.StatsUpdater, + start: {PhilomenaWeb.StatsUpdater, :start_link, [[]]} + } + end + + def start_link([]) do + {:ok, spawn_link(&run/0)} + end + + defp run do + :timer.sleep(:timer.seconds(300)) + {gallery_count, gallery_size, distinct_creators, images_in_galleries} = galleries() {open_reports, report_count, response_time} = moderation() {open_commissions, commission_items} = commissions() @@ -25,27 +37,42 @@ defmodule PhilomenaWeb.StatController do {forums, topics, posts} = forums() {users, users_24h} = users() - render( - conn, - "index.html", - image_aggs: image_aggs, - comment_aggs: comment_aggs, - forums_count: forums, - topics_count: topics, - posts_count: posts, - users_count: users, - users_24h: users_24h, - open_commissions: open_commissions, - commission_items: commission_items, - open_reports: open_reports, - report_stat_count: report_count, - response_time: response_time, - gallery_count: gallery_count, - gallery_size: gallery_size, - distinct_creators: distinct_creators, - images_in_galleries: images_in_galleries, - title: "Statistics" - ) + result = + Phoenix.View.render( + PhilomenaWeb.StatView, + "index.html", + image_aggs: image_aggs, + comment_aggs: comment_aggs, + forums_count: forums, + topics_count: topics, + posts_count: posts, + users_count: users, + users_24h: users_24h, + open_commissions: open_commissions, + commission_items: commission_items, + open_reports: open_reports, + report_stat_count: report_count, + response_time: response_time, + gallery_count: gallery_count, + gallery_size: gallery_size, + distinct_creators: distinct_creators, + images_in_galleries: images_in_galleries + ) + + now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) + + static_page = + %{ + title: "Statistics", + slug: "stats", + body: Phoenix.HTML.safe_to_string(result), + created_at: now, + updated_at: now + } + + Repo.insert_all(StaticPage, [static_page], on_conflict: {:replace, [:body, :updated_at]}, conflict_target: :slug) + + run() end defp aggregations do