return of the stats page

This commit is contained in:
byte[] 2020-05-06 11:12:18 -04:00
parent b54704fba5
commit ef21b7c7d0
4 changed files with 54 additions and 27 deletions

View file

@ -82,7 +82,7 @@
},
{
"title": "Statistics",
"url": "/stats"
"url": "/pages/stats"
},
{
"title": "About",

View file

@ -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}

View file

@ -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]

View file

@ -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,8 +37,9 @@ defmodule PhilomenaWeb.StatController do
{forums, topics, posts} = forums()
{users, users_24h} = users()
render(
conn,
result =
Phoenix.View.render(
PhilomenaWeb.StatView,
"index.html",
image_aggs: image_aggs,
comment_aggs: comment_aggs,
@ -43,9 +56,23 @@ defmodule PhilomenaWeb.StatController do
gallery_count: gallery_count,
gallery_size: gallery_size,
distinct_creators: distinct_creators,
images_in_galleries: images_in_galleries,
title: "Statistics"
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