Remove leftover FullLineUserFactors.kt

Merge-request: IJ-MR-160073
Merged-by: Teodora Stojcheska <teodora.stojcheska@jetbrains.com>

GitOrigin-RevId: a22c39158f99a228b1cf47d31d2ec505e621d4bc
This commit is contained in:
Teodora Stojcheska
2025-04-22 07:07:53 +00:00
committed by intellij-monorepo-bot
parent 6c3aa96d43
commit f6cc7a5824
4 changed files with 1 additions and 228 deletions
@@ -42,8 +42,6 @@ object UserFactorDescriptions {
Descriptor.register("prefixMatchingType", ::PrefixMatchingTypeUpdater, ::PrefixMatchingTypeReader)
val TEMPLATES_USAGE: UserFactorDescription<TemplatesUsageUpdater, TemplatesUsageReader> =
Descriptor.register("templatesUsage", ::TemplatesUsageUpdater, ::TemplatesUsageReader)
val FULL_LINE_FACTORS: UserFactorDescription<FullLineFactorsUpdater, FullLineFactorsReader> =
Descriptor.register("fullLineFactors", ::FullLineFactorsUpdater, ::FullLineFactorsReader)
fun isKnownFactor(id: String): Boolean = id in IDS
@@ -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<FullLineFactorsReader>("fullLineAcceptanceRateSmoothedBy$duration",
UserFactorDescriptions.FULL_LINE_FACTORS) {
override fun compute(reader: FullLineFactorsReader): String = reader.smoothedAcceptanceRate(duration).toString()
}
class FullLineSelectionCountDecayedBy(private val duration: Duration)
: UserFactorBase<FullLineFactorsReader>("fullLineSelectionCountDecayedBy$duration", UserFactorDescriptions.FULL_LINE_FACTORS) {
override fun compute(reader: FullLineFactorsReader): String = reader.selectionCountDecayedBy(duration).toString()
}
class FullLineShowUpCountDecayedBy(private val duration: Duration)
: UserFactorBase<FullLineFactorsReader>("fullLineShowUpCountDecayedBy$duration", UserFactorDescriptions.FULL_LINE_FACTORS) {
override fun compute(reader: FullLineFactorsReader): String = reader.showUpCountDecayedBy(duration).toString()
}
class FullLineTimeSinceLastSelection
: UserFactorBase<FullLineFactorsReader>("fullLineTimeSinceLastSelection", UserFactorDescriptions.FULL_LINE_FACTORS) {
override fun compute(reader: FullLineFactorsReader): String? = reader.lastSelectionTimeToday()?.let(::timeSince)
}
class FullLineTimeSinceLastShowUp
: UserFactorBase<FullLineFactorsReader>("fullLineTimeSinceLastShowUp", UserFactorDescriptions.FULL_LINE_FACTORS) {
override fun compute(reader: FullLineFactorsReader): String? = reader.lastShowUpTimeToday()?.let(::timeSince)
}
class FullLineWasSelected
: UserFactorBase<FullLineFactorsReader>("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()
@@ -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))
}
@@ -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)
}
}