From f6cc7a5824f59d2d8b92dec372ab0c0193675c11 Mon Sep 17 00:00:00 2001 From: Teodora Stojcheska Date: Tue, 22 Apr 2025 07:07:53 +0000 Subject: [PATCH] Remove leftover FullLineUserFactors.kt Merge-request: IJ-MR-160073 Merged-by: Teodora Stojcheska GitOrigin-RevId: a22c39158f99a228b1cf47d31d2ec505e621d4bc --- .../personalization/UserFactorDescriptions.kt | 2 - .../impl/FullLineUserFactors.kt | 136 ------------------ .../impl/UserFactorsManagerImpl.kt | 11 +- .../ml/personalization/DecayingCountTest.kt | 80 ----------- 4 files changed, 1 insertion(+), 228 deletions(-) delete mode 100644 plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/FullLineUserFactors.kt delete mode 100644 plugins/completion-ml-ranking/test/com/intellij/completion/ml/personalization/DecayingCountTest.kt diff --git a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/UserFactorDescriptions.kt b/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/UserFactorDescriptions.kt index e1ec11067546..13ad2e71bbd6 100644 --- a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/UserFactorDescriptions.kt +++ b/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/UserFactorDescriptions.kt @@ -42,8 +42,6 @@ object UserFactorDescriptions { Descriptor.register("prefixMatchingType", ::PrefixMatchingTypeUpdater, ::PrefixMatchingTypeReader) val TEMPLATES_USAGE: UserFactorDescription = Descriptor.register("templatesUsage", ::TemplatesUsageUpdater, ::TemplatesUsageReader) - val FULL_LINE_FACTORS: UserFactorDescription = - Descriptor.register("fullLineFactors", ::FullLineFactorsUpdater, ::FullLineFactorsReader) fun isKnownFactor(id: String): Boolean = id in IDS diff --git a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/FullLineUserFactors.kt b/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/FullLineUserFactors.kt deleted file mode 100644 index 9ec140d12503..000000000000 --- a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/FullLineUserFactors.kt +++ /dev/null @@ -1,136 +0,0 @@ -package com.intellij.completion.ml.personalization.impl - -import com.intellij.completion.ml.personalization.* -import java.time.Instant -import kotlin.math.pow -import kotlin.time.Duration -import kotlin.time.Duration.Companion.days -import kotlin.time.Duration.Companion.hours -import kotlin.time.DurationUnit - -private const val WAS_SELECTED = "was_selected" -private const val SELECTION = "selection" -private const val SHOW_UP = "show_up" - -private const val GLOBAL_ACCEPTANCE_RATE = 0.2 -private const val GLOBAL_ALPHA = 10 - -val DECAY_DURATIONS = listOf(1.hours, 1.days, 7.days) - -fun lastTimeName(name: String) = "last_${name}_time" -fun decayingCountName(name: String, decayDuration: Duration) = "${name}_count_decayed_by_$decayDuration" - -class FullLineFactorsReader(factor: DailyAggregatedDoubleFactor) : UserFactorReaderBase(factor) { - fun lastSelectionTimeToday(): Double? = getTodayFactor(lastTimeName(SELECTION)) - fun lastShowUpTimeToday(): Double? = getTodayFactor(lastTimeName(SHOW_UP)) - fun wasSelected(): Double? = getTodayFactor(WAS_SELECTED) - - private fun getTodayFactor(name: String) = factor.onDate(DateUtil.today())?.get(name) - - fun smoothedAcceptanceRate(decayDuration: Duration): Double { - val timestamp = currentEpochSeconds() - return globallySmoothedRatio(selectionCountDecayedBy(decayDuration, timestamp), showUpCountDecayedBy(decayDuration, timestamp)) - } - - fun selectionCountDecayedBy(decayDuration: Duration, timestamp: Double = currentEpochSeconds()) = - factor.aggregateDecayingCount(SELECTION, decayDuration, timestamp) - - fun showUpCountDecayedBy(decayDuration: Duration, timestamp: Double = currentEpochSeconds()) = - factor.aggregateDecayingCount(SHOW_UP, decayDuration, timestamp) -} - -private fun currentEpochSeconds() = Instant.now().epochSecond.toDouble() - - -class FullLineFactorsUpdater(factor: MutableDoubleFactor) : UserFactorUpdaterBase(factor) { - fun fireLookupElementSelected() { - val timestamp = currentEpochSeconds() - for (duration in DECAY_DURATIONS) { - factor.increment(SELECTION, duration, timestamp) - } - factor.wasSelected(true) - } - - fun fireLookupElementShowUp() { - val timestamp = currentEpochSeconds() - for (duration in DECAY_DURATIONS) { - factor.increment(SHOW_UP, duration, timestamp) - } - factor.wasSelected(false) - } -} - -class FullLineSmoothedAcceptanceRate(private val duration: Duration) - : UserFactorBase("fullLineAcceptanceRateSmoothedBy$duration", - UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String = reader.smoothedAcceptanceRate(duration).toString() -} - -class FullLineSelectionCountDecayedBy(private val duration: Duration) - : UserFactorBase("fullLineSelectionCountDecayedBy$duration", UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String = reader.selectionCountDecayedBy(duration).toString() -} - -class FullLineShowUpCountDecayedBy(private val duration: Duration) - : UserFactorBase("fullLineShowUpCountDecayedBy$duration", UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String = reader.showUpCountDecayedBy(duration).toString() -} - -class FullLineTimeSinceLastSelection - : UserFactorBase("fullLineTimeSinceLastSelection", UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String? = reader.lastSelectionTimeToday()?.let(::timeSince) - -} - -class FullLineTimeSinceLastShowUp - : UserFactorBase("fullLineTimeSinceLastShowUp", UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String? = reader.lastShowUpTimeToday()?.let(::timeSince) -} - -class FullLineWasSelected - : UserFactorBase("fullLineWasSelected", UserFactorDescriptions.FULL_LINE_FACTORS) { - override fun compute(reader: FullLineFactorsReader): String? = reader.wasSelected()?.toString() -} - -fun DailyAggregatedDoubleFactor.aggregateDecayingCount(name: String, decayDuration: Duration, timestamp: Double): Double { - var result = 0.0 - for (day in availableDays()) { - result += get(name, decayDuration, day, timestamp) ?: 0.0 - } - return result -} - -fun DailyAggregatedDoubleFactor.get(name: String, decayDuration: Duration, day: Day, timestamp: Double): Double? { - val onDate = onDate(day) ?: return null - val lastTimeName = lastTimeName(name) - val decayingCountName = decayingCountName(name, decayDuration) - val lastTime = onDate[lastTimeName] ?: return null - val decayingCount = onDate[decayingCountName] - return decayingCount.decay(timestamp - lastTime, decayDuration) -} - -fun MutableDoubleFactor.increment(name: String, decayDuration: Duration, timestamp: Double) { - updateOnDate(DateUtil.today()) { - val lastTimeName = lastTimeName(name) - val decayingCountName = decayingCountName(name, decayDuration) - this[decayingCountName] = this[lastTimeName]?.let { this[decayingCountName].decay(timestamp - it, decayDuration) + 1 } ?: 1.0 - this[lastTimeName] = timestamp - } -} - -private fun MutableDoubleFactor.wasSelected(boolean: Boolean) { - updateOnDate(DateUtil.today()) { - this[WAS_SELECTED] = if (boolean) 1.0 else 0.0 - } -} - -private fun Double?.decay(duration: Double, decayDuration: Duration) = - if (this == null) 0.0 - else if (duration * this == 0.0) this - else 0.5.pow(duration / decayDuration.toDouble(DurationUnit.SECONDS)) * this - -private fun globallySmoothedRatio(quotient: Double?, divisor: Double?) = - if (divisor == null) GLOBAL_ACCEPTANCE_RATE - else ((quotient ?: 0.0) + GLOBAL_ACCEPTANCE_RATE * GLOBAL_ALPHA) / (divisor + GLOBAL_ALPHA) - -private fun timeSince(epochSeconds: Double) = (Instant.now().epochSecond - epochSeconds.toLong()).toString() diff --git a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/UserFactorsManagerImpl.kt b/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/UserFactorsManagerImpl.kt index 3453a609927e..84d2b72c993a 100644 --- a/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/UserFactorsManagerImpl.kt +++ b/plugins/completion-ml-ranking/src/com/intellij/completion/ml/personalization/impl/UserFactorsManagerImpl.kt @@ -61,16 +61,7 @@ class UserFactorsManagerImpl : UserFactorsManager { register(TemplatesRatio()) - for (duration in DECAY_DURATIONS) { - register(FullLineSmoothedAcceptanceRate(duration)) - register(FullLineSelectionCountDecayedBy(duration)) - register(FullLineShowUpCountDecayedBy(duration)) - } - register(FullLineTimeSinceLastSelection()) - register(FullLineTimeSinceLastShowUp()) - register(FullLineWasSelected()) - - for (type in PrefixMatchingType.values()) + for (type in PrefixMatchingType.entries) register(PrefixMatchingTypeRatio(type)) } diff --git a/plugins/completion-ml-ranking/test/com/intellij/completion/ml/personalization/DecayingCountTest.kt b/plugins/completion-ml-ranking/test/com/intellij/completion/ml/personalization/DecayingCountTest.kt deleted file mode 100644 index 9b7e085f92bd..000000000000 --- a/plugins/completion-ml-ranking/test/com/intellij/completion/ml/personalization/DecayingCountTest.kt +++ /dev/null @@ -1,80 +0,0 @@ -package com.intellij.completion.ml.personalization - -import com.intellij.completion.ml.personalization.impl.* -import com.intellij.testFramework.UsefulTestCase -import java.util.* -import kotlin.time.Duration -import kotlin.time.Duration.Companion.days -import kotlin.time.Duration.Companion.hours -import kotlin.time.DurationUnit - -class DecayingCountTest : UsefulTestCase() { - private companion object { - const val TEST_NAME = "test" - - val TODAY = DateUtil.today() - - fun Day.timestamp(): Double { - return with(Calendar.getInstance()) { - set(year, month, dayOfMonth, 18, 0) - toInstant().epochSecond.toDouble() - } - } - - fun Day.update(count: Int): Day { - return with(Calendar.getInstance()) { - set(year, month - 1, dayOfMonth) - add(Calendar.DATE, count) - DateUtil.byDate(time) - } - } - - private fun Duration.inSeconds() = toDouble(DurationUnit.SECONDS) - } - - fun `test decaying factor incrementation`() { - val decayDuration = 1.hours - fun DailyAggregatedDoubleFactor.get(timestamp: Double) = get(TEST_NAME, decayDuration, TODAY, timestamp) - fun MutableDoubleFactor.increment(timestamp: Double) = increment(TEST_NAME, decayDuration, timestamp) - val timestamp = TODAY.timestamp() - - val factor = UserFactorStorageBase.DailyAggregateFactor() - assertTrue(factor.get(timestamp) == null) - factor.increment(timestamp) - assertTrue(factor.get(timestamp) == 1.0) - assertTrue(factor.get(timestamp + 1.hours.inSeconds()) == 0.5) - assertTrue(factor.get(timestamp + 2.hours.inSeconds()) == 0.25) - factor.increment(timestamp + 1.hours.inSeconds()) - assertTrue(factor.get(timestamp + 2.hours.inSeconds()) == 0.75) - } - - fun `test decaying factor aggregation`() { - val decayDuration = 1.days - fun DailyAggregatedDoubleFactor.aggregateDecayingCount(timestamp: Double) = aggregateDecayingCount(TEST_NAME, decayDuration, timestamp) - - val factor = UserFactorStorageBase.DailyAggregateFactor() - - val decayingCountName = decayingCountName(TEST_NAME, 1.days) - val lastTimeName = lastTimeName(TEST_NAME) - - val theDayBefore = TODAY.update(-1) - val twoDaysBefore = TODAY.update(-2) - val threeDaysBefore = TODAY.update(-3) - - factor.setOnDate(threeDaysBefore, decayingCountName, 80.0) - factor.setOnDate(threeDaysBefore, lastTimeName, threeDaysBefore.timestamp()) - assertTrue(factor.aggregateDecayingCount(threeDaysBefore.timestamp()) == 80.0) - - factor.setOnDate(twoDaysBefore, decayingCountName, 120.0) - factor.setOnDate(twoDaysBefore, lastTimeName, twoDaysBefore.timestamp()) - assertTrue(factor.aggregateDecayingCount(twoDaysBefore.timestamp()) == 40 + 120.0) - - factor.setOnDate(theDayBefore, decayingCountName, 80.0) - factor.setOnDate(theDayBefore, lastTimeName, theDayBefore.timestamp()) - assertTrue(factor.aggregateDecayingCount(theDayBefore.timestamp()) == 20 + 60 + 80.0) - - factor.setOnDate(TODAY, decayingCountName, 50.0) - factor.setOnDate(TODAY, lastTimeName, TODAY.timestamp()) - assertTrue(factor.aggregateDecayingCount(TODAY.timestamp()) == 10 + 30 + 40 + 50.0) - } -} \ No newline at end of file