fill out stats page

This commit is contained in:
byte[] 2019-12-08 10:49:54 -05:00
parent 8fb062503e
commit 7de9c69434
2 changed files with 94 additions and 5 deletions

View file

@ -7,11 +7,19 @@ defmodule PhilomenaWeb.StatController do
alias Philomena.Forums.Forum
alias Philomena.Posts.Post
alias Philomena.Users.User
alias Philomena.Galleries.Gallery
alias Philomena.Galleries.Interaction
alias Philomena.Commissions.Commission
alias Philomena.Commissions.Item
alias Philomena.Reports.Report
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
{image_aggs, comment_aggs } = aggregations()
{gallery_count, gallery_size, distinct_creators, images_in_galleries} = galleries()
{open_reports, report_count, response_time} = moderation()
{open_commissions, commission_items} = commissions()
{image_aggs, comment_aggs} = aggregations()
{forums, topics, posts} = forums()
{users, users_24h} = users()
@ -24,7 +32,16 @@ defmodule PhilomenaWeb.StatController do
topics_count: topics,
posts_count: posts,
users_count: users,
users_24h: users_24h
users_24h: users_24h,
open_commissions: open_commissions,
commission_items: commission_items,
open_reports: open_reports,
report_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
)
end
@ -47,7 +64,7 @@ defmodule PhilomenaWeb.StatController do
first_post = Repo.one(first(Post))
last_post = Repo.one(last(Post))
{forums, last_topic.id - first_topic.id, last_post.id - first_post.id}
{forums, diff(last_topic, first_topic), diff(last_post, first_post)}
end
defp users do
@ -60,6 +77,54 @@ defmodule PhilomenaWeb.StatController do
|> where([u], u.created_at > ^time)
|> Repo.aggregate(:count, :id)
{last_user.id - first_user.id, last_24h}
{diff(last_user, first_user), last_24h}
end
defp galleries do
gallery_count = Repo.aggregate(Gallery, :count, :id)
gallery_size = Float.round(Repo.aggregate(Gallery, :avg, :image_count) || 0.0, 2)
distinct_creators =
Gallery
|> distinct(:creator_id)
|> Repo.aggregate(:count, :id)
first_gi = Repo.one(first(Interaction))
last_gi = Repo.one(last(Interaction))
{gallery_count, gallery_size, distinct_creators, diff(last_gi, first_gi)}
end
defp commissions do
open_commissions = Repo.aggregate(where(Commission, open: true), :count, :id)
commission_items = Repo.aggregate(Item, :count, :id)
{open_commissions, commission_items}
end
defp moderation do
open_reports = Repo.aggregate(where(Report, open: true), :count, :id)
first_report = Repo.one(first(Report))
last_report = Repo.one(last(Report))
closed_reports =
Report
|> where(open: false)
|> order_by(desc: :created_at)
|> limit(250)
|> Repo.all()
response_time =
closed_reports
|> Enum.reduce(0, & &2 + NaiveDateTime.diff(&1.updated_at, &1.created_at, :second))
|> Kernel./(safe_length(closed_reports) * 3600)
|> trunc()
{open_reports, diff(last_report, first_report), response_time}
end
defp diff(nil, nil), do: 0
defp diff(%{id: id2}, %{id: id1}), do: id2 - id1
defp safe_length([]), do: 1
defp safe_length(list), do: length(list)
end

View file

@ -73,4 +73,28 @@ elixir:
' users on the site. Of these,
span.stat>
= number_with_delimiter(@users_24h)
' have joined in the last 24 hours.
' have joined in the last 24 hours.
h3 Commissions
p
' There are
span.stat>
= number_with_delimiter(@open_commissions)
' open commission listings on the site, offering a total of
span.stat>
= number_with_delimiter(@commission_items)
' items.
h3 Moderation
p
' We have received
span.stat>
= number_with_delimiter(@report_count)
' reports. Out of these reports,
=> number_with_delimiter(@open_reports)
' reports are outstanding and awaiting action.
p
' On the last 250 reports we've received, it's taken us on average
span.stat>
= @response_time
' hour(s) between a report being made and the report being resolved.