Skip to content

Commit

Permalink
use fetch API for retrieving query results
Browse files Browse the repository at this point in the history
  • Loading branch information
prohde committed Jun 4, 2024
1 parent 558b4d9 commit a916c61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
3 changes: 2 additions & 1 deletion FedSDM/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def query() -> str:


@bp.route('/feedback', methods=['POST'])
@use_kwargs({'fed': fields.Str(required=True)}, location='query')
#@use_kwargs({'fed': fields.Str(required=True)}, location='query')
@use_kwargs({
'fed': fields.Str(required=True),
'pred': fields.Str(required=True),
'row': fields.List(fields.Str(), required=True, data_key='row[]'),
'columns': fields.List(fields.Str(), required=True, data_key='columns[]'),
Expand Down
82 changes: 40 additions & 42 deletions FedSDM/static/js/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,41 @@ addFeedbackDialog.on('hidden.bs.modal', function() {
});
$('#add-feedback-btn').on('click', function() { addFeedback(true) });

function addFeedback(close) {
async function addFeedback(close) {
allFeedbackFields.removeClass('ui-state-error');
const valid = checkSelection(feedbackPredicates, 'column') && checkLength(feedbackDesc, 'description', 2, 500);
let valid = checkSelection(feedbackPredicates, 'column') && checkLength(feedbackDesc, 'description', 2, 500);
console.log({'desc': feedbackDesc.val(), 'pred': feedbackPredicates.val(), 'query': yasqe.getValue(), 'row': selectedRowData, 'columns': queryVars});

if (valid) {
$.ajax({
type: 'POST',
headers: { Accept: 'application/json' },
url: '/query/feedback?fed=' + federation,
data: {
'desc': feedbackDesc.val(),
'pred': feedbackPredicates.val(),
'query': yasqe.getValue(),
'row': selectedRowData,
'columns': queryVars
},
dataType: 'json',
crossDomain: true,
success: function(data) {
let data = new FormData();
data.append('fed', federation)
data.append('desc', feedbackDesc.val());
data.append('pred', feedbackPredicates.val());
data.append('query', yasqe.getValue());
data.append('row[]', selectedRowData);
data.append('columns[]', queryVars);
console.log(data);

valid = await fetch('/query/feedback', {
method: 'POST',
headers: { Accept: 'application/text' },
body: data
})
.then(res => res.text())
.then(data => {
console.log(data);
if (data === null || data.length === 0) { $('#validateTips').html('Error while adding feedback!') }
},
error: function(jqXHR, textStatus) {
console.log(jqXHR.status);
console.log(jqXHR.responseText);
console.log(textStatus);
}
});
if (data === null || data.length === 0) {
$('#validateTips').html('Error while adding feedback!');
return false;
}
return true;
})
.catch(err => console.log(err));
} else {
close = false;
console.log('Invalid data...');
}
if (close) { addFeedbackDialog.modal('hide') }
if (valid && close) { addFeedbackDialog.modal('hide') }
return valid;
}

Expand All @@ -210,15 +211,14 @@ $('#add_feedback').on('click', function() {
feedbackPredicates.append('<option value="All">All</option>');
});

function show_incremental(vars) {
async function show_incremental(vars) {
if (response === true) {
// No new request can be sent unless a response from the last request was received
response = false;
if (shouldStop === false) {
let req = $.ajax({
type: 'GET',
url: '/query/nextresult',
success: function(data) {
await fetch('/query/nextresult')
.then(res => res.json())
.then(data => {
let row = data.result;
let elemTimeTotal = $('#time_total');
if (row.length === 0 || row === 'EOF') {
Expand Down Expand Up @@ -250,18 +250,16 @@ function show_incremental(vars) {
});
});
response = true;
}
});
req.done(function() {
// This makes it able to send new request on the next interval
if (response === true && shouldStop === false) {
response = true;
show_incremental(vars);
} else {
shouldStop = false;
$('#btnStop').prop('disabled', true);
}
});
})
.catch(err => console.log(err));

if (response === true && shouldStop === false) {
response = true;
await show_incremental(vars);
} else {
shouldStop = false;
$('#btnStop').prop('disabled', true);
}
}
}
}
Expand Down

0 comments on commit a916c61

Please sign in to comment.