Skip to content

Commit da03ed3

Browse files
Gargronhiyuki2578
authored andcommitted
Add breakdown of usage by source to admin UI for hashtags (mastodon#11517)
Allows determining where the majority of posts in a hashtag come from on a given day at a glance.
1 parent bd7f0b0 commit da03ed3

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

app/controllers/admin/tags_controller.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Admin
44
class TagsController < BaseController
55
before_action :set_tags, only: :index
66
before_action :set_tag, except: :index
7+
before_action :set_usage_by_domain, except: :index
8+
before_action :set_counters, except: :index
79

810
def index
911
authorize :tag, :index?
@@ -33,6 +35,21 @@ def set_tag
3335
@tag = Tag.find(params[:id])
3436
end
3537

38+
def set_usage_by_domain
39+
@usage_by_domain = @tag.statuses
40+
.where(visibility: :public)
41+
.where(Status.arel_table[:id].gteq(Mastodon::Snowflake.id_at(Time.now.utc.beginning_of_day)))
42+
.joins(:account)
43+
.group('accounts.domain')
44+
.reorder('statuses_count desc')
45+
.pluck('accounts.domain, count(*) AS statuses_count')
46+
end
47+
48+
def set_counters
49+
@accounts_today = @tag.history.first[:accounts]
50+
@accounts_week = Redis.current.pfcount(*current_week_days.map { |day| "activity:tags:#{@tag.id}:#{day}:accounts" })
51+
end
52+
3653
def filtered_tags
3754
scope = Tag
3855
scope = scope.discoverable if filter_params[:context] == 'directory'
@@ -49,5 +66,13 @@ def filter_params
4966
def tag_params
5067
params.require(:tag).permit(:name, :trendable, :usable, :listable)
5168
end
69+
70+
def current_week_days
71+
now = Time.now.utc.beginning_of_day.to_date
72+
73+
(Date.commercial(now.cwyear, now.cweek)..now).map do |date|
74+
date.to_time.utc.beginning_of_day.to_i
75+
end
76+
end
5277
end
5378
end

app/views/admin/tags/show.html.haml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
- content_for :page_title do
22
= "##{@tag.name}"
33

4+
.dashboard__counters
5+
%div
6+
= link_to web_url("timelines/tag/#{@tag.name}") do
7+
.dashboard__counters__num= number_with_delimiter @accounts_today
8+
.dashboard__counters__label= t 'admin.tags.accounts_today'
9+
%div
10+
%div
11+
.dashboard__counters__num= number_with_delimiter @accounts_week
12+
.dashboard__counters__label= t 'admin.tags.accounts_week'
13+
%div
14+
= link_to explore_hashtag_path(@tag) do
15+
.dashboard__counters__num= number_with_delimiter @tag.accounts_count
16+
.dashboard__counters__label= t 'admin.tags.directory'
17+
18+
%hr.spacer/
19+
420
= simple_form_for @tag, url: admin_tag_path(@tag.id) do |f|
521
= render 'shared/error_messages', object: @tag
622

@@ -14,3 +30,16 @@
1430

1531
.actions
1632
= f.button :button, t('generic.save_changes'), type: :submit
33+
34+
%hr.spacer/
35+
36+
%h3= t 'admin.tags.breakdown'
37+
38+
.table-wrapper
39+
%table.table
40+
%tbody
41+
- @usage_by_domain.each do |(domain, count)|
42+
%tr
43+
%th= domain || site_hostname
44+
%td= "#{number_with_delimiter((count.to_f / @tag.history[0][:uses].to_f) * 100)}%"
45+
%td= number_with_delimiter count

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ en:
499499
title: Account statuses
500500
with_media: With media
501501
tags:
502+
accounts_today: Unique uses today
503+
accounts_week: Unique uses this week
504+
breakdown: Breakdown of today's usage by source
502505
context: Context
503506
directory: In directory
504507
in_directory: "%{count} in directory"

0 commit comments

Comments
 (0)