Display errors, convert integers

This commit is contained in:
Kane York 2015-06-30 19:51:38 -07:00
parent 497f71896d
commit 1ea74847ae
7 changed files with 81 additions and 12 deletions

View File

@ -4,5 +4,5 @@ export default Ember.TextField.extend({
this.get('params')[this.get('pname')] = value;
}
return this.get('params')[this.get('pname')];
}.property()
}.property('params', 'pname')
});

View File

@ -137,14 +137,17 @@ export default Ember.ArrayController.extend({
explain: this.get('explain')
}
}).then(function(result) {
self.set('results', result);
if (!result.success) {
return popupAjaxError(result);
self.set('showResults', false);
return;
}
console.log(result);
self.set('showResults', true);
}).catch(function(result) {
self.set('showResults', false);
self.set('results', result);
}).catch(popupAjaxError).finally(function() {
}).finally(function() {
self.set('loading', false);
});
}

View File

@ -5,10 +5,20 @@ Query = RestModel.extend({
dirty: false,
params: {},
_init: function() {
this._super();
if (!this.get('options')) {
this.set('options', {defaults:{}});
}
this.set('dirty', false);
}.on('init'),
_initParams: function() {
this.resetParams();
}.on('init').observes('param_names'),
options: Em.computed.alias('qopts'),
markDirty: function() {
this.set('dirty', true);
}.observes('name', 'description', 'sql', 'options', 'options.defaults'),
@ -37,7 +47,11 @@ Query = RestModel.extend({
const currentParams = this.get('params');
let defaults = {};
(this.get('param_names') || []).forEach(function(name) {
defaults[name] = currentParams[name];
if (currentParams[name]) {
defaults[name] = currentParams[name];
} else {
delete defaults[name];
}
});
this.set('options.defaults', defaults);
},
@ -80,7 +94,7 @@ Query = RestModel.extend({
});
Query.reopenClass({
updatePropertyNames: ["name", "description", "sql", "options"]
updatePropertyNames: ["name", "description", "sql", "qopts"]
});
export default Query;

View File

@ -1,24 +1,38 @@
<h3>Queries</h3>
<h2>Queries</h2>
<div class="query-list">
{{combo-box valueAttribute="id" value=selectedQueryId nameProperty="listName" content=content castInteger="true" nameChanges="true"}}
{{d-button action="showCreate" icon="plus" class="no-text"}}
{{d-button action="importQuery" label="explorer.import.label" icon="upload"}}
</div>
{{#if showCreate}}
<div class="query-create">
{{text-field value=newQueryName placeholderKey="explorer.create_placeholder"}}
{{d-button action="create" label="explorer.create" icon="plus" class="btn-primary"}}
</div>
{{/if}}
<hr>
<div class="query-edit">
{{partial "admin/plugins-explorer-show" model=selectedItem}}
</div>
{{conditional-loading-spinner condition=loading}}
<div class="query-run">
{{#if selectedItem.param_names}}
<div class="query-params">
<div class="param-save">
{{d-button action="saveDefaults" label="explorer.save_params"}}
{{d-button action="resetParams" label="explorer.reset_params"}}
</div>
{{#each selectedItem.param_names as |pname|}}
{{param-field params=selectedItem.params pname=pname}} {{pname}}
<div class="param">
{{param-field params=selectedItem.params pname=pname}}
<span class="param-name">{{pname}}</span>
</div>
{{/each}}
</div>
{{/if}}
@ -27,10 +41,17 @@
</div>
{{d-button action="run" label="explorer.run" disabled=runDisabled}}
</div>
<hr>
{{conditional-loading-spinner condition=loading}}
{{#if results}}
<div class="query-results">
{{query-result query=selectedItem content=results}}
{{#if showResults}}
{{query-result query=selectedItem content=results}}
{{else}}
{{#each results.errors as |err|}}
<pre class="query-error"><code>{{~err}}</code></pre>
{{/each}}
{{/if}}
</div>
{{/if}}

View File

@ -33,6 +33,18 @@
input {
margin: 9px;
}
.param {
display: inline-block;
overflow-x: visible;
}
.param-save {
float: right;
margin: 9px;
}
.param-name {
display: inline-block;
width: 70px;
}
}
.query-list, .query-edit, .query-results, .query-params {

View File

@ -38,3 +38,5 @@ en:
run_time: "Query completed in {{value}} ms."
column: "Column {{number}}"
explain_label: "Include query plan?"
save_params: "Set Defaults"
reset_params: "Reset"

View File

@ -79,6 +79,15 @@ after_initialize do
query_args = (query.qopts[:defaults] || {}).with_indifferent_access.merge(params)
# Rudimentary types
query_args.map! do |arg|
if arg =~ /\A\d+\z/
arg.to_i
else
arg
end
end
time_start, time_end, explain, err, result = nil
begin
ActiveRecord::Base.connection.transaction do
@ -89,7 +98,7 @@ after_initialize do
/*
* DataExplorer Query
* Query: /admin/plugins/explorer/#{query.id}
* Query: /admin/plugins/explorer?id=#{query.id}
* Started by: #{opts[:current_user]}
*/
WITH query AS (
@ -290,11 +299,20 @@ SQL
render json: {success: true, errors: []}
end
# Return value:
# success - true/false. if false, inspect the errors value.
# errors - array of strings.
# params - hash. Echo of the query parameters as executed.
# duration - float. Time to execute the query, in milliseconds, to 1 decimal place.
# columns - array of strings. Titles of the returned columns, in order.
# explain - string. (Optional - pass explain=true in the request) Postgres query plan, UNIX newlines.
# rows - array of array of strings. Results of the query. In the same order as 'columns'.
def run
query = DataExplorer::Query.find(params[:id].to_i)
query_params = MultiJson.load(params[:params])
opts = {current_user: current_user.username}
opts[:explain] = true if params[:explain] == "true"
opts[:limit] = params[:limit].to_i if params[:limit]
result = DataExplorer.run_query(query, query_params, opts)
if result[:error]
@ -320,7 +338,6 @@ SQL
json = {
success: true,
errors: [],
params: query_params,
duration: (result[:duration_nanos].to_f / 1_000_000).round(1),
columns: cols,
}