From e9e04254ebd74451a3e0ffef8a06d38ebcad5296 Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Wed, 30 Oct 2019 15:20:11 +0200 Subject: [PATCH 1/4] Parameter feedback - #3 Added in Widgets --- .../dashboards/dashboard-widget/VisualizationWidget.jsx | 6 ++++-- client/app/components/queries/visualization-embed.html | 2 +- client/app/components/queries/visualization-embed.js | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/client/app/components/dashboards/dashboard-widget/VisualizationWidget.jsx b/client/app/components/dashboards/dashboard-widget/VisualizationWidget.jsx index 3a4e31245b..44df51eea5 100644 --- a/client/app/components/dashboards/dashboard-widget/VisualizationWidget.jsx +++ b/client/app/components/dashboards/dashboard-widget/VisualizationWidget.jsx @@ -75,6 +75,8 @@ RefreshIndicator.defaultProps = { refreshStartedAt: null }; function VisualizationWidgetHeader({ widget, refreshStartedAt, parameters, onParametersUpdate }) { const canViewQuery = currentUser.hasPermission('view_query'); + const queryResult = widget.getQueryResult(); + const errorData = queryResult && queryResult.getErrorData(); return ( <> @@ -90,8 +92,8 @@ function VisualizationWidgetHeader({ widget, refreshStartedAt, parameters, onPar {!isEmpty(parameters) && ( -
- +
+
)} diff --git a/client/app/components/queries/visualization-embed.html b/client/app/components/queries/visualization-embed.html index 563e4021e5..44bb137a29 100644 --- a/client/app/components/queries/visualization-embed.html +++ b/client/app/components/queries/visualization-embed.html @@ -13,7 +13,7 @@

- +
diff --git a/client/app/components/queries/visualization-embed.js b/client/app/components/queries/visualization-embed.js index fd3fa1c7f0..24b37837d6 100644 --- a/client/app/components/queries/visualization-embed.js +++ b/client/app/components/queries/visualization-embed.js @@ -14,6 +14,7 @@ const VisualizationEmbed = { this.refreshQueryResults = () => { this.loading = true; this.error = null; + this.errorData = {}; this.refreshStartedAt = moment(); this.query .getQueryResultPromise() @@ -24,6 +25,7 @@ const VisualizationEmbed = { .catch((error) => { this.loading = false; this.error = error.getError(); + this.errorData = error.getErrorData(); }); }; From 8ae3db32960abece1da74936148138abe37d84d1 Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Thu, 31 Oct 2019 11:10:52 +0200 Subject: [PATCH 2/4] Added cypress tests --- .../integration/query/parameter_spec.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/client/cypress/integration/query/parameter_spec.js b/client/cypress/integration/query/parameter_spec.js index 2fe23a9bc3..c1bfd77f51 100644 --- a/client/cypress/integration/query/parameter_spec.js +++ b/client/cypress/integration/query/parameter_spec.js @@ -1,4 +1,4 @@ -import { createQuery } from '../../support/redash-api'; +import { createQuery, createDashboard, addWidget } from '../../support/redash-api'; const { get } = Cypress._; @@ -613,6 +613,25 @@ describe('Parameter', () => { expectValueValidationError(); cy.percySnapshot('Validation error in query page'); }); + + it('shows validation error in visualization embed', function () { + cy.visit(`/embed/query/${this.query.id}/visualization/${this.vizId}?api_key=${this.query.api_key}`); + expectValueValidationError(); + cy.percySnapshot('Validation error in visualization embed'); + }); + + it('shows validation error in dashboard widget', function () { + createDashboard('Foo') + .then(({ slug, id }) => { + this.dashboardUrl = `/dashboard/${slug}`; + return addWidget(id, this.vizId); + }) + .then(() => { + cy.visit(this.dashboardUrl); + }); + expectValueValidationError(); + cy.percySnapshot('Validation error in dashboard widget'); + }); }); describe('Apply Changes', () => { From db729a96896cc644ded2482f9aa706ca540e850c Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Thu, 31 Oct 2019 11:29:50 +0200 Subject: [PATCH 3/4] Making sure widget-level param is selected --- .../cypress/integration/query/parameter_spec.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/client/cypress/integration/query/parameter_spec.js b/client/cypress/integration/query/parameter_spec.js index c1bfd77f51..2d45af40f6 100644 --- a/client/cypress/integration/query/parameter_spec.js +++ b/client/cypress/integration/query/parameter_spec.js @@ -620,17 +620,27 @@ describe('Parameter', () => { cy.percySnapshot('Validation error in visualization embed'); }); - it('shows validation error in dashboard widget', function () { + it('shows validation error in widget-level parameter', function () { createDashboard('Foo') .then(({ slug, id }) => { this.dashboardUrl = `/dashboard/${slug}`; - return addWidget(id, this.vizId); + return addWidget(id, this.vizId, { + parameterMappings: { + 'test-parameter': { + type: 'widget-level', + title: '', + name: 'test-parameter', + mapTo: 'test-parameter', + value: null, + }, + }, + }); }) .then(() => { cy.visit(this.dashboardUrl); }); expectValueValidationError(); - cy.percySnapshot('Validation error in dashboard widget'); + cy.percySnapshot('Validation error in widget-level parameter'); }); }); From e34234daa774b8fd179f564e51e4a5d1ab646db1 Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Fri, 21 Jul 2023 21:36:21 +0200 Subject: [PATCH 4/4] Parameter feedback - #4 Added in Dashboard params (#4321) * Parameter feedback - #4 Added in Dashboard params * Added cypress test * Moved to service * Parameter feedback - #5 Unsaved indication (#4322) * Parameter feedback - #5 Unsaved indication * Added ANGULAR_REMOVE_ME * Added cypress test * Fixed percy screenshot * Some code improvements * Parameter input feedback - #6 Better value normalization (#4327) --- client/app/components/ParameterValueInput.jsx | 10 ++-- client/app/components/Parameters.jsx | 23 ++++++++-- client/app/pages/dashboards/dashboard.html | 4 +- .../dashboards/public-dashboard-page.html | 4 +- client/app/pages/queries/query.html | 2 +- client/app/pages/queries/source-view.js | 18 +++++++- client/app/services/dashboard.js | 33 +++++++++++++ .../services/parameters/NumberParameter.js | 6 +-- .../app/services/parameters/TextParameter.js | 9 +++- .../parameters/tests/NumberParameter.test.js | 5 -- client/app/services/query.js | 15 ++++-- .../integration/query/parameter_spec.js | 46 +++++++++++++++++++ 12 files changed, 146 insertions(+), 29 deletions(-) diff --git a/client/app/components/ParameterValueInput.jsx b/client/app/components/ParameterValueInput.jsx index 7f28ad33a8..88aef8c41e 100644 --- a/client/app/components/ParameterValueInput.jsx +++ b/client/app/components/ParameterValueInput.jsx @@ -5,7 +5,7 @@ import Input from 'antd/lib/input'; import InputNumber from 'antd/lib/input-number'; import DateParameter from '@/components/dynamic-parameters/DateParameter'; import DateRangeParameter from '@/components/dynamic-parameters/DateRangeParameter'; -import { isEqual } from 'lodash'; +import { isEqual, trim } from 'lodash'; import { QueryBasedParameterInput } from './QueryBasedParameterInput'; import './ParameterValueInput.less'; @@ -59,7 +59,7 @@ class ParameterValueInput extends React.Component { } onSelect = (value) => { - const isDirty = !isEqual(value, this.props.value); + const isDirty = !isEqual(trim(value), trim(this.props.value)); this.setState({ value, isDirty }); this.props.onSelect(value, isDirty); } @@ -140,13 +140,11 @@ class ParameterValueInput extends React.Component { const { className } = this.props; const { value } = this.state; - const normalize = val => (isNaN(val) ? undefined : val); - return ( this.onSelect(normalize(val))} + value={value} + onChange={val => this.onSelect(val)} /> ); } diff --git a/client/app/components/Parameters.jsx b/client/app/components/Parameters.jsx index f1ead2904e..b07f7c7165 100644 --- a/client/app/components/Parameters.jsx +++ b/client/app/components/Parameters.jsx @@ -1,6 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { size, filter, forEach, extend, get } from 'lodash'; +import { size, filter, forEach, extend, get, includes } from 'lodash'; import { react2angular } from 'react2angular'; import { SortableContainer, SortableElement, DragHandle } from '@/components/sortable'; import { $location } from '@/services/ng'; @@ -34,6 +34,7 @@ export class Parameters extends React.Component { queryResultErrorData: PropTypes.shape({ parameters: PropTypes.objectOf(PropTypes.string), }), + unsavedParameters: PropTypes.arrayOf(PropTypes.string), }; static defaultProps = { @@ -44,6 +45,7 @@ export class Parameters extends React.Component { onPendingValuesChange: () => {}, onParametersEdit: () => {}, queryResultErrorData: {}, + unsavedParameters: null, }; constructor(props) { @@ -140,7 +142,22 @@ export class Parameters extends React.Component { const { queryResultErrorData } = this.props; const error = get(queryResultErrorData, ['parameters', param.name], false); if (error) { - return [error, 'error']; + const feedback = {error}; + return [feedback, 'error']; + } + + // unsaved + const { unsavedParameters } = this.props; + if (includes(unsavedParameters, param.name)) { + const feedback = ( + <> + Unsaved{' '} + + + + + ); + return [feedback, 'warning']; } return []; @@ -172,7 +189,7 @@ export class Parameters extends React.Component {
{feedback} : null} + help={feedback || null} >
-
- +
+
diff --git a/client/app/pages/dashboards/public-dashboard-page.html b/client/app/pages/dashboards/public-dashboard-page.html index 2c1d251d3f..7246e8d86f 100644 --- a/client/app/pages/dashboards/public-dashboard-page.html +++ b/client/app/pages/dashboards/public-dashboard-page.html @@ -1,8 +1,8 @@
-
- +
+
diff --git a/client/app/pages/queries/query.html b/client/app/pages/queries/query.html index 605d8fb6f1..34855c792f 100644 --- a/client/app/pages/queries/query.html +++ b/client/app/pages/queries/query.html @@ -200,7 +200,7 @@

+ on-values-change="executeQuery" on-pending-values-change="applyParametersChanges" on-parameters-edit="onParametersUpdated" unsaved-parameters="getUnsavedParameters()">
diff --git a/client/app/pages/queries/source-view.js b/client/app/pages/queries/source-view.js index f5d72898b1..1d6257c000 100644 --- a/client/app/pages/queries/source-view.js +++ b/client/app/pages/queries/source-view.js @@ -1,4 +1,4 @@ -import { map, debounce } from 'lodash'; +import { map, debounce, isEmpty, isEqual } from 'lodash'; import template from './query.html'; import EditParameterSettingsDialog from '@/components/EditParameterSettingsDialog'; @@ -109,6 +109,22 @@ function QuerySourceCtrl( $scope.$watch('query.query', (newQueryText) => { $scope.isDirty = newQueryText !== queryText; }); + + $scope.unsavedParameters = null; + $scope.getUnsavedParameters = () => { + if (!$scope.isDirty || !queryText) { + return null; + } + const unsavedParameters = $scope.query.$parameters.getUnsavedParameters(queryText); + if (isEmpty(unsavedParameters)) { + return null; + } + // avoiding Angular infdig (ANGULAR_REMOVE_ME) + if (!isEqual(unsavedParameters, $scope.unsavedParameters)) { + $scope.unsavedParameters = unsavedParameters; + } + return $scope.unsavedParameters; + }; } export default function init(ngModule) { diff --git a/client/app/services/dashboard.js b/client/app/services/dashboard.js index 509ab387cf..00dc483566 100644 --- a/client/app/services/dashboard.js +++ b/client/app/services/dashboard.js @@ -249,6 +249,39 @@ function DashboardService($resource, $http, $location, currentUser) { }); }; + let currentQueryResultsErrorData; // swap for useMemo ANGULAR_REMOVE_ME + resource.prototype.getQueryResultsErrorData = function getQueryResultsErrorData() { + const dashboardErrors = _.map(this.widgets, (widget) => { + // get result + const result = widget.getQueryResult(); + if (!result) { + return null; + } + + // get error data + const errorData = result.getErrorData(); + if (_.isEmpty(errorData)) { + return null; + } + + // dashboard params only + const localParamNames = _.map(widget.getLocalParameters(), p => p.name); + const filtered = _.omit(errorData.parameters, localParamNames); + + return filtered; + }); + + const merged = _.assign({}, ...dashboardErrors); + const errorData = _.isEmpty(merged) ? null : { parameters: merged }; + + // avoiding Angular infdig (ANGULAR_REMOVE_ME) + if (!_.isEqual(currentQueryResultsErrorData, errorData)) { + currentQueryResultsErrorData = errorData; + } + + return currentQueryResultsErrorData; + }; + return resource; } diff --git a/client/app/services/parameters/NumberParameter.js b/client/app/services/parameters/NumberParameter.js index 997cdb4b29..17abe0cc65 100644 --- a/client/app/services/parameters/NumberParameter.js +++ b/client/app/services/parameters/NumberParameter.js @@ -1,4 +1,4 @@ -import { toNumber, isNull } from 'lodash'; +import { toNumber, trim } from 'lodash'; import { Parameter } from '.'; class NumberParameter extends Parameter { @@ -9,11 +9,11 @@ class NumberParameter extends Parameter { // eslint-disable-next-line class-methods-use-this normalizeValue(value) { - if (isNull(value)) { + if (!trim(value)) { return null; } const normalizedValue = toNumber(value); - return !isNaN(normalizedValue) ? normalizedValue : null; + return !isNaN(normalizedValue) ? normalizedValue : value; } } diff --git a/client/app/services/parameters/TextParameter.js b/client/app/services/parameters/TextParameter.js index 645adbc8e3..0488392e88 100644 --- a/client/app/services/parameters/TextParameter.js +++ b/client/app/services/parameters/TextParameter.js @@ -1,4 +1,4 @@ -import { toString, isEmpty } from 'lodash'; +import { toString, isEmpty, trim } from 'lodash'; import { Parameter } from '.'; class TextParameter extends Parameter { @@ -15,6 +15,13 @@ class TextParameter extends Parameter { } return normalizedValue; } + + getExecutionValue() { + if (!trim(this.value)) { + return null; + } + return this.value; + } } export default TextParameter; diff --git a/client/app/services/parameters/tests/NumberParameter.test.js b/client/app/services/parameters/tests/NumberParameter.test.js index 2a292ea880..9cce92ef28 100644 --- a/client/app/services/parameters/tests/NumberParameter.test.js +++ b/client/app/services/parameters/tests/NumberParameter.test.js @@ -17,10 +17,5 @@ describe('NumberParameter', () => { const normalizedValue = param.normalizeValue(42); expect(normalizedValue).toBe(42); }); - - test('returns null when not possible to convert to number', () => { - const normalizedValue = param.normalizeValue('notanumber'); - expect(normalizedValue).toBeNull(); - }); }); }); diff --git a/client/app/services/query.js b/client/app/services/query.js index c8f19ac176..a43dfc7fa3 100644 --- a/client/app/services/query.js +++ b/client/app/services/query.js @@ -2,8 +2,8 @@ import moment from 'moment'; import debug from 'debug'; import Mustache from 'mustache'; import { - zipObject, isEmpty, map, includes, union, - uniq, has, identity, extend, each, some, + zipObject, isEmpty, map, includes, union, isNil, + uniq, has, identity, extend, each, some, reject, } from 'lodash'; import { Parameter } from './parameters'; @@ -35,13 +35,13 @@ class Parameters { this.initFromQueryString(queryString); } - parseQuery() { + parseQuery(queryText = this.query.query) { const fallback = () => map(this.query.options.parameters, i => i.name); let parameters = []; - if (this.query.query !== undefined) { + if (!isNil(queryText)) { try { - const parts = Mustache.parse(this.query.query); + const parts = Mustache.parse(queryText); parameters = uniq(collectParams(parts)); } catch (e) { logger('Failed parsing parameters: ', e); @@ -124,6 +124,11 @@ class Parameters { each(this.get(), p => p.applyPendingValue()); } + getUnsavedParameters(queryText) { + const savedParameters = this.parseQuery(queryText); + return reject(this.get(), p => includes(savedParameters, p.name)).map(p => p.name); + } + toUrlParams() { if (this.get().length === 0) { return ''; diff --git a/client/cypress/integration/query/parameter_spec.js b/client/cypress/integration/query/parameter_spec.js index 2d45af40f6..5979c4c3a0 100644 --- a/client/cypress/integration/query/parameter_spec.js +++ b/client/cypress/integration/query/parameter_spec.js @@ -614,6 +614,29 @@ describe('Parameter', () => { cy.percySnapshot('Validation error in query page'); }); + it('shows unsaved feedback in query page', function () { + cy.visit(`/queries/${this.query.id}/source`); + + cy.getByTestId('QueryEditor') + .get('.ace_text-input') + .type(' {{ newparam }}', { force: true, parseSpecialCharSequences: false }); + + cy.getByTestId('ParameterName-newparam') + .find('.ant-form-item-control') + .should('have.class', 'has-warning') + .find('.ant-form-explain') + .as('Feedback'); + + cy.get('@Feedback') + .should('contain.text', 'Unsaved') + .should('not.have.class', 'show-help-appear'); // assures ant animation ended for screenshot + + cy.percySnapshot('Unsaved feedback in query page'); + + cy.getByTestId('SaveButton').click(); + cy.get('@Feedback').should('not.exist'); + }); + it('shows validation error in visualization embed', function () { cy.visit(`/embed/query/${this.query.id}/visualization/${this.vizId}?api_key=${this.query.api_key}`); expectValueValidationError(); @@ -642,6 +665,29 @@ describe('Parameter', () => { expectValueValidationError(); cy.percySnapshot('Validation error in widget-level parameter'); }); + + it('shows validation error in dashboard-level parameter', function () { + createDashboard('Foo') + .then(({ slug, id }) => { + this.dashboardUrl = `/dashboard/${slug}`; + return addWidget(id, this.vizId, { + parameterMappings: { + 'test-parameter': { + type: 'dashboard-level', + title: '', + name: 'test-parameter', + mapTo: 'test-parameter', + value: null, + }, + }, + }); + }) + .then(() => { + cy.visit(this.dashboardUrl); + }); + expectValueValidationError(); + cy.percySnapshot('Validation error in dashboard-level parameter'); + }); }); describe('Apply Changes', () => {