From 10041b18ec33327e8ee2a7eca33c46009e1ca2b3 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Sun, 3 Aug 2025 17:58:23 -0300 Subject: [PATCH] initial speed calc state --- .../src/components/prompts/UploadFiles.vue | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/prompts/UploadFiles.vue b/frontend/src/components/prompts/UploadFiles.vue index d07a8145..f7e5baf0 100644 --- a/frontend/src/components/prompts/UploadFiles.vue +++ b/frontend/src/components/prompts/UploadFiles.vue @@ -84,7 +84,7 @@ const eta = ref(Infinity); const fileStore = useFileStore(); const uploadStore = useUploadStore(); -const { sentBytes } = storeToRefs(uploadStore); +const { sentBytes, totalBytes } = storeToRefs(uploadStore); const byteToMbyte = partial({ exponent: 2 }); @@ -99,9 +99,19 @@ const totalMbytes = computed(() => byteToMbyte(uploadStore.totalBytes)); const speedMbytes = computed(() => byteToMbyte(speed.value)); let lastSpeedUpdate: number = 0; -const recentSpeeds: number[] = []; +let recentSpeeds: 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 bytesSinceLastUpdate = sentBytes - oldSentBytes; const currentSpeed = bytesSinceLastUpdate / elapsedTime; @@ -114,7 +124,12 @@ const calculateSpeed = (sentBytes: number, oldSentBytes: number) => { const recentSpeedsAverage = 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(); calculateEta(); @@ -135,6 +150,14 @@ const calculateEta = () => { watch(sentBytes, calculateSpeed); +watch(totalBytes, (totalBytes, oldTotalBytes) => { + if (oldTotalBytes > 0) { + return; + } + + lastSpeedUpdate = Date.now(); +}); + const formattedETA = computed(() => { if (!eta.value || eta.value === Infinity) { return "--:--:--";