A platform for community discussion. Free, open, simple.
UX: Data explorer has a new query page (#39094)
Natalie Tay3944aaa36758
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.gjsindex 56a529b1204..7ac7c086604 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@@ -4,7 +4,6 @@ import { LinkTo } from "@ember/routing";import AdminFilterControls from "discourse/admin/components/admin-filter-controls";import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";import DPageSubheader from "discourse/components/d-page-subheader";-import Form from "discourse/components/form";import LoadMore from "discourse/components/load-more";import PickFilesButton from "discourse/components/pick-files-button";import TableHeaderToggle from "discourse/components/table-header-toggle";@@ -22,7 +21,7 @@ export default <template><DPageSubheader @titleLabel={{i18n "explorer.queries"}}><:actions as |actions|><actions.Primary- @action={{@controller.displayCreate}}+ @route="adminPlugins.show.explorer.new"@icon="plus"@label="explorer.create"/>@@ -48,36 +47,6 @@ export default <template>@loading={{@controller.searchLoading}}><:aboveFilters>- {{#if @controller.showCreate}}- <div class="query-create">- <Form- @data={{@controller.createFormData}}- @onSubmit={{@controller.create}}- as |form|- >- <form.Field- @name="name"- @title={{i18n "explorer.create_placeholder"}}- @validation="required"- @format="large"- @type="input"- as |field|- >- <field.Control />- </form.Field>- <form.Actions>- <form.Submit @label="explorer.create" @icon="plus" />- <form.Button- @action={{@controller.hideCreate}}- class="btn-default"- >- {{i18n "cancel"}}- </form.Button>- </form.Actions>- </Form>- </div>- {{/if}}-{{#if @controller.othersDirty}}<div class="warning">{{icon "triangle-exclamation"}}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.gjsnew file mode 100644index 00000000000..0b1e5624888--- /dev/null+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs@@ -0,0 +1,42 @@+import BackButton from "discourse/components/back-button";+import Form from "discourse/components/form";+import { i18n } from "discourse-i18n";++export default <template>+ <div class="admin-detail">+ <BackButton+ @route="adminPlugins.show.explorer.index"+ @label="explorer.queries"+ />++ <Form+ @data={{@controller.model}}+ @onSubmit={{@controller.create}}+ class="query-new"+ as |form|+ >+ <form.Field+ @name="name"+ @title={{i18n "explorer.create_placeholder"}}+ @validation="required"+ @format="large"+ @type="input"+ as |field|+ >+ <field.Control />+ </form.Field>+ <form.Field+ @name="description"+ @title={{i18n "explorer.description_placeholder"}}+ @format="large"+ @type="textarea"+ as |field|+ >+ <field.Control />+ </form.Field>+ <form.Actions>+ <form.Submit @label="explorer.create" @icon="plus" />+ </form.Actions>+ </Form>+ </div>+</template>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.jsindex 1557b1ba727..9a38be6cabc 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@@ -15,13 +15,10 @@ export default class PluginsExplorerController extends Controller {@service appEvents;@service router;@service store;- @service toasts;@tracked sortByProperty = "last_run_at";@tracked sortDescending = true;@tracked params;- @tracked createFormData = { name: "" };- @tracked showCreate;@tracked loading = false;@tracked searchLoading = false;@@ -132,17 +129,6 @@ export default class PluginsExplorerController extends Controller {}}- @action- displayCreate() {- this.showCreate = true;- this.createFormData = { name: "" };- }-- @action- hideCreate() {- this.showCreate = false;- }-@actiononTextFilterChange(event) {this._currentFilter = event.target?.value || "";@@ -229,24 +215,4 @@ export default class PluginsExplorerController extends Controller {);});}-- @action- async create({ name }) {- try {- this.loading = true;- const result = await this.store- .createRecord("query", { name: name.trim() })- .save();- this.toasts.success({- data: { message: i18n("explorer.query_created") },- });- this.showCreate = false;- this.createFormData = { name: "" };- this.addCreatedRecord(result.target);- } catch (error) {- popupAjaxError(error);- } finally {- this.loading = 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.jsnew file mode 100644index 00000000000..7648cf8fcf1--- /dev/null+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js@@ -0,0 +1,38 @@+import { tracked } from "@glimmer/tracking";+import Controller from "@ember/controller";+import { action } from "@ember/object";+import { service } from "@ember/service";+import { popupAjaxError } from "discourse/lib/ajax-error";+import { i18n } from "discourse-i18n";++export default class AdminPluginsExplorerNew extends Controller {+ @service store;+ @service router;+ @service toasts;++ @tracked loading = false;++ @action+ async create({ name, description }) {+ try {+ this.loading = true;+ const result = await this.store+ .createRecord("query", {+ name: name.trim(),+ description: description?.trim(),+ })+ .save();+ this.toasts.success({+ data: { message: i18n("explorer.query_created") },+ });+ this.router.transitionTo(+ "adminPlugins.show.explorer.details",+ result.target.id+ );+ } catch (error) {+ popupAjaxError(error);+ } finally {+ this.loading = false;+ }+ }+}diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/explorer-route-map.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/explorer-route-map.jsindex a696b1cee71..27db94b952a 100644--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/explorer-route-map.js+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/explorer-route-map.js@@ -3,6 +3,7 @@ export default {path: "/plugins",map() {this.route("explorer", { path: "queries" }, function () {+ this.route("new");this.route("details", { path: "/:query_id" });});},diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.jsindex 3e63e39b6be..8a70a8b135f 100644--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js@@ -77,6 +77,6 @@ export default class Query extends RestModel {// Importingreturn this.updateProperties();}- return this.getProperties("name");+ return this.getProperties("name", "description");}}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.jsnew file mode 100644index 00000000000..39db3983be0--- /dev/null+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js@@ -0,0 +1,7 @@+import DiscourseRoute from "discourse/routes/discourse";++export default class AdminPluginsExplorerNew extends DiscourseRoute {+ model() {+ return { name: "", description: "" };+ }+}diff --git a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scssindex aeb9f7b7864..01e6e21a6ad 100644--- a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss+++ b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss@@ -336,7 +336,6 @@ table.group-reports {}.disscourse-data-explorer-query-list,-.query-create,.query-edit,.query-results,.query-params,@@ -344,15 +343,6 @@ table.group-reports {margin: 10px 0;}-.query-create {- display: flex;-- input {- margin-right: 0.5em;- margin-bottom: 0;- }-}-.query-results {section {width: 100%;diff --git a/plugins/discourse-data-explorer/config/routes.rb b/plugins/discourse-data-explorer/config/routes.rbindex 219ba232926..8629a229986 100644--- a/plugins/discourse-data-explorer/config/routes.rb+++ b/plugins/discourse-data-explorer/config/routes.rb@@ -3,7 +3,8 @@DiscourseDataExplorer::Engine.routes.draw doroot to: "query#index"get "queries" => "query#index"- get "queries/:id" => "query#show"+ get "queries/new" => "query#index"+ get "queries/:id" => "query#show", :constraints => { id: /-?\d+/ }scope "/", defaults: { format: :json } doget "schema" => "query#schema"diff --git a/plugins/discourse-data-explorer/spec/system/new_query_spec.rb b/plugins/discourse-data-explorer/spec/system/new_query_spec.rbnew file mode 100644index 00000000000..3f030ec390d--- /dev/null+++ b/plugins/discourse-data-explorer/spec/system/new_query_spec.rb@@ -0,0 +1,44 @@+# frozen_string_literal: true++describe "Data explorer new query" do+ fab!(:admin)++ let(:query_runner) { PageObjects::Pages::DataExplorerQueryRunner.new }++ before do+ SiteSetting.data_explorer_enabled = true+ sign_in admin+ end++ it "navigates to the new query page from the index" do+ visit("/admin/plugins/discourse-data-explorer/queries")+ find(".d-page-subheader .btn-primary").click+ expect(page).to have_current_path("/admin/plugins/discourse-data-explorer/queries/new")+ end++ it "creates a query with name and description" do+ query_runner+ .visit_new_query+ .fill_new_query_name("Test Query")+ .fill_new_query_description("A test description")+ .submit_new_query++ query = DiscourseDataExplorer::Query.last+ expect(query.name).to eq("Test Query")+ expect(query.description).to eq("A test description")++ expect(page).to have_current_path("/admin/plugins/discourse-data-explorer/queries/#{query.id}")+ expect(query_runner).to have_query_name("Test Query")+ expect(query_runner).to have_query_description("A test description")+ end++ it "creates a query with name only" do+ query_runner.visit_new_query.fill_new_query_name("Name Only Query").submit_new_query++ query = DiscourseDataExplorer::Query.last+ expect(query.name).to eq("Name Only Query")++ expect(page).to have_current_path("/admin/plugins/discourse-data-explorer/queries/#{query.id}")+ expect(query_runner).to have_query_name("Name Only Query")+ end+enddiff --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.rbindex d22c127d88f..6e35391d44a 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@@ -65,6 +65,30 @@ module PageObjectstext: text,)end++ def visit_new_query+ page.visit("/admin/plugins/discourse-data-explorer/queries/new")+ self+ end++ def fill_new_query_name(text)+ page.find(".query-new [data-name='name'] input").fill_in(with: text)+ self+ end++ def fill_new_query_description(text)+ page.find(".query-new [data-name='description'] textarea").fill_in(with: text)+ self+ end++ def submit_new_query+ page.find(".query-new .btn-primary").click+ self+ end++ def has_query_description?(text)+ page.has_css?(".query-edit .desc", text: text)+ endendendenddiff --git a/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.js b/plugins/discourse-data-explorer/test/javascripts/acceptance/new-query-test.jsindex a8f88ed9fbe..7b2f0db7a29 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@@ -41,18 +41,10 @@ acceptance("Data Explorer Plugin | New Query", function (needs) {return helper.response({query: {id: -15,- sql: "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS\n(SELECT date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' AS period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' AS period_end)\nSELECT t.id AS topic_id,\n t.category_id,\n COUNT(p.id) AS reply_count\nFROM topics t\nJOIN posts p ON t.id = p.topic_id\nJOIN query_period qp ON p.created_at >= qp.period_start\nAND p.created_at <= qp.period_end\nWHERE t.archetype = 'regular'\nAND t.user_id > 0\nGROUP BY t.id\nORDER BY COUNT(p.id) DESC, t.score DESC\nLIMIT 100\n",+ sql: "SELECT 1",name: "foo",- description:- "based on the number of replies, it accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",- param_info: [- {- identifier: "months_ago",- type: "int",- default: "1",- nullable: false,- },- ],+ description: "a test query",+ param_info: [],created_at: "2021-02-05T16:42:45.572Z",username: "system",group_ids: [],@@ -67,18 +59,10 @@ acceptance("Data Explorer Plugin | New Query", function (needs) {return helper.response({query: {id: -15,- sql: "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS\n(SELECT date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' AS period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' AS period_end)\nSELECT t.id AS topic_id,\n t.category_id,\n COUNT(p.id) AS reply_count\nFROM topics t\nJOIN posts p ON t.id = p.topic_id\nJOIN query_period qp ON p.created_at >= qp.period_start\nAND p.created_at <= qp.period_end\nWHERE t.archetype = 'regular'\nAND t.user_id > 0\nGROUP BY t.id\nORDER BY COUNT(p.id) DESC, t.score DESC\nLIMIT 100\n",+ sql: "SELECT 1",name: "foo",- description:- "based on the number of replies, it accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",- param_info: [- {- identifier: "months_ago",- type: "int",- default: "1",- nullable: false,- },- ],+ description: "a test query",+ param_info: [],created_at: "2021-02-05T16:42:45.572Z",username: "system",group_ids: [],@@ -90,12 +74,21 @@ acceptance("Data Explorer Plugin | New Query", function (needs) {});});- test("creates a new query", async function (assert) {+ test("navigates to new query page and creates a query", async function (assert) {await visit("/admin/plugins/discourse-data-explorer/queries");await click(".d-page-subheader .btn-primary");- await fillIn(".query-create input", "foo");- await click(".query-create button");+ assert.strictEqual(+ currentURL(),+ "/admin/plugins/discourse-data-explorer/queries/new"+ );++ await fillIn(".query-new [data-name='name'] input", "foo");+ await fillIn(+ ".query-new [data-name='description'] textarea",+ "a test query"+ );+ await click(".query-new .btn-primary");assert.strictEqual(currentURL(),