Skip to content

Commit

Permalink
FIX: Scheduled PM with Data Explorer Results not sending PM (#250)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
dbattersby committed Jul 4, 2023
1 parent 7bf6c7d commit ee308c6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/result_to_markdown.rb
Expand Up @@ -17,7 +17,7 @@ def self.convert(pg_result)

row.each_with_index do |col, col_index|
col_name = pg_result.fields[col_index]
related = relations.dig(colrender[col_index].to_sym) if col_index < colrender.size
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)
Expand All @@ -30,7 +30,7 @@ def self.convert(pg_result)
if column.nil?
row_data[col_index] = col
else
row_data[col_index] = related_row[column]
row_data[col_index] = "#{related_row[column]} (#{col})"
end
else
row_data[col_index] = col
Expand All @@ -41,7 +41,7 @@ def self.convert(pg_result)
end

table_headers = pg_result.fields.map { |c| " #{c.gsub("_id", "")} |" }.join
table_body = pg_result.fields.size.times.map { " :-----: |" }.join
table_body = pg_result.fields.size.times.map { " :----- |" }.join

"|#{table_headers}\n|#{table_body}\n#{result_data.join}"
end
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Expand Up @@ -100,7 +100,7 @@ module ::DiscourseDataExplorer
script do |_, fields, automation|
recipients = Array(fields.dig("recipients", "value"))
query_id = fields.dig("query_id", "value")
query_params = fields.dig("query_params", "value")
query_params = fields.dig("query_params", "value") || {}

unless SiteSetting.data_explorer_enabled
Rails.logger.warn "#{DiscourseDataExplorer.plugin_name} - plugin must be enabled to run automation #{automation.id}"
Expand Down
34 changes: 34 additions & 0 deletions spec/result_to_markdown_spec.rb
@@ -0,0 +1,34 @@
# frozen_string_literal: true

describe DiscourseDataExplorer::ResultToMarkdown do
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(: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
it "format results as a markdown table with headers and columns" do
result = described_class.convert(query_result[:pg_result])

table = <<~MD
| liker_user | liked_user | count |
| :----- | :----- | :----- |
MD

expect(result).to include(table)
end

it "enriches result data within the table rows" do
PostActionCreator.new(user, post, PostActionType.types[:like]).perform
result = described_class.convert(query_result[:pg_result])

expect(result).to include(
"| #{user.username} (#{user.id}) | #{post.user.username} (#{post.user.id}) | 1 |\n",
)
end
end
end

0 comments on commit ee308c6

Please sign in to comment.