경량 집계 버킷(AppUsageBucket) 도입 및 1년 주기 원시 로그 자동 정리

- AppUsageBucket 모델 추가 (schemaVersion 202611160001L): (itemType, itemKey, dayOfWeek, hour) 단위로 횟수/최초·최근 시각/월·일 비트마스크를 압축 저장해 데이터 증가에도 추천 조회 속도 일정 유지
- WorkersDb:
  - logAppUsage(), logContactInteraction(), syncSystemUsageStats()에서 AppUsageBucket 증분 집계 및 추천 캐시 무효화
  - 기존 AppUsageLog 최초 1회 자동 백필(backfillUsageBucketsIfNeeded)
  - 2분 TTL 추천 결과 인메모리 캐시 및 하루 1회 365일 경과 원시 로그 자동 정리(cleanupOldUsageLogsIfNeeded)
This commit is contained in:
2026-11-16 14:39:34 +09:00
parent 6925c80015
commit d7da59bec6
2 changed files with 197 additions and 45 deletions
@@ -0,0 +1,25 @@
package bums.lunatic.launcher.model
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
/**
* 시간대/요일별 사용 빈도를 압축 저장하는 경량 집계 테이블.
* 원시 로그(AppUsageLog)가 대량으로 쌓이거나 1년 경과 후 삭제되어도
* 추천 점수 계산을 일정한 속도(O(버킷 수))로 유지하기 위해 사용합니다.
*/
class AppUsageBucket : RealmObject {
@PrimaryKey
var bucketKey: String = "" // "${itemType}|${itemKey}|${dayOfWeek}|${hour}"
var itemKey: String = "" // 패키지명 또는 연락처 ID
var itemType: String = "APP" // "APP" 또는 "CONTACT"
var dayOfWeek: Int = 0 // 1(일)~7(토)
var hour: Int = 0 // 0~23
var count: Int = 0 // 누적 실행/접촉 횟수
var firstTimestamp: Long = 0L // 최초 기록 시각 (장기 패턴 판별용)
var lastTimestamp: Long = 0L // 최근 기록 시각 (최신성 감쇠용)
var dayOfMonthMask: Int = 0 // 1~31일 비트마스크 (매월 N일 패턴)
var monthMask: Int = 0 // 0~11월 비트마스크 (매년 M월 N일 패턴)
}
@@ -4,7 +4,9 @@ import android.app.usage.UsageStatsManager
import android.content.Context import android.content.Context
import bums.lunatic.launcher.common.letTrue import bums.lunatic.launcher.common.letTrue
import bums.lunatic.launcher.model.AppInfo import bums.lunatic.launcher.model.AppInfo
import bums.lunatic.launcher.model.AppUsageBucket
import bums.lunatic.launcher.model.AppUsageLog import bums.lunatic.launcher.model.AppUsageLog
import io.realm.kotlin.MutableRealm
import bums.lunatic.launcher.model.Astro import bums.lunatic.launcher.model.Astro
import bums.lunatic.launcher.model.BotCommandEentitie import bums.lunatic.launcher.model.BotCommandEentitie
import bums.lunatic.launcher.model.Condition import bums.lunatic.launcher.model.Condition
@@ -77,7 +79,7 @@ object WorkersDb {
// WeatherForcast::class, Location::class, Current::class, Forecast::class, Condition::class, Forecastday::class, Day::class, Astro::class, Hour::class, // WeatherForcast::class, Location::class, Current::class, Forecast::class, Condition::class, Forecastday::class, Day::class, Astro::class, Hour::class,
LocationLog::class, LocationLog::class,
LastInfo::class, HistoryItem::class, ReaderConfig::class, ContentsCollection::class, ContentsPageInfo::class, LastInfo::class, HistoryItem::class, ReaderConfig::class, ContentsCollection::class, ContentsPageInfo::class,
AppUsageLog::class, AppUsageLog::class, AppUsageBucket::class,
WidgetData::class, WidgetData::class,
ExpressionItem::class,ExpressionWord::class,QuoteItem::class ExpressionItem::class,ExpressionWord::class,QuoteItem::class
) )
@@ -170,17 +172,24 @@ object WorkersDb {
} }
if (datetime.equals(UsageUpdateType.JC) == false) { if (datetime.equals(UsageUpdateType.JC) == false) {
val nowTs = System.currentTimeMillis()
val logMonth = calendar.get(Calendar.MONTH)
val logDay = calendar.get(Calendar.DAY_OF_MONTH)
val logDow = calendar.get(Calendar.DAY_OF_WEEK)
val logHour = calendar.get(Calendar.HOUR_OF_DAY)
copyToRealm(AppUsageLog().apply { copyToRealm(AppUsageLog().apply {
itemKey = key itemKey = key
itemType = type.name itemType = type.name
timestamp = System.currentTimeMillis() timestamp = nowTs
month = calendar.get(Calendar.MONTH) month = logMonth
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) dayOfMonth = logDay
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) dayOfWeek = logDow
hour = calendar.get(Calendar.HOUR_OF_DAY) hour = logHour
}) })
upsertUsageBucket(key, type.name, nowTs, logMonth, logDay, logDow, logHour)
} }
} }
invalidateRecommendationCache()
} }
fun logContactInteraction(phoneNumber: String, updateType: UsageUpdateType) { fun logContactInteraction(phoneNumber: String, updateType: UsageUpdateType) {
@@ -211,48 +220,154 @@ object WorkersDb {
} }
} }
key?.let { key -> key?.let { key ->
// 2. 추천 시스템이 사용하는 AppUsageLog에도 기록 추가 // 2. 추천 시스템이 사용하는 AppUsageLog 및 경량 집계 버킷에 기록 추가
val nowTs = System.currentTimeMillis()
val logMonth = calendar.get(Calendar.MONTH)
val logDay = calendar.get(Calendar.DAY_OF_MONTH)
val logDow = calendar.get(Calendar.DAY_OF_WEEK)
val logHour = calendar.get(Calendar.HOUR_OF_DAY)
copyToRealm(AppUsageLog().apply { copyToRealm(AppUsageLog().apply {
itemKey = key itemKey = key
itemType = UsageLogType.CONTACT.name itemType = UsageLogType.CONTACT.name
timestamp = System.currentTimeMillis() timestamp = nowTs
month = calendar.get(Calendar.MONTH) month = logMonth
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) dayOfMonth = logDay
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) dayOfWeek = logDow
hour = calendar.get(Calendar.HOUR_OF_DAY) hour = logHour
})
upsertUsageBucket(key, UsageLogType.CONTACT.name, nowTs, logMonth, logDay, logDow, logHour)
}
}
}
invalidateRecommendationCache()
}
private const val REC_CACHE_TTL_MS = 2 * 60 * 1000L
private const val ONE_DAY_MS = 24L * 60 * 60 * 1000L
@Volatile
private var cachedRecommendationsResult: Triple<Long, Int, List<ScoredItem>>? = null
@Volatile
private var lastLogCleanupTime = 0L
private fun invalidateRecommendationCache() {
cachedRecommendationsResult = null
}
private fun MutableRealm.upsertUsageBucket(
itemKey: String,
itemType: String,
timestamp: Long,
month: Int,
dayOfMonth: Int,
dayOfWeek: Int,
hour: Int
) {
if (itemKey.isEmpty() || dayOfWeek <= 0) return
val key = "$itemType|$itemKey|$dayOfWeek|$hour"
val dayBit = if (dayOfMonth in 1..31) (1 shl (dayOfMonth - 1)) else 0
val monthBit = if (month in 0..11) (1 shl month) else 0
val existing = query<AppUsageBucket>("bucketKey == $0", key).first().find()
if (existing != null) {
existing.count += 1
if (existing.firstTimestamp == 0L || timestamp < existing.firstTimestamp) {
existing.firstTimestamp = timestamp
}
if (timestamp > existing.lastTimestamp) {
existing.lastTimestamp = timestamp
}
existing.dayOfMonthMask = existing.dayOfMonthMask or dayBit
existing.monthMask = existing.monthMask or monthBit
} else {
copyToRealm(AppUsageBucket().apply {
bucketKey = key
this.itemKey = itemKey
this.itemType = itemType
this.dayOfWeek = dayOfWeek
this.hour = hour
this.count = 1
this.firstTimestamp = timestamp
this.lastTimestamp = timestamp
this.dayOfMonthMask = dayBit
this.monthMask = monthBit
}) })
} }
} }
private fun backfillUsageBucketsIfNeeded(realm: Realm) {
if (realm.query<AppUsageBucket>().count().find() > 0L) return
val rawLogs = realm.query<AppUsageLog>().find()
if (rawLogs.isEmpty()) return
realm.writeBlocking {
val cal = Calendar.getInstance()
for (log in rawLogs) {
var m = log.month
var d = log.dayOfMonth
var dow = log.dayOfWeek
var h = log.hour
// 과거 syncSystemUsageStats 누락분(dayOfWeek==0)은 timestamp로 복원
if (dow <= 0 && log.timestamp > 0L) {
cal.timeInMillis = log.timestamp
m = cal.get(Calendar.MONTH)
d = cal.get(Calendar.DAY_OF_MONTH)
dow = cal.get(Calendar.DAY_OF_WEEK)
h = cal.get(Calendar.HOUR_OF_DAY)
}
upsertUsageBucket(log.itemKey, log.itemType, log.timestamp, m, d, dow, h)
}
} }
} }
// [핵심] 현재 시간 상황에 맞는 추천 리스트 가져오기 private fun cleanupOldUsageLogsIfNeeded(realm: Realm) {
fun getContextualRecommendations(limit: Int = 18): List<ScoredItem> {
val realm = getRealm()
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val calendar = Calendar.getInstance().apply { timeInMillis = now } if (now - lastLogCleanupTime < ONE_DAY_MS) return
lastLogCleanupTime = now
val oneYearAgo = now - (365L * ONE_DAY_MS)
realm.writeBlocking {
val expiredLogs = query<AppUsageLog>("timestamp < $0", oneYearAgo).find()
if (expiredLogs.isNotEmpty()) {
delete(expiredLogs)
}
}
}
// [핵심] 경량 집계 버킷(AppUsageBucket) + 단기 메모리 캐시를 활용한 고속 컨텍스트 추천
fun getContextualRecommendations(limit: Int = 18): List<ScoredItem> {
val now = System.currentTimeMillis()
cachedRecommendationsResult?.let { (cachedAt, cachedLimit, items) ->
if (now - cachedAt < REC_CACHE_TTL_MS && cachedLimit >= limit) {
return items.take(limit)
}
}
val realm = getRealm()
backfillUsageBucketsIfNeeded(realm)
val calendar = Calendar.getInstance().apply { timeInMillis = now }
val curMonth = calendar.get(Calendar.MONTH) val curMonth = calendar.get(Calendar.MONTH)
val curDay = calendar.get(Calendar.DAY_OF_MONTH) val curDay = calendar.get(Calendar.DAY_OF_MONTH)
val curDow = calendar.get(Calendar.DAY_OF_WEEK) val curDow = calendar.get(Calendar.DAY_OF_WEEK)
val curHour = calendar.get(Calendar.HOUR_OF_DAY) val curHour = calendar.get(Calendar.HOUR_OF_DAY)
val isCurWeekend = curDow == Calendar.SATURDAY || curDow == Calendar.SUNDAY val isCurWeekend = curDow == Calendar.SATURDAY || curDow == Calendar.SUNDAY
val oneDayMs = 24L * 60 * 60 * 1000L val curDayBit = if (curDay in 1..31) (1 shl (curDay - 1)) else 0
val curMonthBit = if (curMonth in 0..11) (1 shl curMonth) else 0
// 최근 1년치 로그 조회 (연간/월간 반복 패턴 포함)
val cutoffTime = now - (365L * oneDayMs)
val logs = realm.query<AppUsageLog>("timestamp > $0", cutoffTime).find()
val buckets = realm.query<AppUsageBucket>().find()
val scores = HashMap<String, Double>() val scores = HashMap<String, Double>()
val keys = HashMap<String, String>() val keys = HashMap<String, String>()
val types = HashMap<String, String>() val types = HashMap<String, String>()
for (log in logs) { for (bucket in buckets) {
val daysAgo = ((now - log.timestamp).coerceAtLeast(0L)).toDouble() / oneDayMs val daysAgo = ((now - bucket.lastTimestamp).coerceAtLeast(0L)).toDouble() / ONE_DAY_MS
val spanDays = ((now - bucket.firstTimestamp).coerceAtLeast(0L)).toDouble() / ONE_DAY_MS
var score = 1.0 // 기본 빈도 점수 var score = 1.0 // 기본 빈도 점수
// 1. 시간대 매칭 (자정 23시<->0시 경계 포함 원형 거리) // 1. 시간대 매칭 (자정 23시<->0시 경계 포함 원형 거리)
val rawDiff = kotlin.math.abs(log.hour - curHour) val rawDiff = kotlin.math.abs(bucket.hour - curHour)
val hourDiff = minOf(rawDiff, 24 - rawDiff) val hourDiff = minOf(rawDiff, 24 - rawDiff)
score += when (hourDiff) { score += when (hourDiff) {
0 -> 5.0 0 -> 5.0
@@ -261,45 +376,50 @@ object WorkersDb {
else -> 0.0 else -> 0.0
} }
// 2. 요일 및 평일/주말 패턴 (오늘 당일 로그 중복 보너스 방지) // 2. 요일 및 평일/주말 패턴 (과거 이력이 있는 경우 요일 패턴 반영)
if (daysAgo >= 1.0) { if (daysAgo >= 1.0 || spanDays >= 7.0) {
if (log.dayOfWeek == curDow) { if (bucket.dayOfWeek == curDow) {
score += 3.0 score += 3.0
} else { } else {
val isLogWeekend = log.dayOfWeek == Calendar.SATURDAY || log.dayOfWeek == Calendar.SUNDAY val isBucketWeekend = bucket.dayOfWeek == Calendar.SATURDAY || bucket.dayOfWeek == Calendar.SUNDAY
if (isLogWeekend == isCurWeekend) score += 1.0 if (isBucketWeekend == isCurWeekend) score += 1.0
} }
} }
// 3. 매월 N일 반복 패턴 (최소 25일 이상 지난 과거 기록에만 적용) // 3. 매월 N일 반복 패턴 (최소 25일 이상 이력이 쌓인 버킷에만 적용)
if (daysAgo >= 25.0 && log.dayOfMonth == curDay) { if (spanDays >= 25.0 && (bucket.dayOfMonthMask and curDayBit) != 0) {
score += 6.0 score += 6.0
} }
// 4. 매년 같은 월/일 반복 패턴 (최소 330일 이상 지난 기록에만 적용) // 4. 매년 같은 월/일 반복 패턴 (최소 330일 이상 이력이 쌓인 버킷에만 적용)
if (daysAgo >= 330.0 && log.month == curMonth && log.dayOfMonth == curDay) { if (spanDays >= 330.0 &&
(bucket.monthMask and curMonthBit) != 0 &&
(bucket.dayOfMonthMask and curDayBit) != 0
) {
score += 10.0 score += 10.0
} }
// 5. 완만한 최신성 감쇠 (7일 경과 시 0.5배, 오늘 기록의 독점 방지) // 5. 누적 빈도 가중치(로그 스케일) + 완만한 7일 반감기 감쇠
val freqWeight = 1.0 + kotlin.math.ln(bucket.count.coerceAtLeast(1).toDouble())
val decay = 1.0 / (daysAgo / 7.0 + 1.0) val decay = 1.0 / (daysAgo / 7.0 + 1.0)
val mapKey = "${log.itemType}:${log.itemKey}" val mapKey = "${bucket.itemType}:${bucket.itemKey}"
scores[mapKey] = (scores[mapKey] ?: 0.0) + (score * decay) scores[mapKey] = (scores[mapKey] ?: 0.0) + (score * freqWeight * decay)
keys[mapKey] = log.itemKey keys[mapKey] = bucket.itemKey
types[mapKey] = log.itemType types[mapKey] = bucket.itemType
} }
// 점수 높은 순으로 정렬하여 상위 N개 반환 val result = scores.entries
return scores.entries
.sortedByDescending { it.value } .sortedByDescending { it.value }
.take(limit) .take(limit)
.map { .map {
ScoredItem(keys[it.key] ?: it.key, types[it.key] ?: "APP", it.value) ScoredItem(keys[it.key] ?: it.key, types[it.key] ?: "APP", it.value)
} }
cachedRecommendationsResult = Triple(now, limit, result)
return result
} }
var schemaVersion : Long = 202611160000L var schemaVersion : Long = 202611160001L
private var pRealm : Realm? = null private var pRealm : Realm? = null
fun getRealm() : Realm { fun getRealm() : Realm {
@@ -459,20 +579,27 @@ object WorkersDb {
if (existingLog.isEmpty()) { if (existingLog.isEmpty()) {
val logCal = Calendar.getInstance().apply { timeInMillis = lastUsedTime } val logCal = Calendar.getInstance().apply { timeInMillis = lastUsedTime }
val logMonth = logCal.get(Calendar.MONTH)
val logDay = logCal.get(Calendar.DAY_OF_MONTH)
val logDow = logCal.get(Calendar.DAY_OF_WEEK)
val logHour = logCal.get(Calendar.HOUR_OF_DAY)
copyToRealm(AppUsageLog().apply { copyToRealm(AppUsageLog().apply {
itemKey = pkgName itemKey = pkgName
itemType = "APP" itemType = "APP"
timestamp = lastUsedTime timestamp = lastUsedTime
month = logCal.get(Calendar.MONTH) month = logMonth
dayOfMonth = logCal.get(Calendar.DAY_OF_MONTH) dayOfMonth = logDay
dayOfWeek = logCal.get(Calendar.DAY_OF_WEEK) dayOfWeek = logDow
hour = logCal.get(Calendar.HOUR_OF_DAY) hour = logHour
}) })
upsertUsageBucket(pkgName, "APP", lastUsedTime, logMonth, logDay, logDow, logHour)
} }
} }
} }
} }
} }
invalidateRecommendationCache()
cleanupOldUsageLogsIfNeeded(realm)
} }