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

Csv comunica #3

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 35 additions & 14 deletions src/ldf-client-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,21 +718,32 @@ if (typeof global.process === 'undefined')
},

_downloadCSV: function () {
if (!this._resultCount) return alert('Please execute a query to download');
else {
let csvContent = 'data:text/csv;charset=utf-8,';
let header = Object.keys(this.bindingResults[0]);
csvContent += [header].map(e => e.join(',')) + '\n';
this.bindingResults.forEach(function (v, i, a) {
let line = [];
header.forEach(function (h, i) {
line.push(v[h]);
});
csvContent += [line].map(e => e.join(',')) + '\n';
});
let encodedUri = encodeURI(csvContent);
window.open(encodedUri);
// Let the worker execute the query
var context = {
...this._getQueryContext(),
sources: Object.keys(this.options.selectedDatasources).map(function (datasource) {
var type;
var posAt = datasource.indexOf('@');
if (posAt > 0) {
type = datasource.substr(0, posAt);
datasource = datasource.substr(posAt + 1, datasource.length);
}
datasource = resolve(datasource, window.location.href);
return { type: type, value: datasource };
}),
};
var prefixesString = '';
if (this.options.queryFormat === 'sparql') {
for (var prefix in this.options.prefixes)
prefixesString += 'PREFIX ' + prefix + ': <' + this.options.prefixes[prefix] + '>\n';
}
var query = prefixesString + this.$queryTextsIndexed[this.options.queryFormat].val();
this._queryWorker.postMessage({
type: 'querycsv',
query: query,
context: context,
resultsToTree: this.options.resultsToTree,
});
},
_downloadLog: function () {
let filename = 'execution.log';
Expand Down Expand Up @@ -1047,6 +1058,16 @@ if (typeof global.process === 'undefined')
return self._logAppender(data.log);
case 'error': return this.onerror(data.error);
case 'webIdName': return self._setWebIdName(data.name);
case 'resultCsv':
let filename = 'data.csv';
let blob = new Blob([data.data], { type: 'text/json' });
let e = document.createEvent('MouseEvents');
let a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
};
this._queryWorker.onerror = function (error) {
Expand Down
22 changes: 21 additions & 1 deletion src/ldf-client-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ function initEngine(config) {

// Handlers of incoming messages
var handlers = {
querycsv: async function (config) {
initEngine(config);
engine.query(config.query, config.context).then(async function (result) {
const { data } = await engine.resultToString(result, 'text/csv');
function readableToString(stream) {
const chunks = [];
return new Promise((res, rej) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (e) => { rej(e); });
stream.on('end', () => { res(Buffer.concat(chunks).toString('utf-8')); });
});
}
if (result.resultType === 'bindings') {
let resultsCsv = await readableToString(data);
postMessage({
type: 'resultCsv',
data: resultsCsv,
});
}
});
},
// Execute the given query with the given options
query: function (config) {
initEngine(config);
Expand All @@ -48,7 +69,6 @@ var handlers = {
.then(async function (result) {
// Post query metadata
postMessage({ type: 'queryInfo', queryType: result.resultType });

var bindings = result.resultType === 'bindings';
var resultsToTree = config.resultsToTree;
switch (result.resultType) {
Expand Down