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", "title": "Statistics",
"url": "/stats" "url": "/pages/stats"
}, },
{ {
"title": "About", "title": "About",

View file

@ -26,6 +26,7 @@ defmodule Philomena.Application do
# Start the endpoint when the application starts # Start the endpoint when the application starts
PhilomenaWeb.Endpoint, PhilomenaWeb.Endpoint,
PhilomenaWeb.StatsUpdater,
# Connection drainer for SIGTERM # Connection drainer for SIGTERM
{RanchConnectionDrainer, ranch_ref: PhilomenaWeb.Endpoint.HTTP, shutdown: 30_000} {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 "/dnp", DnpEntryController, only: [:index, :show]
resources "/staff", StaffController, only: [:index] resources "/staff", StaffController, only: [:index]
resources "/stats", StatController, only: [:index]
resources "/channels", ChannelController, only: [:index, :show] resources "/channels", ChannelController, only: [:index, :show]
resources "/settings", SettingController, only: [:edit, :update], singleton: true resources "/settings", SettingController, only: [:edit, :update], singleton: true
resources "/duplicate_reports", DuplicateReportController, only: [:index, :show, :create] resources "/duplicate_reports", DuplicateReportController, only: [:index, :show, :create]

View file

@ -1,6 +1,4 @@
defmodule PhilomenaWeb.StatController do defmodule PhilomenaWeb.StatsUpdater do
use PhilomenaWeb, :controller
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.Servers.Config alias Philomena.Servers.Config
alias Philomena.Images.Image alias Philomena.Images.Image
@ -14,10 +12,24 @@ defmodule PhilomenaWeb.StatController do
alias Philomena.Commissions.Commission alias Philomena.Commissions.Commission
alias Philomena.Commissions.Item alias Philomena.Commissions.Item
alias Philomena.Reports.Report alias Philomena.Reports.Report
alias Philomena.StaticPages.StaticPage
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query 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() {gallery_count, gallery_size, distinct_creators, images_in_galleries} = galleries()
{open_reports, report_count, response_time} = moderation() {open_reports, report_count, response_time} = moderation()
{open_commissions, commission_items} = commissions() {open_commissions, commission_items} = commissions()
@ -25,27 +37,42 @@ defmodule PhilomenaWeb.StatController do
{forums, topics, posts} = forums() {forums, topics, posts} = forums()
{users, users_24h} = users() {users, users_24h} = users()
render( result =
conn, Phoenix.View.render(
"index.html", PhilomenaWeb.StatView,
image_aggs: image_aggs, "index.html",
comment_aggs: comment_aggs, image_aggs: image_aggs,
forums_count: forums, comment_aggs: comment_aggs,
topics_count: topics, forums_count: forums,
posts_count: posts, topics_count: topics,
users_count: users, posts_count: posts,
users_24h: users_24h, users_count: users,
open_commissions: open_commissions, users_24h: users_24h,
commission_items: commission_items, open_commissions: open_commissions,
open_reports: open_reports, commission_items: commission_items,
report_stat_count: report_count, open_reports: open_reports,
response_time: response_time, report_stat_count: report_count,
gallery_count: gallery_count, response_time: response_time,
gallery_size: gallery_size, gallery_count: gallery_count,
distinct_creators: distinct_creators, gallery_size: gallery_size,
images_in_galleries: images_in_galleries, distinct_creators: distinct_creators,
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 end
defp aggregations do defp aggregations do