Add factors based on prefix length when completion was performed
authorVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Tue, 5 Dec 2017 10:41:00 +0000 (13:41 +0300)
committerVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Tue, 5 Dec 2017 10:41:00 +0000 (13:41 +0300)
plugins/stats-collector/src/com/intellij/stats/completion/LookupCompletedTracker.kt
plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorBase.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorDescriptions.kt
plugins/stats-collector/src/com/intellij/stats/personalization/impl/PrefixLengthFactor.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/impl/UserFactorsManagerImpl.kt

index 74aab0d9b805c8bdb202270b88c8b33d4cfd4b91..acb86c66d934bf16d18983ad2d965828b13b4cb6 100644 (file)
@@ -32,16 +32,14 @@ class LookupCompletedTracker : LookupAdapter() {
         val lookup = event?.lookup as? LookupImpl ?: return
         val element = lookup.currentItem ?: return
         if (isSelectedByTyping(lookup, element)) {
-            processTypedSelect(lookup)
-            processElementSelected(lookup, element)
+            processTypedSelect(lookup, element)
         }
     }
 
     override fun itemSelected(event: LookupEvent?) {
         val lookup = event?.lookup as? LookupImpl ?: return
         val element = event.item ?: return
-        processExplicitSelect(lookup)
-        processElementSelected(lookup, element)
+        processExplicitSelect(lookup, element)
     }
 
     private fun isSelectedByTyping(lookup: LookupImpl, element: LookupElement): Boolean =
@@ -55,13 +53,22 @@ class LookupCompletedTracker : LookupAdapter() {
         relevanceMap.forEach { name, value -> userFactorsManager.getFeatureFactor(name)?.update(value) }
     }
 
-    private fun processExplicitSelect(lookup: LookupImpl) {
+    private fun processExplicitSelect(lookup: LookupImpl, element: LookupElement) {
+        processElementSelected(lookup, element)
+
         UserFactorStorage.applyOnBoth(lookup.project, UserFactorDescriptions.COMPLETION_FINISH_TYPE) { updater ->
             updater.fireExplicitCompletionPerformed()
         }
+
+        val prefixLength = lookup.getPrefixLength(element)
+        UserFactorStorage.applyOnBoth(lookup.project, UserFactorDescriptions.PREFIX_LENGTH_ON_COMPLETION) { updater ->
+            updater.fireCompletionPerformed(prefixLength)
+        }
     }
 
-    private fun processTypedSelect(lookup: LookupImpl) {
+    private fun processTypedSelect(lookup: LookupImpl, element: LookupElement) {
+        processElementSelected(lookup, element)
+
         UserFactorStorage.applyOnBoth(lookup.project, UserFactorDescriptions.COMPLETION_FINISH_TYPE) { updater ->
             updater.fireTypedSelectPerformed()
         }
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorBase.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorBase.kt
new file mode 100644 (file)
index 0000000..9893bc5
--- /dev/null
@@ -0,0 +1,12 @@
+package com.intellij.stats.personalization
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+abstract class UserFactorBase<in R : FactorReader>(override val id: String, private val descriptor: UserFactorDescription<*, R>) : UserFactor {
+    override final fun compute(storage: UserFactorStorage): String? {
+        return compute(storage.getFactorReader(descriptor))
+    }
+
+    abstract fun compute(reader: R): String?
+}
\ No newline at end of file
index 35352a68e6e4d9fad9c738207d2e05761edc49b5..d5b8681d3df9afb685f1b7a81970cd4d946c46df 100644 (file)
@@ -10,7 +10,7 @@ object UserFactorDescriptions {
     val COMPLETION_FINISH_TYPE =
             Descriptor("completionFinishedType", ::CompletionFinishTypeUpdater, ::CompletionFinishTypeReader)
     val COMPLETION_USAGE = Descriptor("completionUsage", ::CompletionUsageUpdater, ::CompletionUsageReader)
-
+    val PREFIX_LENGTH_ON_COMPLETION = Descriptor("prefixLength", ::PrefixLengthUpdater, ::PrefixLengthReader)
 
     class Descriptor<out U : FactorUpdater, out R : FactorReader>(
             override val factorId: String,
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/impl/PrefixLengthFactor.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/impl/PrefixLengthFactor.kt
new file mode 100644 (file)
index 0000000..7d2e845
--- /dev/null
@@ -0,0 +1,42 @@
+package com.intellij.stats.personalization.impl
+
+import com.intellij.stats.personalization.*
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+class PrefixLengthReader(factor: DailyAggregatedDoubleFactor) : UserFactorReaderBase(factor) {
+    fun getCountsByPrefixLength(): Map<Int, Double> {
+        return factor.aggregateSum().asIterable().associate { (key, value) -> key.toInt() to value }
+    }
+
+    fun getAveragePrefixLength(): Double? {
+        val lengthToCount = getCountsByPrefixLength()
+        if (lengthToCount.isEmpty()) return null
+
+        val totalChars = lengthToCount.asSequence().sumByDouble { it.key * it.value }
+        val completionCount = lengthToCount.asSequence().sumByDouble { it.value }
+
+        if (completionCount == 0.0) return null
+        return totalChars / completionCount
+    }
+}
+
+class PrefixLengthUpdater(factor: MutableDoubleFactor) : UserFactorUpdaterBase(factor) {
+    fun fireCompletionPerformed(prefixLength: Int) {
+        factor.incrementOnToday(prefixLength.toString())
+    }
+}
+
+class MostFrequentPrefixLength : UserFactorBase<PrefixLengthReader>("mostFrequentPrefixLength",
+        UserFactorDescriptions.PREFIX_LENGTH_ON_COMPLETION) {
+    override fun compute(reader: PrefixLengthReader): String? {
+        return reader.getCountsByPrefixLength().maxBy { it.value }?.key?.toString()
+    }
+}
+
+class AveragePrefixLength : UserFactorBase<PrefixLengthReader>("", UserFactorDescriptions.PREFIX_LENGTH_ON_COMPLETION) {
+    override fun compute(reader: PrefixLengthReader): String? {
+        return reader.getAveragePrefixLength()?.toString()
+    }
+}
\ No newline at end of file
index 01a2b60901d8cec28affdef3198477017f93f05a..e29af0e9a1ab60b90972fa98721ca54c22bf1ed7 100644 (file)
@@ -29,6 +29,9 @@ class UserFactorsManagerImpl : UserFactorsManager, ProjectComponent {
         register(TodayCompletionUsageCount())
         register(TotalUsageCount())
         register(WeekAverageUsageCount())
+
+        register(MostFrequentPrefixLength())
+        register(AveragePrefixLength())
     }
 
     override fun getAllFactors(): List<UserFactor> = userFactors.values.toList()