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

add upload progress #274

Merged
merged 3 commits into from
Mar 4, 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
4 changes: 2 additions & 2 deletions app/plot_app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

email_notifications_config = dict(_conf.items('email_notifications'))
email_notifications_config['public_flightreport'] = \
[ s.strip() for s in email_notifications_config['public_flightreport'].split(',')]
[ s.strip() for s in email_notifications_config['public_flightreport'].split(',') if s]
email_notifications_config['public_flightreport_bad'] = \
[ s.strip() for s in email_notifications_config['public_flightreport_bad'].split(',')]
[ s.strip() for s in email_notifications_config['public_flightreport_bad'].split(',') if s]

__DOMAIN_NAME = _conf.get('general', 'domain_name')
__HTTP_PROTOCOL = _conf.get('general', 'http_protocol')
Expand Down
7 changes: 3 additions & 4 deletions app/plot_app/configured_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ def generate_plots(ulog, px4_ulog, db_data, vehicle_data, link_to_3d_page,
changed_params=changed_params, x_range=x_range)
num_rc_channels = 8
if data_plot.dataset:
max_channels = np.amax(data_plot.dataset.data['channel_count'])
if max_channels < num_rc_channels: num_rc_channels = max_channels
num_rc_channels = min(np.amax(data_plot.dataset.data['channel_count']), num_rc_channels)
legends = []
for i in range(num_rc_channels):
channel_names = px4_ulog.get_configured_rc_input_names(i)
Expand Down Expand Up @@ -618,8 +617,8 @@ def generate_plots(ulog, px4_ulog, db_data, vehicle_data, link_to_3d_page,
# only plot if at least one of the outputs is not constant
all_constant = True
if data_plot.dataset:
max_outputs = np.amax(data_plot.dataset.data['noutputs'])
if max_outputs < num_actuator_outputs: num_actuator_outputs = max_outputs
num_actuator_outputs = min(np.amax(data_plot.dataset.data['noutputs']),
num_actuator_outputs)

for i in range(num_actuator_outputs):
output_data = data_plot.dataset.data['output['+str(i)+']']
Expand Down
45 changes: 45 additions & 0 deletions app/plot_app/static/js/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,50 @@ $(function() { // on startup
restoreSetting('email');
restoreSetting('access');
updateAccess();

// Ajax file upload with progress updates
$("#upload-form").on('submit', function (e) {
e.preventDefault();
var form_data = new FormData(this);
var progress_bar = $('.progress');
progress_bar.show();
var upload_button = $('#upload-button');
upload_button.hide();

$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percent_complete = (evt.loaded / evt.total) * 99; // max to 99, as the redirect will take time too
progress_bar.find('.progress-bar').width(percent_complete + '%');
progress_bar.find('.progress-bar').html(percent_complete.toFixed(0) + '%');
}
}, false);
return xhr;
},
type: 'POST',
url: '/upload',
data: form_data,
cache: false,
contentType: false,
processData: false,
success: function (data) {
// Handle the response from the server
console.log(data);
var json_response = JSON.parse(data);
window.location.href = json_response.url
},
error: function (data, textStatus) {
// Handle errors, if any
console.error('Error uploading file.');
console.error(textStatus);
var progress_bar = $('.progress');
progress_bar.hide();
var upload_failure = $('#upload-failure');
upload_failure.show();
}
});
});
});

15 changes: 14 additions & 1 deletion app/plot_app/templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h3>Upload a Log File</h3>


<table class="spaced">
<form enctype="multipart/form-data" action="/upload" method="post">
<form enctype="multipart/form-data" id="upload-form">
<tr>
<td width="30%" class="left">Description <small>(optional)</small>:</td>
<td width="70%">
Expand Down Expand Up @@ -102,6 +102,9 @@ <h3>Upload a Log File</h3>
<!-- automatically set depending on the "access" value -->
<input type="hidden" class="custom-control-input" name="public"
id="public" value="false">
<!-- instruct server not to redirect, but return the URL instead -->
<input type="hidden" class="custom-control-input" name="redirect"
id="redirect" value="false">
</td>
</tr>
<tr>
Expand All @@ -121,6 +124,16 @@ <h3>Upload a Log File</h3>
<td></td><td><br/><input class="btn btn-primary align-right"
id="upload-button" type="submit" value="Upload" onclick="return validateForm();" /></td>
</tr>
<tr>
<td colspan="2">
<div class="progress mt-3" style="height: 20px; display: none;">
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
</div>
<div id="upload-failure" class="align-right" style="display: none;">
<b>File upload failed.</b>
</div>
</td>
</tr>
</form>
</table>

Expand Down
12 changes: 9 additions & 3 deletions app/tornado_handlers/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import print_function
import datetime
import json
import os
from html import escape
import sys
Expand Down Expand Up @@ -115,7 +116,7 @@ def post(self, *args, **kwargs):
['description', 'email',
'allowForAnalysis', 'obfuscated', 'source', 'type',
'feedback', 'windSpeed', 'rating', 'videoUrl', 'public',
'vehicleName'])
'vehicleName', 'redirect'])
description = escape(form_data['description'].decode("utf-8"))
email = form_data['email'].decode("utf-8")
upload_type = 'personal'
Expand All @@ -136,6 +137,9 @@ def post(self, *args, **kwargs):
feedback = ''
if 'feedback' in form_data:
feedback = escape(form_data['feedback'].decode("utf-8"))
should_redirect = source != 'QGroundControl'
if 'redirect' in form_data:
should_redirect = form_data['redirect'].decode("utf-8") == 'true'
wind_speed = -1
rating = ''
stored_email = ''
Expand Down Expand Up @@ -291,9 +295,11 @@ def post(self, *args, **kwargs):
# send notification emails
send_notification_email(email, full_plot_url, delete_url, info)

# do not redirect for QGC
if source != 'QGroundControl':
if should_redirect:
self.redirect(url)
else:
# Return plot url as json
self.write(json.dumps({"url": url}))

except CustomHTTPError:
raise
Expand Down
Loading