Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add data explorer query tags (#43807)

David Battersbya8b024fac5c1
diff --git a/db/structure.sql b/db/structure.sql
index b3daa63b7cd..a81215dc485 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -3961,6 +3961,68 @@ CREATE SEQUENCE public.data_explorer_query_stats_id_seq
ALTER SEQUENCE public.data_explorer_query_stats_id_seq OWNED BY public.data_explorer_query_stats.id;
+--
+-- Name: data_explorer_query_tags; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.data_explorer_query_tags (
+ id bigint NOT NULL,
+ query_id bigint NOT NULL,
+ query_tag_id bigint NOT NULL,
+ created_at timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
+);
+
+
+--
+-- Name: data_explorer_query_tags_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.data_explorer_query_tags_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: data_explorer_query_tags_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.data_explorer_query_tags_id_seq OWNED BY public.data_explorer_query_tags.id;
+
+
+--
+-- Name: data_explorer_tags; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.data_explorer_tags (
+ id bigint NOT NULL,
+ name character varying(100) NOT NULL,
+ created_at timestamp(6) without time zone NOT NULL,
+ updated_at timestamp(6) without time zone NOT NULL
+);
+
+
+--
+-- Name: data_explorer_tags_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.data_explorer_tags_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: data_explorer_tags_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.data_explorer_tags_id_seq OWNED BY public.data_explorer_tags.id;
+
+
--
-- Name: developers; Type: TABLE; Schema: public; Owner: -
--
@@ -14323,6 +14385,20 @@ ALTER TABLE ONLY public.data_explorer_query_groups ALTER COLUMN id SET DEFAULT n
ALTER TABLE ONLY public.data_explorer_query_stats ALTER COLUMN id SET DEFAULT nextval('public.data_explorer_query_stats_id_seq'::regclass);
+--
+-- Name: data_explorer_query_tags id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.data_explorer_query_tags ALTER COLUMN id SET DEFAULT nextval('public.data_explorer_query_tags_id_seq'::regclass);
+
+
+--
+-- Name: data_explorer_tags id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.data_explorer_tags ALTER COLUMN id SET DEFAULT nextval('public.data_explorer_tags_id_seq'::regclass);
+
+
--
-- Name: developers id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -16897,6 +16973,22 @@ ALTER TABLE ONLY public.data_explorer_query_stats
ADD CONSTRAINT data_explorer_query_stats_pkey PRIMARY KEY (id);
+--
+-- Name: data_explorer_query_tags data_explorer_query_tags_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.data_explorer_query_tags
+ ADD CONSTRAINT data_explorer_query_tags_pkey PRIMARY KEY (id);
+
+
+--
+-- Name: data_explorer_tags data_explorer_tags_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.data_explorer_tags
+ ADD CONSTRAINT data_explorer_tags_pkey PRIMARY KEY (id);
+
+
--
-- Name: developers developers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -19375,6 +19467,27 @@ CREATE INDEX idx_chat_messages_thread_id_id_user_id_not_deleted ON public.chat_m
CREATE INDEX idx_chat_pinned_messages_channel_created ON public.chat_pinned_messages USING btree (chat_channel_id, created_at DESC);
+--
+-- Name: idx_data_explorer_query_tags_on_query_tag; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX idx_data_explorer_query_tags_on_query_tag ON public.data_explorer_query_tags USING btree (query_id, query_tag_id);
+
+
+--
+-- Name: idx_data_explorer_query_tags_on_tag_query; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX idx_data_explorer_query_tags_on_tag_query ON public.data_explorer_query_tags USING btree (query_tag_id, query_id);
+
+
+--
+-- Name: idx_data_explorer_tags_on_name; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX idx_data_explorer_tags_on_name ON public.data_explorer_tags USING btree (name);
+
+
--
-- Name: idx_discourse_automation_user_global_notices; Type: INDEX; Schema: public; Owner: -
--
@@ -25397,6 +25510,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260921074918'),
('20260921015711'),
('20260918061735'),
+('20260917145657'),
('20260915204557'),
('20260915191328'),
('20260914213908'),
diff --git a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
index b979b3927ec..aa0d64340cb 100644
--- a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
@@ -20,6 +20,7 @@ import QueryModeSwitch from "discourse/plugins/discourse-data-explorer/discourse
import QueryResultDownloadButtons from "discourse/plugins/discourse-data-explorer/discourse/components/query-result-download-buttons";
import QueryResultsWrapper from "discourse/plugins/discourse-data-explorer/discourse/components/query-results-wrapper";
import QueryRunSplitButton from "discourse/plugins/discourse-data-explorer/discourse/components/query-run-split-button";
+import QueryTagChooser from "discourse/plugins/discourse-data-explorer/discourse/components/query-tag-chooser";
const PaneSeparator = <template>
<DResizeSeparator
@@ -96,15 +97,31 @@ export default class QueriesEdit extends Component {
{{/if}}
{{#unless @controller.model.destroyed}}
- <div class="groups">
- <span class="label">{{i18n "explorer.allow_groups"}}</span>
- <span>
+ <div class="query-edit__metadata-fields">
+ <div class="query-edit__metadata-field">
+ <span class="query-edit__metadata-label">
+ {{i18n "explorer.allow_groups"}}
+ </span>
<GroupChooser
@content={{@controller.groupOptions}}
@onChange={{@controller.updateGroupIds}}
@value={{@controller.model.group_ids}}
/>
- </span>
+ </div>
+
+ <div class="query-edit__metadata-field">
+ <span class="query-edit__metadata-label">
+ {{i18n "explorer.query_tags"}}
+ </span>
+ <QueryTagChooser
+ @allowDefaultTag={{@controller.model.is_default}}
+ @availableTags={{@controller.availableTags}}
+ @mandatoryValues={{@controller.mandatoryTags}}
+ @mandatoryValueTitle={{i18n "explorer.default_tag_mandatory"}}
+ @onChange={{@controller.updateTags}}
+ @value={{@controller.model.tags}}
+ />
+ </div>
</div>
{{/unless}}
diff --git a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/index.gjs b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/index.gjs
index 784c2428fd4..4cdd310dca4 100644
--- a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/index.gjs
+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/index.gjs
@@ -5,6 +5,7 @@ import { not } from "discourse/truth-helpers";
import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
import DFilterControls from "discourse/ui-kit/d-filter-controls";
import DLoadMore from "discourse/ui-kit/d-load-more";
+import DMultiSelect from "discourse/ui-kit/d-multi-select";
import DPageSubheader from "discourse/ui-kit/d-page-subheader";
import DPickFilesButton from "discourse/ui-kit/d-pick-files-button";
import DTableHeaderToggle from "discourse/ui-kit/d-table-header-toggle";
@@ -36,7 +37,9 @@ export default <template>
</DPageSubheader>
<DFilterControls
+ @additionalFiltersActive={{@controller.hasTagFilter}}
@array={{@controller.model.content}}
+ @initialTextFilter={{@controller.textFilter}}
@inputPlaceholder={{i18n "explorer.search_placeholder"}}
@loading={{@controller.searchLoading}}
@noResultsMessage={{i18n "explorer.no_search_results"}}
@@ -52,6 +55,19 @@ export default <template>
{{/if}}
</:aboveFilters>
+ <:additionalFilters>
+ <DMultiSelect
+ class="query-tag-filter"
+ @label={{i18n "explorer.filter_by_tag"}}
+ @loadFn={{@controller.loadTags}}
+ @onChange={{@controller.onTagFilterChange}}
+ @selection={{@controller.tagSelection}}
+ >
+ <:selection as |tag|>{{tag.name}}</:selection>
+ <:result as |tag|>{{tag.name}}</:result>
+ </DMultiSelect>
+ </:additionalFilters>
+
<:content as |filteredQueries|>
{{#if @controller.model.content.length}}
<DConditionalLoadingSpinner @condition={{@controller.loading}} />
@@ -128,18 +144,28 @@ export default <template>
>
<div
class="d-table__overview-name query-name"
- >{{query.name}}
- {{#if query.is_default}}
- <span class="query-badge">{{i18n
- "explorer.default_query"
- }}</span>
- {{/if}}
- </div>
+ >{{query.name}}</div>
<div
class="query-desc"
title={{query.description}}
>{{query.description}}</div>
</LinkTo>
+ {{#if query.tags.length}}
+ <div class="d-table__badges query-tags">
+ {{#each query.tags as |tag|}}
+ <a
+ class="d-table-badge"
+ href={{@controller.tagFilterHref tag}}
+ title={{i18n "explorer.filter_by_this_tag"}}
+ {{on "click" (fn @controller.addTagFilter tag)}}
+ >
+ <span
+ class="d-table-badge__content"
+ >{{tag}}</span>
+ </a>
+ {{/each}}
+ </div>
+ {{/if}}
</td>
<td class="d-table__cell --detail query-created-by">
<div class="d-table__mobile-label">
diff --git a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
index 53ac2f4984b..816f811e590 100644
--- a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
@@ -14,6 +14,7 @@ import { i18n } from "discourse-i18n";
import ExplorerSchema from "discourse/plugins/discourse-data-explorer/discourse/components/explorer-schema";
import QueryModeSwitch from "discourse/plugins/discourse-data-explorer/discourse/components/query-mode-switch";
import QueryResult from "discourse/plugins/discourse-data-explorer/discourse/components/query-result";
+import QueryTagChooser from "discourse/plugins/discourse-data-explorer/discourse/components/query-tag-chooser";
export default <template>
<div class="admin-detail">
@@ -151,15 +152,30 @@ export default <template>
{{on "input" @controller.updateDescription}}
/>
- <label class="query-new__field-label">
- {{i18n "explorer.allow_groups"}}
- </label>
- <GroupChooser
- class="query-group-select"
- @content={{@controller.groupOptions}}
- @onChange={{@controller.updateAiGroupIds}}
- @value={{@controller.aiGroupIds}}
- />
+ <div class="query-new__access-fields">
+ <div class="query-new__access-field">
+ <label class="query-new__field-label">
+ {{i18n "explorer.allow_groups"}}
+ </label>
+ <GroupChooser
+ class="query-group-select"
+ @content={{@controller.groupOptions}}
+ @onChange={{@controller.updateAiGroupIds}}
+ @value={{@controller.aiGroupIds}}
+ />
+ </div>
+
+ <div class="query-new__access-field">
+ <label class="query-new__field-label">
+ {{i18n "explorer.query_tags"}}
+ </label>
+ <QueryTagChooser
+ @availableTags={{@controller.availableTags}}
+ @onChange={{@controller.updateAiTags}}
+ @value={{@controller.aiTags}}
+ />
+ </div>
+ </div>
</div>
<div class="query-new__actions">
@@ -204,22 +220,43 @@ export default <template>
>
<field.Control />
</form.Field>
- <form.Field
- @format="full"
- @name="groupIds"
- @title={{i18n "explorer.allow_groups"}}
- @type="custom"
- as |field|
- >
- <field.Control>
- <GroupChooser
- class="query-group-select"
- @content={{@controller.groupOptions}}
- @onChange={{field.set}}
- @value={{field.value}}
- />
- </field.Control>
- </form.Field>
+ <form.Row as |row|>
+ <row.Col @size={{6}}>
+ <form.Field
+ @format="full"
+ @name="groupIds"
+ @title={{i18n "explorer.allow_groups"}}
+ @type="custom"
+ as |field|
+ >
+ <field.Control>
+ <GroupChooser
+ class="query-group-select"
+ @content={{@controller.groupOptions}}
+ @onChange={{field.set}}
+ @value={{field.value}}
+ />
+ </field.Control>
+ </form.Field>
+ </row.Col>
+ <row.Col @size={{6}}>
+ <form.Field
+ @format="full"
+ @name="tags"
+ @title={{i18n "explorer.query_tags"}}
+ @type="custom"
+ as |field|
+ >
+ <field.Control>
+ <QueryTagChooser
+ @availableTags={{@controller.availableTags}}
+ @onChange={{field.set}}
+ @value={{field.value}}
+ />
+ </field.Control>
+ </form.Field>
+ </row.Col>
+ </form.Row>
<label class="query-new__sql-label">
{{i18n "explorer.ai.sql_label"}}
</label>
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.rb
index ee26b0768a9..ca8211b3d7f 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
@@ -20,11 +20,12 @@ module DiscourseDataExplorer
limit = INDEX_LIMIT
offset = params[:offset].to_i
filter = params[:filter]
+ tag_names = QueryTag.normalize_all((params[:tags].presence || params[:tag]).to_s.split(","))
order_column = SORTABLE_COLUMNS.include?(params[:order]) ? params[:order] : "last_run_at"
order_direction = params[:ascending] == "true" ? :asc : :desc
- base_scope = DiscourseDataExplorer::Query.where(hidden: false).includes(:groups)
+ base_scope = DiscourseDataExplorer::Query.where(hidden: false).includes(:groups, :tags)
if order_column == "username"
base_scope =
@@ -44,9 +45,12 @@ module DiscourseDataExplorer
)
end
+ base_scope = base_scope.filter_by_tags(tag_names) if tag_names.present?
+
persisted_count = base_scope.count
- unpersisted_defaults = DiscourseDataExplorer::Query.unpersisted_defaults(search: filter)
+ unpersisted_defaults =
+ DiscourseDataExplorer::Query.unpersisted_defaults(search: filter, tags: tag_names)
total_rows = persisted_count + unpersisted_defaults.size
@@ -65,10 +69,12 @@ module DiscourseDataExplorer
json = serialize_data(queries, QuerySerializer, root: "queries")
json["total_rows_queries"] = total_rows
+ json["extras"] = { "tags" => DiscourseDataExplorer::Query.available_tags }
if next_offset < persisted_count
load_more_params = { offset: next_offset }
load_more_params[:filter] = filter if filter.present?
+ load_more_params[:tags] = tag_names.join(",") if tag_names.present?
load_more_params[:order] = order_column if order_column != "last_run_at"
load_more_params[:ascending] = "true" if order_direction == :asc
base_path = request.path.delete_suffix(".json")
@@ -102,6 +108,10 @@ module DiscourseDataExplorer
render json: Group.all.select(:id, :name).as_json(only: %i[id name]), root: false
end
+ def tags
+ render json: Query.available_tags, root: false
+ end
+
def group_reports_index
return raise Discourse::NotFound unless guardian.user_is_a_member_of_group?(@group)
@@ -146,7 +156,7 @@ module DiscourseDataExplorer
end
def create
- query_params = params.require(:query).permit(:name, :description, :sql)
+ query_params = params.require(:query).permit(:name, :description, :sql, tags: [])
group_ids = params.require(:query)[:group_ids]
query =
@@ -209,15 +219,26 @@ module DiscourseDataExplorer
def update
sql_changed = @query.sql != params.dig(:query, :sql)
+ submitted_query = params.require(:query)
+ query_params = submitted_query.permit(:name, :sql, :description, tags: [])
+ tag_names = query_params.delete(:tags)
ActiveRecord::Base.transaction do
- @query.update!(
- params.require(:query).permit(:name, :sql, :description).merge(hidden: false),
- )
+ @query.update!(query_params.merge(hidden: false))
+ if tag_names || submitted_query.key?(:tags_present)
+ QueryTag.sync!(query: @query, names: tag_names || [])
+ end
- group_ids = params.require(:query)[:group_ids]
- QueryGroup.where.not(group_id: group_ids).where(query_id: @query.id).delete_all
- group_ids&.each { |group_id| @query.query_groups.find_or_create_by!(group_id: group_id) }
+ if submitted_query.key?(:group_ids) || submitted_query.key?(:group_ids_present)
+ group_ids = Array.wrap(submitted_query[:group_ids]).reject(&:blank?)
+ query_groups = QueryGroup.where(query_id: @query.id)
+ if group_ids.empty?
+ query_groups.delete_all
+ else
+ query_groups.where.not(group_id: group_ids).delete_all
+ end
+ group_ids.each { |group_id| @query.query_groups.find_or_create_by!(group_id: group_id) }
+ end
end
QueryRunner.invalidate(@query.id) if sql_changed
diff --git a/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query.rb b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query.rb
index 4013b752df9..901fb36b113 100644
--- a/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query.rb
+++ b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query.rb
@@ -16,8 +16,18 @@ module DiscourseDataExplorer
class Query < ActiveRecord::Base
self.table_name = "data_explorer_queries"
+ DEFAULT_TAG = "default"
+
has_many :query_groups
has_many :groups, through: :query_groups
+ has_many :query_tag_mappings,
+ class_name: "DiscourseDataExplorer::QueryTagMapping",
+ dependent: :delete_all
+ has_many :tags,
+ -> { order(:name) },
+ class_name: "DiscourseDataExplorer::QueryTag",
+ through: :query_tag_mappings,
+ source: :query_tag
belongs_to :user
validates :name, presence: true
@@ -31,6 +41,25 @@ module DiscourseDataExplorer
end
scope :user_queries, -> { where(hidden: false).where("id > 0") }
+ scope :filter_by_tags,
+ ->(tag_names) do
+ Array
+ .wrap(tag_names)
+ .reduce(all) do |scope, tag_name|
+ default_query_condition =
+ tag_name == DEFAULT_TAG ? "data_explorer_queries.id < 0 OR" : ""
+ scope.where(<<~SQL, tag_name)
+ (#{default_query_condition} EXISTS (
+ SELECT 1
+ FROM data_explorer_query_tags query_tag_mappings
+ JOIN data_explorer_tags query_tags
+ ON query_tags.id = query_tag_mappings.query_tag_id
+ WHERE query_tag_mappings.query_id = data_explorer_queries.id
+ AND query_tags.name = ?
+ ))
+ SQL
+ end
+ end
def self.is_default_query?(id)
id.to_i < 0
@@ -62,7 +91,20 @@ module DiscourseDataExplorer
QueryFinder.find(id)
end
- def self.unpersisted_defaults(search: nil)
+ def self.available_tags
+ tag_names =
+ QueryTag
+ .joins(query_tag_mappings: :query)
+ .where(data_explorer_queries: { hidden: false })
+ .distinct
+ .pluck(:name)
+ (tag_names << DEFAULT_TAG).uniq.sort_by(&:downcase)
+ end
+
+ def self.unpersisted_defaults(search: nil, tags: nil, tag: nil)
+ selected_tags = QueryTag.normalize_all(tags || tag)
+ return [] if selected_tags.any? { |name| name != DEFAULT_TAG }
+
persisted_ids = where(hidden: false).where("id < 0").pluck(:id).to_set
query_text = search&.downcase
@@ -81,6 +123,12 @@ module DiscourseDataExplorer
end
end
+ def tag_names
+ names = tags.map(&:name)
+ names.unshift(DEFAULT_TAG) if self.class.is_default_query?(id)
+ names.uniq
+ end
+
private
# for `Query.unscoped.find`
diff --git a/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag.rb b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag.rb
new file mode 100644
index 00000000000..1c066b2b3bc
--- /dev/null
+++ b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag.rb
@@ -0,0 +1,117 @@
+# frozen_string_literal: true
+
+module DiscourseDataExplorer
+ class QueryTag < ActiveRecord::Base
+ self.table_name = "data_explorer_tags"
+
+ MAX_TAGS_PER_QUERY = 10
+ MAX_NAME_LENGTH = 100
+
+ has_many :query_tag_mappings,
+ class_name: "DiscourseDataExplorer::QueryTagMapping",
+ dependent: :delete_all
+
+ validates :name,
+ presence: true,
+ uniqueness: true,
+ length: {
+ maximum: MAX_NAME_LENGTH,
+ },
+ format: {
+ without: /,/,
+ }
+
+ def self.normalize_name(name)
+ name.to_s.strip.downcase.gsub(/[[:space:]]+/, " ")
+ end
+
+ def self.normalize_all(names)
+ Array.wrap(names).map { |name| normalize_name(name) }.reject(&:blank?).uniq
+ end
+
+ def self.validate_names(names, errors)
+ if names.size > MAX_TAGS_PER_QUERY
+ errors.add(
+ :base,
+ I18n.t("discourse_data_explorer.errors.too_many_tags", max: MAX_TAGS_PER_QUERY),
+ )
+ end
+
+ if names.any? { |name| name.length > MAX_NAME_LENGTH }
+ errors.add(
+ :base,
+ I18n.t("discourse_data_explorer.errors.tag_too_long", max: MAX_NAME_LENGTH),
+ )
+ end
+
+ if names.any? { |name| name.include?(",") }
+ errors.add(:base, I18n.t("discourse_data_explorer.errors.tag_contains_comma"))
+ end
+ end
+
+ def self.sync!(query:, names:)
+ names = normalize_all(names)
+ validate_names!(query:, names:)
+ names.delete(Query::DEFAULT_TAG) if query.id.negative?
+
+ desired = resolve_or_create!(names)
+ desired_ids = desired.map(&:id)
+ current_ids = QueryTagMapping.where(query_id: query.id).pluck(:query_tag_id)
+
+ removed_ids = current_ids - desired_ids
+ if removed_ids.present?
+ QueryTagMapping.where(query_id: query.id, query_tag_id: removed_ids).delete_all
+ end
+ (desired_ids - current_ids).each do |query_tag_id|
+ QueryTagMapping.create!(query_id: query.id, query_tag_id:)
+ end
+
+ prune!(removed_ids)
+ query.association(:tags).reset
+ desired
+ end
+
+ def self.prune!(tag_ids)
+ return if tag_ids.blank?
+
+ where(id: tag_ids).where.not(id: QueryTagMapping.select(:query_tag_id)).delete_all
+ end
+
+ def self.resolve_or_create!(names)
+ return [] if names.blank?
+
+ now = Time.zone.now
+ insert_all(
+ names.map { |name| { name:, created_at: now, updated_at: now } },
+ unique_by: :idx_data_explorer_tags_on_name,
+ )
+ where(name: names).to_a
+ end
+
+ def self.validate_names!(query:, names:)
+ errors = ActiveModel::Errors.new(query)
+ validate_names(query.id.negative? ? (names | [Query::DEFAULT_TAG]) : names, errors)
+ if !Query.is_default_query?(query.id) && names.include?(Query::DEFAULT_TAG)
+ errors.add(:base, I18n.t("discourse_data_explorer.errors.default_tag_reserved"))
+ end
+ return if errors.empty?
+
+ errors.each { |error| query.errors.import(error) }
+ raise ActiveRecord::RecordInvalid.new(query)
+ end
+ end
+end
+
+# == Schema Information
+#
+# Table name: data_explorer_tags
+#
+# id :bigint not null, primary key
+# name :string(100) not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+# Indexes
+#
+# idx_data_explorer_tags_on_name (name) UNIQUE
+#
diff --git a/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag_mapping.rb b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag_mapping.rb
new file mode 100644
index 00000000000..dd0aaa90441
--- /dev/null
+++ b/plugins/discourse-data-explorer/app/models/discourse_data_explorer/query_tag_mapping.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module DiscourseDataExplorer
+ class QueryTagMapping < ActiveRecord::Base
+ self.table_name = "data_explorer_query_tags"
+
+ belongs_to :query, class_name: "DiscourseDataExplorer::Query"
+ belongs_to :query_tag, class_name: "DiscourseDataExplorer::QueryTag"
+ end
+end
+
+# == Schema Information
+#
+# Table name: data_explorer_query_tags
+#
+# id :bigint not null, primary key
+# created_at :datetime not null
+# query_id :bigint not null
+# query_tag_id :bigint not null
+#
+# Indexes
+#
+# idx_data_explorer_query_tags_on_query_tag (query_id,query_tag_id) UNIQUE
+# idx_data_explorer_query_tags_on_tag_query (query_tag_id,query_id) UNIQUE
+#
diff --git a/plugins/discourse-data-explorer/app/resources/discourse_data_explorer/query_resource.rb b/plugins/discourse-data-explorer/app/resources/discourse_data_explorer/query_resource.rb
index 55467f7fb53..6fa7431b20c 100644
--- a/plugins/discourse-data-explorer/app/resources/discourse_data_explorer/query_resource.rb
+++ b/plugins/discourse-data-explorer/app/resources/discourse_data_explorer/query_resource.rb
@@ -4,12 +4,13 @@ module DiscourseDataExplorer
class QueryResource < JsonApiKit::Resource
namespace "data-explorer"
- scope { Query.where(hidden: false) }
+ scope { Query.where(hidden: false).includes(:tags) }
attribute :name
attribute :description
attribute :created_at
attribute :last_run_at
+ attribute(:tags) { it.tag_names }
attribute :sql, readable: ->(guardian) { guardian.is_admin? }
attribute(:param_info) { it.params.uniq(&:identifier).map(&:to_hash) }
attribute(:is_default) { it.id.negative? }
@@ -33,5 +34,9 @@ module DiscourseDataExplorer
table = Query.arel_table
scope.where(table[:name].matches(pattern).or(table[:description].matches(pattern)))
end
+
+ filter :tag do |scope, value|
+ scope.filter_by_tags(QueryTag.normalize_name(value))
+ end
end
end
diff --git a/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_serializer.rb b/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_serializer.rb
index 4f19cd336dd..cf5cf8b72b8 100644
--- a/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_serializer.rb
+++ b/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_serializer.rb
@@ -2,7 +2,15 @@
module DiscourseDataExplorer
class QuerySerializer < ActiveModel::Serializer
- attributes :id, :name, :description, :username, :group_ids, :last_run_at, :user_id, :is_default
+ attributes :id,
+ :name,
+ :description,
+ :username,
+ :group_ids,
+ :last_run_at,
+ :user_id,
+ :is_default,
+ :tags
def username
object&.user&.username
@@ -15,5 +23,9 @@ module DiscourseDataExplorer
def is_default
object.id < 0
end
+
+ def tags
+ object.tag_names
+ end
end
end
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-tag-chooser.gjs b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-tag-chooser.gjs
new file mode 100644
index 00000000000..e53db47c2a1
--- /dev/null
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-tag-chooser.gjs
@@ -0,0 +1,58 @@
+import Component from "@glimmer/component";
+import { hash } from "@ember/helper";
+import ListSetting from "discourse/select-kit/components/list-setting";
+
+const DEFAULT_QUERY_TAG = "default";
+
+class QueryTagListSetting extends ListSetting {
+ validateCreate(filter, content) {
+ return (
+ (this.allowDefaultTag ||
+ filter.trim().toLowerCase() !== DEFAULT_QUERY_TAG) &&
+ super.validateCreate(filter, content)
+ );
+ }
+}
+
+export default class QueryTagChooser extends Component {
+ get choices() {
+ const tags = [
+ ...(this.args.availableTags ?? []),
+ ...(this.args.value ?? []),
+ ];
+ return this.#uniqueTags(
+ this.args.allowDefaultTag
+ ? tags
+ : tags.filter((tag) => tag?.trim().toLowerCase() !== DEFAULT_QUERY_TAG)
+ );
+ }
+
+ #uniqueTags(tags = []) {
+ const tagsByName = new Map();
+
+ for (const tag of tags) {
+ const name = tag?.trim();
+ if (name && !tagsByName.has(name.toLowerCase())) {
+ tagsByName.set(name.toLowerCase(), name);
+ }
+ }
+
+ return [...tagsByName.values()].sort((left, right) =>
+ left.localeCompare(right, undefined, { sensitivity: "base" })
+ );
+ }
+
+ <template>
+ <QueryTagListSetting
+ class="query-tag-chooser"
+ ...attributes
+ @allowDefaultTag={{@allowDefaultTag}}
+ @choices={{this.choices}}
+ @mandatoryValues={{@mandatoryValues}}
+ @mandatoryValueTitle={{@mandatoryValueTitle}}
+ @onChange={{@onChange}}
+ @options={{hash allowAny=true}}
+ @value={{@value}}
+ />
+ </template>
+}
diff --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.js
index cb0ad776238..c961e7e9c8c 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
@@ -20,6 +20,7 @@ import {
} from "discourse/plugins/discourse-data-explorer/discourse/lib/data-explorer-store";
import Query from "discourse/plugins/discourse-data-explorer/discourse/models/query";
+const DEFAULT_QUERY_TAG = "default";
const HIDE_SCHEMA_KEY = "hide_schema";
export default class PluginsExplorerController extends Controller {
@@ -122,6 +123,10 @@ export default class PluginsExplorerController extends Controller {
});
}
+ get mandatoryTags() {
+ return this.model.is_default ? DEFAULT_QUERY_TAG : null;
+ }
+
get hasResults() {
return !!this.results?.rows?.length;
}
@@ -166,6 +171,7 @@ export default class PluginsExplorerController extends Controller {
description: this.model.description ?? "",
sql: this.model.sql ?? "",
group_ids: [...(this.model.group_ids ?? [])].sort().join(","),
+ tags: [...(this.model.tags ?? [])].sort().join(","),
};
this.dirty = false;
}
@@ -180,12 +186,14 @@ export default class PluginsExplorerController extends Controller {
description: this.model.description ?? "",
sql: this.model.sql ?? "",
group_ids: [...(this.model.group_ids ?? [])].sort().join(","),
+ tags: [...(this.model.tags ?? [])].sort().join(","),
};
this.dirty =
current.name !== this._pristine.name ||
current.description !== this._pristine.description ||
current.sql !== this._pristine.sql ||
- current.group_ids !== this._pristine.group_ids;
+ current.group_ids !== this._pristine.group_ids ||
+ current.tags !== this._pristine.tags;
}
initView() {
@@ -372,6 +380,15 @@ export default class PluginsExplorerController extends Controller {
this.recomputeDirty();
}
+ @action
+ updateTags(value) {
+ const tags = this.model.is_default
+ ? [DEFAULT_QUERY_TAG, ...value.filter((tag) => tag !== DEFAULT_QUERY_TAG)]
+ : value;
+ this.model.set("tags", tags);
+ this.recomputeDirty();
+ }
+
@action
editName() {
this.editingName = true;
@@ -483,10 +500,11 @@ export default class PluginsExplorerController extends Controller {
return;
}
}
+ const stringifiedParams = JSON.stringify(params);
this.setProperties({
loading: true,
showResults: false,
- params: JSON.stringify(params),
+ params: params === null ? null : stringifiedParams,
});
ajax(
@@ -496,7 +514,7 @@ export default class PluginsExplorerController extends Controller {
{
type: "POST",
data: {
- params: JSON.stringify(params),
+ params: stringifiedParams,
explain,
},
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/index.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/index.js
index a760dd64421..a15d9644423 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/index.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/index.js
@@ -8,6 +8,8 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseDebounce from "discourse/lib/debounce";
import { bind } from "discourse/lib/decorators";
import { INPUT_DELAY } from "discourse/lib/environment";
+import getURL from "discourse/lib/get-url";
+import { applyQueryParams } from "discourse/lib/url";
import { i18n } from "discourse-i18n";
export default class PluginsExplorerController extends Controller {
@@ -20,23 +22,29 @@ export default class PluginsExplorerController extends Controller {
@tracked params;
@tracked loading = false;
@tracked searchLoading = false;
+ @tracked queryTags = [];
+ @tracked tags = "";
- queryParams = ["id"];
+ queryParams = ["id", "tags"];
explain = false;
acceptedImportFileTypes = ["application/json"];
order = null;
form = null;
+ #latestQueryRequest;
_currentFilter = "";
get parsedParams() {
return this.params ? JSON.parse(this.params) : null;
}
- get _fetchParams() {
+ get fetchParams() {
const params = {};
if (this._currentFilter) {
params.filter = this._currentFilter;
}
+ if (this.currentTags.length) {
+ params.tags = this.currentTags.join(",");
+ }
if (this.sortByProperty !== "last_run_at") {
params.order = this.sortByProperty;
}
@@ -46,6 +54,22 @@ export default class PluginsExplorerController extends Controller {
return params;
}
+ get hasTagFilter() {
+ return this.currentTags.length > 0;
+ }
+
+ get currentTags() {
+ return this.tags ? this.tags.split(",") : [];
+ }
+
+ get textFilter() {
+ return this._currentFilter;
+ }
+
+ get tagSelection() {
+ return this.currentTags.map((tag) => ({ id: tag, name: tag }));
+ }
+
addCreatedRecord(record) {
this.model.content.push(record);
this.router.transitionTo("adminPlugins.show.explorer.edit", record.id);
@@ -83,13 +107,52 @@ export default class PluginsExplorerController extends Controller {
@action
onTextFilterChange(event) {
this._currentFilter = event.target?.value || "";
+ this.#latestQueryRequest = null;
this.searchLoading = true;
discourseDebounce(this, this._fetchQueries, INPUT_DELAY);
}
+ @action
+ async loadTags(searchTerm) {
+ const term = (searchTerm || "").toLowerCase();
+ return this.queryTags
+ .filter((tag) => tag.toLowerCase().includes(term))
+ .map((tag) => ({ id: tag, name: tag }));
+ }
+
+ @action
+ onTagFilterChange(selection) {
+ this.tags = selection.map((tag) => tag.name).join(",");
+ this.searchLoading = true;
+ this._fetchQueries();
+ }
+
+ @action
+ addTagFilter(tagName, event) {
+ event?.preventDefault();
+
+ if (this.currentTags.includes(tagName)) {
+ return;
+ }
+
+ this.tags = [...this.currentTags, tagName].join(",");
+ this.searchLoading = true;
+ this._fetchQueries();
+ }
+
+ @action
+ tagFilterHref(tagName) {
+ return getURL(
+ applyQueryParams(this.router.urlFor("adminPlugins.show.explorer.index"), {
+ tags: tagName,
+ })
+ );
+ }
+
@action
onResetFilters() {
this._currentFilter = "";
+ this.tags = "";
this.searchLoading = true;
this._fetchQueries();
}
@@ -145,25 +208,37 @@ export default class PluginsExplorerController extends Controller {
}
async _fetchQueries() {
+ const model = this.model;
+ const request = (this.#latestQueryRequest = {});
+
try {
const result = await ajax(
"/admin/plugins/discourse-data-explorer/queries.json",
- { data: this._fetchParams }
+ { data: this.fetchParams }
);
+ if (request !== this.#latestQueryRequest || model !== this.model) {
+ return;
+ }
+
const queries = result.queries.map((q) =>
this.store.createRecord("query", q)
);
- this.model.content.splice(0, this.model.content.length, ...queries);
- this.model.totalRows = result.total_rows_queries || queries.length;
- this.model.loadMoreUrl = result.load_more_queries || null;
+ model.content.splice(0, model.content.length, ...queries);
+ model.totalRows = result.total_rows_queries || queries.length;
+ model.loadMoreUrl = result.load_more_queries || null;
+ this.queryTags = result.extras?.tags ?? this.queryTags;
this._setGroupNames(queries);
} catch (e) {
- popupAjaxError(e);
+ if (request === this.#latestQueryRequest && model === this.model) {
+ popupAjaxError(e);
+ }
} finally {
- this.searchLoading = false;
+ if (request === this.#latestQueryRequest) {
+ this.searchLoading = false;
+ }
}
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
index 046f4230e12..5446fd92038 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
@@ -34,7 +34,9 @@ export default class AdminPluginsExplorerNew extends Controller {
@tracked mode = rememberedMode() ?? "ai";
@tracked schema = null;
@tracked groups = null;
+ @tracked availableTags = [];
@tracked aiGroupIds = [];
+ @tracked aiTags = [];
@tracked hideSchema = dataExplorerStore.get(HIDE_SCHEMA_KEY) === "true";
@tracked manualSql = "SELECT 1";
@tracked previewLoading = false;
@@ -42,7 +44,7 @@ export default class AdminPluginsExplorerNew extends Controller {
@tracked showPreview = false;
@tracked view = "sql";
- manualFormData = { name: "", description: "", groupIds: [] };
+ manualFormData = { name: "", description: "", groupIds: [], tags: [] };
_teardownAiGeneration = null;
get previewDisabled() {
@@ -132,7 +134,12 @@ export default class AdminPluginsExplorerNew extends Controller {
}
@action
- async create({ name, description, groupIds }) {
+ updateAiTags(value) {
+ this.aiTags = value;
+ }
+
+ @action
+ async create({ name, description, groupIds, tags }) {
try {
this.loading = true;
const result = await this.store
@@ -141,6 +148,7 @@ export default class AdminPluginsExplorerNew extends Controller {
description: description?.trim(),
sql: this.manualSql,
group_ids: groupIds,
+ tags,
})
.save();
this.toasts.success({
@@ -261,6 +269,7 @@ export default class AdminPluginsExplorerNew extends Controller {
description: this.generatedDescription,
sql: this.generatedSql,
group_ids: this.aiGroupIds,
+ tags: this.aiTags,
})
.save();
this.toasts.success({
@@ -308,11 +317,18 @@ export default class AdminPluginsExplorerNew extends Controller {
this.mode = rememberedMode() ?? "ai";
this.schema = null;
this.groups = null;
+ this.availableTags = [];
this.aiGroupIds = [];
+ this.aiTags = [];
this.hideSchema = dataExplorerStore.get(HIDE_SCHEMA_KEY) === "true";
this.manualSql = "SELECT 1";
this.loading = false;
- this.manualFormData = { name: "", description: "", groupIds: [] };
+ this.manualFormData = {
+ name: "",
+ description: "",
+ groupIds: [],
+ tags: [],
+ };
this.previewLoading = false;
this.previewResults = null;
this.showPreview = false;
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
index ffea27b41c2..4f8fcbea374 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
@@ -11,6 +11,7 @@ export default class Query extends RestModel {
"created_at",
"group_ids",
"last_run_at",
+ "tags",
];
params = {};
@@ -66,6 +67,8 @@ export default class Query extends RestModel {
updateProperties() {
const props = this.getProperties(Query.updatePropertyNames);
+ props.group_ids_present = true;
+ props.tags_present = true;
if (this.destroyed) {
props.id = this.id;
}
@@ -76,6 +79,6 @@ export default class Query extends RestModel {
if (this.sql) {
return this.updateProperties();
}
- return this.getProperties("name", "description", "group_ids");
+ return this.getProperties("name", "description", "group_ids", "tags");
}
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
index 91d47b1909f..e0f3f5bbff1 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
@@ -14,43 +14,35 @@ export default class AdminPluginsExplorerQueriesDetails extends DiscourseRoute {
},
};
- model(params, transition) {
- const groupPromise = ajax(
- "/admin/plugins/discourse-data-explorer/groups.json"
- );
- const schemaPromise = ajax(
- "/admin/plugins/discourse-data-explorer/schema.json",
- {
- cache: true,
- }
- );
-
+ async model(params, transition) {
const data = {};
const urlParams = transition.to.queryParams.params;
if (urlParams) {
data.params = urlParams;
}
- const queryPromise = ajax(
- `/admin/plugins/discourse-data-explorer/queries/${params.query_id}`,
- { data }
- );
- return groupPromise.then((groups) => {
- let groupNames = {};
- groups.forEach((g) => {
- groupNames[g.id] = g.name;
- });
- return schemaPromise.then((schema) => {
- return queryPromise.then((queryResponse) => {
- const model = this.store.createRecord("query", queryResponse.query);
- model.set(
- "group_names",
- (model.group_ids || []).map((id) => groupNames[id])
- );
- return { model, schema, groups };
- });
- });
+ const [groups, schema, availableTags, queryResponse] = await Promise.all([
+ ajax("/admin/plugins/discourse-data-explorer/groups.json"),
+ ajax("/admin/plugins/discourse-data-explorer/schema.json", {
+ cache: true,
+ }),
+ ajax("/admin/plugins/discourse-data-explorer/queries/tags.json"),
+ ajax(
+ `/admin/plugins/discourse-data-explorer/queries/${params.query_id}`,
+ { data }
+ ),
+ ]);
+ const groupNames = {};
+ groups.forEach((group) => {
+ groupNames[group.id] = group.name;
});
+ const model = this.store.createRecord("query", queryResponse.query);
+ model.set(
+ "group_names",
+ (model.group_ids || []).map((id) => groupNames[id])
+ );
+
+ return { model, schema, groups, availableTags };
}
setupController(controller, model, transition) {
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/index.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/index.js
index a29c6d21256..e8d5d0c7fd1 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/index.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/index.js
@@ -15,10 +15,19 @@ export default class AdminPluginsExplorerIndex extends DiscourseRoute {
}
}
- async model() {
+ async model(_params, transition) {
+ const filterParams = this.controllerFor(
+ "adminPlugins.show.explorer.index"
+ ).fetchParams;
+ const tags = transition.to.queryParams.tags;
+ if (tags) {
+ filterParams.tags = tags;
+ } else {
+ delete filterParams.tags;
+ }
const [groups, model] = await Promise.all([
ajax("/admin/plugins/discourse-data-explorer/groups.json"),
- this.store.findAll("query"),
+ this.store.findAll("query", filterParams),
]);
const groupNames = {};
@@ -31,7 +40,7 @@ export default class AdminPluginsExplorerIndex extends DiscourseRoute {
(query.group_ids || []).map((id) => groupNames[id])
);
});
- return { model, groups };
+ return { model, groups, queryTags: model.extras?.tags ?? [] };
}
setupController(controller, model) {
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
index 01d7f15552b..3c75932742c 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
@@ -3,19 +3,24 @@ import DiscourseRoute from "discourse/routes/discourse";
export default class AdminPluginsExplorerNew extends DiscourseRoute {
async model() {
- const [schema, groups] = await Promise.all([
+ const [schema, groups, availableTags] = await Promise.all([
ajax("/admin/plugins/discourse-data-explorer/schema.json", {
cache: true,
}),
ajax("/admin/plugins/discourse-data-explorer/groups.json"),
+ ajax("/admin/plugins/discourse-data-explorer/queries/tags.json"),
]);
- return { schema, groups };
+ return { schema, groups, availableTags };
}
setupController(controller, model) {
controller.resetState();
- controller.setProperties({ schema: model.schema, groups: model.groups });
+ controller.setProperties({
+ schema: model.schema,
+ groups: model.groups,
+ availableTags: model.availableTags,
+ });
}
resetController(controller) {
diff --git a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
index 6040fb4ce05..1b6df890d44 100644
--- a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
+++ b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
@@ -385,32 +385,34 @@ table.group-reports {
white-space: pre-wrap;
}
- .groups {
- margin: 10px 0;
+ &__metadata-fields {
display: flex;
- flex-direction: row;
- align-items: center;
-
- @include viewport.until(md) {
- flex-direction: column;
- align-items: flex-start;
- }
+ flex-wrap: wrap;
+ gap: var(--space-4);
+ margin: 10px 0;
+ }
- .label {
- margin-right: 10px;
- color: var(--primary-high);
- }
+ &__metadata-field {
+ display: flex;
+ flex-direction: column;
+ align-items: flex-start;
+ gap: var(--space-2);
- .name {
- display: inline;
+ @include viewport.from(md) {
+ flex-direction: row;
+ align-items: center;
}
.select-kit.multi-select {
@include viewport.until(md) {
- width: 360px;
+ width: min(22.5rem, 100%);
}
}
}
+
+ &__metadata-label {
+ color: var(--primary-high);
+ }
}
.query-run {
@@ -705,6 +707,18 @@ table.group-reports {
margin-top: 1.5em;
}
+ .query-new__access-fields {
+ display: flex;
+ flex-wrap: wrap;
+ gap: var(--space-4);
+ margin-top: 1em;
+ }
+
+ .query-new__access-field {
+ flex: 1 1 15rem;
+ min-width: 0;
+ }
+
.query-new__preview {
margin-top: 0.5em;
}
@@ -735,7 +749,8 @@ table.group-reports {
min-height: 60px;
}
- .query-group-select {
+ .query-group-select,
+ .query-tag-chooser {
width: 100%;
}
@@ -982,18 +997,6 @@ table.group-reports {
max-width: 100%;
}
- .query-badge {
- font-size: var(--font-down-2);
- padding: 0.15rem 0.3rem;
- border-radius: 4px;
- color: var(--primary-high);
- background: var(--primary-low);
- letter-spacing: 0.25px;
- align-items: center;
- display: inline-flex;
- gap: var(--space-1);
- }
-
.query-desc {
@include line-clamp(2);
color: var(--primary-high);
diff --git a/plugins/discourse-data-explorer/config/locales/client.en.yml b/plugins/discourse-data-explorer/config/locales/client.en.yml
index bba4f8db900..fbdb2818c58 100644
--- a/plugins/discourse-data-explorer/config/locales/client.en.yml
+++ b/plugins/discourse-data-explorer/config/locales/client.en.yml
@@ -23,7 +23,6 @@ en:
description: "Write a custom SQL query to surface anything specific to your community."
explorer:
resize_editor: "Resize the query editor"
- default_query: "Default"
default_query_notice: "This is a default query, the SQL cannot be edited. To edit the SQL, create a new query."
view_query: "View"
queries: "Queries"
@@ -146,6 +145,8 @@ en:
other: "Showing top %{count} results."
query_name: "Query"
query_groups: "Groups"
+ query_tags: "Tags"
+ default_tag_mandatory: "The default tag cannot be removed from bundled queries."
link: "Link for"
report_name: "Report"
query_description: "Description"
@@ -155,6 +156,8 @@ en:
save_params: "Set defaults"
reset_params: "Reset"
search_placeholder: "Search..."
+ filter_by_tag: "Filter by tag"
+ filter_by_this_tag: "Filter the list by this tag"
no_search_results: "Sorry, we couldn't find any results matching your text."
form:
errors:
diff --git a/plugins/discourse-data-explorer/config/locales/server.en.yml b/plugins/discourse-data-explorer/config/locales/server.en.yml
index 572073fd413..ebb602311ca 100644
--- a/plugins/discourse-data-explorer/config/locales/server.en.yml
+++ b/plugins/discourse-data-explorer/config/locales/server.en.yml
@@ -25,7 +25,11 @@ en:
error_agent_not_configured: "AI agent is not configured. Please check the AI features settings."
error_no_sql_returned: "AI did not return a valid SQL query. Please try again or write the query manually."
errors:
+ default_tag_reserved: "The default tag is reserved for system queries."
+ tag_contains_comma: "Tags cannot contain commas."
+ tag_too_long: "A tag can have at most %{max} characters."
tool_not_allowed: "You are not allowed to use this tool"
+ too_many_tags: "A query can have at most %{max} tags."
discourse_workflows:
sql_action:
no_query_selected: "No query selected."
diff --git a/plugins/discourse-data-explorer/config/routes.rb b/plugins/discourse-data-explorer/config/routes.rb
index 6d0f8606cfb..634b22a1dcc 100644
--- a/plugins/discourse-data-explorer/config/routes.rb
+++ b/plugins/discourse-data-explorer/config/routes.rb
@@ -9,6 +9,7 @@ DiscourseDataExplorer::Engine.routes.draw do
scope "/", defaults: { format: :json } do
get "schema" => "query#schema"
get "groups" => "query#groups"
+ get "queries/tags" => "query#tags"
post "queries/generate" => "query#generate_with_ai"
post "queries/preview" => "query#preview"
post "queries" => "query#create"
@@ -44,6 +45,7 @@ Discourse::Application.routes.draw do
scope "/", defaults: { format: :json } do
get "/admin/plugins/explorer/schema" => "discourse_data_explorer/query#schema"
get "/admin/plugins/explorer/groups" => "discourse_data_explorer/query#groups"
+ get "/admin/plugins/explorer/queries/tags" => "discourse_data_explorer/query#tags"
post "/admin/plugins/explorer/queries/generate" =>
"discourse_data_explorer/query#generate_with_ai"
post "/admin/plugins/explorer/queries/preview" => "discourse_data_explorer/query#preview"
diff --git a/plugins/discourse-data-explorer/db/migrate/20260917145657_create_data_explorer_query_tags.rb b/plugins/discourse-data-explorer/db/migrate/20260917145657_create_data_explorer_query_tags.rb
new file mode 100644
index 00000000000..0faff2324a7
--- /dev/null
+++ b/plugins/discourse-data-explorer/db/migrate/20260917145657_create_data_explorer_query_tags.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class CreateDataExplorerQueryTags < ActiveRecord::Migration[8.0]
+ def change
+ create_table :data_explorer_tags do |t|
+ t.string :name, null: false, limit: 100
+ t.timestamps null: false
+ end
+ add_index :data_explorer_tags, :name, unique: true, name: "idx_data_explorer_tags_on_name"
+
+ create_table :data_explorer_query_tags do |t|
+ t.bigint :query_id, null: false
+ t.bigint :query_tag_id, null: false
+ t.datetime :created_at, null: false, default: -> { "CURRENT_TIMESTAMP" }
+ end
+ add_index :data_explorer_query_tags,
+ %i[query_id query_tag_id],
+ unique: true,
+ name: "idx_data_explorer_query_tags_on_query_tag"
+ add_index :data_explorer_query_tags,
+ %i[query_tag_id query_id],
+ unique: true,
+ name: "idx_data_explorer_query_tags_on_tag_query"
+ end
+end
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
index 07d4e94c959..0ae248beee6 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
@@ -9,12 +9,16 @@ module DiscourseDataExplorer
attrs = { name: name, user_id: user.id, last_run_at: Time.now }
attrs[:description] = query_params[:description] if query_params[:description].present?
attrs[:sql] = query_params[:sql] if query_params[:sql].present?
+ tag_names = query_params[:tags] if query_params.key?(:tags)
- query = Query.create!(attrs)
+ Query.transaction do
+ query = Query.create!(attrs)
+ QueryTag.sync!(query:, names: tag_names) if tag_names
- group_ids&.each { |group_id| query.query_groups.find_or_create_by!(group_id: group_id) }
+ group_ids&.each { |group_id| query.query_groups.find_or_create_by!(group_id: group_id) }
- query
+ query
+ end
end
end
end
diff --git a/plugins/discourse-data-explorer/spec/models/query_spec.rb b/plugins/discourse-data-explorer/spec/models/query_spec.rb
index e3c020399dc..8316d58a522 100644
--- a/plugins/discourse-data-explorer/spec/models/query_spec.rb
+++ b/plugins/discourse-data-explorer/spec/models/query_spec.rb
@@ -4,8 +4,23 @@ RSpec.describe DiscourseDataExplorer::Query do
before { SiteSetting.data_explorer_enabled = true }
describe ".find" do
- it "returns default queries" do
- expect(DiscourseDataExplorer::Query.find(-1)).to be_present
+ it "returns default queries with the default tag" do
+ query = DiscourseDataExplorer::Query.find(-1)
+
+ expect(query).to be_present
+ expect(query.tag_names).to eq([DiscourseDataExplorer::Query::DEFAULT_TAG])
+ end
+
+ it "preserves additional tags while keeping the default tag mandatory" do
+ query = DiscourseDataExplorer::Query.find(-1)
+ query.save!
+ DiscourseDataExplorer::QueryTag.sync!(query:, names: ["Staff"])
+
+ expect(DiscourseDataExplorer::Query.find(-1).tag_names).to eq(%w[default staff])
+
+ DiscourseDataExplorer::QueryTag.sync!(query:, names: [])
+
+ expect(DiscourseDataExplorer::Query.find(-1).tag_names).to eq(["default"])
end
end
@@ -14,4 +29,28 @@ RSpec.describe DiscourseDataExplorer::Query do
expect(DiscourseDataExplorer::Query.unscoped.find(-1)).to be_present
end
end
+
+ describe "#record_run!" do
+ it "keeps the default tag virtual after running a bundled query" do
+ query = DiscourseDataExplorer::Query.find(-1)
+
+ query.record_run!
+
+ expect(query.tag_names).to eq([DiscourseDataExplorer::Query::DEFAULT_TAG])
+ expect(query.tags).to be_empty
+ expect(
+ DiscourseDataExplorer::QueryTag.exists?(name: DiscourseDataExplorer::Query::DEFAULT_TAG),
+ ).to eq(false)
+ end
+ end
+
+ describe ".unpersisted_defaults" do
+ it "assigns and filters by the default tag" do
+ defaults = DiscourseDataExplorer::Query.unpersisted_defaults(tag: "default")
+
+ expect(defaults).to be_present
+ expect(defaults.flat_map(&:tag_names).uniq).to eq(["default"])
+ expect(DiscourseDataExplorer::Query.unpersisted_defaults(tag: "Custom")).to be_empty
+ end
+ end
end
diff --git a/plugins/discourse-data-explorer/spec/models/query_tag_spec.rb b/plugins/discourse-data-explorer/spec/models/query_tag_spec.rb
new file mode 100644
index 00000000000..0c8043636eb
--- /dev/null
+++ b/plugins/discourse-data-explorer/spec/models/query_tag_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseDataExplorer::QueryTag do
+ fab!(:query)
+
+ describe ".normalize_all" do
+ it "normalizes all tag names consistently" do
+ expect(described_class.normalize_all([" Ops ", "OPS", "a \t b", "Default", "", nil])).to eq(
+ ["ops", "a b", "default"],
+ )
+ end
+ end
+
+ describe ".sync!" do
+ fab!(:other_query, :query)
+
+ it "assigns normalized tags to the query" do
+ described_class.sync!(query:, names: ["Ops", " billing "])
+
+ expect(query.tags.map(&:name)).to eq(%w[billing ops])
+ end
+
+ it "removes orphaned tags while retaining tags used by another query" do
+ described_class.sync!(query:, names: %w[ops billing])
+ described_class.sync!(query: other_query, names: %w[ops])
+ described_class.sync!(query:, names: [])
+
+ expect(query.tags).to be_empty
+ expect(other_query.tags.map(&:name)).to eq(%w[ops])
+ expect(described_class.pluck(:name)).to contain_exactly("ops")
+ end
+
+ it "rejects more than the allowed number of tags" do
+ expect {
+ described_class.sync!(query:, names: 11.times.map { |index| "tag-#{index}" })
+ }.to raise_error(
+ ActiveRecord::RecordInvalid,
+ "Validation failed: A query can have at most 10 tags.",
+ )
+
+ expect(query.tags).to be_empty
+ end
+
+ it "reserves the default tag for system queries" do
+ expect { described_class.sync!(query:, names: ["Default"]) }.to raise_error(
+ ActiveRecord::RecordInvalid,
+ "Validation failed: The default tag is reserved for system queries.",
+ )
+
+ expect(query.tags).to be_empty
+ end
+
+ it "stores only additional tags for bundled queries" do
+ bundled_query = DiscourseDataExplorer::Query.find(-1)
+ bundled_query.save!
+
+ described_class.sync!(query: bundled_query, names: %w[Default Staff])
+
+ expect(bundled_query.tag_names).to eq(%w[default staff])
+ expect(bundled_query.tags.map(&:name)).to eq(["staff"])
+ end
+ end
+end
diff --git a/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb b/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
index 09517e0d53d..3f03e81ec12 100644
--- a/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
@@ -84,6 +84,7 @@ RSpec.describe "JSON:API queries", type: :request do
"isDefault" => false,
"createdAt" => query.created_at.as_json,
"lastRunAt" => query.last_run_at.as_json,
+ "tags" => [],
},
"links" => {
"self" => "#{base}/queries/#{query.id}",
@@ -182,6 +183,7 @@ RSpec.describe "JSON:API queries", type: :request do
"isDefault" => false,
"createdAt" => middle.created_at.as_json,
"lastRunAt" => middle.last_run_at.as_json,
+ "tags" => [],
},
"links" => {
"self" => "#{base}/queries/#{middle.id}",
diff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
index 4474edd11d6..22b9168230c 100644
--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
@@ -15,6 +15,7 @@ describe DiscourseDataExplorer::QueryController do
sql: sql,
hidden: opts[:hidden] || false,
)
+ DiscourseDataExplorer::QueryTag.sync!(query:, names: opts[:tags]) if opts[:tags]
group_ids.each { |group_id| query.query_groups.create!(group_id: group_id) }
query
end
@@ -126,6 +127,71 @@ describe DiscourseDataExplorer::QueryController do
expect(response.status).to eq(200)
expect(response_json["queries"].count).to eq(DiscourseDataExplorer::Queries.default.count)
end
+
+ it "filters by tag and applies text search within the active tag" do
+ DiscourseDataExplorer::Query.destroy_all
+ make_query("SELECT 1", name: "Monthly staff report", tags: ["Staff"])
+ make_query("SELECT 2", name: "Daily staff report", tags: %w[Staff Daily])
+ make_query("SELECT 3", name: "Monthly member report", tags: ["Members"])
+
+ get "/admin/plugins/discourse-data-explorer/queries.json",
+ params: {
+ tag: "Staff",
+ filter: "monthly",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response_json["queries"].map { |query| query["name"] }).to eq(
+ ["Monthly staff report"],
+ )
+ expect(response_json["extras"]["tags"]).to eq(%w[daily default members staff])
+ end
+
+ it "matches every selected tag" do
+ DiscourseDataExplorer::Query.destroy_all
+ make_query("SELECT 1", name: "Shared report", tags: %w[Staff Daily])
+ make_query("SELECT 2", name: "Staff report", tags: ["Staff"])
+ make_query("SELECT 3", name: "Daily report", tags: ["Daily"])
+
+ get "/admin/plugins/discourse-data-explorer/queries.json", params: { tags: "staff,daily" }
+
+ expect(response.status).to eq(200)
+ expect(response_json["queries"].map { |query| query["name"] }).to eq(["Shared report"])
+ end
+
+ it "matches a persisted default query by its virtual and additional tags" do
+ DiscourseDataExplorer::Query.destroy_all
+ query = DiscourseDataExplorer::Query.find(-1)
+ query.save!
+ DiscourseDataExplorer::QueryTag.sync!(query:, names: ["Staff"])
+
+ get "/admin/plugins/discourse-data-explorer/queries.json", params: { tags: "default,staff" }
+
+ expect(response.status).to eq(200)
+ expect(response_json["queries"].map { |result| result["id"] }).to eq([query.id])
+ end
+
+ it "returns the default tag for bundled queries" do
+ DiscourseDataExplorer::Query.destroy_all
+
+ get "/admin/plugins/discourse-data-explorer/queries.json", params: { tag: "default" }
+
+ expect(response.status).to eq(200)
+ expect(response_json["queries"]).to be_present
+ expect(response_json["queries"].flat_map { |query| query["tags"] }.uniq).to eq(["default"])
+ end
+ end
+
+ describe "#tags" do
+ it "returns tags from visible queries and bundled defaults" do
+ make_query("SELECT 1", tags: %w[Staff Monthly])
+ make_query("SELECT 2", tags: ["Hidden"], hidden: true)
+
+ get "/admin/plugins/discourse-data-explorer/queries/tags.json"
+
+ expect(response.status).to eq(200)
+ expect(response_json).to eq(%w[default monthly staff])
+ end
end
describe "#create" do
@@ -139,11 +205,13 @@ describe DiscourseDataExplorer::QueryController do
description: "A description",
sql: "SELECT 1",
group_ids: [group.id],
+ tags: %w[Staff Monthly],
},
}
expect(response.status).to eq(200)
expect(response_json["query"]["group_ids"]).to eq([group.id])
+ expect(response_json["query"]["tags"]).to eq(%w[monthly staff])
end
it "creates a query without groups when none are given" do
@@ -158,6 +226,20 @@ describe DiscourseDataExplorer::QueryController do
expect(response.status).to eq(200)
expect(response_json["query"]["group_ids"]).to eq([])
end
+
+ it "rejects the default tag on user-created queries" do
+ expect {
+ post "/admin/plugins/discourse-data-explorer/queries.json",
+ params: {
+ query: {
+ name: "My query",
+ tags: ["Default"],
+ },
+ }
+ }.not_to change { DiscourseDataExplorer::Query.user_queries.count }
+
+ expect(response.status).to eq(422)
+ end
end
describe "#update" do
@@ -196,6 +278,80 @@ describe DiscourseDataExplorer::QueryController do
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to eq(["Name can't be blank"])
end
+
+ it "updates query tags while preserving group access when groups are omitted" do
+ query = make_query("SELECT 1", { tags: ["Staff"] }, [group2.id])
+
+ put "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json",
+ params: {
+ query: {
+ tags: %w[Staff Monthly],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(query.reload.tag_names).to eq(%w[monthly staff])
+ expect(query.groups.pluck(:id)).to eq([group2.id])
+ end
+
+ it "removes the last tag when the empty selection is submitted" do
+ query = make_query("SELECT 1", { tags: ["Staff"] }, [group2.id])
+
+ put "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json",
+ params: {
+ query: {
+ tags_present: true,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response_json["query"]["tags"]).to eq([])
+ expect(query.reload.tag_names).to eq([])
+ expect(query.groups.pluck(:id)).to eq([group2.id])
+ end
+
+ it "clears group access when an empty group selection is submitted" do
+ query = make_query("SELECT 1", {}, [group2.id])
+
+ put "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json",
+ params: {
+ query: {
+ group_ids_present: true,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response_json["query"]["group_ids"]).to eq([])
+ expect(query.reload.groups).to be_empty
+ end
+
+ it "updates additional tags on default queries without removing the default tag" do
+ query = DiscourseDataExplorer::Query.find(-4)
+ query.save!
+ DiscourseDataExplorer::QueryTag.sync!(query:, names: %w[Default Staff Monthly])
+
+ put "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json",
+ params: {
+ query: {
+ tags: ["Staff"],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response_json["query"]["tags"]).to eq(%w[default staff])
+ expect(DiscourseDataExplorer::Query.find(-4).tag_names).to eq(%w[default staff])
+
+ put "/admin/plugins/discourse-data-explorer/queries/#{query.id}.json",
+ params: {
+ query: {
+ tags_present: true,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response_json["query"]["tags"]).to eq(["default"])
+ expect(DiscourseDataExplorer::Query.find(-4).tag_names).to eq(["default"])
+ end
end
describe "#destroy" do
diff --git a/plugins/discourse-data-explorer/spec/resources/discourse_data_explorer/query_resource_spec.rb b/plugins/discourse-data-explorer/spec/resources/discourse_data_explorer/query_resource_spec.rb
index 50fa12a38bb..78b7cbd73ae 100644
--- a/plugins/discourse-data-explorer/spec/resources/discourse_data_explorer/query_resource_spec.rb
+++ b/plugins/discourse-data-explorer/spec/resources/discourse_data_explorer/query_resource_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe DiscourseDataExplorer::QueryResource, type: :resource do
it { is_expected.to declare_type(:queries) }
it { is_expected.to declare_namespace("data-explorer") }
- it { is_expected.to expose(:name, :description, :created_at, :last_run_at) }
+ it { is_expected.to expose(:name, :description, :created_at, :last_run_at, :tags) }
it { is_expected.to expose(:param_info, :is_default) }
it { is_expected.to expose(:sql).readable_by(admin.guardian).hidden_from(Guardian.new) }
@@ -17,7 +17,7 @@ RSpec.describe DiscourseDataExplorer::QueryResource, type: :resource do
it { is_expected.to sort_on(:name, :last_run_at, "user.username") }
it { is_expected.to sort_by_default(last_run_at: :desc) }
- it { is_expected.to filter_on(:search) }
+ it { is_expected.to filter_on(:search, :tag) }
it { is_expected.to anchor_on(:id, :name, :last_run_at) }
it { is_expected.to paginate(default: 50, max: 100) }
diff --git a/plugins/discourse-data-explorer/spec/system/page_objects/pages/data_explorer_query_runner.rb b/plugins/discourse-data-explorer/spec/system/page_objects/pages/data_explorer_query_runner.rb
index 773d94d7b11..dac91ac2b28 100644
--- a/plugins/discourse-data-explorer/spec/system/page_objects/pages/data_explorer_query_runner.rb
+++ b/plugins/discourse-data-explorer/spec/system/page_objects/pages/data_explorer_query_runner.rb
@@ -115,9 +115,9 @@ module PageObjects
end
def has_query_groups?(*group_names)
- PageObjects::Components::SelectKit.new(
- ".query-edit .groups .select-kit",
- ).has_selected_names?(*group_names)
+ PageObjects::Components::SelectKit.new(".query-edit .group-chooser").has_selected_names?(
+ *group_names,
+ )
end
def submit_new_query
diff --git a/plugins/discourse-data-explorer/test/javascripts/acceptance/list-queries-test.js b/plugins/discourse-data-explorer/test/javascripts/acceptance/list-queries-test.js
index 221e95db397..a275b8a904e 100644
--- a/plugins/discourse-data-explorer/test/javascripts/acceptance/list-queries-test.js
+++ b/plugins/discourse-data-explorer/test/javascripts/acceptance/list-queries-test.js
@@ -1,5 +1,14 @@
-import { visit } from "@ember/test-helpers";
+import {
+ click,
+ currentURL,
+ fillIn,
+ find,
+ settled,
+ visit,
+ waitUntil,
+} from "@ember/test-helpers";
import { test } from "qunit";
+import pretender from "discourse/tests/helpers/create-pretender";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { i18n } from "discourse-i18n";
@@ -7,6 +16,13 @@ acceptance("List Queries", function (needs) {
needs.user();
needs.settings({ data_explorer_enabled: true });
+ let lastQueryParams;
+ let handleQueries;
+
+ needs.hooks.beforeEach(() => {
+ lastQueryParams = null;
+ });
+
needs.pretender((server, helper) => {
server.get("/admin/plugins/discourse-data-explorer.json", () => {
return helper.response({
@@ -28,33 +44,90 @@ acceptance("List Queries", function (needs) {
return helper.response([]);
});
- server.get("/admin/plugins/discourse-data-explorer/queries", () => {
+ server.get("/admin/plugins/discourse-data-explorer/schema.json", () => {
+ return helper.response({});
+ });
+
+ server.get(
+ "/admin/plugins/discourse-data-explorer/queries/tags.json",
+ () => {
+ return helper.response(["default", "staff"]);
+ }
+ );
+
+ server.get("/admin/plugins/discourse-data-explorer/queries/1", () => {
return helper.response({
- queries: [
- {
- id: -6,
- name: "Top 100 Likers",
- description:
- "returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
- username: "system",
- group_ids: [],
- last_run_at: "2021-02-11T08:29:59.337Z",
- user_id: -1,
- },
- {
- id: -5,
- name: "Top 100 Active Topics",
- description:
- "based on the number of replies, it accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
- username: "system",
- group_ids: [],
- last_run_at: "2021-02-08T15:37:49.188Z",
- user_id: -1,
- },
- ],
- total_rows_queries: 2,
+ query: {
+ id: 1,
+ name: "Staff activity",
+ description: "Shows staff activity.",
+ sql: "SELECT 1",
+ param_info: [],
+ group_ids: [],
+ tags: ["staff"],
+ user_id: 1,
+ is_default: false,
+ },
});
});
+
+ handleQueries = (request) => {
+ lastQueryParams = request.queryParams;
+ const queries = [
+ {
+ id: -6,
+ name: "Top 100 Likers",
+ description:
+ "returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
+ username: "system",
+ group_ids: [],
+ last_run_at: "2021-02-11T08:29:59.337Z",
+ user_id: -1,
+ tags: ["default", "staff"],
+ },
+ {
+ id: -5,
+ name: "Top 100 Active Topics",
+ description:
+ "based on the number of replies, it accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
+ username: "system",
+ group_ids: [],
+ last_run_at: "2021-02-08T15:37:49.188Z",
+ user_id: -1,
+ tags: ["default"],
+ },
+ {
+ id: 1,
+ name: "Staff activity",
+ description: "Shows staff activity.",
+ username: "admin",
+ group_ids: [],
+ last_run_at: "2021-02-07T15:37:49.188Z",
+ user_id: 1,
+ tags: ["staff"],
+ },
+ ];
+ const tags = request.queryParams.tags?.split(",") ?? [];
+ const filter = request.queryParams.filter?.toLowerCase();
+ const filteredQueries = queries.filter((query) => {
+ return (
+ tags.every((tag) => query.tags.includes(tag)) &&
+ (!filter || query.name.toLowerCase().includes(filter))
+ );
+ });
+
+ return helper.response({
+ queries: filteredQueries,
+ total_rows_queries: filteredQueries.length,
+ extras: { tags: ["default", "staff"] },
+ });
+ };
+
+ server.get("/admin/plugins/discourse-data-explorer/queries", handleQueries);
+ server.get(
+ "/admin/plugins/discourse-data-explorer/queries.json",
+ handleQueries
+ );
});
test("renders the page with the list of queries", async function (assert) {
@@ -78,7 +151,7 @@ acceptance("List Queries", function (needs) {
assert
.dom("div.container table.recent-queries tbody tr")
- .exists({ count: 2 }, "the list of queries was rendered");
+ .exists({ count: 3 }, "the list of queries was rendered");
assert
.dom("div.container table.recent-queries tbody tr:nth-child(1) td")
@@ -87,5 +160,154 @@ acceptance("List Queries", function (needs) {
assert
.dom("div.container table.recent-queries tbody tr:nth-child(2) td")
.hasText(/^\s*Top 100 Active Topics/, "The second query was rendered");
+
+ assert
+ .dom(".query-badge")
+ .doesNotExist("default queries do not render a separate badge");
+
+ assert
+ .dom(".query-row:first-child .query-tags .d-table-badge")
+ .exists({ count: 2 }, "the query tags appear below its description");
+
+ assert
+ .dom(".query-tag-filter .d-multi-select-trigger__label")
+ .hasText(
+ i18n("explorer.filter_by_tag"),
+ "the tag filter shows its placeholder initially"
+ );
+ });
+
+ test("filters by multiple tags", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries");
+
+ await click(".query-tag-filter.d-multi-select-trigger");
+ await click(".d-multi-select__result:first-child");
+
+ assert
+ .dom(".query-tag-filter .d-multi-select-trigger__selection-label")
+ .hasText("default", "the selected default tag is displayed in lowercase");
+
+ assert
+ .dom("div.container table.recent-queries tbody tr")
+ .exists({ count: 2 }, "only default queries are shown");
+
+ await click(".d-multi-select__result:first-child");
+
+ assert
+ .dom(".query-tag-filter .d-multi-select-trigger__selected-item")
+ .exists({ count: 2 }, "both selected tags remain visible");
+ assert.strictEqual(lastQueryParams.tags, "default,staff");
+ assert.strictEqual(
+ new URLSearchParams(currentURL().split("?")[1]).get("tags"),
+ "default,staff",
+ "the selected tags are reflected in the URL"
+ );
+ assert
+ .dom("div.container table.recent-queries tbody tr td")
+ .hasText(/^\s*Top 100 Likers/, "only queries with both tags are shown");
+ });
+
+ test("filters by tags from the URL", async function (assert) {
+ await visit(
+ "/admin/plugins/discourse-data-explorer/queries?tags=default,staff"
+ );
+
+ assert.strictEqual(lastQueryParams.tags, "default,staff");
+ assert
+ .dom(".query-tag-filter .d-multi-select-trigger__selected-item")
+ .exists({ count: 2 }, "both URL tags are selected");
+ assert
+ .dom(".recent-queries tbody tr")
+ .exists({ count: 1 }, "only the query with both tags is shown");
+
+ await fillIn(".d-filter-controls__input", "likers");
+ assert.strictEqual(lastQueryParams.tags, "default,staff");
+ assert.strictEqual(lastQueryParams.filter, "likers");
+ });
+
+ test("filters by clicking a query tag", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries");
+ await click(".query-row:first-child .query-tags .d-table-badge:last-child");
+
+ assert.strictEqual(lastQueryParams.tags, "staff");
+ assert
+ .dom(".recent-queries tbody tr")
+ .exists({ count: 2 }, "only queries with the clicked tag are shown");
+ });
+
+ test("keeps tag and text filters when returning from a query", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries");
+
+ await click(".query-tag-filter.d-multi-select-trigger");
+ await click(".d-multi-select__result:nth-child(2)");
+ await fillIn(".d-filter-controls__input", "activity");
+
+ await click(".d-table__overview-link");
+ await click(".back-button");
+
+ assert.strictEqual(
+ lastQueryParams.tags,
+ "staff",
+ "the tag is sent on return"
+ );
+ assert.strictEqual(
+ lastQueryParams.filter,
+ "activity",
+ "the text search is sent on return"
+ );
+ assert.strictEqual(
+ new URLSearchParams(currentURL().split("?")[1]).get("tags"),
+ "staff",
+ "the tag remains in the URL on return"
+ );
+ assert
+ .dom(".query-tag-filter .d-multi-select-trigger__selection-label")
+ .hasText("staff", "the selected tag remains visible");
+ assert
+ .dom(".d-filter-controls__input")
+ .hasValue("activity", "the text search remains visible");
+ assert
+ .dom("div.container table.recent-queries tbody tr")
+ .exists({ count: 1 }, "the returned list still applies both filters");
+ assert
+ .dom("div.container table.recent-queries tbody tr td")
+ .hasText(/^\s*Staff activity/, "the matching query remains visible");
+ });
+
+ test("ignores an older tag-filter response that finishes last", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries");
+
+ const requests = [];
+ pretender.get(
+ "/admin/plugins/discourse-data-explorer/queries.json",
+ (request) => {
+ requests.push(request);
+ return handleQueries(request);
+ },
+ true
+ );
+
+ const controller = this.owner.lookup(
+ "controller:adminPlugins.show.explorer.index"
+ );
+ controller.onTagFilterChange([{ id: "default", name: "default" }]);
+ controller.onTagFilterChange([{ id: "staff", name: "staff" }]);
+
+ await waitUntil(() => requests.length === 2);
+ pretender.resolve(requests[1]);
+ await waitUntil(() =>
+ find(".recent-queries tbody tr:last-child td")?.textContent.includes(
+ "Staff activity"
+ )
+ );
+ pretender.resolve(requests[0]);
+ await settled();
+
+ assert
+ .dom(".recent-queries tbody tr:last-child td")
+ .hasText(
+ /^\s*Staff activity/,
+ "the latest filter results remain visible"
+ );
});
});
diff --git a/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.js b/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.js
index a8c906c1a26..f667ab59927 100644
--- a/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.js
+++ b/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.js
@@ -63,6 +63,10 @@ acceptance("New Query", function (needs) {
});
});
+ server.get("/admin/plugins/discourse-data-explorer/queries/tags.json", () =>
+ helper.response(["default", "staff"])
+ );
+
server.get("/admin/plugins/discourse-data-explorer/queries", () => {
return helper.response({
queries: [],
@@ -81,6 +85,7 @@ acceptance("New Query", function (needs) {
created_at: "2021-02-05T16:42:45.572Z",
username: "system",
group_ids: [41],
+ tags: ["monthly"],
last_run_at: "2021-02-08T15:37:49.188Z",
hidden: false,
user_id: -1,
@@ -134,8 +139,32 @@ acceptance("New Query", function (needs) {
".query-new__manual-form [data-name='description'] textarea",
"a test query"
);
+ const tags = selectKit(
+ ".query-new__manual-form [data-name='tags'] .query-tag-chooser"
+ );
+ await tags.expand();
+ assert.deepEqual(
+ tags.displayedContent().map((row) => row.name),
+ ["staff"],
+ "the system-only default tag is not offered"
+ );
+ await tags.fillInFilter("Default");
+ assert.deepEqual(
+ tags.displayedContent(),
+ [],
+ "the default tag cannot be created manually"
+ );
+ await tags.emptyFilter();
+ await tags.fillInFilter("Monthly");
+ await tags.selectRowByValue("Monthly");
await click(".query-new__manual-form .btn-primary");
+ assert.deepEqual(
+ createParams.getAll("query[tags][]"),
+ ["Monthly"],
+ "the newly created tag is sent with the query"
+ );
+
assert.strictEqual(
currentURL(),
"/admin/plugins/discourse-data-explorer/queries/-15"
@@ -253,6 +282,9 @@ acceptance("New Query - AI", function (needs) {
server.get("/admin/plugins/discourse-data-explorer/schema.json", () =>
helper.response({ topics: [] })
);
+ server.get("/admin/plugins/discourse-data-explorer/queries/tags.json", () =>
+ helper.response(["default", "staff"])
+ );
server.get("/admin/plugins/discourse-data-explorer/queries", () =>
helper.response({ queries: [] })
);
@@ -288,6 +320,7 @@ acceptance("New Query - AI", function (needs) {
description: "",
param_info: [],
group_ids: [],
+ tags: ["staff"],
hidden: false,
user_id: -1,
},
@@ -398,6 +431,14 @@ acceptance("New Query - AI", function (needs) {
);
await groups.selectRowByValue(41);
+ const tags = selectKit(".query-new__fields .query-tag-chooser");
+ await tags.expand();
+ assert.deepEqual(
+ tags.displayedContent().map((row) => row.name),
+ ["staff"],
+ "the system-only default tag is not offered in the AI form"
+ );
+ await tags.selectRowByValue("staff");
await click(".query-new__save-btn");
assert.deepEqual(
@@ -405,6 +446,11 @@ acceptance("New Query - AI", function (needs) {
["41"],
"the selected groups are sent with the new query"
);
+ assert.deepEqual(
+ createParams.getAll("query[tags][]"),
+ ["staff"],
+ "the selected tag is sent with the new query"
+ );
});
test("saving transitions to the edit page without running the query", async function (assert) {
diff --git a/plugins/discourse-data-explorer/test/javascripts/acceptance/param-input-test.js b/plugins/discourse-data-explorer/test/javascripts/acceptance/param-input-test.js
index f18263eeb2b..57ed4f08098 100644
--- a/plugins/discourse-data-explorer/test/javascripts/acceptance/param-input-test.js
+++ b/plugins/discourse-data-explorer/test/javascripts/acceptance/param-input-test.js
@@ -37,6 +37,10 @@ acceptance("Param Input", function (needs) {
return helper.response({});
});
+ server.get("/admin/plugins/discourse-data-explorer/queries/tags.json", () =>
+ helper.response(["default"])
+ );
+
server.get("/admin/plugins/discourse-data-explorer/queries", () => {
return helper.response({
queries: [
diff --git a/plugins/discourse-data-explorer/test/javascripts/acceptance/run-query-test.js b/plugins/discourse-data-explorer/test/javascripts/acceptance/run-query-test.js
index 04af0149895..5bbd640d722 100644
--- a/plugins/discourse-data-explorer/test/javascripts/acceptance/run-query-test.js
+++ b/plugins/discourse-data-explorer/test/javascripts/acceptance/run-query-test.js
@@ -1,5 +1,6 @@
import {
click,
+ currentURL,
find,
triggerEvent,
triggerKeyEvent,
@@ -8,6 +9,7 @@ import {
import { test } from "qunit";
import sinon from "sinon";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
import {
settleGestureFrame,
stubPointerCapture,
@@ -36,8 +38,13 @@ acceptance("Run Query", function (needs) {
needs.user();
needs.settings({ data_explorer_enabled: true });
+ let updateParams;
+ let query2Tags;
+
needs.hooks.beforeEach(() => {
sinon.stub(window, "open");
+ updateParams = null;
+ query2Tags = ["existing"];
});
needs.hooks.afterEach(() => {
@@ -122,6 +129,10 @@ acceptance("Run Query", function (needs) {
});
});
+ server.get("/admin/plugins/discourse-data-explorer/queries/tags.json", () =>
+ helper.response(["default", "existing"])
+ );
+
server.get("/admin/plugins/discourse-data-explorer/queries", () => {
return helper.response({
queries: [
@@ -132,6 +143,7 @@ acceptance("Run Query", function (needs) {
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
username: "system",
group_ids: [],
+ tags: ["default"],
last_run_at: "2021-02-11T08:29:59.337Z",
user_id: -1,
is_default: true,
@@ -142,6 +154,7 @@ acceptance("Run Query", function (needs) {
description: "",
username: "system",
group_ids: [],
+ tags: ["existing"],
last_run_at: "2023-05-04T22:16:23.858Z",
user_id: 1,
is_default: false,
@@ -169,6 +182,7 @@ acceptance("Run Query", function (needs) {
created_at: "2021-02-02T12:21:11.449Z",
username: "system",
group_ids: [],
+ tags: ["default", "existing"],
last_run_at: "2021-02-11T08:29:59.337Z",
hidden: false,
user_id: -1,
@@ -188,6 +202,7 @@ acceptance("Run Query", function (needs) {
created_at: "2023-05-04T22:16:06.007Z",
username: "system",
group_ids: [],
+ tags: query2Tags,
last_run_at: "2023-05-04T22:16:23.858Z",
hidden: false,
user_id: 1,
@@ -246,6 +261,50 @@ acceptance("Run Query", function (needs) {
});
});
+ server.put(
+ "/admin/plugins/discourse-data-explorer/queries/2",
+ (request) => {
+ updateParams = new URLSearchParams(request.requestBody);
+ query2Tags = updateParams
+ .getAll("query[tags][]")
+ .map((tag) => tag.toLowerCase());
+ return helper.response({
+ query: {
+ id: 2,
+ sql: 'SELECT 0 zero, null "null", false "false"',
+ name: "What about 0?",
+ description: "",
+ param_info: [],
+ group_ids: [],
+ tags: query2Tags,
+ hidden: false,
+ user_id: 1,
+ },
+ });
+ }
+ );
+
+ server.put(
+ "/admin/plugins/discourse-data-explorer/queries/-6",
+ (request) => {
+ updateParams = new URLSearchParams(request.requestBody);
+ return helper.response({
+ query: {
+ id: -6,
+ sql: "SELECT 1",
+ name: "Top 100 Likers",
+ description: "",
+ param_info: [],
+ group_ids: [],
+ tags: ["default", "monthly"],
+ hidden: false,
+ user_id: -1,
+ is_default: true,
+ },
+ });
+ }
+ );
+
server.get("/session/csrf.json", function () {
return helper.response({
csrf: "mgk906YLagHo2gOgM1ddYjAN4hQolBdJCqlY6jYzAYs= ",
@@ -429,6 +488,102 @@ acceptance("Run Query", function (needs) {
assert
.dom("div.query-results tbody td:nth-child(3)")
.hasText("false", "renders 'false' values");
+
+ assert.strictEqual(
+ currentURL(),
+ "/admin/plugins/discourse-data-explorer/queries/2",
+ "running a query without inputs does not add a null params query string"
+ );
+ });
+
+ test("adds a new tag while editing a query", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries/2");
+
+ const tags = selectKit(".query-edit .query-tag-chooser");
+ await tags.expand();
+ assert.false(
+ tags.displayedContent().some((row) => row.name === "default"),
+ "the system-only default tag is not offered on user queries"
+ );
+ await tags.fillInFilter("Default");
+ assert.deepEqual(
+ tags.displayedContent(),
+ [],
+ "the default tag cannot be created on user queries"
+ );
+ await tags.emptyFilter();
+ await tags.fillInFilter("Monthly");
+ await tags.selectRowByValue("Monthly");
+
+ assert
+ .dom(".query-run-split__primary span")
+ .hasText(i18n("explorer.saverun"), "changing tags marks the query dirty");
+
+ await click(".query-run-split__primary");
+
+ assert.deepEqual(
+ updateParams.getAll("query[tags][]"),
+ ["existing", "Monthly"],
+ "the edited tags are saved"
+ );
+ assert.strictEqual(
+ updateParams.get("query[group_ids_present]"),
+ "true",
+ "the group selection is explicitly submitted"
+ );
+ assert.deepEqual(
+ updateParams.getAll("query[group_ids][]"),
+ [],
+ "an empty group selection sends no group IDs"
+ );
+ });
+
+ test("removes the last tag while editing a query", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries/2");
+
+ const tags = selectKit(".query-edit .query-tag-chooser");
+ await tags.expand();
+ await tags.deselectItemByValue("existing");
+ await click(".query-run-split__primary");
+
+ assert.strictEqual(
+ updateParams.get("query[tags_present]"),
+ "true",
+ "the empty tag selection is explicitly submitted"
+ );
+ assert.deepEqual(
+ updateParams.getAll("query[tags][]"),
+ [],
+ "the removed tag is absent from the request"
+ );
+
+ await visit("/admin/plugins/discourse-data-explorer/queries");
+ await visit("/admin/plugins/discourse-data-explorer/queries/2");
+
+ assert
+ .dom(".query-edit .query-tag-chooser .selected-choice")
+ .doesNotExist("the removed tag stays absent after reloading the query");
+ });
+
+ test("edits additional tags on a default query", async function (assert) {
+ await visit("/admin/plugins/discourse-data-explorer/queries/-6");
+
+ const tags = selectKit(".query-edit .query-tag-chooser");
+ await tags.expand();
+ assert
+ .dom(".query-tag-chooser .tag-choice.disabled")
+ .hasText("default", "the default tag cannot be removed");
+
+ await tags.deselectItemByValue("existing");
+ await tags.fillInFilter("Monthly");
+ await tags.selectRowByValue("Monthly");
+ await click(".query-run-split__primary");
+
+ assert.deepEqual(
+ updateParams.getAll("query[tags][]"),
+ ["default", "Monthly"],
+ "additional tags can be removed and added"
+ );
});
test("automatically runs query when run query parameter is present", async function (assert) {