A platform for community discussion. Free, open, simple.
FIX: Remove deleted Data Explorer queries from the admin dashboard (#44807)
David Battersby12c0379cd609
diff --git a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rbindex 38da7e72416..79a2d5ca44a 100644--- a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb+++ b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb@@ -251,6 +251,10 @@ module DiscourseDataExplorerdef destroyquery = Query.find(params[:id])query.update!(hidden: true)+ ::AdminDashboard::Reports::Mounter.unmount(+ source: AdminDashboardReportProvider::SOURCE_NAME,+ identifier: query.id.to_s,+ )render json: { success: true, errors: [] }enddiff --git a/plugins/discourse-data-explorer/app/jobs/scheduled/delete_hidden_queries.rb b/plugins/discourse-data-explorer/app/jobs/scheduled/delete_hidden_queries.rbindex 6dac3add8d1..b877b08b835 100644--- a/plugins/discourse-data-explorer/app/jobs/scheduled/delete_hidden_queries.rb+++ b/plugins/discourse-data-explorer/app/jobs/scheduled/delete_hidden_queries.rb@@ -15,6 +15,25 @@ module Jobsdays_ago: 7.days.ago,).delete_all++ unmount_missing_queries_from_dashboard+ end++ private++ # Catches cards left behind before deleting a query unmounted it; they're+ # invisible on the dashboard but still count toward its cap.+ def unmount_missing_queries_from_dashboard+ AdminDashboardReport+ .where(source: DiscourseDataExplorer::AdminDashboardReportProvider::SOURCE_NAME)+ .where(<<~SQL)+ identifier IN (SELECT id::text FROM data_explorer_queries WHERE hidden)+ OR (+ identifier NOT LIKE '-%'+ AND identifier NOT IN (SELECT id::text FROM data_explorer_queries)+ )+ SQL+ .delete_allendendenddiff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.jsindex 0f5db16f953..e9d72db371e 100644--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js@@ -494,6 +494,7 @@ export default class PluginsExplorerController extends Controller {this.showResults = false;await this.store.destroyRecord("query", this.model);this.model.set("destroyed", true);+ this.dashboardMounted = false;} catch (error) {popupAjaxError(error);} finally {diff --git a/plugins/discourse-data-explorer/spec/jobs/scheduled/delete_hidden_queries_spec.rb b/plugins/discourse-data-explorer/spec/jobs/scheduled/delete_hidden_queries_spec.rbindex f06e5e3f0e1..3c972ddeaa0 100644--- a/plugins/discourse-data-explorer/spec/jobs/scheduled/delete_hidden_queries_spec.rb+++ b/plugins/discourse-data-explorer/spec/jobs/scheduled/delete_hidden_queries_spec.rb@@ -65,4 +65,18 @@ describe Jobs::DeleteHiddenQueries dodescribed_class.new.execute(nil)expect(DiscourseDataExplorer::Query.all.length).to eq(4)end++ it "unmounts dashboard cards for hidden and deleted queries" do+ visible = Fabricate(:query)+ hidden = Fabricate(:query, hidden: true)+ %W[#{visible.id} #{hidden.id} 999999 -1].each do |identifier|+ AdminDashboardReport.create!(source: "data_explorer_query", identifier:)+ end++ described_class.new.execute(nil)++ expect(+ AdminDashboardReport.where(source: "data_explorer_query").pluck(:identifier),+ ).to contain_exactly(visible.id.to_s, "-1")+ endenddiff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rbindex e99bac22e07..56d30445e93 100644--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb@@ -446,6 +446,29 @@ describe DiscourseDataExplorer::QueryController doexpect(response.status).to eq(200)expect(query.reload.hidden).to eq(true)end++ it "frees the query's dashboard slot so another report can take it" do+ query = make_query("SELECT 1 as value")+ AdminDashboardReport.create!(source: "data_explorer_query", identifier: query.id.to_s)+ (AdminDashboardReport::VISIBLE_CAP - AdminDashboardReport.count).times do |i|+ AdminDashboardReport.create!(source: "data_explorer_query", identifier: "-#{i + 1}")+ end+ other_query = make_query("SELECT 2 as value")++ delete "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json"++ expect(+ AdminDashboardReport.exists?(source: "data_explorer_query", identifier: query.id.to_s),+ ).to eq(false)++ post "/admin/dashboard/reports/mount.json",+ params: {+ source: "data_explorer_query",+ identifier: other_query.id.to_s,+ }++ expect(response.status).to eq(201)+ endenddescribe "#run" do