Updated home.js again: Live upload speed...

Hmm...

This should theoretically be more accurate.
This will keep uploadprogress data of the past >1s to calculate
total bytes sent in 1s, in real time.
As opposed to only calculating per second.

But hmm...

I'm starting to feel like I'm spending too much time on this feature.
This commit is contained in:
Bobby Wibowo 2020-04-22 03:28:51 +07:00
parent 9c7b0e22d7
commit f1cceca563
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
4 changed files with 48 additions and 20 deletions

2
dist/js/home.js vendored

File diff suppressed because one or more lines are too long

2
dist/js/home.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -340,10 +340,10 @@ page.prepareDropzone = () => {
}
// Attach necessary data for initial upload speed calculation
if (xhr._upSpeedCalc === undefined)
xhr._upSpeedCalc = {
bytes: 0,
timestamp: Date.now()
if (xhr._uplSpeedCalc === undefined)
xhr._uplSpeedCalc = {
lastSent: 0,
data: [{ timestamp: Date.now(), bytes: 0 }]
}
// If not chunked uploads, add extra headers
@ -384,22 +384,50 @@ page.prepareDropzone = () => {
prefix = `Uploading chunk ${chunkIndex}/${file.upload.totalChunkCount}\u2026`
}
// Upload speed calculation
// Real-time upload speed calculation
let prettyBytesPerSec
if (!skipProgress) {
const now = Date.now()
const elapsedSecs = (now - xhr._upSpeedCalc.timestamp) / 1000
// Calculate only if at least 1s has elapsed
if (elapsedSecs >= 1) {
const bytesSent = upl.bytesSent - xhr._upSpeedCalc.bytes
const bytesPerSec = elapsedSecs ? (bytesSent / elapsedSecs) : 0
// Update data for next upload speed calculation
xhr._upSpeedCalc.bytes = upl.bytesSent
xhr._upSpeedCalc.timestamp = now
// Store latest pretty speed to be used in next iteration, if less than 1s elapsed
xhr._upSpeedCalc.lastPretty = prettyBytesPerSec = page.getPrettyBytes(bytesPerSec)
} else if (xhr._upSpeedCalc.lastPretty) {
prettyBytesPerSec = xhr._upSpeedCalc.lastPretty
const bytesSent = upl.bytesSent - xhr._uplSpeedCalc.lastSent
// Push data of current iteration
xhr._uplSpeedCalc.lastSent = upl.bytesSent
xhr._uplSpeedCalc.data.push({ timestamp: now, bytes: bytesSent })
// Wait till at least the 2nd iteration (3 data including initial data)
const length = xhr._uplSpeedCalc.data.length
if (length > 2) {
// Calculate using data from all iterations
let elapsed = 0
let bytesPerSec = 0
let fullSec = false
let i = length - 1 // Always start with 2nd from last item
while (i--) {
// Splice data of unrequired iterations
if (fullSec) {
xhr._uplSpeedCalc.data.splice(i, 1)
continue
}
// Sum data
elapsed = now - xhr._uplSpeedCalc.data[i].timestamp
if (elapsed > 1000) {
const excessDuration = elapsed - 1000
const newerIterationElapsed = now - xhr._uplSpeedCalc.data[i + 1].timestamp
const duration = elapsed - newerIterationElapsed
const fragment = (duration - excessDuration) / duration * xhr._uplSpeedCalc.data[i + 1].bytes
bytesPerSec += fragment
fullSec = true
} else {
bytesPerSec += xhr._uplSpeedCalc.data[i + 1].bytes
}
}
// If not enough data
if (!fullSec)
bytesPerSec = 1000 / elapsed * bytesPerSec
// Get pretty bytes
prettyBytesPerSec = page.getPrettyBytes(bytesPerSec)
}
}

View File

@ -1,5 +1,5 @@
{
"1": "1587464783",
"1": "1587500638",
"2": "1581416390",
"3": "1581416390",
"4": "1581416390",