-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Allows CSV file result to be attached in automated PMs (#318)
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>
- Loading branch information
Showing
7 changed files
with
157 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
describe DiscourseDataExplorer::ResultFormatConverter do | ||
fab!(:user) | ||
fab!(:post) | ||
fab!(:query) { DiscourseDataExplorer::Query.find(-1) } | ||
|
||
let(:query_params) { [{ from_days_ago: 0 }, { duration_days: 15 }] } | ||
let(:query_result) { DiscourseDataExplorer::DataExplorer.run_query(query, query_params) } | ||
|
||
before { SiteSetting.data_explorer_enabled = true } | ||
|
||
describe ".convert" do | ||
context "for csv files" do | ||
it "format results as a csv table with headers and columns" do | ||
result = described_class.convert(:csv, query_result) | ||
|
||
table = <<~CSV | ||
liker_user_id,liked_user_id,count | ||
CSV | ||
|
||
expect(result).to include(table) | ||
end | ||
end | ||
|
||
context "for json files" do | ||
it "format results as a json file" do | ||
result = described_class.convert(:json, query_result, { query_params: }) | ||
|
||
expect(result[:columns]).to contain_exactly("liker_user_id", "liked_user_id", "count") | ||
expect(result[:params]).to eq(query_params) | ||
end | ||
end | ||
end | ||
end |