Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate start/end when scheduling queries #7508

Merged
merged 2 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,19 @@ To allow scheduled queries, add the following to your `config.py`:
},
'start_date': {
'type': 'string',
'format': 'date-time',
'title': 'Start date',
# date-time is parsed using the Sugar library, see
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we already had some logic or a library that handles relative dates. I am a little iffy about using this library because it looks like it's maintained by one developer and the last version was 5 months ago so I am not sure how often this library is maintained.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khtruong we use moment.js, but it can't parse relative dates like these, which is what we really want.

# https://sugarjs.com/dates/#/Parsing for examples
'format': 'date-time',
'default': 'tomorrow at 9am',
},
'end_date': {
'type': 'string',
'format': 'date-time',
'title': 'End date',
# date-time is parsed using the Sugar library, see
# https://sugarjs.com/dates/#/Parsing for examples
'format': 'date-time',
'default': '9am in 30 days',
},
'schedule_interval': {
'type': 'string',
Expand All @@ -890,6 +896,16 @@ To allow scheduled queries, add the following to your `config.py`:
),
},
},
'VALIDATION': [
# ensure that start_date <= end_date
{
'name': 'less_equal',
'arguments': ['start_date', 'end_date'],
'message': 'End date cannot be before start date',
# this is where the error message is shown
'container': 'end_date',
},
],
},
}

Expand Down
13 changes: 13 additions & 0 deletions superset/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions superset/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"redux-thunk": "^2.1.0",
"redux-undo": "^1.0.0-beta9-9-7",
"shortid": "^2.2.6",
"sugar": "^2.0.6",
"underscore": "^1.8.3",
"urijs": "^1.18.10",
"viewport-mercator-project": "^6.1.1"
Expand Down
50 changes: 48 additions & 2 deletions superset/assets/src/SqlLab/components/ScheduleQueryButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,56 @@
import React from 'react';
import PropTypes from 'prop-types';
import Form from 'react-jsonschema-form';
import Sugar from 'sugar';
import { t } from '@superset-ui/translation';

import Button from '../../components/Button';
import ModalTrigger from '../../components/ModalTrigger';

const validators = {
greater: (a, b) => a > b,
greater_equal: (a, b) => a >= b,
less: (a, b) => a < b,
less_equal: (a, b) => a <= b,
};

function getJSONSchema() {
const jsonSchema = window.featureFlags.SCHEDULED_QUERIES.JSONSCHEMA;
// parse date-time into usable value (eg, 'today' => `new Date()`)
Object.entries(jsonSchema.properties).forEach(([key, properties]) => {
if (properties.default && properties.format === 'date-time') {
jsonSchema.properties[key] = {
...properties,
default: Sugar.Date.format(Sugar.Date.create(properties.default), 'ISO8601'),
};
}
});
return jsonSchema;
}

function getUISchema() {
return window.featureFlags.SCHEDULED_QUERIES.UISCHEMA;
}

function getValidationRules() {
return window.featureFlags.SCHEDULED_QUERIES.VALIDATION || [];
}

function getValidator() {
const rules = getValidationRules();
return (formData, errors) => {
rules.forEach((rule) => {
const test = validators[rule.name];
const args = rule.arguments.map(name => formData[name]);
const container = rule.container || rule.arguments.slice(-1)[0];
if (!test(...args)) {
errors[container].addError(rule.message);
}
});
return errors;
};
}

const propTypes = {
defaultLabel: PropTypes.string,
sql: PropTypes.string.isRequired,
Expand Down Expand Up @@ -79,9 +124,10 @@ class ScheduleQueryButton extends React.PureComponent {
renderModalBody() {
return (
<Form
schema={window.featureFlags.SCHEDULED_QUERIES.JSONSCHEMA}
uiSchema={window.featureFlags.SCHEDULED_QUERIES.UISCHEMA}
schema={getJSONSchema()}
uiSchema={getUISchema()}
onSubmit={this.onSchedule}
validate={getValidator()}
/>
);
}
Expand Down