DEV: Move reports to concerns for autoloading (#11066)

This commit is contained in:
Mark VanLandingham 2020-10-29 11:49:09 -05:00 committed by GitHub
parent c5eba3f9ee
commit c750a02f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
79 changed files with 1807 additions and 1573 deletions

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
module Reports::Bookmarks
extend ActiveSupport::Concern
class_methods do
def report_bookmarks(report)
report.icon = 'bookmark'
category_filter = report.filters.dig(:category)
report.add_filter('category', default: category_filter)
report.data = []
Bookmark.count_per_day(
category_id: category_filter,
start_date: report.start_date,
end_date: report.end_date
).each do |date, count|
report.data << { x: date, y: count }
end
add_counts report, Bookmark, 'bookmarks.created_at'
end
end
end

View File

@ -0,0 +1,49 @@
# frozen_string_literal: true
module Reports::ConsolidatedPageViews
extend ActiveSupport::Concern
class_methods do
def report_consolidated_page_views(report)
filters = %w[
page_view_logged_in
page_view_anon
page_view_crawler
]
report.modes = [:stacked_chart]
tertiary = ColorScheme.hex_for_name('tertiary') || '0088cc'
danger = ColorScheme.hex_for_name('danger') || 'e45735'
requests = filters.map do |filter|
color = report.rgba_color(tertiary)
if filter == "page_view_anon"
color = report.lighten_color(tertiary, 0.25)
end
if filter == "page_view_crawler"
color = report.rgba_color(danger, 0.75)
end
{
req: filter,
label: I18n.t("reports.consolidated_page_views.xaxis.#{filter}"),
color: color,
data: ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
}
end
requests.each do |request|
request[:data] = request[:data].where('date >= ? AND date <= ?', report.start_date, report.end_date)
.order(date: :asc)
.group(:date)
.sum(:count)
.map { |date, count| { x: date, y: count } }
end
report.data = requests
end
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
module Reports::DailyEngagedUsers
extend ActiveSupport::Concern
class_methods do
def report_daily_engaged_users(report)
report.average = true
report.data = []
data = UserAction.count_daily_engaged_users(report.start_date, report.end_date)
if report.facets.include?(:prev30Days)
prev30DaysData = UserAction.count_daily_engaged_users(report.start_date - 30.days, report.start_date)
report.prev30Days = prev30DaysData.sum { |k, v| v }
end
if report.facets.include?(:total)
report.total = UserAction.count_daily_engaged_users
end
if report.facets.include?(:prev_period)
prev_data = UserAction.count_daily_engaged_users(report.prev_start_date, report.prev_end_date)
prev = prev_data.sum { |k, v| v }
if prev > 0
prev = prev / ((report.end_date - report.start_date) / 1.day)
end
report.prev_period = prev
end
data.each do |key, value|
report.data << { x: key, y: value }
end
end
end
end

View File

@ -0,0 +1,57 @@
# frozen_string_literal: true
module Reports::DauByMau
extend ActiveSupport::Concern
class_methods do
def report_dau_by_mau(report)
report.labels = [
{
type: :date,
property: :x,
title: I18n.t("reports.default.labels.day")
},
{
type: :percent,
property: :y,
title: I18n.t("reports.default.labels.percent")
},
]
report.average = true
report.percent = true
data_points = UserVisit.count_by_active_users(report.start_date, report.end_date)
report.data = []
compute_dau_by_mau = Proc.new { |data_point|
if data_point["mau"] == 0
0
else
((data_point["dau"].to_f / data_point["mau"].to_f) * 100).ceil(2)
end
}
dau_avg = Proc.new { |start_date, end_date|
data_points = UserVisit.count_by_active_users(start_date, end_date)
if !data_points.empty?
sum = data_points.sum { |data_point| compute_dau_by_mau.call(data_point) }
(sum.to_f / data_points.count.to_f).ceil(2)
end
}
data_points.each do |data_point|
report.data << { x: data_point["date"], y: compute_dau_by_mau.call(data_point) }
end
if report.facets.include?(:prev_period)
report.prev_period = dau_avg.call(report.prev_start_date, report.prev_end_date)
end
if report.facets.include?(:prev30Days)
report.prev30Days = dau_avg.call(report.start_date - 30.days, report.start_date)
end
end
end
end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Reports::Emails
extend ActiveSupport::Concern
class_methods do
def report_emails(report)
report_about report, EmailLog
end
end
end

View File

@ -0,0 +1,36 @@
# frozen_string_literal: true
module Reports::Flags
extend ActiveSupport::Concern
class_methods do
def report_flags(report)
category_id, include_subcategories = report.add_category_filter
report.icon = 'flag'
report.higher_is_better = false
basic_report_about(
report,
ReviewableFlaggedPost,
:count_by_date,
report.start_date,
report.end_date,
category_id,
include_subcategories
)
countable = ReviewableFlaggedPost.scores_with_topics
if category_id
if include_subcategories
countable = countable.where('topics.category_id IN (?)', Category.subcategory_ids(category_id))
else
countable = countable.where('topics.category_id = ?', category_id)
end
end
add_counts report, countable, 'reviewable_scores.created_at'
end
end
end

View File

@ -0,0 +1,178 @@
# frozen_string_literal: true
module Reports::FlagsStatus
extend ActiveSupport::Concern
class_methods do
def report_flags_status(report)
report.modes = [:table]
report.labels = [
{
type: :post,
properties: {
topic_id: :topic_id,
number: :post_number,
truncated_raw: :post_type
},
title: I18n.t("reports.flags_status.labels.flag")
},
{
type: :user,
properties: {
username: :staff_username,
id: :staff_id,
avatar: :staff_avatar_template
},
title: I18n.t("reports.flags_status.labels.assigned")
},
{
type: :user,
properties: {
username: :poster_username,
id: :poster_id,
avatar: :poster_avatar_template
},
title: I18n.t("reports.flags_status.labels.poster")
},
{
type: :user,
properties: {
username: :flagger_username,
id: :flagger_id,
avatar: :flagger_avatar_template
},
title: I18n.t("reports.flags_status.labels.flagger")
},
{
type: :seconds,
property: :response_time,
title: I18n.t("reports.flags_status.labels.time_to_resolution")
}
]
report.data = []
flag_types = PostActionType.flag_types
sql = <<~SQL
WITH period_actions AS (
SELECT id,
post_action_type_id,
created_at,
agreed_at,
disagreed_at,
deferred_at,
agreed_by_id,
disagreed_by_id,
deferred_by_id,
post_id,
user_id,
COALESCE(disagreed_at, agreed_at, deferred_at) AS responded_at
FROM post_actions
WHERE post_action_type_id IN (#{flag_types.values.join(',')})
AND created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
ORDER BY created_at DESC
),
poster_data AS (
SELECT pa.id,
p.user_id AS poster_id,
p.topic_id as topic_id,
p.post_number as post_number,
u.username_lower AS poster_username,
u.uploaded_avatar_id AS poster_avatar_id
FROM period_actions pa
JOIN posts p
ON p.id = pa.post_id
JOIN users u
ON u.id = p.user_id
),
flagger_data AS (
SELECT pa.id,
u.id AS flagger_id,
u.username_lower AS flagger_username,
u.uploaded_avatar_id AS flagger_avatar_id
FROM period_actions pa
JOIN users u
ON u.id = pa.user_id
),
staff_data AS (
SELECT pa.id,
u.id AS staff_id,
u.username_lower AS staff_username,
u.uploaded_avatar_id AS staff_avatar_id
FROM period_actions pa
JOIN users u
ON u.id = COALESCE(pa.agreed_by_id, pa.disagreed_by_id, pa.deferred_by_id)
)
SELECT
sd.staff_username,
sd.staff_id,
sd.staff_avatar_id,
pd.poster_username,
pd.poster_id,
pd.poster_avatar_id,
pd.post_number,
pd.topic_id,
fd.flagger_username,
fd.flagger_id,
fd.flagger_avatar_id,
pa.post_action_type_id,
pa.created_at,
pa.agreed_at,
pa.disagreed_at,
pa.deferred_at,
pa.agreed_by_id,
pa.disagreed_by_id,
pa.deferred_by_id,
COALESCE(pa.disagreed_at, pa.agreed_at, pa.deferred_at) AS responded_at
FROM period_actions pa
FULL OUTER JOIN staff_data sd
ON sd.id = pa.id
FULL OUTER JOIN flagger_data fd
ON fd.id = pa.id
FULL OUTER JOIN poster_data pd
ON pd.id = pa.id
SQL
DB.query(sql).each do |row|
data = {}
data[:post_type] = flag_types.key(row.post_action_type_id).to_s
data[:post_number] = row.post_number
data[:topic_id] = row.topic_id
if row.staff_id
data[:staff_username] = row.staff_username
data[:staff_id] = row.staff_id
data[:staff_avatar_template] = User.avatar_template(row.staff_username, row.staff_avatar_id)
end
if row.poster_id
data[:poster_username] = row.poster_username
data[:poster_id] = row.poster_id
data[:poster_avatar_template] = User.avatar_template(row.poster_username, row.poster_avatar_id)
end
if row.flagger_id
data[:flagger_id] = row.flagger_id
data[:flagger_username] = row.flagger_username
data[:flagger_avatar_template] = User.avatar_template(row.flagger_username, row.flagger_avatar_id)
end
if row.agreed_by_id
data[:resolution] = I18n.t("reports.flags_status.values.agreed")
elsif row.disagreed_by_id
data[:resolution] = I18n.t("reports.flags_status.values.disagreed")
elsif row.deferred_by_id
data[:resolution] = I18n.t("reports.flags_status.values.deferred")
else
data[:resolution] = I18n.t("reports.flags_status.values.no_action")
end
data[:response_time] = row.responded_at ? row.responded_at - row.created_at : nil
report.data << data
end
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Reports::Likes
extend ActiveSupport::Concern
class_methods do
def report_likes(report)
report.icon = 'heart'
post_action_report report, PostActionType.types[:like]
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Reports::MobileVisits
extend ActiveSupport::Concern
class_methods do
def report_mobile_visits(report)
basic_report_about report, UserVisit, :mobile_by_day, report.start_date, report.end_date
report.total = UserVisit.where(mobile: true).count
report.prev30Days = UserVisit.where(mobile: true).where("visited_at >= ? and visited_at < ?", report.start_date - 30.days, report.start_date).count
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Reports::ModeratorWarningPrivateMessages
extend ActiveSupport::Concern
class_methods do
def report_moderator_warning_private_messages(report)
report.icon = 'envelope'
private_messages_report report, TopicSubtype.moderator_warning
end
end
end

View File

@ -0,0 +1,191 @@
# frozen_string_literal: true
module Reports::ModeratorsActivity
extend ActiveSupport::Concern
class_methods do
def report_moderators_activity(report)
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.moderators_activity.labels.moderator"),
},
{
property: :flag_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.flag_count")
},
{
type: :seconds,
property: :time_read,
title: I18n.t("reports.moderators_activity.labels.time_read")
},
{
property: :topic_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.topic_count")
},
{
property: :pm_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.pm_count")
},
{
property: :post_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.post_count")
},
{
property: :revision_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.revision_count")
}
]
report.modes = [:table]
report.data = []
query = <<~SQL
WITH mods AS (
SELECT
id AS user_id,
username_lower AS username,
uploaded_avatar_id
FROM users u
WHERE u.moderator = 'true'
AND u.id > 0
),
time_read AS (
SELECT SUM(uv.time_read) AS time_read,
uv.user_id
FROM mods m
JOIN user_visits uv
ON m.user_id = uv.user_id
WHERE uv.visited_at >= '#{report.start_date}'
AND uv.visited_at <= '#{report.end_date}'
GROUP BY uv.user_id
),
flag_count AS (
WITH period_actions AS (
SELECT agreed_by_id,
disagreed_by_id
FROM post_actions
WHERE post_action_type_id IN (#{PostActionType.flag_types_without_custom.values.join(',')})
AND created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
),
agreed_flags AS (
SELECT pa.agreed_by_id AS user_id,
COUNT(*) AS flag_count
FROM mods m
JOIN period_actions pa
ON pa.agreed_by_id = m.user_id
GROUP BY agreed_by_id
),
disagreed_flags AS (
SELECT pa.disagreed_by_id AS user_id,
COUNT(*) AS flag_count
FROM mods m
JOIN period_actions pa
ON pa.disagreed_by_id = m.user_id
GROUP BY disagreed_by_id
)
SELECT
COALESCE(af.user_id, df.user_id) AS user_id,
COALESCE(af.flag_count, 0) + COALESCE(df.flag_count, 0) AS flag_count
FROM agreed_flags af
FULL OUTER JOIN disagreed_flags df
ON df.user_id = af.user_id
),
revision_count AS (
SELECT pr.user_id,
COUNT(*) AS revision_count
FROM mods m
JOIN post_revisions pr
ON pr.user_id = m.user_id
JOIN posts p
ON p.id = pr.post_id
WHERE pr.created_at >= '#{report.start_date}'
AND pr.created_at <= '#{report.end_date}'
AND p.user_id <> pr.user_id
GROUP BY pr.user_id
),
topic_count AS (
SELECT t.user_id,
COUNT(*) AS topic_count
FROM mods m
JOIN topics t
ON t.user_id = m.user_id
WHERE t.archetype = 'regular'
AND t.created_at >= '#{report.start_date}'
AND t.created_at <= '#{report.end_date}'
GROUP BY t.user_id
),
post_count AS (
SELECT p.user_id,
COUNT(*) AS post_count
FROM mods m
JOIN posts p
ON p.user_id = m.user_id
JOIN topics t
ON t.id = p.topic_id
WHERE t.archetype = 'regular'
AND p.created_at >= '#{report.start_date}'
AND p.created_at <= '#{report.end_date}'
GROUP BY p.user_id
),
pm_count AS (
SELECT p.user_id,
COUNT(*) AS pm_count
FROM mods m
JOIN posts p
ON p.user_id = m.user_id
JOIN topics t
ON t.id = p.topic_id
WHERE t.archetype = 'private_message'
AND p.created_at >= '#{report.start_date}'
AND p.created_at <= '#{report.end_date}'
GROUP BY p.user_id
)
SELECT
m.user_id,
m.username,
m.uploaded_avatar_id,
tr.time_read,
fc.flag_count,
rc.revision_count,
tc.topic_count,
pc.post_count,
pmc.pm_count
FROM mods m
LEFT JOIN time_read tr ON tr.user_id = m.user_id
LEFT JOIN flag_count fc ON fc.user_id = m.user_id
LEFT JOIN revision_count rc ON rc.user_id = m.user_id
LEFT JOIN topic_count tc ON tc.user_id = m.user_id
LEFT JOIN post_count pc ON pc.user_id = m.user_id
LEFT JOIN pm_count pmc ON pmc.user_id = m.user_id
ORDER BY m.username
SQL
DB.query(query).each do |row|
mod = {}
mod[:username] = row.username
mod[:user_id] = row.user_id
mod[:user_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
mod[:time_read] = row.time_read
mod[:flag_count] = row.flag_count
mod[:revision_count] = row.revision_count
mod[:topic_count] = row.topic_count
mod[:post_count] = row.post_count
mod[:pm_count] = row.pm_count
report.data << mod
end
end
end
end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
module Reports::NewContributors
extend ActiveSupport::Concern
class_methods do
def report_new_contributors(report)
report.data = []
data = User.real.count_by_first_post(report.start_date, report.end_date)
if report.facets.include?(:prev30Days)
prev30DaysData = User.real.count_by_first_post(report.start_date - 30.days, report.start_date)
report.prev30Days = prev30DaysData.sum { |k, v| v }
end
if report.facets.include?(:total)
report.total = User.real.count_by_first_post
end
if report.facets.include?(:prev_period)
prev_period_data = User.real.count_by_first_post(report.prev_start_date, report.prev_end_date)
report.prev_period = prev_period_data.sum { |k, v| v }
# report.prev_data = prev_period_data.map { |k, v| { x: k, y: v } }
end
data.each do |key, value|
report.data << { x: key, y: value }
end
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Reports::NotifyModeratorsPrivateMessages
extend ActiveSupport::Concern
class_methods do
def report_notify_moderators_private_messages(report)
report.icon = 'envelope'
private_messages_report report, TopicSubtype.notify_moderators
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Reports::NotifyUserPrivateMessages
extend ActiveSupport::Concern
class_methods do
def report_notify_user_private_messages(report)
report.icon = 'envelope'
private_messages_report report, TopicSubtype.notify_user
end
end
end

View File

@ -0,0 +1,116 @@
# frozen_string_literal: true
module Reports::PostEdits
extend ActiveSupport::Concern
class_methods do
def report_post_edits(report)
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
type: :date,
property: :created_at,
title: I18n.t("reports.post_edits.labels.edited_at")
},
{
type: :post,
properties: {
topic_id: :topic_id,
number: :post_number,
truncated_raw: :post_raw
},
title: I18n.t("reports.post_edits.labels.post")
},
{
type: :user,
properties: {
username: :editor_username,
id: :editor_id,
avatar: :editor_avatar_template,
},
title: I18n.t("reports.post_edits.labels.editor")
},
{
type: :user,
properties: {
username: :author_username,
id: :author_id,
avatar: :author_avatar_template,
},
title: I18n.t("reports.post_edits.labels.author")
},
{
type: :text,
property: :edit_reason,
title: I18n.t("reports.post_edits.labels.edit_reason")
},
]
report.data = []
builder = DB.build <<~SQL
SELECT
pr.user_id AS editor_id,
editor.username AS editor_username,
editor.uploaded_avatar_id AS editor_avatar_id,
p.user_id AS author_id,
author.username AS author_username,
author.uploaded_avatar_id AS author_avatar_id,
pr.number AS revision_version,
p.version AS post_version,
pr.post_id,
LEFT(p.raw, 40) AS post_raw,
p.topic_id,
p.post_number,
p.edit_reason,
pr.created_at
FROM post_revisions pr
JOIN posts p
ON p.id = pr.post_id
JOIN users author
ON author.id = p.user_id
JOIN users editor
ON editor.id = pr.user_id
/*join*/
/*where*/
ORDER BY pr.created_at ASC
/*limit*/
SQL
if category_id
builder.join "topics t ON t.id = p.topic_id"
if include_subcategories
builder.where("t.category_id IN (?)", Category.subcategory_ids(category_id))
else
builder.where("t.category_id = ?", category_id)
end
end
builder.where("editor.id > 0 AND editor.id != author.id")
builder.where("pr.created_at >= :start_date", start_date: report.start_date)
builder.where("pr.created_at <= :end_date", end_date: report.end_date)
result = builder.query
result.each do |r|
revision = {}
revision[:editor_id] = r.editor_id
revision[:editor_username] = r.editor_username
revision[:editor_avatar_template] = User.avatar_template(r.editor_username, r.editor_avatar_id)
revision[:author_id] = r.author_id
revision[:author_username] = r.author_username
revision[:author_avatar_template] = User.avatar_template(r.author_username, r.author_avatar_id)
revision[:edit_reason] = r.revision_version == r.post_version ? r.edit_reason : nil
revision[:created_at] = r.created_at
revision[:post_raw] = r.post_raw
revision[:topic_id] = r.topic_id
revision[:post_number] = r.post_number
report.data << revision
end
end
end
end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Reports::Posts
extend ActiveSupport::Concern
class_methods do
def report_posts(report)
report.modes = [:table, :chart]
category_id, include_subcategories = report.add_category_filter
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date, category_id, include_subcategories
countable = Post.public_posts.where(post_type: Post.types[:regular])
if category_id
if include_subcategories
countable = countable.joins(:topic).where('topics.category_id IN (?)', Category.subcategory_ids(category_id))
else
countable = countable.joins(:topic).where('topics.category_id = ?', category_id)
end
end
add_counts report, countable, 'posts.created_at'
end
end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module Reports::ProfileViews
extend ActiveSupport::Concern
class_methods do
def report_profile_views(report)
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
start_date = report.start_date
end_date = report.end_date
basic_report_about report, UserProfileView, :profile_views_by_day, start_date, end_date, group_filter
report.total = UserProfile.sum(:views)
report.prev30Days = UserProfileView.where('viewed_at >= ? AND viewed_at < ?', start_date - 30.days, start_date + 1).count
end
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module Reports::Signups
extend ActiveSupport::Concern
class_methods do
def report_signups(report)
report.icon = 'user-plus'
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
if group_filter
basic_report_about report, User.real, :count_by_signup_date, report.start_date, report.end_date, group_filter
add_counts report, User.real, 'users.created_at'
else
report_about report, User.real, :count_by_signup_date
end
end
end
end

View File

@ -0,0 +1,65 @@
# frozen_string_literal: true
module Reports::StaffLogins
extend ActiveSupport::Concern
class_methods do
def report_staff_logins(report)
report.modes = [:table]
report.data = []
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.staff_logins.labels.user")
},
{
property: :location,
title: I18n.t("reports.staff_logins.labels.location")
},
{
property: :created_at,
type: :precise_date,
title: I18n.t("reports.staff_logins.labels.login_at")
}
]
sql = <<~SQL
SELECT
t1.created_at created_at,
t1.client_ip client_ip,
u.username username,
u.uploaded_avatar_id uploaded_avatar_id,
u.id user_id
FROM (
SELECT DISTINCT ON (t.client_ip, t.user_id) t.client_ip, t.user_id, t.created_at
FROM user_auth_token_logs t
WHERE t.user_id IN (#{User.admins.pluck(:id).join(',')})
AND t.created_at >= :start_date
AND t.created_at <= :end_date
ORDER BY t.client_ip, t.user_id, t.created_at DESC
LIMIT #{report.limit || 20}
) t1
JOIN users u ON u.id = t1.user_id
ORDER BY created_at DESC
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
data = {}
data[:avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:user_id] = row.user_id
data[:username] = row.username
data[:location] = DiscourseIpInfo.get(row.client_ip)[:location]
data[:created_at] = row.created_at
report.data << data
end
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
module Reports::StorageStats
extend ActiveSupport::Concern
class_methods do
def report_storage_stats(report)
backup_stats = begin
BackupRestore::BackupStore.create.stats
rescue BackupRestore::BackupStore::StorageError
nil
end
report.data = {
backups: backup_stats,
uploads: {
used_bytes: DiskSpace.uploads_used_bytes,
free_bytes: DiskSpace.uploads_free_bytes
}
}
end
end
end

View File

@ -0,0 +1,81 @@
# frozen_string_literal: true
module Reports::SuspiciousLogins
extend ActiveSupport::Concern
class_methods do
def report_suspicious_logins(report)
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.suspicious_logins.labels.user")
},
{
property: :client_ip,
title: I18n.t("reports.suspicious_logins.labels.client_ip")
},
{
property: :location,
title: I18n.t("reports.suspicious_logins.labels.location")
},
{
property: :browser,
title: I18n.t("reports.suspicious_logins.labels.browser")
},
{
property: :device,
title: I18n.t("reports.suspicious_logins.labels.device")
},
{
property: :os,
title: I18n.t("reports.suspicious_logins.labels.os")
},
{
type: :date,
property: :login_time,
title: I18n.t("reports.suspicious_logins.labels.login_time")
},
]
report.data = []
sql = <<~SQL
SELECT u.id user_id, u.username, u.uploaded_avatar_id, t.client_ip, t.user_agent, t.created_at login_time
FROM user_auth_token_logs t
JOIN users u ON u.id = t.user_id
WHERE t.action = 'suspicious'
AND t.created_at >= :start_date
AND t.created_at <= :end_date
ORDER BY t.created_at DESC
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
data = {}
ipinfo = DiscourseIpInfo.get(row.client_ip)
browser = BrowserDetection.browser(row.user_agent)
device = BrowserDetection.device(row.user_agent)
os = BrowserDetection.os(row.user_agent)
data[:username] = row.username
data[:user_id] = row.user_id
data[:avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:client_ip] = row.client_ip.to_s
data[:location] = ipinfo[:location]
data[:browser] = I18n.t("user_auth_tokens.browser.#{browser}")
data[:device] = I18n.t("user_auth_tokens.device.#{device}")
data[:os] = I18n.t("user_auth_tokens.os.#{os}")
data[:login_time] = row.login_time
report.data << data
end
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Reports::SystemPrivateMessages
extend ActiveSupport::Concern
class_methods do
def report_system_private_messages(report)
report.icon = 'envelope'
private_messages_report report, TopicSubtype.system_message
end
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
module Reports::TimeToFirstResponse
extend ActiveSupport::Concern
class_methods do
def report_time_to_first_response(report)
category_filter = report.filters.dig(:category)
category_id, include_subcategories = report.add_category_filter
report.icon = 'reply'
report.higher_is_better = false
report.data = []
Topic.time_to_first_response_per_day(report.start_date, report.end_date, category_id: category_id, include_subcategories: include_subcategories).each do |r|
report.data << { x: r['date'], y: r['hours'].to_f.round(2) }
end
report.total = Topic.time_to_first_response_total(category_id: category_id, include_subcategories: include_subcategories)
report.prev30Days = Topic.time_to_first_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: category_id, include_subcategories: include_subcategories)
end
end
end

View File

@ -0,0 +1,83 @@
# frozen_string_literal: true
module Reports::TopIgnoredUsers
extend ActiveSupport::Concern
class_methods do
def report_top_ignored_users(report)
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
id: :ignored_user_id,
username: :ignored_username,
avatar: :ignored_user_avatar_template,
},
title: I18n.t("reports.top_ignored_users.labels.ignored_user")
},
{
type: :number,
properties: [
:ignores_count,
],
title: I18n.t("reports.top_ignored_users.labels.ignores_count")
},
{
type: :number,
properties: [
:mutes_count,
],
title: I18n.t("reports.top_ignored_users.labels.mutes_count")
}
]
report.data = []
sql = <<~SQL
WITH ignored_users AS (
SELECT
ignored_user_id as user_id,
COUNT(*) AS ignores_count
FROM ignored_users
WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
GROUP BY ignored_user_id
ORDER BY COUNT(*) DESC
LIMIT :limit
),
muted_users AS (
SELECT
muted_user_id as user_id,
COUNT(*) AS mutes_count
FROM muted_users
WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
GROUP BY muted_user_id
ORDER BY COUNT(*) DESC
LIMIT :limit
)
SELECT u.id as user_id,
u.username as username,
u.uploaded_avatar_id as uploaded_avatar_id,
ig.ignores_count as ignores_count,
COALESCE(mu.mutes_count, 0) as mutes_count,
ig.ignores_count + COALESCE(mu.mutes_count, 0) as total
FROM users as u
JOIN ignored_users as ig ON ig.user_id = u.id
LEFT OUTER JOIN muted_users as mu ON mu.user_id = u.id
ORDER BY total DESC
SQL
DB.query(sql, limit: report.limit || 250).each do |row|
report.data << {
ignored_user_id: row.user_id,
ignored_username: row.username,
ignored_user_avatar_template: User.avatar_template(row.username, row.uploaded_avatar_id),
ignores_count: row.ignores_count,
mutes_count: row.mutes_count,
}
end
end
end
end

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
module Reports::TopReferredTopics
extend ActiveSupport::Concern
class_methods do
def report_top_referred_topics(report)
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
type: :topic,
properties: {
title: :topic_title,
id: :topic_id
},
title: I18n.t('reports.top_referred_topics.labels.topic')
},
{
property: :num_clicks,
type: :number,
title: I18n.t('reports.top_referred_topics.labels.num_clicks')
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8,
category_id: category_id,
include_subcategories: include_subcategories
}
result = nil
result = IncomingLinksReport.find(:top_referred_topics, options)
report.data = result.data
end
end
end

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
module Reports::TopReferrers
extend ActiveSupport::Concern
class_methods do
def report_top_referrers(report)
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.top_referrers.labels.user")
},
{
property: :num_clicks,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_clicks")
},
{
property: :num_topics,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_topics")
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8
}
result = IncomingLinksReport.find(:top_referrers, options)
report.data = result.data
end
end
end

View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
module Reports::TopTrafficSources
extend ActiveSupport::Concern
class_methods do
def report_top_traffic_sources(report)
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
property: :domain,
title: I18n.t('reports.top_traffic_sources.labels.domain')
},
{
property: :num_clicks,
type: :number,
title: I18n.t('reports.top_traffic_sources.labels.num_clicks')
},
{
property: :num_topics,
type: :number,
title: I18n.t('reports.top_traffic_sources.labels.num_topics')
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8,
category_id: category_id,
include_subcategories: include_subcategories
}
result = IncomingLinksReport.find(:top_traffic_sources, options)
report.data = result.data
end
end
end

View File

@ -0,0 +1,88 @@
# frozen_string_literal: true
module Reports::TopUploads
extend ActiveSupport::Concern
class_methods do
def report_top_uploads(report)
report.modes = [:table]
extension_filter = report.filters.dig(:file_extension)
report.add_filter('file_extension',
type: 'list',
default: extension_filter || 'any',
choices: (SiteSetting.authorized_extensions.split('|') + Array(extension_filter)).uniq
)
report.labels = [
{
type: :link,
properties: [
:file_url,
:file_name,
],
title: I18n.t("reports.top_uploads.labels.filename")
},
{
type: :user,
properties: {
username: :author_username,
id: :author_id,
avatar: :author_avatar_template,
},
title: I18n.t("reports.top_uploads.labels.author")
},
{
type: :text,
property: :extension,
title: I18n.t("reports.top_uploads.labels.extension")
},
{
type: :bytes,
property: :filesize,
title: I18n.t("reports.top_uploads.labels.filesize")
},
]
report.data = []
sql = <<~SQL
SELECT
u.id as user_id,
u.username,
u.uploaded_avatar_id,
up.filesize,
up.original_filename,
up.extension,
up.url
FROM uploads up
JOIN users u
ON u.id = up.user_id
/*where*/
ORDER BY up.filesize DESC
LIMIT #{report.limit || 250}
SQL
builder = DB.build(sql)
builder.where("up.id > :seeded_id_threshold", seeded_id_threshold: Upload::SEEDED_ID_THRESHOLD)
builder.where("up.created_at >= :start_date", start_date: report.start_date)
builder.where("up.created_at < :end_date", end_date: report.end_date)
if extension_filter
builder.where("up.extension = :extension", extension: extension_filter.sub(/^\./, ''))
end
builder.query.each do |row|
data = {}
data[:author_id] = row.user_id
data[:author_username] = row.username
data[:author_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:filesize] = row.filesize
data[:extension] = row.extension
data[:file_url] = Discourse.store.cdn_url(row.url)
data[:file_name] = row.original_filename.truncate(25)
report.data << data
end
end
end
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
module Reports::Topics
extend ActiveSupport::Concern
class_methods do
def report_topics(report)
category_id, include_subcategories = report.add_category_filter
basic_report_about report, Topic, :listable_count_per_day, report.start_date, report.end_date, category_id, include_subcategories
countable = Topic.listable_topics
countable = countable.where(category_id: include_subcategories ? Category.subcategory_ids(category_id) : category_id) if category_id
add_counts report, countable, 'topics.created_at'
end
end
end

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
module Reports::TopicsWithNoResponse
extend ActiveSupport::Concern
class_methods do
def report_topics_with_no_response(report)
category_id, include_subcategories = report.add_category_filter
report.data = []
Topic.with_no_response_per_day(report.start_date, report.end_date, category_id, include_subcategories).each do |r|
report.data << { x: r['date'], y: r['count'].to_i }
end
report.total = Topic.with_no_response_total(category_id: category_id, include_subcategories: include_subcategories)
report.prev30Days = Topic.with_no_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: category_id, include_subcategories: include_subcategories)
end
end
end

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Reports::TrendingSearch
extend ActiveSupport::Concern
class_methods do
def report_trending_search(report)
report.labels = [
{
property: :term,
type: :text,
title: I18n.t("reports.trending_search.labels.term")
},
{
property: :searches,
type: :number,
title: I18n.t("reports.trending_search.labels.searches")
},
{
type: :percent,
property: :ctr,
title: I18n.t("reports.trending_search.labels.click_through")
}
]
report.data = []
report.modes = [:table]
trends = SearchLog.trending_from(report.start_date,
end_date: report.end_date,
limit: report.limit
)
trends.each do |trend|
report.data << {
term: trend.term,
searches: trend.searches,
ctr: trend.ctr
}
end
end
end
end

View File

@ -0,0 +1,83 @@
# frozen_string_literal: true
module Reports::TrustLevelGrowth
extend ActiveSupport::Concern
class_methods do
def report_trust_level_growth(report)
report.modes = [:stacked_chart]
filters = %w[
tl1_reached
tl2_reached
tl3_reached
tl4_reached
]
sql = <<~SQL
SELECT
date(created_at),
(
count(*) filter (WHERE previous_value::integer < 1 AND new_value = '1')
) as tl1_reached,
(
count(*) filter (WHERE previous_value::integer < 2 AND new_value = '2')
) as tl2_reached,
(
count(*) filter (WHERE previous_value::integer < 3 AND new_value = '3')
) as tl3_reached,
(
count(*) filter (WHERE previous_value::integer < 4 AND new_value = '4')
) as tl4_reached
FROM user_histories
WHERE (
created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
)
AND (
action = #{UserHistory.actions[:change_trust_level]}
OR action = #{UserHistory.actions[:auto_trust_level_change]}
)
GROUP BY date(created_at)
SQL
data = Hash[ filters.collect { |x| [x, []] } ]
builder = DB.build(sql)
builder.query.each do |row|
filters.each do |filter|
data[filter] << {
x: row.date.strftime("%Y-%m-%d"),
y: row.instance_variable_get("@#{filter}")
}
end
end
tertiary = ColorScheme.hex_for_name('tertiary') || '0088cc'
quaternary = ColorScheme.hex_for_name('quaternary') || 'e45735'
requests = filters.map do |filter|
color = report.rgba_color(quaternary)
if filter == "tl1_reached"
color = report.lighten_color(tertiary, 0.25)
end
if filter == "tl2_reached"
color = report.rgba_color(tertiary)
end
if filter == "tl3_reached"
color = report.lighten_color(quaternary, 0.25)
end
{
req: filter,
label: I18n.t("reports.trust_level_growth.xaxis.#{filter}"),
color: color,
data: data[filter]
}
end
report.data = requests
end
end
end

View File

@ -0,0 +1,91 @@
# frozen_string_literal: true
module Reports::UserFlaggingRatio
extend ActiveSupport::Concern
class_methods do
def report_user_flagging_ratio(report)
report.data = []
report.modes = [:table]
report.dates_filtering = true
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.user_flagging_ratio.labels.user")
},
{
type: :number,
property: :disagreed_flags,
title: I18n.t("reports.user_flagging_ratio.labels.disagreed_flags")
},
{
type: :number,
property: :agreed_flags,
title: I18n.t("reports.user_flagging_ratio.labels.agreed_flags")
},
{
type: :number,
property: :ignored_flags,
title: I18n.t("reports.user_flagging_ratio.labels.ignored_flags")
},
{
type: :number,
property: :score,
title: I18n.t("reports.user_flagging_ratio.labels.score")
},
]
statuses = ReviewableScore.statuses
agreed = "SUM(CASE WHEN rs.status = #{statuses[:agreed]} THEN 1 ELSE 0 END)::numeric"
disagreed = "SUM(CASE WHEN rs.status = #{statuses[:disagreed]} THEN 1 ELSE 0 END)::numeric"
ignored = "SUM(CASE WHEN rs.status = #{statuses[:ignored]} THEN 1 ELSE 0 END)::numeric"
sql = <<~SQL
SELECT u.id,
u.username,
u.uploaded_avatar_id as avatar_id,
CASE WHEN u.silenced_till IS NOT NULL THEN 't' ELSE 'f' END as silenced,
#{disagreed} AS disagreed_flags,
#{agreed} AS agreed_flags,
#{ignored} AS ignored_flags,
(
CASE #{disagreed} WHEN 0 THEN #{agreed} * #{agreed}
ELSE ROUND((1-(#{agreed} / #{disagreed})) * (#{disagreed} - #{agreed})) END
) AS score
FROM users AS u
INNER JOIN reviewable_scores AS rs ON rs.user_id = u.id
WHERE u.id > 0
AND rs.created_at >= :start_date
AND rs.created_at <= :end_date
GROUP BY u.id,
u.username,
u.uploaded_avatar_id,
u.silenced_till
ORDER BY score DESC
LIMIT 100
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
flagger = {}
flagger[:user_id] = row.id
flagger[:username] = row.username
flagger[:avatar_template] = User.avatar_template(row.username, row.avatar_id)
flagger[:disagreed_flags] = row.disagreed_flags
flagger[:ignored_flags] = row.ignored_flags
flagger[:agreed_flags] = row.agreed_flags
flagger[:score] = row.score
report.data << flagger
end
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Reports::UserToUserPrivateMessages
extend ActiveSupport::Concern
class_methods do
def report_user_to_user_private_messages(report)
report.icon = 'envelope'
private_messages_report report, TopicSubtype.user_to_user
end
end
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
module Reports::UserToUserPrivateMessagesWithReplies
extend ActiveSupport::Concern
class_methods do
def report_user_to_user_private_messages_with_replies(report)
report.icon = 'envelope'
topic_subtype = TopicSubtype.user_to_user
subject = Post.where('posts.user_id > 0')
basic_report_about report, subject, :private_messages_count_per_day, report.start_date, report.end_date, topic_subtype
subject = Post.private_posts.where('posts.user_id > 0').with_topic_subtype(topic_subtype)
add_counts report, subject, 'posts.created_at'
end
end
end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Reports::UsersByTrustLevel
extend ActiveSupport::Concern
class_methods do
def report_users_by_trust_level(report)
report.data = []
report.modes = [:table]
report.dates_filtering = false
report.labels = [
{
property: :key,
title: I18n.t("reports.users_by_trust_level.labels.level")
},
{
property: :y,
type: :number,
title: I18n.t("reports.default.labels.count")
}
]
User.real.group('trust_level').count.sort.each do |level, count|
key = TrustLevel.levels[level.to_i]
url = Proc.new { |k| "/admin/users/list/#{k}" }
report.data << { url: url.call(key), key: key, x: level.to_i, y: count }
end
end
end
end

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
module Reports::UsersByType
extend ActiveSupport::Concern
class_methods do
def report_users_by_type(report)
report.data = []
report.modes = [:table]
report.dates_filtering = false
report.labels = [
{
property: :x,
title: I18n.t("reports.users_by_type.labels.type")
},
{
property: :y,
type: :number,
title: I18n.t("reports.default.labels.count")
}
]
label = Proc.new { |x| I18n.t("reports.users_by_type.xaxis_labels.#{x}") }
url = Proc.new { |key| "/admin/users/list/#{key}" }
admins = User.real.admins.count
report.data << { url: url.call("admins"), icon: "shield-alt", key: "admins", x: label.call("admin"), y: admins } if admins > 0
moderators = User.real.moderators.count
report.data << { url: url.call("moderators"), icon: "shield-alt", key: "moderators", x: label.call("moderator"), y: moderators } if moderators > 0
suspended = User.real.suspended.count
report.data << { url: url.call("suspended"), icon: "ban", key: "suspended", x: label.call("suspended"), y: suspended } if suspended > 0
silenced = User.real.silenced.count
report.data << { url: url.call("silenced"), icon: "ban", key: "silenced", x: label.call("silenced"), y: silenced } if silenced > 0
end
end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module Reports::Visits
extend ActiveSupport::Concern
class_methods do
def report_visits(report)
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
report.icon = 'user'
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date, group_filter
add_counts report, UserVisit, 'visited_at'
report.prev30Days = UserVisit.where('visited_at >= ? and visited_at < ?', report.start_date - 30.days, report.start_date).count
end
end
end

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
module Reports::WebCrawlers
extend ActiveSupport::Concern
class_methods do
def report_web_crawlers(report)
report.labels = [
{
type: :string,
property: :user_agent,
title: I18n.t('reports.web_crawlers.labels.user_agent')
},
{
property: :count,
type: :number,
title: I18n.t('reports.web_crawlers.labels.page_views')
}
]
report.modes = [:table]
report.data = WebCrawlerRequest.where('date >= ? and date <= ?', report.start_date, report.end_date)
.limit(200)
.order('sum_count DESC')
.group(:user_agent).sum(:count)
.map { |ua, count| { user_agent: ua, count: count } }
end
end
end

View File

@ -7,6 +7,46 @@ class Report
FILTERS = [:name, :start_date, :end_date, :category, :group, :trust_level, :file_extension, :include_subcategories]
include Reports::PostEdits
include Reports::TopTrafficSources
include Reports::TopicsWithNoResponse
include Reports::DauByMau
include Reports::FlagsStatus
include Reports::Emails
include Reports::Likes
include Reports::SystemPrivateMessages
include Reports::UsersByType
include Reports::StorageStats
include Reports::NotifyModeratorsPrivateMessages
include Reports::SuspiciousLogins
include Reports::TopReferredTopics
include Reports::Signups
include Reports::NotifyUserPrivateMessages
include Reports::NewContributors
include Reports::TrendingSearch
include Reports::UserToUserPrivateMessages
include Reports::Flags
include Reports::Topics
include Reports::Posts
include Reports::Bookmarks
include Reports::StaffLogins
include Reports::DailyEngagedUsers
include Reports::UserToUserPrivateMessagesWithReplies
include Reports::MobileVisits
include Reports::TopReferrers
include Reports::WebCrawlers
include Reports::ModeratorsActivity
include Reports::TopIgnoredUsers
include Reports::UserFlaggingRatio
include Reports::TrustLevelGrowth
include Reports::ConsolidatedPageViews
include Reports::Visits
include Reports::TimeToFirstResponse
include Reports::UsersByTrustLevel
include Reports::ModeratorWarningPrivateMessages
include Reports::ProfileViews
include Reports::TopUploads
attr_accessor :type, :data, :total, :prev30Days, :start_date,
:end_date, :labels, :prev_period, :facets, :limit, :average,
:percent, :higher_is_better, :icon, :modes, :prev_data,
@ -369,43 +409,3 @@ class Report
.map! { |rgb| rgb.to_i }
end
end
require_relative "reports/visits"
require_relative "reports/visits_mobile"
require_relative "reports/consolidated_page_views"
require_relative "reports/top_ignored_users"
require_relative "reports/top_uploads"
require_relative "reports/moderators_activity"
require_relative "reports/signups"
require_relative "reports/storage_stats"
require_relative "reports/suspicious_logins"
require_relative "reports/new_contributors"
require_relative "reports/users_by_trust_level"
require_relative "reports/staff_logins"
require_relative "reports/users_by_type"
require_relative "reports/user_flagging_ratio"
require_relative "reports/post_edits"
require_relative "reports/daily_engaged_users"
require_relative "reports/flags_status"
require_relative "reports/trending_search"
require_relative "reports/top_referrers"
require_relative "reports/top_traffic_sources"
require_relative "reports/top_referred_topics"
require_relative "reports/notify_user_private_messages"
require_relative "reports/user_to_user_private_messages"
require_relative "reports/user_to_user_private_messages_with_replies"
require_relative "reports/system_private_messages"
require_relative "reports/moderator_warning_private_messages"
require_relative "reports/notify_moderators_private_messages"
require_relative "reports/flags"
require_relative "reports/likes"
require_relative "reports/bookmarks"
require_relative "reports/dau_by_mau"
require_relative "reports/profile_views"
require_relative "reports/topics"
require_relative "reports/posts"
require_relative "reports/time_to_first_response"
require_relative "reports/topics_with_no_response"
require_relative "reports/emails"
require_relative "reports/web_crawlers"
require_relative "reports/trust_level_growth"

View File

@ -1,18 +0,0 @@
# frozen_string_literal: true
Report.add_report('bookmarks') do |report|
report.icon = 'bookmark'
category_filter = report.filters.dig(:category)
report.add_filter('category', default: category_filter)
report.data = []
Bookmark.count_per_day(
category_id: category_filter,
start_date: report.start_date,
end_date: report.end_date
).each do |date, count|
report.data << { x: date, y: count }
end
add_counts report, Bookmark, 'bookmarks.created_at'
end

View File

@ -1,43 +0,0 @@
# frozen_string_literal: true
Report.add_report("consolidated_page_views") do |report|
filters = %w[
page_view_logged_in
page_view_anon
page_view_crawler
]
report.modes = [:stacked_chart]
tertiary = ColorScheme.hex_for_name('tertiary') || '0088cc'
danger = ColorScheme.hex_for_name('danger') || 'e45735'
requests = filters.map do |filter|
color = report.rgba_color(tertiary)
if filter == "page_view_anon"
color = report.lighten_color(tertiary, 0.25)
end
if filter == "page_view_crawler"
color = report.rgba_color(danger, 0.75)
end
{
req: filter,
label: I18n.t("reports.consolidated_page_views.xaxis.#{filter}"),
color: color,
data: ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
}
end
requests.each do |request|
request[:data] = request[:data].where('date >= ? AND date <= ?', report.start_date, report.end_date)
.order(date: :asc)
.group(:date)
.sum(:count)
.map { |date, count| { x: date, y: count } }
end
report.data = requests
end

View File

@ -1,32 +0,0 @@
# frozen_string_literal: true
Report.add_report("daily_engaged_users") do |report|
report.average = true
report.data = []
data = UserAction.count_daily_engaged_users(report.start_date, report.end_date)
if report.facets.include?(:prev30Days)
prev30DaysData = UserAction.count_daily_engaged_users(report.start_date - 30.days, report.start_date)
report.prev30Days = prev30DaysData.sum { |k, v| v }
end
if report.facets.include?(:total)
report.total = UserAction.count_daily_engaged_users
end
if report.facets.include?(:prev_period)
prev_data = UserAction.count_daily_engaged_users(report.prev_start_date, report.prev_end_date)
prev = prev_data.sum { |k, v| v }
if prev > 0
prev = prev / ((report.end_date - report.start_date) / 1.day)
end
report.prev_period = prev
end
data.each do |key, value|
report.data << { x: key, y: value }
end
end

View File

@ -1,51 +0,0 @@
# frozen_string_literal: true
Report.add_report("dau_by_mau") do |report|
report.labels = [
{
type: :date,
property: :x,
title: I18n.t("reports.default.labels.day")
},
{
type: :percent,
property: :y,
title: I18n.t("reports.default.labels.percent")
},
]
report.average = true
report.percent = true
data_points = UserVisit.count_by_active_users(report.start_date, report.end_date)
report.data = []
compute_dau_by_mau = Proc.new { |data_point|
if data_point["mau"] == 0
0
else
((data_point["dau"].to_f / data_point["mau"].to_f) * 100).ceil(2)
end
}
dau_avg = Proc.new { |start_date, end_date|
data_points = UserVisit.count_by_active_users(start_date, end_date)
if !data_points.empty?
sum = data_points.sum { |data_point| compute_dau_by_mau.call(data_point) }
(sum.to_f / data_points.count.to_f).ceil(2)
end
}
data_points.each do |data_point|
report.data << { x: data_point["date"], y: compute_dau_by_mau.call(data_point) }
end
if report.facets.include?(:prev_period)
report.prev_period = dau_avg.call(report.prev_start_date, report.prev_end_date)
end
if report.facets.include?(:prev30Days)
report.prev30Days = dau_avg.call(report.start_date - 30.days, report.start_date)
end
end

View File

@ -1,5 +0,0 @@
# frozen_string_literal: true
Report.add_report("emails") do |report|
report_about report, EmailLog
end

View File

@ -1,30 +0,0 @@
# frozen_string_literal: true
Report.add_report('flags') do |report|
category_id, include_subcategories = report.add_category_filter
report.icon = 'flag'
report.higher_is_better = false
basic_report_about(
report,
ReviewableFlaggedPost,
:count_by_date,
report.start_date,
report.end_date,
category_id,
include_subcategories
)
countable = ReviewableFlaggedPost.scores_with_topics
if category_id
if include_subcategories
countable = countable.where('topics.category_id IN (?)', Category.subcategory_ids(category_id))
else
countable = countable.where('topics.category_id = ?', category_id)
end
end
add_counts report, countable, 'reviewable_scores.created_at'
end

View File

@ -1,172 +0,0 @@
# frozen_string_literal: true
Report.add_report("flags_status") do |report|
report.modes = [:table]
report.labels = [
{
type: :post,
properties: {
topic_id: :topic_id,
number: :post_number,
truncated_raw: :post_type
},
title: I18n.t("reports.flags_status.labels.flag")
},
{
type: :user,
properties: {
username: :staff_username,
id: :staff_id,
avatar: :staff_avatar_template
},
title: I18n.t("reports.flags_status.labels.assigned")
},
{
type: :user,
properties: {
username: :poster_username,
id: :poster_id,
avatar: :poster_avatar_template
},
title: I18n.t("reports.flags_status.labels.poster")
},
{
type: :user,
properties: {
username: :flagger_username,
id: :flagger_id,
avatar: :flagger_avatar_template
},
title: I18n.t("reports.flags_status.labels.flagger")
},
{
type: :seconds,
property: :response_time,
title: I18n.t("reports.flags_status.labels.time_to_resolution")
}
]
report.data = []
flag_types = PostActionType.flag_types
sql = <<~SQL
WITH period_actions AS (
SELECT id,
post_action_type_id,
created_at,
agreed_at,
disagreed_at,
deferred_at,
agreed_by_id,
disagreed_by_id,
deferred_by_id,
post_id,
user_id,
COALESCE(disagreed_at, agreed_at, deferred_at) AS responded_at
FROM post_actions
WHERE post_action_type_id IN (#{flag_types.values.join(',')})
AND created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
ORDER BY created_at DESC
),
poster_data AS (
SELECT pa.id,
p.user_id AS poster_id,
p.topic_id as topic_id,
p.post_number as post_number,
u.username_lower AS poster_username,
u.uploaded_avatar_id AS poster_avatar_id
FROM period_actions pa
JOIN posts p
ON p.id = pa.post_id
JOIN users u
ON u.id = p.user_id
),
flagger_data AS (
SELECT pa.id,
u.id AS flagger_id,
u.username_lower AS flagger_username,
u.uploaded_avatar_id AS flagger_avatar_id
FROM period_actions pa
JOIN users u
ON u.id = pa.user_id
),
staff_data AS (
SELECT pa.id,
u.id AS staff_id,
u.username_lower AS staff_username,
u.uploaded_avatar_id AS staff_avatar_id
FROM period_actions pa
JOIN users u
ON u.id = COALESCE(pa.agreed_by_id, pa.disagreed_by_id, pa.deferred_by_id)
)
SELECT
sd.staff_username,
sd.staff_id,
sd.staff_avatar_id,
pd.poster_username,
pd.poster_id,
pd.poster_avatar_id,
pd.post_number,
pd.topic_id,
fd.flagger_username,
fd.flagger_id,
fd.flagger_avatar_id,
pa.post_action_type_id,
pa.created_at,
pa.agreed_at,
pa.disagreed_at,
pa.deferred_at,
pa.agreed_by_id,
pa.disagreed_by_id,
pa.deferred_by_id,
COALESCE(pa.disagreed_at, pa.agreed_at, pa.deferred_at) AS responded_at
FROM period_actions pa
FULL OUTER JOIN staff_data sd
ON sd.id = pa.id
FULL OUTER JOIN flagger_data fd
ON fd.id = pa.id
FULL OUTER JOIN poster_data pd
ON pd.id = pa.id
SQL
DB.query(sql).each do |row|
data = {}
data[:post_type] = flag_types.key(row.post_action_type_id).to_s
data[:post_number] = row.post_number
data[:topic_id] = row.topic_id
if row.staff_id
data[:staff_username] = row.staff_username
data[:staff_id] = row.staff_id
data[:staff_avatar_template] = User.avatar_template(row.staff_username, row.staff_avatar_id)
end
if row.poster_id
data[:poster_username] = row.poster_username
data[:poster_id] = row.poster_id
data[:poster_avatar_template] = User.avatar_template(row.poster_username, row.poster_avatar_id)
end
if row.flagger_id
data[:flagger_id] = row.flagger_id
data[:flagger_username] = row.flagger_username
data[:flagger_avatar_template] = User.avatar_template(row.flagger_username, row.flagger_avatar_id)
end
if row.agreed_by_id
data[:resolution] = I18n.t("reports.flags_status.values.agreed")
elsif row.disagreed_by_id
data[:resolution] = I18n.t("reports.flags_status.values.disagreed")
elsif row.deferred_by_id
data[:resolution] = I18n.t("reports.flags_status.values.deferred")
else
data[:resolution] = I18n.t("reports.flags_status.values.no_action")
end
data[:response_time] = row.responded_at ? row.responded_at - row.created_at : nil
report.data << data
end
end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
Report.add_report("likes") do |report|
report.icon = 'heart'
post_action_report report, PostActionType.types[:like]
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
Report.add_report("moderator_warning_private_messages") do |report|
report.icon = 'envelope'
private_messages_report report, TopicSubtype.moderator_warning
end

View File

@ -1,185 +0,0 @@
# frozen_string_literal: true
Report.add_report("moderators_activity") do |report|
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.moderators_activity.labels.moderator"),
},
{
property: :flag_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.flag_count")
},
{
type: :seconds,
property: :time_read,
title: I18n.t("reports.moderators_activity.labels.time_read")
},
{
property: :topic_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.topic_count")
},
{
property: :pm_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.pm_count")
},
{
property: :post_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.post_count")
},
{
property: :revision_count,
type: :number,
title: I18n.t("reports.moderators_activity.labels.revision_count")
}
]
report.modes = [:table]
report.data = []
query = <<~SQL
WITH mods AS (
SELECT
id AS user_id,
username_lower AS username,
uploaded_avatar_id
FROM users u
WHERE u.moderator = 'true'
AND u.id > 0
),
time_read AS (
SELECT SUM(uv.time_read) AS time_read,
uv.user_id
FROM mods m
JOIN user_visits uv
ON m.user_id = uv.user_id
WHERE uv.visited_at >= '#{report.start_date}'
AND uv.visited_at <= '#{report.end_date}'
GROUP BY uv.user_id
),
flag_count AS (
WITH period_actions AS (
SELECT agreed_by_id,
disagreed_by_id
FROM post_actions
WHERE post_action_type_id IN (#{PostActionType.flag_types_without_custom.values.join(',')})
AND created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
),
agreed_flags AS (
SELECT pa.agreed_by_id AS user_id,
COUNT(*) AS flag_count
FROM mods m
JOIN period_actions pa
ON pa.agreed_by_id = m.user_id
GROUP BY agreed_by_id
),
disagreed_flags AS (
SELECT pa.disagreed_by_id AS user_id,
COUNT(*) AS flag_count
FROM mods m
JOIN period_actions pa
ON pa.disagreed_by_id = m.user_id
GROUP BY disagreed_by_id
)
SELECT
COALESCE(af.user_id, df.user_id) AS user_id,
COALESCE(af.flag_count, 0) + COALESCE(df.flag_count, 0) AS flag_count
FROM agreed_flags af
FULL OUTER JOIN disagreed_flags df
ON df.user_id = af.user_id
),
revision_count AS (
SELECT pr.user_id,
COUNT(*) AS revision_count
FROM mods m
JOIN post_revisions pr
ON pr.user_id = m.user_id
JOIN posts p
ON p.id = pr.post_id
WHERE pr.created_at >= '#{report.start_date}'
AND pr.created_at <= '#{report.end_date}'
AND p.user_id <> pr.user_id
GROUP BY pr.user_id
),
topic_count AS (
SELECT t.user_id,
COUNT(*) AS topic_count
FROM mods m
JOIN topics t
ON t.user_id = m.user_id
WHERE t.archetype = 'regular'
AND t.created_at >= '#{report.start_date}'
AND t.created_at <= '#{report.end_date}'
GROUP BY t.user_id
),
post_count AS (
SELECT p.user_id,
COUNT(*) AS post_count
FROM mods m
JOIN posts p
ON p.user_id = m.user_id
JOIN topics t
ON t.id = p.topic_id
WHERE t.archetype = 'regular'
AND p.created_at >= '#{report.start_date}'
AND p.created_at <= '#{report.end_date}'
GROUP BY p.user_id
),
pm_count AS (
SELECT p.user_id,
COUNT(*) AS pm_count
FROM mods m
JOIN posts p
ON p.user_id = m.user_id
JOIN topics t
ON t.id = p.topic_id
WHERE t.archetype = 'private_message'
AND p.created_at >= '#{report.start_date}'
AND p.created_at <= '#{report.end_date}'
GROUP BY p.user_id
)
SELECT
m.user_id,
m.username,
m.uploaded_avatar_id,
tr.time_read,
fc.flag_count,
rc.revision_count,
tc.topic_count,
pc.post_count,
pmc.pm_count
FROM mods m
LEFT JOIN time_read tr ON tr.user_id = m.user_id
LEFT JOIN flag_count fc ON fc.user_id = m.user_id
LEFT JOIN revision_count rc ON rc.user_id = m.user_id
LEFT JOIN topic_count tc ON tc.user_id = m.user_id
LEFT JOIN post_count pc ON pc.user_id = m.user_id
LEFT JOIN pm_count pmc ON pmc.user_id = m.user_id
ORDER BY m.username
SQL
DB.query(query).each do |row|
mod = {}
mod[:username] = row.username
mod[:user_id] = row.user_id
mod[:user_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
mod[:time_read] = row.time_read
mod[:flag_count] = row.flag_count
mod[:revision_count] = row.revision_count
mod[:topic_count] = row.topic_count
mod[:post_count] = row.post_count
mod[:pm_count] = row.pm_count
report.data << mod
end
end

View File

@ -1,26 +0,0 @@
# frozen_string_literal: true
Report.add_report("new_contributors") do |report|
report.data = []
data = User.real.count_by_first_post(report.start_date, report.end_date)
if report.facets.include?(:prev30Days)
prev30DaysData = User.real.count_by_first_post(report.start_date - 30.days, report.start_date)
report.prev30Days = prev30DaysData.sum { |k, v| v }
end
if report.facets.include?(:total)
report.total = User.real.count_by_first_post
end
if report.facets.include?(:prev_period)
prev_period_data = User.real.count_by_first_post(report.prev_start_date, report.prev_end_date)
report.prev_period = prev_period_data.sum { |k, v| v }
# report.prev_data = prev_period_data.map { |k, v| { x: k, y: v } }
end
data.each do |key, value|
report.data << { x: key, y: value }
end
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
Report.add_report("notify_moderators_private_messages") do |report|
report.icon = 'envelope'
private_messages_report report, TopicSubtype.notify_moderators
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
Report.add_report("notify_user_private_messages") do |report|
report.icon = 'envelope'
private_messages_report report, TopicSubtype.notify_user
end

View File

@ -1,110 +0,0 @@
# frozen_string_literal: true
Report.add_report('post_edits') do |report|
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
type: :date,
property: :created_at,
title: I18n.t("reports.post_edits.labels.edited_at")
},
{
type: :post,
properties: {
topic_id: :topic_id,
number: :post_number,
truncated_raw: :post_raw
},
title: I18n.t("reports.post_edits.labels.post")
},
{
type: :user,
properties: {
username: :editor_username,
id: :editor_id,
avatar: :editor_avatar_template,
},
title: I18n.t("reports.post_edits.labels.editor")
},
{
type: :user,
properties: {
username: :author_username,
id: :author_id,
avatar: :author_avatar_template,
},
title: I18n.t("reports.post_edits.labels.author")
},
{
type: :text,
property: :edit_reason,
title: I18n.t("reports.post_edits.labels.edit_reason")
},
]
report.data = []
builder = DB.build <<~SQL
SELECT
pr.user_id AS editor_id,
editor.username AS editor_username,
editor.uploaded_avatar_id AS editor_avatar_id,
p.user_id AS author_id,
author.username AS author_username,
author.uploaded_avatar_id AS author_avatar_id,
pr.number AS revision_version,
p.version AS post_version,
pr.post_id,
LEFT(p.raw, 40) AS post_raw,
p.topic_id,
p.post_number,
p.edit_reason,
pr.created_at
FROM post_revisions pr
JOIN posts p
ON p.id = pr.post_id
JOIN users author
ON author.id = p.user_id
JOIN users editor
ON editor.id = pr.user_id
/*join*/
/*where*/
ORDER BY pr.created_at ASC
/*limit*/
SQL
if category_id
builder.join "topics t ON t.id = p.topic_id"
if include_subcategories
builder.where("t.category_id IN (?)", Category.subcategory_ids(category_id))
else
builder.where("t.category_id = ?", category_id)
end
end
builder.where("editor.id > 0 AND editor.id != author.id")
builder.where("pr.created_at >= :start_date", start_date: report.start_date)
builder.where("pr.created_at <= :end_date", end_date: report.end_date)
result = builder.query
result.each do |r|
revision = {}
revision[:editor_id] = r.editor_id
revision[:editor_username] = r.editor_username
revision[:editor_avatar_template] = User.avatar_template(r.editor_username, r.editor_avatar_id)
revision[:author_id] = r.author_id
revision[:author_username] = r.author_username
revision[:author_avatar_template] = User.avatar_template(r.author_username, r.author_avatar_id)
revision[:edit_reason] = r.revision_version == r.post_version ? r.edit_reason : nil
revision[:created_at] = r.created_at
revision[:post_raw] = r.post_raw
revision[:topic_id] = r.topic_id
revision[:post_number] = r.post_number
report.data << revision
end
end

View File

@ -1,20 +0,0 @@
# frozen_string_literal: true
Report.add_report('posts') do |report|
report.modes = [:table, :chart]
category_id, include_subcategories = report.add_category_filter
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date, category_id, include_subcategories
countable = Post.public_posts.where(post_type: Post.types[:regular])
if category_id
if include_subcategories
countable = countable.joins(:topic).where('topics.category_id IN (?)', Category.subcategory_ids(category_id))
else
countable = countable.joins(:topic).where('topics.category_id = ?', category_id)
end
end
add_counts report, countable, 'posts.created_at'
end

View File

@ -1,13 +0,0 @@
# frozen_string_literal: true
Report.add_report('profile_views') do |report|
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
start_date = report.start_date
end_date = report.end_date
basic_report_about report, UserProfileView, :profile_views_by_day, start_date, end_date, group_filter
report.total = UserProfile.sum(:views)
report.prev30Days = UserProfileView.where('viewed_at >= ? AND viewed_at < ?', start_date - 30.days, start_date + 1).count
end

View File

@ -1,15 +0,0 @@
# frozen_string_literal: true
Report.add_report('signups') do |report|
report.icon = 'user-plus'
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
if group_filter
basic_report_about report, User.real, :count_by_signup_date, report.start_date, report.end_date, group_filter
add_counts report, User.real, 'users.created_at'
else
report_about report, User.real, :count_by_signup_date
end
end

View File

@ -1,59 +0,0 @@
# frozen_string_literal: true
Report.add_report("staff_logins") do |report|
report.modes = [:table]
report.data = []
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.staff_logins.labels.user")
},
{
property: :location,
title: I18n.t("reports.staff_logins.labels.location")
},
{
property: :created_at,
type: :precise_date,
title: I18n.t("reports.staff_logins.labels.login_at")
}
]
sql = <<~SQL
SELECT
t1.created_at created_at,
t1.client_ip client_ip,
u.username username,
u.uploaded_avatar_id uploaded_avatar_id,
u.id user_id
FROM (
SELECT DISTINCT ON (t.client_ip, t.user_id) t.client_ip, t.user_id, t.created_at
FROM user_auth_token_logs t
WHERE t.user_id IN (#{User.admins.pluck(:id).join(',')})
AND t.created_at >= :start_date
AND t.created_at <= :end_date
ORDER BY t.client_ip, t.user_id, t.created_at DESC
LIMIT #{report.limit || 20}
) t1
JOIN users u ON u.id = t1.user_id
ORDER BY created_at DESC
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
data = {}
data[:avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:user_id] = row.user_id
data[:username] = row.username
data[:location] = DiscourseIpInfo.get(row.client_ip)[:location]
data[:created_at] = row.created_at
report.data << data
end
end

View File

@ -1,17 +0,0 @@
# frozen_string_literal: true
Report.add_report("storage_stats") do |report|
backup_stats = begin
BackupRestore::BackupStore.create.stats
rescue BackupRestore::BackupStore::StorageError
nil
end
report.data = {
backups: backup_stats,
uploads: {
used_bytes: DiskSpace.uploads_used_bytes,
free_bytes: DiskSpace.uploads_free_bytes
}
}
end

View File

@ -1,75 +0,0 @@
# frozen_string_literal: true
Report.add_report("suspicious_logins") do |report|
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.suspicious_logins.labels.user")
},
{
property: :client_ip,
title: I18n.t("reports.suspicious_logins.labels.client_ip")
},
{
property: :location,
title: I18n.t("reports.suspicious_logins.labels.location")
},
{
property: :browser,
title: I18n.t("reports.suspicious_logins.labels.browser")
},
{
property: :device,
title: I18n.t("reports.suspicious_logins.labels.device")
},
{
property: :os,
title: I18n.t("reports.suspicious_logins.labels.os")
},
{
type: :date,
property: :login_time,
title: I18n.t("reports.suspicious_logins.labels.login_time")
},
]
report.data = []
sql = <<~SQL
SELECT u.id user_id, u.username, u.uploaded_avatar_id, t.client_ip, t.user_agent, t.created_at login_time
FROM user_auth_token_logs t
JOIN users u ON u.id = t.user_id
WHERE t.action = 'suspicious'
AND t.created_at >= :start_date
AND t.created_at <= :end_date
ORDER BY t.created_at DESC
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
data = {}
ipinfo = DiscourseIpInfo.get(row.client_ip)
browser = BrowserDetection.browser(row.user_agent)
device = BrowserDetection.device(row.user_agent)
os = BrowserDetection.os(row.user_agent)
data[:username] = row.username
data[:user_id] = row.user_id
data[:avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:client_ip] = row.client_ip.to_s
data[:location] = ipinfo[:location]
data[:browser] = I18n.t("user_auth_tokens.browser.#{browser}")
data[:device] = I18n.t("user_auth_tokens.device.#{device}")
data[:os] = I18n.t("user_auth_tokens.os.#{os}")
data[:login_time] = row.login_time
report.data << data
end
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
Report.add_report("system_private_messages") do |report|
report.icon = 'envelope'
private_messages_report report, TopicSubtype.system_message
end

View File

@ -1,18 +0,0 @@
# frozen_string_literal: true
Report.add_report('time_to_first_response') do |report|
category_filter = report.filters.dig(:category)
category_id, include_subcategories = report.add_category_filter
report.icon = 'reply'
report.higher_is_better = false
report.data = []
Topic.time_to_first_response_per_day(report.start_date, report.end_date, category_id: category_id, include_subcategories: include_subcategories).each do |r|
report.data << { x: r['date'], y: r['hours'].to_f.round(2) }
end
report.total = Topic.time_to_first_response_total(category_id: category_id, include_subcategories: include_subcategories)
report.prev30Days = Topic.time_to_first_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: category_id, include_subcategories: include_subcategories)
end

View File

@ -1,77 +0,0 @@
# frozen_string_literal: true
Report.add_report("top_ignored_users") do |report|
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
id: :ignored_user_id,
username: :ignored_username,
avatar: :ignored_user_avatar_template,
},
title: I18n.t("reports.top_ignored_users.labels.ignored_user")
},
{
type: :number,
properties: [
:ignores_count,
],
title: I18n.t("reports.top_ignored_users.labels.ignores_count")
},
{
type: :number,
properties: [
:mutes_count,
],
title: I18n.t("reports.top_ignored_users.labels.mutes_count")
}
]
report.data = []
sql = <<~SQL
WITH ignored_users AS (
SELECT
ignored_user_id as user_id,
COUNT(*) AS ignores_count
FROM ignored_users
WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
GROUP BY ignored_user_id
ORDER BY COUNT(*) DESC
LIMIT :limit
),
muted_users AS (
SELECT
muted_user_id as user_id,
COUNT(*) AS mutes_count
FROM muted_users
WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
GROUP BY muted_user_id
ORDER BY COUNT(*) DESC
LIMIT :limit
)
SELECT u.id as user_id,
u.username as username,
u.uploaded_avatar_id as uploaded_avatar_id,
ig.ignores_count as ignores_count,
COALESCE(mu.mutes_count, 0) as mutes_count,
ig.ignores_count + COALESCE(mu.mutes_count, 0) as total
FROM users as u
JOIN ignored_users as ig ON ig.user_id = u.id
LEFT OUTER JOIN muted_users as mu ON mu.user_id = u.id
ORDER BY total DESC
SQL
DB.query(sql, limit: report.limit || 250).each do |row|
report.data << {
ignored_user_id: row.user_id,
ignored_username: row.username,
ignored_user_avatar_template: User.avatar_template(row.username, row.uploaded_avatar_id),
ignores_count: row.ignores_count,
mutes_count: row.mutes_count,
}
end
end

View File

@ -1,34 +0,0 @@
# frozen_string_literal: true
Report.add_report('top_referred_topics') do |report|
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
type: :topic,
properties: {
title: :topic_title,
id: :topic_id
},
title: I18n.t('reports.top_referred_topics.labels.topic')
},
{
property: :num_clicks,
type: :number,
title: I18n.t('reports.top_referred_topics.labels.num_clicks')
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8,
category_id: category_id,
include_subcategories: include_subcategories
}
result = nil
result = IncomingLinksReport.find(:top_referred_topics, options)
report.data = result.data
end

View File

@ -1,36 +0,0 @@
# frozen_string_literal: true
Report.add_report("top_referrers") do |report|
report.modes = [:table]
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.top_referrers.labels.user")
},
{
property: :num_clicks,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_clicks")
},
{
property: :num_topics,
type: :number,
title: I18n.t("reports.top_referrers.labels.num_topics")
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8
}
result = IncomingLinksReport.find(:top_referrers, options)
report.data = result.data
end

View File

@ -1,35 +0,0 @@
# frozen_string_literal: true
Report.add_report('top_traffic_sources') do |report|
category_id, include_subcategories = report.add_category_filter
report.modes = [:table]
report.labels = [
{
property: :domain,
title: I18n.t('reports.top_traffic_sources.labels.domain')
},
{
property: :num_clicks,
type: :number,
title: I18n.t('reports.top_traffic_sources.labels.num_clicks')
},
{
property: :num_topics,
type: :number,
title: I18n.t('reports.top_traffic_sources.labels.num_topics')
}
]
options = {
end_date: report.end_date,
start_date: report.start_date,
limit: report.limit || 8,
category_id: category_id,
include_subcategories: include_subcategories
}
result = IncomingLinksReport.find(:top_traffic_sources, options)
report.data = result.data
end

View File

@ -1,82 +0,0 @@
# frozen_string_literal: true
Report.add_report('top_uploads') do |report|
report.modes = [:table]
extension_filter = report.filters.dig(:file_extension)
report.add_filter('file_extension',
type: 'list',
default: extension_filter || 'any',
choices: (SiteSetting.authorized_extensions.split('|') + Array(extension_filter)).uniq
)
report.labels = [
{
type: :link,
properties: [
:file_url,
:file_name,
],
title: I18n.t("reports.top_uploads.labels.filename")
},
{
type: :user,
properties: {
username: :author_username,
id: :author_id,
avatar: :author_avatar_template,
},
title: I18n.t("reports.top_uploads.labels.author")
},
{
type: :text,
property: :extension,
title: I18n.t("reports.top_uploads.labels.extension")
},
{
type: :bytes,
property: :filesize,
title: I18n.t("reports.top_uploads.labels.filesize")
},
]
report.data = []
sql = <<~SQL
SELECT
u.id as user_id,
u.username,
u.uploaded_avatar_id,
up.filesize,
up.original_filename,
up.extension,
up.url
FROM uploads up
JOIN users u
ON u.id = up.user_id
/*where*/
ORDER BY up.filesize DESC
LIMIT #{report.limit || 250}
SQL
builder = DB.build(sql)
builder.where("up.id > :seeded_id_threshold", seeded_id_threshold: Upload::SEEDED_ID_THRESHOLD)
builder.where("up.created_at >= :start_date", start_date: report.start_date)
builder.where("up.created_at < :end_date", end_date: report.end_date)
if extension_filter
builder.where("up.extension = :extension", extension: extension_filter.sub(/^\./, ''))
end
builder.query.each do |row|
data = {}
data[:author_id] = row.user_id
data[:author_username] = row.username
data[:author_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:filesize] = row.filesize
data[:extension] = row.extension
data[:file_url] = Discourse.store.cdn_url(row.url)
data[:file_name] = row.original_filename.truncate(25)
report.data << data
end
end

View File

@ -1,12 +0,0 @@
# frozen_string_literal: true
Report.add_report('topics') do |report|
category_id, include_subcategories = report.add_category_filter
basic_report_about report, Topic, :listable_count_per_day, report.start_date, report.end_date, category_id, include_subcategories
countable = Topic.listable_topics
countable = countable.where(category_id: include_subcategories ? Category.subcategory_ids(category_id) : category_id) if category_id
add_counts report, countable, 'topics.created_at'
end

View File

@ -1,14 +0,0 @@
# frozen_string_literal: true
Report.add_report('topics_with_no_response') do |report|
category_id, include_subcategories = report.add_category_filter
report.data = []
Topic.with_no_response_per_day(report.start_date, report.end_date, category_id, include_subcategories).each do |r|
report.data << { x: r['date'], y: r['count'].to_i }
end
report.total = Topic.with_no_response_total(category_id: category_id, include_subcategories: include_subcategories)
report.prev30Days = Topic.with_no_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: category_id, include_subcategories: include_subcategories)
end

View File

@ -1,38 +0,0 @@
# frozen_string_literal: true
Report.add_report("trending_search") do |report|
report.labels = [
{
property: :term,
type: :text,
title: I18n.t("reports.trending_search.labels.term")
},
{
property: :searches,
type: :number,
title: I18n.t("reports.trending_search.labels.searches")
},
{
type: :percent,
property: :ctr,
title: I18n.t("reports.trending_search.labels.click_through")
}
]
report.data = []
report.modes = [:table]
trends = SearchLog.trending_from(report.start_date,
end_date: report.end_date,
limit: report.limit
)
trends.each do |trend|
report.data << {
term: trend.term,
searches: trend.searches,
ctr: trend.ctr
}
end
end

View File

@ -1,77 +0,0 @@
# frozen_string_literal: true
Report.add_report("trust_level_growth") do |report|
report.modes = [:stacked_chart]
filters = %w[
tl1_reached
tl2_reached
tl3_reached
tl4_reached
]
sql = <<~SQL
SELECT
date(created_at),
(
count(*) filter (WHERE previous_value::integer < 1 AND new_value = '1')
) as tl1_reached,
(
count(*) filter (WHERE previous_value::integer < 2 AND new_value = '2')
) as tl2_reached,
(
count(*) filter (WHERE previous_value::integer < 3 AND new_value = '3')
) as tl3_reached,
(
count(*) filter (WHERE previous_value::integer < 4 AND new_value = '4')
) as tl4_reached
FROM user_histories
WHERE (
created_at >= '#{report.start_date}'
AND created_at <= '#{report.end_date}'
)
AND (
action = #{UserHistory.actions[:change_trust_level]}
OR action = #{UserHistory.actions[:auto_trust_level_change]}
)
GROUP BY date(created_at)
SQL
data = Hash[ filters.collect { |x| [x, []] } ]
builder = DB.build(sql)
builder.query.each do |row|
filters.each do |filter|
data[filter] << {
x: row.date.strftime("%Y-%m-%d"),
y: row.instance_variable_get("@#{filter}")
}
end
end
tertiary = ColorScheme.hex_for_name('tertiary') || '0088cc'
quaternary = ColorScheme.hex_for_name('quaternary') || 'e45735'
requests = filters.map do |filter|
color = report.rgba_color(quaternary)
if filter == "tl1_reached"
color = report.lighten_color(tertiary, 0.25)
end
if filter == "tl2_reached"
color = report.rgba_color(tertiary)
end
if filter == "tl3_reached"
color = report.lighten_color(quaternary, 0.25)
end
{
req: filter,
label: I18n.t("reports.trust_level_growth.xaxis.#{filter}"),
color: color,
data: data[filter]
}
end
report.data = requests
end

View File

@ -1,85 +0,0 @@
# frozen_string_literal: true
Report.add_report("user_flagging_ratio") do |report|
report.data = []
report.modes = [:table]
report.dates_filtering = true
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :avatar_template,
},
title: I18n.t("reports.user_flagging_ratio.labels.user")
},
{
type: :number,
property: :disagreed_flags,
title: I18n.t("reports.user_flagging_ratio.labels.disagreed_flags")
},
{
type: :number,
property: :agreed_flags,
title: I18n.t("reports.user_flagging_ratio.labels.agreed_flags")
},
{
type: :number,
property: :ignored_flags,
title: I18n.t("reports.user_flagging_ratio.labels.ignored_flags")
},
{
type: :number,
property: :score,
title: I18n.t("reports.user_flagging_ratio.labels.score")
},
]
statuses = ReviewableScore.statuses
agreed = "SUM(CASE WHEN rs.status = #{statuses[:agreed]} THEN 1 ELSE 0 END)::numeric"
disagreed = "SUM(CASE WHEN rs.status = #{statuses[:disagreed]} THEN 1 ELSE 0 END)::numeric"
ignored = "SUM(CASE WHEN rs.status = #{statuses[:ignored]} THEN 1 ELSE 0 END)::numeric"
sql = <<~SQL
SELECT u.id,
u.username,
u.uploaded_avatar_id as avatar_id,
CASE WHEN u.silenced_till IS NOT NULL THEN 't' ELSE 'f' END as silenced,
#{disagreed} AS disagreed_flags,
#{agreed} AS agreed_flags,
#{ignored} AS ignored_flags,
(
CASE #{disagreed} WHEN 0 THEN #{agreed} * #{agreed}
ELSE ROUND((1-(#{agreed} / #{disagreed})) * (#{disagreed} - #{agreed})) END
) AS score
FROM users AS u
INNER JOIN reviewable_scores AS rs ON rs.user_id = u.id
WHERE u.id > 0
AND rs.created_at >= :start_date
AND rs.created_at <= :end_date
GROUP BY u.id,
u.username,
u.uploaded_avatar_id,
u.silenced_till
ORDER BY score DESC
LIMIT 100
SQL
DB.query(sql, start_date: report.start_date, end_date: report.end_date).each do |row|
flagger = {}
flagger[:user_id] = row.id
flagger[:username] = row.username
flagger[:avatar_template] = User.avatar_template(row.username, row.avatar_id)
flagger[:disagreed_flags] = row.disagreed_flags
flagger[:ignored_flags] = row.ignored_flags
flagger[:agreed_flags] = row.agreed_flags
flagger[:score] = row.score
report.data << flagger
end
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
Report.add_report("user_to_user_private_messages") do |report|
report.icon = 'envelope'
private_messages_report report, TopicSubtype.user_to_user
end

View File

@ -1,10 +0,0 @@
# frozen_string_literal: true
Report.add_report("user_to_user_private_messages_with_replies") do |report|
report.icon = 'envelope'
topic_subtype = TopicSubtype.user_to_user
subject = Post.where('posts.user_id > 0')
basic_report_about report, subject, :private_messages_count_per_day, report.start_date, report.end_date, topic_subtype
subject = Post.private_posts.where('posts.user_id > 0').with_topic_subtype(topic_subtype)
add_counts report, subject, 'posts.created_at'
end

View File

@ -1,27 +0,0 @@
# frozen_string_literal: true
Report.add_report("users_by_trust_level") do |report|
report.data = []
report.modes = [:table]
report.dates_filtering = false
report.labels = [
{
property: :key,
title: I18n.t("reports.users_by_trust_level.labels.level")
},
{
property: :y,
type: :number,
title: I18n.t("reports.default.labels.count")
}
]
User.real.group('trust_level').count.sort.each do |level, count|
key = TrustLevel.levels[level.to_i]
url = Proc.new { |k| "/admin/users/list/#{k}" }
report.data << { url: url.call(key), key: key, x: level.to_i, y: count }
end
end

View File

@ -1,36 +0,0 @@
# frozen_string_literal: true
Report.add_report("users_by_type") do |report|
report.data = []
report.modes = [:table]
report.dates_filtering = false
report.labels = [
{
property: :x,
title: I18n.t("reports.users_by_type.labels.type")
},
{
property: :y,
type: :number,
title: I18n.t("reports.default.labels.count")
}
]
label = Proc.new { |x| I18n.t("reports.users_by_type.xaxis_labels.#{x}") }
url = Proc.new { |key| "/admin/users/list/#{key}" }
admins = User.real.admins.count
report.data << { url: url.call("admins"), icon: "shield-alt", key: "admins", x: label.call("admin"), y: admins } if admins > 0
moderators = User.real.moderators.count
report.data << { url: url.call("moderators"), icon: "shield-alt", key: "moderators", x: label.call("moderator"), y: moderators } if moderators > 0
suspended = User.real.suspended.count
report.data << { url: url.call("suspended"), icon: "ban", key: "suspended", x: label.call("suspended"), y: suspended } if suspended > 0
silenced = User.real.silenced.count
report.data << { url: url.call("silenced"), icon: "ban", key: "silenced", x: label.call("silenced"), y: silenced } if silenced > 0
end

View File

@ -1,13 +0,0 @@
# frozen_string_literal: true
Report.add_report('visits') do |report|
group_filter = report.filters.dig(:group)
report.add_filter('group', type: 'group', default: group_filter)
report.icon = 'user'
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date, group_filter
add_counts report, UserVisit, 'visited_at'
report.prev30Days = UserVisit.where('visited_at >= ? and visited_at < ?', report.start_date - 30.days, report.start_date).count
end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
Report.add_report("mobile_visits") do |report|
basic_report_about report, UserVisit, :mobile_by_day, report.start_date, report.end_date
report.total = UserVisit.where(mobile: true).count
report.prev30Days = UserVisit.where(mobile: true).where("visited_at >= ? and visited_at < ?", report.start_date - 30.days, report.start_date).count
end

View File

@ -1,24 +0,0 @@
# frozen_string_literal: true
Report.add_report('web_crawlers') do |report|
report.labels = [
{
type: :string,
property: :user_agent,
title: I18n.t('reports.web_crawlers.labels.user_agent')
},
{
property: :count,
type: :number,
title: I18n.t('reports.web_crawlers.labels.page_views')
}
]
report.modes = [:table]
report.data = WebCrawlerRequest.where('date >= ? and date <= ?', report.start_date, report.end_date)
.limit(200)
.order('sum_count DESC')
.group(:user_agent).sum(:count)
.map { |ua, count| { user_agent: ua, count: count } }
end