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

File-upload: smooth time remaining, bitrate and stabilize user information #30147

Merged
merged 7 commits into from
Aug 16, 2023
Merged
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
54 changes: 40 additions & 14 deletions apps/files/js/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ OC.Uploader.prototype = _.extend({
type: 'PUT',
dropZone: options.dropZone, // restrict dropZone to content div
autoUpload: false,
progressInterval: 300, // increased from the default of 100ms for more stable behvaviour when predicting remaining time
sequentialUploads: false,
limitConcurrentUploads: 4,
/**
Expand Down Expand Up @@ -1151,7 +1152,7 @@ OC.Uploader.prototype = _.extend({

if (this._supportAjaxUploadWithProgress()) {
//remaining time
var lastUpdate, lastSize, bufferSize, buffer, bufferIndex, bufferIndex2, bufferTotal;
var lastUpdate, lastSize, bufferSize, buffer, bufferIndex, bufferTotal, smoothRemainingSeconds, smoothBitrate;

var dragging = false;

Expand All @@ -1169,11 +1170,15 @@ OC.Uploader.prototype = _.extend({
// initial remaining time variables
lastUpdate = new Date().getTime();
lastSize = 0;
bufferSize = 20;
bufferSize = 20; // length of the ring buffer
buffer = [];
bufferIndex = 0;
bufferIndex2 = 0;
bufferIndex = 0; // index of the ring buffer, runs from 0 to bufferSize continuously
bufferTotal = 0;
newTotal = 0;
smoothing = 0.02; // smoothing factor for EMA
h = '';
bufferFilled = false;

for(var i = 0; i < bufferSize; i++){
buffer[i] = 0;
}
Expand All @@ -1192,33 +1197,54 @@ OC.Uploader.prototype = _.extend({
var diffUpdate = (thisUpdate - lastUpdate)/1000; // eg. 2s
lastUpdate = thisUpdate;
var diffSize = data.loaded - lastSize;
if (diffSize <= 0) {
diffSize = lastSize;
}
lastSize = data.loaded;
diffSize = diffSize / diffUpdate; // apply timing factor, eg. 1MiB/2s = 0.5MiB/s, unit is byte per second
var remainingSeconds = ((total - data.loaded) / diffSize);

if(remainingSeconds >= 0) {
// bufferTotal holds the sum of all entries in the buffer, initially 0 like the entries itself
// substract current entry from total and add the current value to total
bufferTotal = bufferTotal - (buffer[bufferIndex]) + remainingSeconds;
// put current value to the entry
buffer[bufferIndex] = remainingSeconds; //buffer to make it smoother

bufferIndex = (bufferIndex + 1) % bufferSize;
bufferIndex2++;
}
var smoothRemainingSeconds;
if (bufferIndex2 > 0 && bufferIndex2 < 20) {
smoothRemainingSeconds = bufferTotal / bufferIndex2;
} else if (bufferSize > 0) {
if (bufferIndex === bufferSize - 1) {
bufferFilled = true;
}
//console.log('#', ' idx: ',bufferIndex, ' Total: ', bufferTotal, ' remainSeconds: ', remainingSeconds, ' during: ', diffUpdate);

if (smoothRemainingSeconds === null) {
smoothRemainingSeconds = bufferTotal / bufferSize;
} else {
smoothRemainingSeconds = 1;
} else{
smoothRemainingSeconds = smoothing * (bufferTotal / bufferSize) + ((1-smoothing) * smoothRemainingSeconds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smoothRemainingSeconds value depends on itself, this leads to NaN when I test it.

Copy link
Contributor

@artonge artonge Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe update the above condition to smoothRemainingSeconds === undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should check for undefined.
if (smoothRemainingSeconds === undefined){

Same for the bitrate:

if (smoothBitrate === undefined){

I just tested this and the NaN problem is fixed.

Copy link
Contributor

@artonge artonge Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you push the change?

}

if (bufferIndex % 4 === 0) {
h = moment.duration(smoothRemainingSeconds, "seconds").humanize({m: 50, h: 50});
}

var h = moment.duration(smoothRemainingSeconds, "seconds").humanize();
if (!(smoothRemainingSeconds >= 0 && smoothRemainingSeconds < 14400)) {
// wait for the buffer to be at least half filled, approximately takes 3s
if (!(smoothRemainingSeconds >= 0 && smoothRemainingSeconds < 14400) || (bufferFilled == false && bufferIndex < bufferSize/2)) {
// show "Uploading ..." for durations longer than 4 hours
h = t('files', 'Uploading …');
}

// smooth bitrate
if (smoothBitrate === null) {
smoothBitrate = data.bitrate;
} else{
smoothBitrate = smoothing * data.bitrate + ((1-smoothing) * smoothBitrate);
}

self._setProgressBarText(h, h, t('files', '{loadedSize} of {totalSize} ({bitrate})' , {
loadedSize: OC.Util.humanFileSize(data.loaded),
totalSize: OC.Util.humanFileSize(total),
bitrate: OC.Util.humanFileSize(data.bitrate / 8) + '/s'
bitrate: OC.Util.humanFileSize(smoothBitrate / 8) + '/s'
}));
self._setProgressBarValue(progress);
self.trigger('progressall', e, data);
Expand Down