Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add ability to skip sending the PM if there are no results #286

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading