2023-03-24 04:38:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ::DiscourseDataExplorer
|
|
|
|
class ReportGenerator
|
2024-03-27 05:40:26 -04:00
|
|
|
def self.generate(query_id, query_params, recipients)
|
2023-03-24 04:38:42 -04:00
|
|
|
query = DiscourseDataExplorer::Query.find(query_id)
|
2024-03-27 05:40:26 -04:00
|
|
|
return [] if !query || recipients.empty?
|
2023-03-24 04:38:42 -04:00
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
recipients = filter_recipients_by_query_access(recipients, query)
|
2023-03-24 04:38:42 -04:00
|
|
|
params = params_to_hash(query_params)
|
|
|
|
|
|
|
|
result = DataExplorer.run_query(query, params)
|
|
|
|
query.update!(last_run_at: Time.now)
|
|
|
|
|
|
|
|
table = ResultToMarkdown.convert(result[:pg_result])
|
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
build_report_pms(query, table, recipients)
|
2023-03-24 04:38:42 -04:00
|
|
|
end
|
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.params_to_hash(query_params)
|
2023-03-24 04:38:42 -04:00
|
|
|
params = JSON.parse(query_params)
|
|
|
|
params_hash = {}
|
|
|
|
|
|
|
|
if !params.blank?
|
|
|
|
param_key, param_value = [], []
|
|
|
|
params.flatten.each.with_index do |data, i|
|
|
|
|
if i % 2 == 0
|
|
|
|
param_key << data
|
|
|
|
else
|
|
|
|
param_value << data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params_hash = Hash[param_key.zip(param_value)]
|
|
|
|
end
|
|
|
|
|
|
|
|
params_hash
|
|
|
|
end
|
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
def self.build_report_pms(query, table = "", targets = [])
|
2023-03-24 04:38:42 -04:00
|
|
|
pms = []
|
2024-03-27 05:40:26 -04:00
|
|
|
targets.each do |target|
|
|
|
|
name = target[0]
|
|
|
|
pm_type = "target_#{target[1]}s"
|
|
|
|
|
2023-03-24 04:38:42 -04:00
|
|
|
pm = {}
|
|
|
|
pm["title"] = "Scheduled Report for #{query.name}"
|
2024-03-27 05:40:26 -04:00
|
|
|
pm[pm_type] = Array(name)
|
|
|
|
pm["raw"] = "Hi #{name}, your data explorer report is ready.\n\n" +
|
2023-03-24 04:38:42 -04:00
|
|
|
"Query Name:\n#{query.name}\n\nHere are the results:\n#{table}\n\n" +
|
2023-07-30 22:14:12 -04:00
|
|
|
"<a href='#{Discourse.base_url}/admin/plugins/explorer?id=#{query.id}'>View query in Data Explorer</a>\n\n" +
|
2023-03-24 04:38:42 -04:00
|
|
|
"Report created at #{Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S")} (#{Time.zone.name})"
|
|
|
|
pms << pm
|
|
|
|
end
|
|
|
|
pms
|
|
|
|
end
|
2024-01-03 01:05:03 -05:00
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
def self.filter_recipients_by_query_access(recipients, query)
|
|
|
|
users = User.where(username: recipients)
|
|
|
|
groups = Group.where(name: recipients)
|
|
|
|
emails = recipients - users.pluck(:username) - groups.pluck(:name)
|
|
|
|
result = []
|
2024-01-03 01:05:03 -05:00
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
users.each do |user|
|
|
|
|
result << [user.username, "username"] if Guardian.new(user).user_can_access_query?(query)
|
|
|
|
end
|
2024-01-03 01:05:03 -05:00
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
groups.each do |group|
|
|
|
|
if group.id == Group::AUTO_GROUPS[:admins] || query.query_groups.exists?(group_id: group.id)
|
|
|
|
result << [group.name, "group_name"]
|
|
|
|
end
|
2024-01-03 01:05:03 -05:00
|
|
|
end
|
2024-03-27 05:40:26 -04:00
|
|
|
|
|
|
|
emails.each { |email| result << [email, "email"] if Email.is_valid?(email) }
|
|
|
|
|
|
|
|
result
|
2024-01-03 01:05:03 -05:00
|
|
|
end
|
2023-03-24 04:38:42 -04:00
|
|
|
end
|
|
|
|
end
|