Add factors based on selected item position
authorVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Tue, 5 Dec 2017 11:16:18 +0000 (14:16 +0300)
committerVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Tue, 5 Dec 2017 11:16:18 +0000 (14:16 +0300)
plugins/stats-collector/src/com/intellij/stats/completion/LookupCompletedTracker.kt
plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorDescriptions.kt
plugins/stats-collector/src/com/intellij/stats/personalization/impl/ItemPositionFactors.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/impl/UserFactorsManagerImpl.kt

index acb86c66d934bf16d18983ad2d965828b13b4cb6..3cb1c8162bf551ac45edbeb5c2e7015da50385a7 100644 (file)
@@ -64,6 +64,13 @@ class LookupCompletedTracker : LookupAdapter() {
         UserFactorStorage.applyOnBoth(lookup.project, UserFactorDescriptions.PREFIX_LENGTH_ON_COMPLETION) { updater ->
             updater.fireCompletionPerformed(prefixLength)
         }
+
+        val itemPosition = lookup.selectedIndex
+        if (itemPosition != -1) {
+            UserFactorStorage.applyOnBoth(lookup.project, UserFactorDescriptions.SELECTED_ITEM_POSITION) { updater ->
+                updater.fireCompletionPerformed(itemPosition)
+            }
+        }
     }
 
     private fun processTypedSelect(lookup: LookupImpl, element: LookupElement) {
index d5b8681d3df9afb685f1b7a81970cd4d946c46df..9d0d184a0ab02c0818b4b2f2ccf70272d384d844 100644 (file)
@@ -11,6 +11,7 @@ object UserFactorDescriptions {
             Descriptor("completionFinishedType", ::CompletionFinishTypeUpdater, ::CompletionFinishTypeReader)
     val COMPLETION_USAGE = Descriptor("completionUsage", ::CompletionUsageUpdater, ::CompletionUsageReader)
     val PREFIX_LENGTH_ON_COMPLETION = Descriptor("prefixLength", ::PrefixLengthUpdater, ::PrefixLengthReader)
+    val SELECTED_ITEM_POSITION = Descriptor("itemPosition", ::ItemPositionUpdater, ::ItemPositionReader)
 
     class Descriptor<out U : FactorUpdater, out R : FactorReader>(
             override val factorId: String,
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ItemPositionFactors.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ItemPositionFactors.kt
new file mode 100644 (file)
index 0000000..1f183b5
--- /dev/null
@@ -0,0 +1,50 @@
+package com.intellij.stats.personalization.impl
+
+import com.intellij.stats.personalization.UserFactorBase
+import com.intellij.stats.personalization.UserFactorDescriptions
+import com.intellij.stats.personalization.UserFactorReaderBase
+import com.intellij.stats.personalization.UserFactorUpdaterBase
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+class ItemPositionReader(factor: DailyAggregatedDoubleFactor) : UserFactorReaderBase(factor) {
+    fun getCountsByPosition(): Map<Int, Double> {
+        return factor.aggregateSum().asIterable().associate { (key, value) -> key.toInt() to value }
+    }
+
+    fun getAveragePosition(): Double? {
+        val positionToCount = getCountsByPosition()
+        if (positionToCount.isEmpty()) return null
+
+        val positionsSum = positionToCount.asSequence().sumByDouble { it.key * it.value }
+        val completionCount = positionToCount.asSequence().sumByDouble { it.value }
+
+        if (completionCount == 0.0) return null
+        return positionsSum / completionCount
+    }
+}
+
+class ItemPositionUpdater(factor: MutableDoubleFactor) : UserFactorUpdaterBase(factor) {
+    fun fireCompletionPerformed(selectedItemOrder: Int) {
+        factor.incrementOnToday(selectedItemOrder.toString())
+    }
+}
+
+class AverageSelectedItemPosition()
+    : UserFactorBase<ItemPositionReader>("averageSelectedPosition", UserFactorDescriptions.SELECTED_ITEM_POSITION) {
+    override fun compute(reader: ItemPositionReader): String? = reader.getAveragePosition()?.toString()
+}
+
+class MaxSelectedItemPosition()
+    : UserFactorBase<ItemPositionReader>("maxSelectedItemPosition", UserFactorDescriptions.SELECTED_ITEM_POSITION) {
+    override fun compute(reader: ItemPositionReader): String? =
+            reader.getCountsByPosition().asSequence().filter { it.value != 0.0 }.maxBy { it.key }?.key?.toString()
+}
+
+class MostFrequentSelectedItemPosition()
+    : UserFactorBase<ItemPositionReader>("mostFrequentItemPosition", UserFactorDescriptions.SELECTED_ITEM_POSITION) {
+    override fun compute(reader: ItemPositionReader): String? =
+            reader.getCountsByPosition().maxBy { it.value }?.key?.toString()
+}
+
index e29af0e9a1ab60b90972fa98721ca54c22bf1ed7..060827a09fecedff7df0531c9cf7855762e0dca0 100644 (file)
@@ -12,7 +12,6 @@ import com.intellij.stats.personalization.UserFactorsManager
  */
 class UserFactorsManagerImpl : UserFactorsManager, ProjectComponent {
     private companion object {
-
         val LOG = Logger.getInstance(UserFactorsManagerImpl::class.java)
     }
     private val userFactors = mutableMapOf<String, UserFactor>()
@@ -32,6 +31,10 @@ class UserFactorsManagerImpl : UserFactorsManager, ProjectComponent {
 
         register(MostFrequentPrefixLength())
         register(AveragePrefixLength())
+
+        register(AverageSelectedItemPosition())
+        register(MaxSelectedItemPosition())
+        register(MostFrequentSelectedItemPosition())
     }
 
     override fun getAllFactors(): List<UserFactor> = userFactors.values.toList()