-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
Implement caching and dynamic data fetching. #1466
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6b5f0ad
Permissions cleanup: remove none and duplicates. (#1967)
bkyryliuk 2a894b4
Rename rv => o in the decorator.
543d983
Address comments.
6243407
Updates
be79e36
Rename var and dropdown text
428a3d0
Cleanup
ef4d1de
Resolve comments.
d7345ee
Add user to the perm check.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,8 @@ class SqlEditorLeftBar extends React.PureComponent { | |
}; | ||
} | ||
componentWillMount() { | ||
this.fetchSchemas(); | ||
this.fetchTables(); | ||
this.fetchSchemas(this.props.queryEditor.dbId); | ||
this.fetchTables(this.props.queryEditor.dbId, this.props.queryEditor.schema); | ||
} | ||
onChange(db) { | ||
const val = (db) ? db.value : null; | ||
|
@@ -58,22 +58,51 @@ class SqlEditorLeftBar extends React.PureComponent { | |
resetState() { | ||
this.props.actions.resetState(); | ||
} | ||
fetchTables(dbId, schema) { | ||
const actualDbId = dbId || this.props.queryEditor.dbId; | ||
if (actualDbId) { | ||
const actualSchema = schema || this.props.queryEditor.schema; | ||
this.setState({ tableLoading: true }); | ||
this.setState({ tableOptions: [] }); | ||
const url = `/superset/tables/${actualDbId}/${actualSchema}`; | ||
getTableNamesBySubStr(input) { | ||
if (!this.props.queryEditor.dbId || !input) { | ||
return Promise.resolve({ options: [] }); | ||
} | ||
const url = `/superset/tables/${this.props.queryEditor.dbId}/\ | ||
${this.props.queryEditor.schema}/${input}`; | ||
return $.get(url).then((data) => ({ options: data.options })); | ||
} | ||
// TODO: move fetching methods to the actions. | ||
fetchTables(dbId, schema, substr) { | ||
if (dbId) { | ||
this.setState({ tableLoading: true, tableOptions: [] }); | ||
const url = `/superset/tables/${dbId}/${schema}/${substr}/`; | ||
$.get(url, (data) => { | ||
let tableOptions = data.tables.map((s) => ({ value: s, label: s })); | ||
const views = data.views.map((s) => ({ value: s, label: '[view] ' + s })); | ||
tableOptions = [...tableOptions, ...views]; | ||
this.setState({ tableOptions }); | ||
this.setState({ tableLoading: false }); | ||
this.setState({ | ||
tableLoading: false, | ||
tableOptions: data.options, | ||
tableLength: data.tableLength, | ||
}); | ||
}); | ||
} | ||
} | ||
changeTable(tableOpt) { | ||
if (!tableOpt) { | ||
this.setState({ tableName: '' }); | ||
return; | ||
} | ||
const namePieces = tableOpt.value.split('.'); | ||
let tableName = namePieces[0]; | ||
let schemaName = this.props.queryEditor.schema; | ||
if (namePieces.length === 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm, i see what's happening here |
||
this.setState({ tableName }); | ||
} else { | ||
schemaName = namePieces[0]; | ||
tableName = namePieces[1]; | ||
this.setState({ tableName }); | ||
this.props.actions.queryEditorSetSchema(this.props.queryEditor, schemaName); | ||
this.fetchTables(this.props.queryEditor.dbId, schemaName); | ||
} | ||
this.setState({ tableLoading: true }); | ||
// TODO: handle setting the tableLoading state depending on success or | ||
// failure of the addTable async call in the action. | ||
this.props.actions.addTable(this.props.queryEditor, tableName, schemaName); | ||
this.setState({ tableLoading: false }); | ||
} | ||
changeSchema(schemaOpt) { | ||
const schema = (schemaOpt) ? schemaOpt.value : null; | ||
this.props.actions.queryEditorSetSchema(this.props.queryEditor, schema); | ||
|
@@ -95,14 +124,6 @@ class SqlEditorLeftBar extends React.PureComponent { | |
closePopover(ref) { | ||
this.refs[ref].hide(); | ||
} | ||
changeTable(tableOpt) { | ||
const tableName = tableOpt.value; | ||
const qe = this.props.queryEditor; | ||
|
||
this.setState({ tableLoading: true }); | ||
this.props.actions.addTable(qe, tableName); | ||
this.setState({ tableLoading: false }); | ||
} | ||
render() { | ||
let networkAlert = null; | ||
if (!this.props.networkOn) { | ||
|
@@ -118,6 +139,8 @@ class SqlEditorLeftBar extends React.PureComponent { | |
dataEndpoint="/databaseasync/api/read?_flt_0_expose_in_sqllab=1" | ||
onChange={this.onChange.bind(this)} | ||
value={this.props.queryEditor.dbId} | ||
databaseId={this.props.queryEditor.dbId} | ||
actions={this.props.actions} | ||
valueRenderer={(o) => ( | ||
<div> | ||
<span className="text-muted">Database:</span> {o.label} | ||
|
@@ -126,8 +149,6 @@ class SqlEditorLeftBar extends React.PureComponent { | |
mutator={this.dbMutator.bind(this)} | ||
placeholder="Select a database" | ||
/> | ||
</div> | ||
<div className="m-t-5"> | ||
<Select | ||
name="select-schema" | ||
placeholder={`Select a schema (${this.state.schemaOptions.length})`} | ||
|
@@ -144,15 +165,29 @@ class SqlEditorLeftBar extends React.PureComponent { | |
/> | ||
</div> | ||
<div className="m-t-5"> | ||
<Select | ||
name="select-table" | ||
ref="selectTable" | ||
isLoading={this.state.tableLoading} | ||
placeholder={`Add a table (${this.state.tableOptions.length})`} | ||
autosize={false} | ||
onChange={this.changeTable.bind(this)} | ||
options={this.state.tableOptions} | ||
/> | ||
{this.props.queryEditor.schema && | ||
<Select | ||
name="select-table" | ||
ref="selectTable" | ||
isLoading={this.state.tableLoading} | ||
value={this.state.tableName} | ||
placeholder={`Add a table (${this.state.tableOptions.length})`} | ||
autosize={false} | ||
onChange={this.changeTable.bind(this)} | ||
options={this.state.tableOptions} | ||
/> | ||
} | ||
{!this.props.queryEditor.schema && | ||
<Select.Async | ||
name="async-select-table" | ||
ref="selectTable" | ||
value={this.state.tableName} | ||
placeholder={"Type to search ..."} | ||
autosize={false} | ||
onChange={this.changeTable.bind(this)} | ||
loadOptions={this.getTableNamesBySubStr.bind(this)} | ||
/> | ||
} | ||
</div> | ||
<hr /> | ||
<div className="m-t-5"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from superset import tables_cache | ||
from flask import request | ||
|
||
|
||
def view_cache_key(*unused_args, **unused_kwargs): | ||
args_hash = hash(frozenset(request.args.items())) | ||
return 'view/{}/{}'.format(request.path, args_hash) | ||
|
||
|
||
def memoized_func(timeout=5 * 60, key=view_cache_key): | ||
"""Use this decorator to cache functions that have predefined first arg. | ||
|
||
memoized_func uses simple_cache and stored the data in memory. | ||
Key is a callable function that takes function arguments and | ||
returns the caching key. | ||
""" | ||
def wrap(f): | ||
def wrapped_f(cls, *args, **kwargs): | ||
cache_key = key(*args, **kwargs) | ||
o = tables_cache.get(cache_key) | ||
if o is not None: | ||
return o | ||
o = f(cls, *args, **kwargs) | ||
tables_cache.set(cache_key, o, timeout=timeout) | ||
return o | ||
return wrapped_f | ||
return wrap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯