discourse/app/jobs/regular/retrieve_report.rb

28 lines
1.0 KiB
Ruby

require_dependency 'report'
module Jobs
class RetrieveReport < Jobs::Base
sidekiq_options retry: false
def execute(args)
raise Discourse::InvalidParameters.new(:report_type) if !args['report_type']
type = args.delete('report_type')
report = Report.new(type)
report.start_date = (args['start_date'].present? ? args['start_date'].to_date : 30.days.ago).beginning_of_day
report.end_date = (args['end_date'].present? ? args['end_date'].to_date : start_date + 30.days).end_of_day
report.category_id = args['category_id'] if args['category_id']
report.group_id = args['group_id'] if args['group_id']
report.facets = args['facets'].map(&:to_sym) if args['facets']
report.limit = args['limit'].to_i if args['limit']
Report.send("report_#{type}", report)
json = report.as_json
Discourse.cache.write(Report.cache_key(report), json, force: true, expires_in: 30.minutes)
MessageBus.publish("/admin/reports/#{type}", json, user_ids: User.staff.pluck(:id))
end
end
end