Skip to content

Commit

Permalink
Add updated Statistics merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Apr 21, 2024
1 parent 034525d commit e489d4b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
8 changes: 4 additions & 4 deletions TowerForge/TowerForge/Metrics/Statistics/Statistic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ extension Statistic {
maxCurrentValue: largerMaxCurrent)
}

static func merge(this: Self, that: Self) -> Self {
static func merge<T: Statistic>(this: T, that: T) -> T {
let largerPermanent = max(this.permanentValue, that.permanentValue)
let largerCurrent = max(this.currentValue, that.currentValue)
let largerMaxCurrent = max(this.maximumCurrentValue, that.maximumCurrentValue)

return Self(permanentValue: largerPermanent,
currentValue: largerCurrent,
maxCurrentValue: largerMaxCurrent)
return T(permanentValue: largerPermanent,
currentValue: largerCurrent,
maxCurrentValue: largerMaxCurrent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,26 @@ extension StatisticsDatabase: Equatable {
rhs = that
}

let mergedStats = StatisticsDatabase()
let mergedStats = StatisticsFactory.getDefaultStatisticsDatabase()

// Merge lhs statistics
for (key, lhsStat) in lhs.statistics {
mergedStats.statistics[key] = lhsStat
}

// Merge rhs statistics and resolve conflicts
for (key, rhsStat) in rhs.statistics {
if let lhsStat = mergedStats.statistics[key] {

// If lhs has the key, compare and choose the one with the greater magnitude.
if lhsStat.permanentValue < rhsStat.permanentValue || lhsStat.currentValue < rhsStat.currentValue {

mergedStats.statistics[key]?.permanentValue = Double.maximumMagnitude(lhsStat.permanentValue,
rhsStat.permanentValue)

mergedStats.statistics[key]?.currentValue = Double.maximumMagnitude(lhsStat.currentValue,
rhsStat.currentValue)

mergedStats.statistics[key]?.currentValue = Double.maximumMagnitude(lhsStat.maximumCurrentValue,
rhsStat.maximumCurrentValue)
}
// If they are equal, lhsStat is already set, so do nothing.
mergedStats.statistics[key] = lhsStat.merge(with: rhsStat)
Logger.log("MERGE-LOOP: Statistic \(key) updated to " +
"\(String(describing: mergedStats.statistics[key]))", self)
} else {

// If lhs does not have the key, simply add the rhs stat.
mergedStats.statistics[key] = rhsStat
Logger.log("MERGE-LOOP: Statistic \(key) created with " +
"\(String(describing: mergedStats.statistics[key]))", self)
}
}

Logger.log("SDB: Merged stats contain \(mergedStats.toString())")
Logger.log("SDB: Merged stats contain \(mergedStats.toString())", self)
return mergedStats
}
}
2 changes: 1 addition & 1 deletion TowerForge/TowerForge/StorageAPI/StorageHandler+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension StorageHandler {
}

if let error = error {
Logger.log("IMPT: onLogin failed due to error: \(error.localizedDescription) from STORAGE_HANDLER", self)
Logger.log("IMPT: onLogin failed from STORAGE_HANDLER: \(error.localizedDescription)", self)
return
}

Expand Down

0 comments on commit e489d4b

Please sign in to comment.