mirror of
https://github.com/discourse/discourse-data-explorer.git
synced 2025-02-10 05:24:55 +00:00
This PR fixes 2 issues that were picked up by users for the Scheduled Data Explorer Report automation script and a couple of small improvements to better match the format of manual data explorer query results. The first issue is that within result_to_markdown the colrender contains null values, and we are currently checking length (previously treated as a packed array but it is actually a sparse array). Therefore we can check if the current index of the array is null rather than checking the size of the array. The second issue addresses the blank query_params field. When the data explorer script does not require any params to be passed in via the automation script then it will have a nil value, however it should be defaulted to {} within the plugin. To improve formatting the markdown table for PMs is now aligned to left and where values are substituted (for example user_id becomes username) we then include the id within brackets, for example: user_id becomes username (user_id)
50 lines
1.5 KiB
Ruby
50 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
include HasSanitizableFields
|
|
|
|
module ::DiscourseDataExplorer
|
|
class ResultToMarkdown
|
|
def self.convert(pg_result)
|
|
relations, colrender = DataExplorer.add_extra_data(pg_result)
|
|
result_data = []
|
|
|
|
# column names to search in place of id columns (topic_id, user_id etc)
|
|
cols = %w[name title username]
|
|
|
|
# find values from extra data, based on result id
|
|
pg_result.values.each do |row|
|
|
row_data = []
|
|
|
|
row.each_with_index do |col, col_index|
|
|
col_name = pg_result.fields[col_index]
|
|
related = relations.dig(colrender[col_index].to_sym) unless colrender[col_index].nil?
|
|
|
|
if related.is_a?(ActiveModel::ArraySerializer)
|
|
related_row = related.object.find_by(id: col)
|
|
if col_name.include?("_id")
|
|
column = cols.find { |c| related_row.try c }
|
|
else
|
|
column = related_row.try(col_name)
|
|
end
|
|
|
|
if column.nil?
|
|
row_data[col_index] = col
|
|
else
|
|
row_data[col_index] = "#{related_row[column]} (#{col})"
|
|
end
|
|
else
|
|
row_data[col_index] = col
|
|
end
|
|
end
|
|
|
|
result_data << row_data.map { |c| "| #{sanitize_field(c.to_s)} " }.join + "|\n"
|
|
end
|
|
|
|
table_headers = pg_result.fields.map { |c| " #{c.gsub("_id", "")} |" }.join
|
|
table_body = pg_result.fields.size.times.map { " :----- |" }.join
|
|
|
|
"|#{table_headers}\n|#{table_body}\n#{result_data.join}"
|
|
end
|
|
end
|
|
end
|