2018-05-03 09:41:41 -04:00
|
|
|
require_dependency 'report'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class RetrieveReport < Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
2018-05-11 01:06:09 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:report_type) if !args['report_type']
|
2018-05-03 09:41:41 -04:00
|
|
|
|
2018-05-11 01:06:09 -04:00
|
|
|
type = args.delete('report_type')
|
2018-05-03 09:41:41 -04:00
|
|
|
report = Report.new(type)
|
2018-05-11 01:06:09 -04:00
|
|
|
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']
|
2018-05-03 09:41:41 -04:00
|
|
|
|
|
|
|
Report.send("report_#{type}", report)
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
json = report.as_json
|
|
|
|
Discourse.cache.write(Report.cache_key(report), json, force: true, expires_in: 30.minutes)
|
2018-05-03 09:41:41 -04:00
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
MessageBus.publish("/admin/reports/#{type}", json, user_ids: User.staff.pluck(:id))
|
2018-05-03 09:41:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|