Skip to content

Commit

Permalink
FEATURE: Add ability to skip sending the PM if there are no results (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nattsw committed Apr 18, 2024
1 parent d8d7bbb commit b9d875c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ en:
label: Data Explorer Query
query_params:
label: Data Explorer Query parameters
skip_empty:
label: Skip sending PM if there are no results

7 changes: 4 additions & 3 deletions lib/report_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

module ::DiscourseDataExplorer
class ReportGenerator
def self.generate(query_id, query_params, recipients)
def self.generate(query_id, query_params, recipients, opts = {})
query = DiscourseDataExplorer::Query.find(query_id)
return [] if !query || recipients.empty?

recipients = filter_recipients_by_query_access(recipients, query)
params = params_to_hash(query_params)

result = DataExplorer.run_query(query, params)
result = DataExplorer.run_query(query, params)[:pg_result]
query.update!(last_run_at: Time.now)

table = ResultToMarkdown.convert(result[:pg_result])
return [] if opts[:skip_empty] && result.values.empty?
table = ResultToMarkdown.convert(result)

build_report_pms(query, table, recipients)
end
Expand Down
4 changes: 3 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module ::DiscourseDataExplorer
field :recipients, component: :email_group_user, required: true
field :query_id, component: :choices, required: true, extra: { content: queries }
field :query_params, component: :"key-value", accepts_placeholders: true
field :skip_empty, component: :boolean

version 1
triggerables [:recurring]
Expand All @@ -97,6 +98,7 @@ module ::DiscourseDataExplorer
recipients = Array(fields.dig("recipients", "value")).uniq
query_id = fields.dig("query_id", "value")
query_params = fields.dig("query_params", "value") || {}
skip_empty = fields.dig("skip_empty", "value") || false

unless SiteSetting.data_explorer_enabled
Rails.logger.warn "#{DiscourseDataExplorer::PLUGIN_NAME} - plugin must be enabled to run automation #{automation.id}"
Expand All @@ -109,7 +111,7 @@ module ::DiscourseDataExplorer
end

DiscourseDataExplorer::ReportGenerator
.generate(query_id, query_params, recipients)
.generate(query_id, query_params, recipients, { skip_empty: })
.each do |pm|
begin
utils.send_pm(pm, automation_id: automation.id, prefers_encrypt: false)
Expand Down
8 changes: 8 additions & 0 deletions spec/automation/recurring_data_explorer_result_pm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,13 @@
"Hi #{another_group.name}, your data explorer report is ready.\n\nQuery Name:\n#{query.name}",
)
end

it "does not send the PM if skip_empty" do
automation.upsert_field!("skip_empty", "boolean", { value: true })

automation.update(last_updated_by_id: admin.id)

expect { automation.trigger! }.to_not change { Post.count }
end
end
end

0 comments on commit b9d875c

Please sign in to comment.