Implement DailyAggregatedDoubleFactor.aggregateAverage
authorVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Mon, 4 Dec 2017 13:16:14 +0000 (16:16 +0300)
committerVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Mon, 4 Dec 2017 13:16:14 +0000 (16:16 +0300)
plugins/stats-collector/src/com/intellij/stats/personalization/impl/DailyAggregatedDoubleFactor.kt

index 598591024a189866a11d891ed6e693dff01c4b06..870a895b262c9bc9bd48e8b5c8685bf93c0f8bc9 100644 (file)
@@ -39,8 +39,21 @@ fun DailyAggregatedDoubleFactor.aggregateSum(): Map<String, Double> = aggregateB
 
 fun DailyAggregatedDoubleFactor.aggregateAverage(): Map<String, Double> {
     val result = mutableMapOf<String, Double>()
-
-    // TODO: implement
+    val counts = mutableMapOf<String, Int>()
+    for (onDate in availableDates().mapNotNull(this::onDate)) {
+        for ((key, value) in onDate) {
+            result.compute(key) { _, old ->
+                if (old != null) {
+                    val n = counts[key]!!.toDouble()
+                    counts.computeIfPresent(key) { _, value -> value + 1 }
+                    (n / (n + 1)) * old + value / (n + 1)
+                } else {
+                    counts[key] = 1
+                    value
+                }
+            }
+        }
+    }
 
     return result
 }