mirror of
https://github.com/discourse/discourse-data-explorer.git
synced 2025-02-18 01:14:57 +00:00
This commit adds an optional setting that allows to attach query results in CSV format as a file to PMs sent by Data Explorer's automation scripts. meta topic: https://meta.discourse.org/t/turn-data-explorer-query-results-into-csv-to-attach-to-discourse-automated-emails/267529 Co-authored-by: Drenmi <drenmi@gmail.com>
60 lines
1.3 KiB
Ruby
60 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
module ::DiscourseDataExplorer
|
|
class ResultFormatConverter
|
|
def self.convert(file_type, result, opts = {})
|
|
self.new(result, opts).send("to_#{file_type}")
|
|
end
|
|
|
|
def initialize(result, opts)
|
|
@result = result
|
|
@opts = opts
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :result
|
|
attr_reader :opts
|
|
|
|
def pg_result
|
|
@pg_result ||= @result[:pg_result]
|
|
end
|
|
|
|
def cols
|
|
@cols ||= pg_result.fields
|
|
end
|
|
|
|
def to_csv
|
|
require "csv"
|
|
CSV.generate do |csv|
|
|
csv << cols
|
|
pg_result.values.each { |row| csv << row }
|
|
end
|
|
end
|
|
|
|
def to_json
|
|
json = {
|
|
success: true,
|
|
errors: [],
|
|
duration: (result[:duration_secs].to_f * 1000).round(1),
|
|
result_count: pg_result.values.length || 0,
|
|
params: opts[:query_params],
|
|
columns: cols,
|
|
default_limit: SiteSetting.data_explorer_query_result_limit,
|
|
}
|
|
json[:explain] = result[:explain] if opts[:explain]
|
|
|
|
if !opts[:download]
|
|
relations, colrender = DataExplorer.add_extra_data(pg_result)
|
|
json[:relations] = relations
|
|
json[:colrender] = colrender
|
|
end
|
|
|
|
json[:rows] = pg_result.values
|
|
|
|
json
|
|
end
|
|
|
|
#TODO: we can move ResultToMarkdown here
|
|
end
|
|
end
|