Skip to content

Commit

Permalink
Use weighted average, increase update freq. to 10 sec
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH authored and kelson42 committed Feb 14, 2025
1 parent ae2c3bf commit c2c6975
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
13 changes: 8 additions & 5 deletions Model/Utilities/DownloadTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class DownloadTime {
time = key
amount = value
}
return mean(averages)
return weightedMean(averages)
}

private func latestSample() -> (CFTimeInterval, Int64)? {
Expand All @@ -83,11 +83,14 @@ final class DownloadTime {
return (lastTime, lastAmount)
}

private func mean(_ values: [Double]) -> Double {
let sum = values.reduce(0) { partialResult, value in
private func weightedMean(_ values: [Double]) -> Double {
let weights: [Double] = (0...values.count).map { (Double($0) + 1.0) * 1.2 }
let sum = values.enumerated().reduce(1.0) { partialResult, iterator in
partialResult + (iterator.element * weights[iterator.offset])
}
let sumOfWeights = weights.reduce(1.0) { partialResult, value in
partialResult + value
}
let average = sum / Double(values.count)
return average
return sum / sumOfWeights
}
}
5 changes: 1 addition & 4 deletions Views/LiveActivity/ActivityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class ActivityService {
publisher: @MainActor @escaping () -> CurrentValueSubject<[UUID: DownloadState], Never> = {
DownloadService.shared.progress.publisher
},
updateFrequency: Double = 1,
updateFrequency: Double = 10,
averageDownloadSpeedFromLastSeconds: Double = 30
) {
assert(updateFrequency > 0)
Expand Down Expand Up @@ -116,9 +116,6 @@ final class ActivityService {

let now = CACurrentMediaTime()
for (key, state) in states {
if downloadTimes[key] == nil {
debugPrint("Creating new downloadTimes for \(key)")
}
let downloadTime: DownloadTime = downloadTimes[key] ?? DownloadTime(
considerLastSeconds: averageDownloadSpeedFromLastSeconds,
total: state.total
Expand Down

0 comments on commit c2c6975

Please sign in to comment.