This repository was archived by the owner on Jul 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
FEATURE: Allows CSV file result to be attached in automated PMs #318
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -130,5 +130,25 @@ | |
| expect(result[1]["target_group_names"]).to eq([group.name]) | ||
| expect(result[2]["target_emails"]).to eq(["john@doe.com"]) | ||
| end | ||
|
|
||
| it "works with attached csv file" do | ||
| SiteSetting.personal_message_enabled_groups = group.id | ||
| DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| freeze_time | ||
|
|
||
| result = | ||
| described_class.generate(query.id, query_params, [user.username], { attach_csv: true }) | ||
|
|
||
| filename = | ||
| "#{query.slug}@#{Slug.for(Discourse.current_hostname, "discourse")}-#{Date.today}.dcqresult.csv" | ||
|
|
||
| expect(result[0]["raw"]).to include( | ||
| "Hi #{user.username}, your data explorer report is ready.\n\n" + | ||
| "Query Name:\n#{query.name}\n\nHere are the results:\nle table\n\n" + | ||
| "<a href='#{Discourse.base_url}/admin/plugins/explorer?id=#{query.id}'>View query in Data Explorer</a>\n\n" + | ||
| "Report created at #{Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S")} (#{Time.zone.name})\n\n" + | ||
| "Appendix: [#{filename}|attachment](upload://", | ||
| ) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking if we can promote the return value from a hash to a class or struct instead. Then we could implement for example
result.empty?and use that on L:15. It could also implementresult.convertwhich could be delegated to the new converter class. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? But I'm not sure. This result structure should be used in many places. Changing it will affect many files.