From e489d4bf4663d9fefbfdc1d0e881a6bf591294c0 Mon Sep 17 00:00:00 2001 From: Rubesh Date: Sun, 21 Apr 2024 10:52:15 +0800 Subject: [PATCH] Add updated Statistics merging --- .../Metrics/Statistics/Statistic.swift | 8 +++--- .../Statistics/StatisticsDatabase+Merge.swift | 26 +++++-------------- .../StorageAPI/StorageHandler+Auth.swift | 2 +- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/TowerForge/TowerForge/Metrics/Statistics/Statistic.swift b/TowerForge/TowerForge/Metrics/Statistics/Statistic.swift index 0fc3540c..d1a4601c 100644 --- a/TowerForge/TowerForge/Metrics/Statistics/Statistic.swift +++ b/TowerForge/TowerForge/Metrics/Statistics/Statistic.swift @@ -190,13 +190,13 @@ extension Statistic { maxCurrentValue: largerMaxCurrent) } - static func merge(this: Self, that: Self) -> Self { + static func merge(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) } } diff --git a/TowerForge/TowerForge/Metrics/Statistics/StatisticsDatabase+Merge.swift b/TowerForge/TowerForge/Metrics/Statistics/StatisticsDatabase+Merge.swift index b3b0c4f0..348cd7d9 100644 --- a/TowerForge/TowerForge/Metrics/Statistics/StatisticsDatabase+Merge.swift +++ b/TowerForge/TowerForge/Metrics/Statistics/StatisticsDatabase+Merge.swift @@ -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 } } diff --git a/TowerForge/TowerForge/StorageAPI/StorageHandler+Auth.swift b/TowerForge/TowerForge/StorageAPI/StorageHandler+Auth.swift index cef9f6a6..d12a39cf 100644 --- a/TowerForge/TowerForge/StorageAPI/StorageHandler+Auth.swift +++ b/TowerForge/TowerForge/StorageAPI/StorageHandler+Auth.swift @@ -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 }