2019-05-03 06:17:27 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
|
class Report
|
2018-08-01 05:35:13 +08:00
|
|
|
|
# Change this line each time report format change
|
|
|
|
|
# and you want to ensure cache is reset
|
2019-04-26 18:17:10 +08:00
|
|
|
|
SCHEMA_VERSION = 4
|
2018-08-01 05:35:13 +08:00
|
|
|
|
|
2020-06-10 23:57:39 +08:00
|
|
|
|
FILTERS = %i[
|
|
|
|
|
name
|
|
|
|
|
start_date
|
|
|
|
|
end_date
|
|
|
|
|
category
|
|
|
|
|
group
|
|
|
|
|
trust_level
|
|
|
|
|
file_extension
|
|
|
|
|
include_subcategories
|
|
|
|
|
]
|
|
|
|
|
|
2020-10-30 00:49:09 +08:00
|
|
|
|
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
|
2022-11-29 19:07:42 +08:00
|
|
|
|
include Reports::ConsolidatedApiRequests
|
2020-10-30 00:49:09 +08:00
|
|
|
|
include Reports::Visits
|
|
|
|
|
include Reports::TimeToFirstResponse
|
|
|
|
|
include Reports::UsersByTrustLevel
|
|
|
|
|
include Reports::ModeratorWarningPrivateMessages
|
|
|
|
|
include Reports::ProfileViews
|
|
|
|
|
include Reports::TopUploads
|
2021-12-03 01:11:55 +08:00
|
|
|
|
include Reports::TopUsersByLikesReceived
|
|
|
|
|
include Reports::TopUsersByLikesReceivedFromInferiorTrustLevel
|
|
|
|
|
include Reports::TopUsersByLikesReceivedFromAVarietyOfPeople
|
2020-10-30 00:49:09 +08:00
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
|
attr_accessor :type,
|
|
|
|
|
:data,
|
|
|
|
|
:total,
|
|
|
|
|
:prev30Days,
|
|
|
|
|
:start_date,
|
2019-04-26 18:17:10 +08:00
|
|
|
|
:end_date,
|
|
|
|
|
:labels,
|
|
|
|
|
:prev_period,
|
|
|
|
|
:facets,
|
|
|
|
|
:limit,
|
|
|
|
|
:average,
|
|
|
|
|
:percent,
|
|
|
|
|
:higher_is_better,
|
|
|
|
|
:icon,
|
|
|
|
|
:modes,
|
|
|
|
|
:prev_data,
|
|
|
|
|
:prev_start_date,
|
|
|
|
|
:prev_end_date,
|
|
|
|
|
:dates_filtering,
|
|
|
|
|
:error,
|
|
|
|
|
:primary_color,
|
|
|
|
|
:secondary_color,
|
|
|
|
|
:filters,
|
|
|
|
|
:available_filters
|
2014-11-05 06:08:39 +08:00
|
|
|
|
|
|
|
|
|
def self.default_days
|
|
|
|
|
30
|
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
2019-06-28 14:50:31 +08:00
|
|
|
|
def self.default_labels
|
|
|
|
|
[
|
|
|
|
|
{ type: :date, property: :x, title: I18n.t("reports.default.labels.day") },
|
|
|
|
|
{ type: :number, property: :y, title: I18n.t("reports.default.labels.count") },
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
|
def initialize(type)
|
|
|
|
|
@type = type
|
2018-08-17 22:19:25 +08:00
|
|
|
|
@start_date ||= Report.default_days.days.ago.utc.beginning_of_day
|
|
|
|
|
@end_date ||= Time.now.utc.end_of_day
|
2018-07-20 02:33:11 +08:00
|
|
|
|
@prev_end_date = @start_date
|
2018-05-18 04:44:33 +08:00
|
|
|
|
@average = false
|
|
|
|
|
@percent = false
|
|
|
|
|
@higher_is_better = true
|
2018-07-20 02:33:11 +08:00
|
|
|
|
@modes = %i[table chart]
|
|
|
|
|
@prev_data = nil
|
|
|
|
|
@dates_filtering = true
|
2019-04-26 18:17:10 +08:00
|
|
|
|
@available_filters = {}
|
|
|
|
|
@filters = {}
|
2018-09-13 23:36:39 +08:00
|
|
|
|
|
|
|
|
|
tertiary = ColorScheme.hex_for_name("tertiary") || "0088cc"
|
|
|
|
|
@primary_color = rgba_color(tertiary)
|
|
|
|
|
@secondary_color = rgba_color(tertiary, 0.1)
|
2013-02-28 11:39:42 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
|
def self.cache_key(report)
|
2018-05-11 11:30:21 +08:00
|
|
|
|
[
|
2020-04-22 16:52:50 +08:00
|
|
|
|
"reports",
|
2018-05-11 11:30:21 +08:00
|
|
|
|
report.type,
|
|
|
|
|
report.start_date.to_date.strftime("%Y%m%d"),
|
|
|
|
|
report.end_date.to_date.strftime("%Y%m%d"),
|
2018-05-15 13:08:23 +08:00
|
|
|
|
report.facets,
|
2018-08-01 05:35:13 +08:00
|
|
|
|
report.limit,
|
2019-04-26 18:17:10 +08:00
|
|
|
|
report.filters.blank? ? nil : MultiJson.dump(report.filters),
|
2018-08-01 05:35:13 +08:00
|
|
|
|
SCHEMA_VERSION,
|
|
|
|
|
].compact.map(&:to_s).join(":")
|
2018-05-03 21:41:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
2019-04-26 18:17:10 +08:00
|
|
|
|
def add_filter(name, options = {})
|
2020-04-22 16:52:50 +08:00
|
|
|
|
if options[:type].blank?
|
|
|
|
|
options[:type] = name
|
2021-11-12 22:52:59 +08:00
|
|
|
|
Discourse.deprecate(
|
|
|
|
|
"#{name} filter should define a `:type` option. Temporarily setting type to #{name}.",
|
|
|
|
|
drop_from: "2.9.0",
|
|
|
|
|
)
|
2020-04-22 16:52:50 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
available_filters[name] = options
|
2019-04-26 18:17:10 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def remove_filter(name)
|
|
|
|
|
available_filters.delete(name)
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-22 16:52:50 +08:00
|
|
|
|
def add_category_filter
|
|
|
|
|
category_id = filters[:category].to_i if filters[:category].present?
|
|
|
|
|
add_filter("category", type: "category", default: category_id)
|
|
|
|
|
return if category_id.blank?
|
|
|
|
|
|
2020-06-10 23:57:39 +08:00
|
|
|
|
include_subcategories = filters[:include_subcategories]
|
2020-04-22 16:52:50 +08:00
|
|
|
|
include_subcategories = !!ActiveRecord::Type::Boolean.new.cast(include_subcategories)
|
2020-06-10 23:57:39 +08:00
|
|
|
|
add_filter("include_subcategories", type: "bool", default: include_subcategories)
|
2020-04-22 16:52:50 +08:00
|
|
|
|
|
|
|
|
|
[category_id, include_subcategories]
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-15 06:14:46 +08:00
|
|
|
|
def self.clear_cache(type = nil)
|
|
|
|
|
pattern = type ? "reports:#{type}:*" : "reports:*"
|
|
|
|
|
|
|
|
|
|
Discourse.cache.keys(pattern).each { |key| Discourse.cache.redis.del(key) }
|
2018-05-03 21:41:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
|
def self.wrap_slow_query(timeout = 20_000)
|
2018-08-01 19:39:57 +08:00
|
|
|
|
ActiveRecord::Base.connection.transaction do
|
2019-08-15 15:27:53 +08:00
|
|
|
|
# Allows only read only transactions
|
|
|
|
|
DB.exec "SET TRANSACTION READ ONLY"
|
2018-08-01 19:39:57 +08:00
|
|
|
|
# Set a statement timeout so we can't tie up the server
|
|
|
|
|
DB.exec "SET LOCAL statement_timeout = #{timeout}"
|
|
|
|
|
yield
|
2018-07-20 02:33:11 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def prev_start_date
|
|
|
|
|
self.start_date - (self.end_date - self.start_date)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def prev_end_date
|
|
|
|
|
self.start_date
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-24 21:19:39 +08:00
|
|
|
|
def as_json(options = nil)
|
2018-05-14 22:34:56 +08:00
|
|
|
|
description = I18n.t("reports.#{type}.description", default: "")
|
2020-03-03 03:30:51 +08:00
|
|
|
|
description_link = I18n.t("reports.#{type}.description_link", default: "")
|
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
|
{
|
2018-08-01 05:35:13 +08:00
|
|
|
|
type: type,
|
2018-12-15 06:14:46 +08:00
|
|
|
|
title: I18n.t("reports.#{type}.title", default: nil),
|
|
|
|
|
xaxis: I18n.t("reports.#{type}.xaxis", default: nil),
|
|
|
|
|
yaxis: I18n.t("reports.#{type}.yaxis", default: nil),
|
2018-08-01 05:35:13 +08:00
|
|
|
|
description: description.presence ? description : nil,
|
2020-03-03 03:30:51 +08:00
|
|
|
|
description_link: description_link.presence ? description_link : nil,
|
2018-08-01 05:35:13 +08:00
|
|
|
|
data: data,
|
|
|
|
|
start_date: start_date&.iso8601,
|
|
|
|
|
end_date: end_date&.iso8601,
|
|
|
|
|
prev_data: self.prev_data,
|
|
|
|
|
prev_start_date: prev_start_date&.iso8601,
|
|
|
|
|
prev_end_date: prev_end_date&.iso8601,
|
|
|
|
|
prev30Days: self.prev30Days,
|
|
|
|
|
dates_filtering: self.dates_filtering,
|
|
|
|
|
report_key: Report.cache_key(self),
|
2018-08-30 20:56:11 +08:00
|
|
|
|
primary_color: self.primary_color,
|
|
|
|
|
secondary_color: self.secondary_color,
|
2019-04-26 18:17:10 +08:00
|
|
|
|
available_filters: self.available_filters.map { |k, v| { id: k }.merge(v) },
|
2019-06-28 14:50:31 +08:00
|
|
|
|
labels: labels || Report.default_labels,
|
2018-08-01 05:35:13 +08:00
|
|
|
|
average: self.average,
|
|
|
|
|
percent: self.percent,
|
|
|
|
|
higher_is_better: self.higher_is_better,
|
|
|
|
|
modes: self.modes,
|
2018-03-16 05:10:45 +08:00
|
|
|
|
}.tap do |json|
|
2018-08-07 04:57:40 +08:00
|
|
|
|
json[:icon] = self.icon if self.icon
|
2018-08-01 09:23:28 +08:00
|
|
|
|
json[:error] = self.error if self.error
|
2018-07-20 02:33:11 +08:00
|
|
|
|
json[:total] = self.total if self.total
|
|
|
|
|
json[:prev_period] = self.prev_period if self.prev_period
|
2018-05-11 11:30:21 +08:00
|
|
|
|
json[:prev30Days] = self.prev30Days if self.prev30Days
|
2018-05-15 13:08:23 +08:00
|
|
|
|
json[:limit] = self.limit if self.limit
|
2018-05-11 11:30:21 +08:00
|
|
|
|
|
2018-03-16 05:10:45 +08:00
|
|
|
|
if type == "page_view_crawler_reqs"
|
|
|
|
|
json[:related_report] = Report.find(
|
|
|
|
|
"web_crawlers",
|
|
|
|
|
start_date: start_date,
|
|
|
|
|
end_date: end_date,
|
|
|
|
|
)&.as_json
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-06-25 08:42:08 +08:00
|
|
|
|
def Report.add_report(name, &block)
|
|
|
|
|
singleton_class.instance_eval { define_method("report_#{name}", &block) }
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-16 14:05:03 +08:00
|
|
|
|
def self._get(type, opts = nil)
|
2014-11-05 06:08:39 +08:00
|
|
|
|
opts ||= {}
|
2015-10-20 04:30:34 +08:00
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
|
# Load the report
|
|
|
|
|
report = Report.new(type)
|
2018-05-03 23:39:37 +08:00
|
|
|
|
report.start_date = opts[:start_date] if opts[:start_date]
|
|
|
|
|
report.end_date = opts[:end_date] if opts[:end_date]
|
2018-05-11 11:30:21 +08:00
|
|
|
|
report.facets = opts[:facets] || %i[total prev30Days]
|
2018-05-15 13:08:23 +08:00
|
|
|
|
report.limit = opts[:limit] if opts[:limit]
|
2018-05-18 04:44:33 +08:00
|
|
|
|
report.average = opts[:average] if opts[:average]
|
|
|
|
|
report.percent = opts[:percent] if opts[:percent]
|
2019-04-26 18:17:10 +08:00
|
|
|
|
report.filters = opts[:filters] if opts[:filters]
|
2019-07-26 15:27:13 +08:00
|
|
|
|
report.labels = Report.default_labels
|
2019-04-26 18:17:10 +08:00
|
|
|
|
|
2018-05-16 14:05:03 +08:00
|
|
|
|
report
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.find_cached(type, opts = nil)
|
|
|
|
|
report = _get(type, opts)
|
|
|
|
|
Discourse.cache.read(cache_key(report))
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-10 00:54:41 +08:00
|
|
|
|
def self.cache(report)
|
|
|
|
|
duration = report.error == :exception ? 1.minute : 35.minutes
|
2019-11-27 13:11:49 +08:00
|
|
|
|
Discourse.cache.write(cache_key(report), report.as_json, expires_in: duration)
|
2018-05-16 14:05:03 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.find(type, opts = nil)
|
2019-02-09 00:25:32 +08:00
|
|
|
|
opts ||= {}
|
|
|
|
|
|
2018-08-01 09:23:28 +08:00
|
|
|
|
begin
|
|
|
|
|
report = _get(type, opts)
|
|
|
|
|
report_method = :"report_#{type}"
|
2015-02-05 13:08:52 +08:00
|
|
|
|
|
2018-08-01 19:39:57 +08:00
|
|
|
|
begin
|
|
|
|
|
wrap_slow_query do
|
|
|
|
|
if respond_to?(report_method)
|
2019-05-07 09:57:55 +08:00
|
|
|
|
public_send(report_method, report)
|
2023-01-21 02:52:49 +08:00
|
|
|
|
elsif type =~ /_reqs\z/
|
|
|
|
|
req_report(report, type.split(/_reqs\z/)[0].to_sym)
|
2018-08-01 19:39:57 +08:00
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue ActiveRecord::QueryCanceled, PG::QueryCanceled => e
|
|
|
|
|
report.error = :timeout
|
2018-08-01 09:23:28 +08:00
|
|
|
|
end
|
|
|
|
|
rescue Exception => e
|
2019-02-09 00:25:32 +08:00
|
|
|
|
# In test mode, don't swallow exceptions by default to help debug errors.
|
|
|
|
|
raise if Rails.env.test? && !opts[:wrap_exceptions_in_test]
|
|
|
|
|
|
2018-09-13 23:36:55 +08:00
|
|
|
|
# ensures that if anything unexpected prevents us from
|
|
|
|
|
# creating a report object we fail elegantly and log an error
|
|
|
|
|
if !report
|
|
|
|
|
Rails.logger.error("Couldn’t create report `#{type}`: <#{e.class} #{e.message}>")
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-01 09:23:28 +08:00
|
|
|
|
report.error = :exception
|
|
|
|
|
|
|
|
|
|
# given reports can be added by plugins we don’t want dashboard failures
|
|
|
|
|
# on report computation, however we do want to log which report is provoking
|
|
|
|
|
# an error
|
2018-10-10 17:43:27 +08:00
|
|
|
|
Rails.logger.error(
|
|
|
|
|
"Error while computing report `#{report.type}`: #{e.message}\n#{e.backtrace.join("\n")}",
|
|
|
|
|
)
|
2015-02-05 13:08:52 +08:00
|
|
|
|
end
|
2015-06-24 21:19:39 +08:00
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
|
report
|
|
|
|
|
end
|
|
|
|
|
|
2015-02-05 08:18:11 +08:00
|
|
|
|
def self.req_report(report, filter = nil)
|
2015-02-06 11:39:04 +08:00
|
|
|
|
data =
|
|
|
|
|
if filter == :page_view_total
|
|
|
|
|
ApplicationRequest.where(
|
|
|
|
|
req_type: [
|
2015-07-04 05:02:57 +08:00
|
|
|
|
ApplicationRequest
|
|
|
|
|
.req_types
|
|
|
|
|
.reject { |k, v| k =~ /mobile/ }
|
|
|
|
|
.map { |k, v| v if k =~ /page_view/ }
|
|
|
|
|
.compact,
|
2015-09-23 13:24:30 +08:00
|
|
|
|
].flatten,
|
|
|
|
|
)
|
2015-02-06 11:39:04 +08:00
|
|
|
|
else
|
2018-08-01 05:35:13 +08:00
|
|
|
|
ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
|
2015-02-06 11:39:04 +08:00
|
|
|
|
end
|
2015-02-05 08:18:11 +08:00
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
|
report.icon = "file" if filter == :page_view_total
|
|
|
|
|
|
2015-02-05 08:18:11 +08:00
|
|
|
|
report.data = []
|
2018-05-03 21:41:41 +08:00
|
|
|
|
data
|
|
|
|
|
.where("date >= ? AND date <= ?", report.start_date, report.end_date)
|
2015-07-07 02:57:11 +08:00
|
|
|
|
.order(date: :asc)
|
2015-04-13 01:46:13 +08:00
|
|
|
|
.group(:date)
|
2015-02-05 08:18:11 +08:00
|
|
|
|
.sum(:count)
|
2015-06-24 21:19:39 +08:00
|
|
|
|
.each { |date, count| report.data << { x: date, y: count } }
|
2015-02-05 08:18:11 +08:00
|
|
|
|
|
2018-02-02 04:50:41 +08:00
|
|
|
|
report.total = data.sum(:count)
|
|
|
|
|
|
|
|
|
|
report.prev30Days =
|
2018-05-03 21:41:41 +08:00
|
|
|
|
data.where("date >= ? AND date < ?", (report.start_date - 31.days), report.start_date).sum(
|
2018-02-02 04:50:41 +08:00
|
|
|
|
:count,
|
|
|
|
|
)
|
2015-02-05 08:18:11 +08:00
|
|
|
|
end
|
|
|
|
|
|
2013-04-01 21:21:34 +08:00
|
|
|
|
def self.report_about(report, subject_class, report_method = :count_per_day)
|
2014-11-06 02:11:23 +08:00
|
|
|
|
basic_report_about report, subject_class, report_method, report.start_date, report.end_date
|
2014-12-30 22:06:15 +08:00
|
|
|
|
add_counts report, subject_class
|
2013-04-01 21:21:34 +08:00
|
|
|
|
end
|
|
|
|
|
|
2013-04-17 04:56:18 +08:00
|
|
|
|
def self.basic_report_about(report, subject_class, report_method, *args)
|
2013-03-08 00:07:59 +08:00
|
|
|
|
report.data = []
|
2018-05-03 23:39:37 +08:00
|
|
|
|
|
2019-05-07 09:27:05 +08:00
|
|
|
|
subject_class
|
|
|
|
|
.public_send(report_method, *args)
|
2015-06-24 21:19:39 +08:00
|
|
|
|
.each { |date, count| report.data << { x: date, y: count } }
|
2013-04-01 21:21:34 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
|
def self.add_prev_data(report, subject_class, report_method, *args)
|
|
|
|
|
if report.modes.include?(:chart) && report.facets.include?(:prev_period)
|
2019-05-07 09:27:05 +08:00
|
|
|
|
prev_data = subject_class.public_send(report_method, *args)
|
2018-07-20 02:33:11 +08:00
|
|
|
|
report.prev_data = prev_data.map { |k, v| { x: k, y: v } }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-30 22:06:15 +08:00
|
|
|
|
def self.add_counts(report, subject_class, query_column = "created_at")
|
2018-05-11 11:30:21 +08:00
|
|
|
|
if report.facets.include?(:prev_period)
|
2018-07-20 02:33:11 +08:00
|
|
|
|
prev_data =
|
|
|
|
|
subject_class.where(
|
2018-05-11 11:30:21 +08:00
|
|
|
|
"#{query_column} >= ? and #{query_column} < ?",
|
2018-07-20 02:33:11 +08:00
|
|
|
|
report.prev_start_date,
|
|
|
|
|
report.prev_end_date,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
report.prev_period = prev_data.count
|
2018-05-11 11:30:21 +08:00
|
|
|
|
end
|
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
|
report.total = subject_class.count if report.facets.include?(:total)
|
2018-05-11 11:30:21 +08:00
|
|
|
|
|
|
|
|
|
if report.facets.include?(:prev30Days)
|
|
|
|
|
report.prev30Days =
|
|
|
|
|
subject_class.where(
|
|
|
|
|
"#{query_column} >= ? and #{query_column} < ?",
|
|
|
|
|
report.start_date - 30.days,
|
|
|
|
|
report.start_date,
|
|
|
|
|
).count
|
|
|
|
|
end
|
2013-03-08 00:07:59 +08:00
|
|
|
|
end
|
|
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
|
def self.post_action_report(report, post_action_type)
|
2020-04-22 16:52:50 +08:00
|
|
|
|
category_id, include_subcategories = report.add_category_filter
|
2019-04-26 18:17:10 +08:00
|
|
|
|
|
2013-03-18 01:53:00 +08:00
|
|
|
|
report.data = []
|
2020-04-22 16:52:50 +08:00
|
|
|
|
PostAction
|
|
|
|
|
.count_per_day_for_type(
|
|
|
|
|
post_action_type,
|
|
|
|
|
category_id: category_id,
|
|
|
|
|
include_subcategories: include_subcategories,
|
|
|
|
|
start_date: report.start_date,
|
|
|
|
|
end_date: report.end_date,
|
2023-01-09 20:20:10 +08:00
|
|
|
|
)
|
2014-07-29 01:17:37 +08:00
|
|
|
|
.each { |date, count| report.data << { x: date, y: count } }
|
2020-04-22 16:52:50 +08:00
|
|
|
|
|
2015-06-24 21:19:39 +08:00
|
|
|
|
countable = PostAction.unscoped.where(post_action_type_id: post_action_type)
|
2020-04-22 16:52:50 +08:00
|
|
|
|
if category_id
|
|
|
|
|
if include_subcategories
|
|
|
|
|
countable =
|
|
|
|
|
countable.joins(post: :topic).where(
|
|
|
|
|
"topics.category_id IN (?)",
|
|
|
|
|
Category.subcategory_ids(category_id),
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
countable = countable.joins(post: :topic).where("topics.category_id = ?", category_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-24 21:19:39 +08:00
|
|
|
|
add_counts report, countable, "post_actions.created_at"
|
2013-03-18 01:53:00 +08:00
|
|
|
|
end
|
2013-04-17 04:56:18 +08:00
|
|
|
|
|
|
|
|
|
def self.private_messages_report(report, topic_subtype)
|
2018-07-20 02:33:11 +08:00
|
|
|
|
report.icon = "envelope"
|
2018-07-23 22:33:12 +08:00
|
|
|
|
subject = Topic.where("topics.user_id > 0")
|
|
|
|
|
basic_report_about report,
|
|
|
|
|
subject,
|
|
|
|
|
:private_message_topics_count_per_day,
|
|
|
|
|
report.start_date,
|
|
|
|
|
report.end_date,
|
|
|
|
|
topic_subtype
|
|
|
|
|
subject = Topic.private_messages.where("topics.user_id > 0").with_subtype(topic_subtype)
|
|
|
|
|
add_counts report, subject, "topics.created_at"
|
2013-04-17 04:56:18 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-30 20:56:11 +08:00
|
|
|
|
def rgba_color(hex, opacity = 1)
|
2019-05-08 00:14:13 +08:00
|
|
|
|
rgbs = hex_to_rgbs(adjust_hex(hex))
|
|
|
|
|
"rgba(#{rgbs.join(",")},#{opacity})"
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-04 14:35:19 +08:00
|
|
|
|
def colors
|
|
|
|
|
%w[#1EB8D1 #9BC53D #721D8D #E84A5F #8A6916]
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-08 00:14:13 +08:00
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def adjust_hex(hex)
|
|
|
|
|
hex = hex.gsub("#", "")
|
|
|
|
|
|
2018-11-16 04:41:05 +08:00
|
|
|
|
if hex.size == 3
|
2018-11-15 07:52:47 +08:00
|
|
|
|
chars = hex.scan(/\w/)
|
2018-11-16 04:41:05 +08:00
|
|
|
|
hex = chars.zip(chars).flatten.join
|
2018-11-15 07:52:47 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
hex = hex.ljust(6, hex.last) if hex.size < 3
|
|
|
|
|
|
2019-05-08 00:14:13 +08:00
|
|
|
|
hex
|
2018-08-30 20:56:11 +08:00
|
|
|
|
end
|
2019-01-21 22:17:04 +08:00
|
|
|
|
|
|
|
|
|
def hex_to_rgbs(hex_color)
|
|
|
|
|
hex_color = hex_color.gsub("#", "")
|
|
|
|
|
rgbs = hex_color.scan(/../)
|
|
|
|
|
rgbs.map! { |color| color.hex }.map! { |rgb| rgb.to_i }
|
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
end
|