From b9d875cc3eb6f0bf05bb533fd14f426e2140e467 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Thu, 18 Apr 2024 21:40:28 +0800 Subject: [PATCH] FEATURE: Add ability to skip sending the PM if there are no results (#286) --- config/locales/client.en.yml | 2 ++ lib/report_generator.rb | 7 ++++--- plugin.rb | 4 +++- spec/automation/recurring_data_explorer_result_pm_spec.rb | 8 ++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 0d93697..3e63947 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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 diff --git a/lib/report_generator.rb b/lib/report_generator.rb index a5f708b..2f504f6 100644 --- a/lib/report_generator.rb +++ b/lib/report_generator.rb @@ -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 diff --git a/plugin.rb b/plugin.rb index 082f2b7..85dbc39 100644 --- a/plugin.rb +++ b/plugin.rb @@ -89,6 +89,7 @@ after_initialize do 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] @@ -97,6 +98,7 @@ after_initialize do 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}" @@ -109,7 +111,7 @@ after_initialize do 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) diff --git a/spec/automation/recurring_data_explorer_result_pm_spec.rb b/spec/automation/recurring_data_explorer_result_pm_spec.rb index 8a687be..b6f8e59 100644 --- a/spec/automation/recurring_data_explorer_result_pm_spec.rb +++ b/spec/automation/recurring_data_explorer_result_pm_spec.rb @@ -84,5 +84,13 @@ describe "RecurringDataExplorerResultPm" do "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