Skip to content

Commit

Permalink
🥚 chore: Update tg animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvis2f committed Dec 31, 2024
1 parent 36b50ea commit 74f55f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
39 changes: 29 additions & 10 deletions api/src/main/java/telegram/files/AvgSpeed.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package telegram.files;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -77,25 +76,45 @@ private long calculateSpeed(long downloadedSize, long timestamp) {
}

private long smoothSpeed(long currentSpeed) {
if (speedPoints.isEmpty()) {
return currentSpeed;
}

List<Long> recentSpeeds = speedPoints.values().stream()
.skip(Math.max(0, speedPoints.size() - smoothingWindowSize))
.map(point -> point.speed)
.collect(Collectors.toList());

recentSpeeds.add(currentSpeed);

// Sort speeds to find median
Collections.sort(recentSpeeds);
long median = recentSpeeds.get(recentSpeeds.size() / 2);
if (recentSpeeds.size() < 2) {
return currentSpeed;
}

double mean = recentSpeeds.stream()
.mapToLong(Long::longValue)
.average()
.orElse(currentSpeed);

double standardDeviation = Math.sqrt(
recentSpeeds.stream()
.mapToDouble(speed -> {
double diff = speed - mean;
return diff * diff;
})
.average()
.orElse(0.0)
);

double upperThreshold = mean + (2 * standardDeviation);
double lowerThreshold = mean - (2 * standardDeviation);

// Filter out speeds that deviate too much from median
double threshold = median * 2.0;
List<Long> filteredSpeeds = recentSpeeds.stream()
.filter(speed -> speed <= threshold)
.filter(speed -> speed >= lowerThreshold && speed <= upperThreshold)
.toList();

// Calculate average of remaining speeds
return (long) filteredSpeeds.stream()
return filteredSpeeds.isEmpty()
? currentSpeed
: (long) filteredSpeeds.stream()
.mapToLong(Long::longValue)
.average()
.orElse(currentSpeed);
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/animations/tg-duck16_hey_out.json

Large diffs are not rendered by default.

0 comments on commit 74f55f2

Please sign in to comment.