discourse-data-explorer/assets/javascripts/discourse/components/query-result.js.es6

181 lines
5.4 KiB
Plaintext
Raw Normal View History

2016-10-30 21:12:01 -04:00
import { ajax } from 'discourse/lib/ajax';
2015-08-26 00:36:39 -04:00
import Badge from 'discourse/models/badge';
2016-11-09 11:58:18 -05:00
import { getOwner } from 'discourse-common/lib/get-owner';
2015-06-30 18:12:12 -04:00
function randomIdShort() {
return 'xxxxxxxx'.replace(/[xy]/g, function() {
2015-09-14 18:34:57 -04:00
/*eslint-disable*/
return (Math.random() * 16 | 0).toString(16);
2015-09-14 18:34:57 -04:00
/*eslint-enable*/
});
}
2015-06-30 18:12:12 -04:00
2015-08-26 00:36:39 -04:00
function transformedRelTable(table, modelClass) {
2015-08-25 23:48:19 -04:00
const result = {};
table.forEach(function(item) {
2015-08-26 00:36:39 -04:00
if (modelClass) {
result[item.id] = modelClass.create(item);
} else {
result[item.id] = item;
}
2015-08-25 23:48:19 -04:00
});
return result;
}
2015-06-30 18:12:12 -04:00
const QueryResultComponent = Ember.Component.extend({
layoutName: 'explorer-query-result',
rows: Em.computed.alias('content.rows'),
columns: Em.computed.alias('content.columns'),
params: Em.computed.alias('content.params'),
explainText: Em.computed.alias('content.explain'),
hasExplain: Em.computed.notEmpty('content.explain'),
colCount: function() {
return this.get('content.columns').length;
}.property('content.columns.length'),
duration: function() {
return I18n.t('explorer.run_time', {value: I18n.toNumber(this.get('content.duration'), {precision: 1})});
}.property('content.duration'),
parameterAry: function() {
let arr = [];
const params = this.get('params');
for (var key in params) {
if (params.hasOwnProperty(key)) {
arr.push({key: key, value: params[key]});
}
}
return arr;
}.property('params.@each'),
2015-08-25 23:48:19 -04:00
columnDispNames: function() {
2015-07-01 00:21:14 -04:00
if (!this.get('columns')) {
return [];
2015-06-30 18:12:12 -04:00
}
2015-09-14 18:34:57 -04:00
return this.get('columns').map(function(colName) {
2015-08-25 23:48:19 -04:00
if (colName.endsWith("_id")) {
return colName.slice(0, -3);
2015-06-30 18:12:12 -04:00
}
2015-08-25 23:48:19 -04:00
const dIdx = colName.indexOf('$');
if (dIdx >= 0) {
return colName.substring(dIdx + 1);
}
return colName;
});
}.property('content', 'columns.@each'),
2015-06-30 18:12:12 -04:00
fallbackTemplate: function() {
2016-11-09 11:58:18 -05:00
return getOwner(this).lookup('template:explorer/text.raw');
}.property(),
2015-08-25 23:48:19 -04:00
columnTemplates: function() {
const self = this;
if (!this.get('columns')) {
return [];
}
return this.get('columns').map(function(colName, idx) {
let viewName = "text";
if (self.get('content.colrender')[idx]) {
viewName = self.get('content.colrender')[idx];
}
// After `findRawTemplates` is in stable this should be updated to use that
let template = getOwner(self).lookup('template:explorer/' + viewName + '.raw');
if (!template) {
template = Discourse.RAW_TEMPLATES[`javascripts/explorer/${viewName}`];
}
2016-11-09 11:58:18 -05:00
return {name: viewName, template };
2015-06-30 18:12:12 -04:00
});
}.property('content', 'columns.@each'),
2015-08-25 23:48:19 -04:00
transformedUserTable: function() {
return transformedRelTable(this.get('content.relations.user'));
}.property('content.relations.user'),
2015-08-26 00:36:39 -04:00
transformedBadgeTable: function() {
return transformedRelTable(this.get('content.relations.badge'), Badge);
}.property('content.relations.badge'),
transformedPostTable: function() {
return transformedRelTable(this.get('content.relations.post'));
}.property('content.relations.post'),
transformedTopicTable: function() {
return transformedRelTable(this.get('content.relations.topic'));
}.property('content.relations.topic'),
transformedGroupTable: function() {
return transformedRelTable(this.get('site.groups'));
}.property('site.groups'),
2015-08-26 00:36:39 -04:00
lookupUser(id) {
2015-08-25 23:48:19 -04:00
return this.get('transformedUserTable')[id];
},
2015-08-26 00:36:39 -04:00
lookupBadge(id) {
return this.get('transformedBadgeTable')[id];
},
lookupPost(id) {
return this.get('transformedPostTable')[id];
},
lookupTopic(id) {
return this.get('transformedTopicTable')[id];
},
lookupGroup(id) {
return this.get('transformedGroupTable')[id];
},
2015-08-26 00:36:39 -04:00
lookupCategory(id) {
return this.site.get('categoriesById')[id];
},
2015-08-25 23:48:19 -04:00
2015-08-03 18:07:29 -04:00
downloadResult(format) {
// Create a frame to submit the form in (?)
// to avoid leaving an about:blank behind
let windowName = randomIdShort();
2015-08-03 18:19:31 -04:00
const newWindowContents = "<style>body{font-size:36px;display:flex;justify-content:center;align-items:center;}</style><body>Click anywhere to close this window once the download finishes.<script>window.onclick=function(){window.close()};</script>";
2015-08-03 18:07:29 -04:00
2015-09-14 18:34:57 -04:00
window.open('data:text/html;base64,' + btoa(newWindowContents), windowName);
2015-08-03 18:07:29 -04:00
let form = document.createElement("form");
form.setAttribute('id', 'query-download-result');
form.setAttribute('method', 'post');
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.' + format + '?download=1'));
form.setAttribute('target', windowName);
form.setAttribute('style', 'display:none;');
2015-09-14 18:34:57 -04:00
function addInput(name, value) {
2015-08-03 18:07:29 -04:00
let field;
field = document.createElement('input');
field.setAttribute('name', name);
field.setAttribute('value', value);
form.appendChild(field);
}
2015-09-14 18:34:57 -04:00
addInput('params', JSON.stringify(this.get('params')));
addInput('explain', this.get('hasExplain'));
addInput('limit', '1000000');
2016-10-30 21:12:01 -04:00
ajax('/session/csrf.json').then(function(csrf) {
2015-09-14 18:34:57 -04:00
addInput('authenticity_token', csrf.csrf);
2015-08-03 18:07:29 -04:00
document.body.appendChild(form);
form.submit();
Em.run.next('afterRender', function() {
document.body.removeChild(form);
2017-08-02 04:19:38 -04:00
});
2015-08-03 18:07:29 -04:00
});
},
actions: {
downloadResultJson() {
this.downloadResult('json');
},
downloadResultCsv() {
this.downloadResult('csv');
}
}
2015-06-30 18:12:12 -04:00
});
export default QueryResultComponent;