From 7de9c69434d3917acfd23268c47c48601ebe4ac8 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 8 Dec 2019 10:49:54 -0500 Subject: [PATCH] fill out stats page --- .../controllers/stat_controller.ex | 73 ++++++++++++++++++- .../templates/stat/index.html.slime | 26 ++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/lib/philomena_web/controllers/stat_controller.ex b/lib/philomena_web/controllers/stat_controller.ex index b7210034..25a82958 100644 --- a/lib/philomena_web/controllers/stat_controller.ex +++ b/lib/philomena_web/controllers/stat_controller.ex @@ -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 diff --git a/lib/philomena_web/templates/stat/index.html.slime b/lib/philomena_web/templates/stat/index.html.slime index b134cb19..b965dc1e 100644 --- a/lib/philomena_web/templates/stat/index.html.slime +++ b/lib/philomena_web/templates/stat/index.html.slime @@ -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. \ No newline at end of file + ' 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. \ No newline at end of file