Skip to content

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

UI: Allow users to write sql in DE /new page (step 2/n) (#40166)

Natalie Tay9b02ce2e3088
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 25ec5d8e115..802fa85879e 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
@@ -2,13 +2,14 @@ import { on } from "@ember/modifier";
import AceEditor from "discourse/components/ace-editor";
import BackButton from "discourse/components/back-button";
import Form from "discourse/components/form";
-import { eq } from "discourse/truth-helpers";
+import { and, eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
import DTextField from "discourse/ui-kit/d-text-field";
import DTextarea from "discourse/ui-kit/d-textarea";
import dIcon from "discourse/ui-kit/helpers/d-icon";
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";
export default <template>
@@ -27,168 +28,153 @@ export default <template>
{{/if}}
</div>
- {{#if @controller.aiQueriesEnabled}}
- <div class="query-new query-new--ai-first">
- {{#if (eq @controller.mode "ai")}}
- <div class="query-new__ai-section">
- <label class="query-new__ai-label">
- <span>{{i18n "explorer.ai.description_title"}}</span>
- {{dIcon "discourse-sparkles"}}
- </label>
+ <div class="query-new">
+ {{#if (and @controller.aiQueriesEnabled (eq @controller.mode "ai"))}}
+ <div class="query-new__ai-section">
+ <label class="query-new__ai-label">
+ <span>{{i18n "explorer.ai.description_title"}}</span>
+ {{dIcon "discourse-sparkles"}}
+ </label>
- <p class="query-new__ai-hint">
- {{i18n "explorer.ai.description_hint"}}
- </p>
+ <p class="query-new__ai-hint">
+ {{i18n "explorer.ai.description_hint"}}
+ </p>
- <DTextarea
- @value={{@controller.aiDescription}}
- {{on "input" @controller.updateAiDescription}}
- {{on "keydown" @controller.handleKeydown}}
- placeholder={{i18n "explorer.ai.description_placeholder"}}
- class="query-new__ai-textarea"
- disabled={{@controller.aiGenerating}}
- />
+ <DTextarea
+ @value={{@controller.aiDescription}}
+ {{on "input" @controller.updateAiDescription}}
+ {{on "keydown" @controller.handleKeydown}}
+ placeholder={{i18n "explorer.ai.description_placeholder"}}
+ class="query-new__ai-textarea"
+ disabled={{@controller.aiGenerating}}
+ />
- <div class="query-new__ai-actions">
- {{#if @controller.hasGenerated}}
- <DButton
- @action={{@controller.generate}}
- @icon="discourse-sparkles"
- @label="explorer.ai.regenerate"
- @disabled={{@controller.aiGenerating}}
- class="btn-default query-new__regenerate-btn"
- />
- {{else}}
- <DButton
- @action={{@controller.generate}}
- @label="explorer.ai.generate"
- @disabled={{@controller.aiGenerating}}
- class="btn-primary query-new__generate-btn"
- />
- {{/if}}
+ <div class="query-new__ai-actions">
+ {{#if @controller.hasGenerated}}
+ <DButton
+ @action={{@controller.generate}}
+ @icon="discourse-sparkles"
+ @label="explorer.ai.regenerate"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-default query-new__regenerate-btn"
+ />
+ {{else}}
+ <DButton
+ @action={{@controller.generate}}
+ @label="explorer.ai.generate"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-primary query-new__generate-btn"
+ />
+ {{/if}}
- {{#if @controller.aiGenerating}}
- <span class="query-new__generating-indicator">
- <DConditionalLoadingSpinner
- @condition={{true}}
- @size="small"
- />
- <span>{{i18n "explorer.ai.generating"}}</span>
- </span>
- {{/if}}
- </div>
+ {{#if @controller.aiGenerating}}
+ <span class="query-new__generating-indicator">
+ <DConditionalLoadingSpinner @condition={{true}} @size="small" />
+ <span>{{i18n "explorer.ai.generating"}}</span>
+ </span>
+ {{/if}}
</div>
+ </div>
- {{#if @controller.hasGenerated}}
- <hr class="query-new__divider" />
+ {{#if @controller.hasGenerated}}
+ <hr class="query-new__divider" />
+ <label class="query-new__field-label">
+ {{i18n "explorer.ai.sql_label"}}
+ </label>
+ <div class="query-new__sql-editor">
+ <AceEditor
+ @content={{@controller.generatedSql}}
+ @onChange={{@controller.updateSql}}
+ @mode="sql"
+ />
+ </div>
+
+ <div class="query-new__fields">
<label class="query-new__field-label">
- {{i18n "explorer.ai.sql_label"}}
+ {{i18n "explorer.query_name"}}
</label>
- <div class="query-new__sql-editor">
- <AceEditor
- @content={{@controller.generatedSql}}
- @onChange={{@controller.updateSql}}
- @mode="sql"
- />
- </div>
-
- <div class="query-new__fields">
- <label class="query-new__field-label">
- {{i18n "explorer.query_name"}}
- </label>
- <DTextField
- @value={{@controller.generatedName}}
- @onChange={{@controller.updateName}}
- class="query-new__name-input"
- />
-
- <label class="query-new__field-label">
- {{i18n "explorer.description_placeholder"}}
- <span class="query-new__optional">
- ({{i18n "explorer.ai.optional"}})
- </span>
- </label>
- <DTextarea
- @value={{@controller.generatedDescription}}
- {{on "input" @controller.updateDescription}}
- class="query-new__description-input"
- />
- </div>
+ <DTextField
+ @value={{@controller.generatedName}}
+ @onChange={{@controller.updateName}}
+ class="query-new__name-input"
+ />
- <div class="query-new__actions">
- <DButton
- @action={{@controller.saveQuery}}
- @label="explorer.ai.save_query"
- @disabled={{@controller.aiGenerating}}
- class="btn-default"
- />
- </div>
- {{/if}}
- {{/if}}
+ <label class="query-new__field-label">
+ {{i18n "explorer.description_placeholder"}}
+ <span class="query-new__optional">
+ ({{i18n "explorer.ai.optional"}})
+ </span>
+ </label>
+ <DTextarea
+ @value={{@controller.generatedDescription}}
+ {{on "input" @controller.updateDescription}}
+ class="query-new__description-input"
+ />
+ </div>
- {{#if (eq @controller.mode "manual")}}
- <Form
- @data={{@controller.model}}
- @onSubmit={{@controller.create}}
- class="query-new__manual-form"
- as |form|
- >
- <form.Field
- @name="name"
- @title={{i18n "explorer.query_name"}}
- @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 class="query-new__actions">
+ <DButton
+ @action={{@controller.saveQuery}}
+ @label="explorer.ai.save_query"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-default"
+ />
+ </div>
{{/if}}
- </div>
- {{else}}
- <Form
- @data={{@controller.model}}
- @onSubmit={{@controller.create}}
- class="query-new"
- as |form|
- >
- <form.Field
- @name="name"
- @title={{i18n "explorer.query_name"}}
- @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|
+ {{else}}
+ <Form
+ @data={{@controller.manualFormData}}
+ @onSubmit={{@controller.create}}
+ class="query-new__manual-form"
+ as |form|
>
- <field.Control />
- </form.Field>
- <form.Actions>
- <form.Submit @label="explorer.create" @icon="plus" />
- </form.Actions>
- </Form>
- {{/if}}
+ <form.Field
+ @name="name"
+ @title={{i18n "explorer.query_name"}}
+ @validation="required"
+ @format="full"
+ @type="input"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ <form.Field
+ @name="description"
+ @title={{i18n "explorer.description_placeholder"}}
+ @format="full"
+ @type="textarea"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ <label class="query-new__sql-label">
+ {{i18n "explorer.ai.sql_label"}}
+ </label>
+ <div class="query-editor {{if @controller.hideSchema 'no-schema'}}">
+ <div class="panels-flex">
+ <div class="editor-panel">
+ <AceEditor
+ @content={{@controller.manualSql}}
+ @onChange={{@controller.updateManualSql}}
+ @mode="sql"
+ />
+ </div>
+ <div class="right-panel">
+ <ExplorerSchema
+ @schema={{@controller.schema}}
+ @hideSchema={{@controller.hideSchema}}
+ @updateHideSchema={{@controller.updateHideSchema}}
+ />
+ </div>
+ </div>
+ <div class="clear"></div>
+ </div>
+ <form.Actions>
+ <form.Submit @label="explorer.create" @icon="plus" />
+ </form.Actions>
+ </Form>
+ {{/if}}
+ </div>
</div>
</template>
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 0a10a666469..de5e6a9d9fb 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
@@ -26,8 +26,12 @@ export default class AdminPluginsExplorerNew extends Controller {
@tracked generatedName = "";
@tracked generatedDescription = "";
@tracked mode = "ai";
+ @tracked schema = null;
+ @tracked hideSchema = false;
+ @tracked manualSql = "SELECT 1";
currentGenerationId = null;
+ manualFormData = { name: "", description: "" };
_aiGenerationTimer = null;
get aiQueriesEnabled() {
@@ -47,6 +51,16 @@ export default class AdminPluginsExplorerNew extends Controller {
this.mode = value;
}
+ @action
+ updateHideSchema(value) {
+ this.hideSchema = value;
+ }
+
+ @action
+ updateManualSql(value) {
+ this.manualSql = value;
+ }
+
@action
async create({ name, description }) {
try {
@@ -55,6 +69,7 @@ export default class AdminPluginsExplorerNew extends Controller {
.createRecord("query", {
name: name.trim(),
description: description?.trim(),
+ sql: this.manualSql,
})
.save();
this.toasts.success({
@@ -203,6 +218,10 @@ export default class AdminPluginsExplorerNew extends Controller {
this.generatedName = "";
this.generatedDescription = "";
this.mode = "ai";
+ this.schema = null;
+ this.hideSchema = false;
+ this.manualSql = "SELECT 1";
this.loading = false;
+ this.manualFormData = { 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.js
index 06d56279e2d..1bef74b06b1 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
@@ -1,8 +1,15 @@
+import { ajax } from "discourse/lib/ajax";
import DiscourseRoute from "discourse/routes/discourse";
export default class AdminPluginsExplorerNew extends DiscourseRoute {
model() {
- return { name: "", description: "" };
+ return ajax("/admin/plugins/discourse-data-explorer/schema.json", {
+ cache: true,
+ }).then((schema) => ({ schema }));
+ }
+
+ setupController(controller, model) {
+ controller.setProperties({ schema: model.schema });
}
resetController(controller) {
diff --git a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
index 6d24a23de99..34a1311eddb 100644
--- a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
+++ b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
@@ -375,10 +375,9 @@ table.group-reports {
}
}
-.query-new--ai-first {
- max-width: 720px;
-
+.query-new {
.query-new__ai-section {
+ max-width: 720px;
margin-bottom: 1em;
}
@@ -479,6 +478,17 @@ table.group-reports {
gap: 0.5em;
margin-top: 1em;
}
+
+ .query-new__sql-label {
+ display: block;
+ font-weight: 700;
+ margin: 1em 0 0.5em;
+ }
+
+ .query-new__manual-form > .query-editor,
+ .query-new__manual-form > .query-new__sql-label {
+ width: 100%;
+ }
}
.query-ai-generating {
diff --git a/plugins/discourse-data-explorer/spec/system/new_query_spec.rb b/plugins/discourse-data-explorer/spec/system/new_query_spec.rb
index 3f030ec390d..0cc22b4ac7f 100644
--- a/plugins/discourse-data-explorer/spec/system/new_query_spec.rb
+++ b/plugins/discourse-data-explorer/spec/system/new_query_spec.rb
@@ -16,16 +16,20 @@ describe "Data explorer new query" do
expect(page).to have_current_path("/admin/plugins/discourse-data-explorer/queries/new")
end
- it "creates a query with name and description" do
+ it "creates a query with name, description, and SQL" do
+ query_runner.visit_new_query
+ expect(page).to have_css(".query-new__manual-form .right-panel .schema", text: "topics")
+
query_runner
- .visit_new_query
.fill_new_query_name("Test Query")
.fill_new_query_description("A test description")
+ .fill_new_query_sql("SELECT 1")
.submit_new_query
query = DiscourseDataExplorer::Query.last
expect(query.name).to eq("Test Query")
expect(query.description).to eq("A test description")
+ expect(query.sql).to eq("SELECT 1")
expect(page).to have_current_path("/admin/plugins/discourse-data-explorer/queries/#{query.id}")
expect(query_runner).to have_query_name("Test Query")
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 642a3b4e2c1..b307824a1c1 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
@@ -82,6 +82,14 @@ module PageObjects
self
end
+ def fill_new_query_sql(text)
+ page.execute_script(
+ "document.querySelector('.query-new .editor-panel .ace_editor').env.editor.setValue(arguments[0], 1);",
+ text,
+ )
+ self
+ end
+
def submit_new_query
page.find(".query-new .btn-primary").click
self
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 07f237891a1..ef7bace2283 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
@@ -28,7 +28,15 @@ acceptance("New Query", function (needs) {
});
server.get("/admin/plugins/discourse-data-explorer/schema.json", () => {
- return helper.response({});
+ return helper.response({
+ topics: [
+ {
+ column_name: "id",
+ data_type: "serial",
+ primary: true,
+ },
+ ],
+ });
});
server.get("/admin/plugins/discourse-data-explorer/queries", () => {
@@ -74,7 +82,7 @@ acceptance("New Query", function (needs) {
});
});
- test("navigates to new query page and creates a query", async function (assert) {
+ test("renders the manual create form and transitions on submit", async function (assert) {
await visit("/admin/plugins/discourse-data-explorer/queries");
await click(".d-page-subheader .btn-primary");
@@ -83,12 +91,19 @@ acceptance("New Query", function (needs) {
"/admin/plugins/discourse-data-explorer/queries/new"
);
- await fillIn(".query-new [data-name='name'] input", "foo");
+ assert
+ .dom(".query-new__manual-form .right-panel .schema")
+ .exists("schema sidebar renders");
+ assert
+ .dom(".query-new__manual-form .editor-panel .ace-wrapper")
+ .exists("SQL editor renders alongside the schema sidebar");
+
+ await fillIn(".query-new__manual-form [data-name='name'] input", "foo");
await fillIn(
- ".query-new [data-name='description'] textarea",
+ ".query-new__manual-form [data-name='description'] textarea",
"a test query"
);
- await click(".query-new .btn-primary");
+ await click(".query-new__manual-form .btn-primary");
assert.strictEqual(
currentURL(),