2015-06-25 17:53:03 -04:00
|
|
|
import showModal from 'discourse/lib/show-modal';
|
2015-06-26 12:16:09 -04:00
|
|
|
import Query from 'discourse/plugins/discourse-data-explorer/discourse/models/query';
|
2015-06-30 15:52:17 -04:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2015-06-25 17:53:03 -04:00
|
|
|
|
2015-06-29 15:10:24 -04:00
|
|
|
export default Ember.ArrayController.extend({
|
2015-06-30 18:56:09 -04:00
|
|
|
queryParams: { selectedQueryId: "id" },
|
2015-06-30 13:20:22 -04:00
|
|
|
selectedQueryId: null,
|
2015-06-30 13:37:48 -04:00
|
|
|
results: null,
|
2015-06-30 18:12:12 -04:00
|
|
|
showResults: false,
|
2015-06-30 12:07:33 -04:00
|
|
|
loading: false,
|
|
|
|
|
2015-06-30 18:56:09 -04:00
|
|
|
explain: true,
|
2015-06-25 17:53:03 -04:00
|
|
|
|
2015-06-30 13:20:22 -04:00
|
|
|
saveDisabled: Ember.computed.not('selectedItem.dirty'),
|
|
|
|
runDisabled: Ember.computed.alias('selectedItem.dirty'),
|
|
|
|
|
|
|
|
selectedItem: function() {
|
|
|
|
const _id = this.get('selectedQueryId');
|
|
|
|
const id = parseInt(_id);
|
|
|
|
return this.get('content').find(function(q) {
|
|
|
|
return q.get('id') === id;
|
|
|
|
});
|
|
|
|
}.property('selectedQueryId'),
|
2015-06-25 17:53:03 -04:00
|
|
|
|
2015-06-30 18:12:12 -04:00
|
|
|
clearResults: function() {
|
|
|
|
this.set('showResults', false);
|
|
|
|
this.set('results', null);
|
|
|
|
}.observes('selectedQueryId'),
|
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
addCreatedRecord(record) {
|
|
|
|
this.pushObject(record);
|
|
|
|
this.set('selectedQueryId', Ember.get(record, 'id'));
|
|
|
|
this.get('selectedItem').set('dirty', false);
|
2015-06-30 18:12:12 -04:00
|
|
|
this.set('showResults', false);
|
2015-06-30 15:52:17 -04:00
|
|
|
this.set('results', null);
|
|
|
|
},
|
|
|
|
|
2015-06-30 13:20:22 -04:00
|
|
|
actions: {
|
2015-06-25 17:53:03 -04:00
|
|
|
dummy() {},
|
|
|
|
|
|
|
|
importQuery() {
|
|
|
|
showModal('import-query');
|
2015-06-30 13:37:48 -04:00
|
|
|
this.set('showCreate', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
showCreate() {
|
|
|
|
this.set('showCreate', true);
|
2015-06-25 17:53:03 -04:00
|
|
|
},
|
|
|
|
|
2015-06-30 12:07:33 -04:00
|
|
|
editName() {
|
2015-06-30 13:20:22 -04:00
|
|
|
this.set('editName', true);
|
2015-06-30 12:07:33 -04:00
|
|
|
},
|
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
download() {
|
|
|
|
window.open(this.get('selectedItem.downloadUrl'), "_blank");
|
|
|
|
},
|
|
|
|
|
2015-06-30 18:12:12 -04:00
|
|
|
resetParams() {
|
|
|
|
this.get('selectedItem').resetParams();
|
|
|
|
},
|
|
|
|
|
|
|
|
saveDefaults() {
|
|
|
|
this.get('selectedItem').saveDefaults();
|
|
|
|
},
|
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
create() {
|
|
|
|
const self = this;
|
|
|
|
this.set('loading', true);
|
|
|
|
this.set('showCreate', false);
|
|
|
|
var newQuery = this.store.createRecord('query', {name: this.get('newQueryName')});
|
|
|
|
newQuery.save().then(function(result) {
|
|
|
|
self.addCreatedRecord(result.target);
|
|
|
|
}).catch(popupAjaxError).finally(function() {
|
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-06-30 12:07:33 -04:00
|
|
|
save() {
|
|
|
|
const self = this;
|
|
|
|
this.set('loading', true);
|
2015-06-30 13:20:22 -04:00
|
|
|
this.get('selectedItem').save().then(function() {
|
|
|
|
const query = self.get('selectedItem');
|
|
|
|
query.markNotDirty();
|
|
|
|
self.set('editName', false);
|
2015-06-30 15:52:17 -04:00
|
|
|
}).catch(popupAjaxError).finally(function() {
|
2015-06-30 12:07:33 -04:00
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
discard() {
|
|
|
|
const self = this;
|
|
|
|
this.set('loading', true);
|
2015-06-30 13:20:22 -04:00
|
|
|
this.store.find('query', this.get('selectedItem.id')).then(function(result) {
|
|
|
|
const query = self.get('selectedItem');
|
|
|
|
query.setProperties(result.getProperties(Query.updatePropertyNames));
|
|
|
|
query.markNotDirty();
|
|
|
|
self.set('editName', false);
|
2015-06-30 13:37:48 -04:00
|
|
|
self.set('results', null);
|
2015-06-30 15:52:17 -04:00
|
|
|
}).catch(popupAjaxError).finally(function() {
|
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
const self = this;
|
|
|
|
const query = this.get('selectedItem');
|
|
|
|
this.set('loading', true);
|
|
|
|
this.store.destroyRecord('query', query).then(function() {
|
|
|
|
query.set('destroyed', true);
|
|
|
|
}).catch(popupAjaxError).finally(function() {
|
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
recover() {
|
|
|
|
const self = this;
|
|
|
|
const query = this.get('selectedItem');
|
|
|
|
this.set('loading', true);
|
|
|
|
query.save().then(function() {
|
|
|
|
query.set('destroyed', false);
|
|
|
|
}).catch(popupAjaxError).finally(function() {
|
2015-06-30 12:07:33 -04:00
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
run() {
|
2015-06-30 18:12:12 -04:00
|
|
|
const self = this;
|
2015-06-30 13:20:22 -04:00
|
|
|
if (this.get('selectedItem.dirty')) {
|
2015-06-30 12:07:33 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-30 18:12:12 -04:00
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
this.set('loading', true);
|
2015-06-30 13:20:22 -04:00
|
|
|
Discourse.ajax("/admin/plugins/explorer/queries/" + this.get('selectedItem.id') + "/run", {
|
2015-06-25 17:53:03 -04:00
|
|
|
type: "POST",
|
|
|
|
data: {
|
2015-06-30 18:12:12 -04:00
|
|
|
params: JSON.stringify(this.get('selectedItem.params')),
|
2015-06-30 18:56:09 -04:00
|
|
|
explain: this.get('explain')
|
2015-06-25 17:53:03 -04:00
|
|
|
}
|
|
|
|
}).then(function(result) {
|
2015-06-30 22:51:38 -04:00
|
|
|
self.set('results', result);
|
2015-06-30 18:12:12 -04:00
|
|
|
if (!result.success) {
|
2015-06-30 22:51:38 -04:00
|
|
|
self.set('showResults', false);
|
|
|
|
return;
|
2015-06-30 18:12:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
self.set('showResults', true);
|
2015-06-30 22:51:38 -04:00
|
|
|
}).catch(function(result) {
|
|
|
|
self.set('showResults', false);
|
2015-06-25 17:53:03 -04:00
|
|
|
self.set('results', result);
|
2015-06-30 22:51:38 -04:00
|
|
|
}).finally(function() {
|
2015-06-25 17:53:03 -04:00
|
|
|
self.set('loading', false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|