initial speed calc state

This commit is contained in:
Ramires Viana 2025-08-03 17:58:23 -03:00
parent c10de19c10
commit 10041b18ec

View File

@ -84,7 +84,7 @@ const eta = ref<number>(Infinity);
const fileStore = useFileStore(); const fileStore = useFileStore();
const uploadStore = useUploadStore(); const uploadStore = useUploadStore();
const { sentBytes } = storeToRefs(uploadStore); const { sentBytes, totalBytes } = storeToRefs(uploadStore);
const byteToMbyte = partial({ exponent: 2 }); const byteToMbyte = partial({ exponent: 2 });
@ -99,9 +99,19 @@ const totalMbytes = computed(() => byteToMbyte(uploadStore.totalBytes));
const speedMbytes = computed(() => byteToMbyte(speed.value)); const speedMbytes = computed(() => byteToMbyte(speed.value));
let lastSpeedUpdate: number = 0; let lastSpeedUpdate: number = 0;
const recentSpeeds: number[] = []; let recentSpeeds: number[] = [];
const calculateSpeed = (sentBytes: number, oldSentBytes: number) => { const calculateSpeed = (sentBytes: number, oldSentBytes: number) => {
if (sentBytes === 0) {
lastSpeedUpdate = 0;
recentSpeeds = [];
eta.value = Infinity;
speed.value = 0;
return;
}
const elapsedTime = (Date.now() - (lastSpeedUpdate ?? 0)) / 1000; const elapsedTime = (Date.now() - (lastSpeedUpdate ?? 0)) / 1000;
const bytesSinceLastUpdate = sentBytes - oldSentBytes; const bytesSinceLastUpdate = sentBytes - oldSentBytes;
const currentSpeed = bytesSinceLastUpdate / elapsedTime; const currentSpeed = bytesSinceLastUpdate / elapsedTime;
@ -114,7 +124,12 @@ const calculateSpeed = (sentBytes: number, oldSentBytes: number) => {
const recentSpeedsAverage = const recentSpeedsAverage =
recentSpeeds.reduce((acc, curr) => acc + curr) / recentSpeeds.length; recentSpeeds.reduce((acc, curr) => acc + curr) / recentSpeeds.length;
speed.value = recentSpeedsAverage * 0.2 + speed.value * 0.8; if (recentSpeeds.length === 1) {
speed.value = recentSpeedsAverage;
} else {
speed.value = recentSpeedsAverage * 0.2 + speed.value * 0.8;
}
lastSpeedUpdate = Date.now(); lastSpeedUpdate = Date.now();
calculateEta(); calculateEta();
@ -135,6 +150,14 @@ const calculateEta = () => {
watch(sentBytes, calculateSpeed); watch(sentBytes, calculateSpeed);
watch(totalBytes, (totalBytes, oldTotalBytes) => {
if (oldTotalBytes > 0) {
return;
}
lastSpeedUpdate = Date.now();
});
const formattedETA = computed(() => { const formattedETA = computed(() => {
if (!eta.value || eta.value === Infinity) { if (!eta.value || eta.value === Infinity) {
return "--:--:--"; return "--:--:--";