feat: add retention period to meshLog. Defaults to 7 days, with a settings dropdown to change (#4078)

This commit is contained in:
Mac DeCourcy 2026-01-02 10:14:16 -08:00 committed by GitHub
parent dc9e51f18f
commit 6f338c4cde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 396 additions and 8 deletions

View file

@ -18,7 +18,15 @@
package com.geeksville.mesh
import android.app.Application
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import co.touchlab.kermit.Logger
import com.geeksville.mesh.worker.MeshLogCleanupWorker
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
@ -29,6 +37,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.meshtastic.core.database.DatabaseManager
import org.meshtastic.core.prefs.mesh.MeshPrefs
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
import java.util.concurrent.TimeUnit
import javax.inject.Inject
/**
* The main application class for Meshtastic.
@ -38,16 +49,65 @@ import org.meshtastic.core.prefs.mesh.MeshPrefs
* user preferences.
*/
@HiltAndroidApp
class MeshUtilApplication : Application() {
class MeshUtilApplication :
Application(),
Configuration.Provider {
@Inject lateinit var workerFactory: HiltWorkerFactory
override fun onCreate() {
super.onCreate()
initializeMaps(this)
// Schedule periodic MeshLog cleanup
scheduleMeshLogCleanup()
enqueueImmediateCleanupIfNeeded()
// Initialize DatabaseManager asynchronously with current device address so DAO consumers have an active DB
val entryPoint = EntryPointAccessors.fromApplication(this, AppEntryPoint::class.java)
CoroutineScope(Dispatchers.Default).launch {
entryPoint.databaseManager().init(entryPoint.meshPrefs().deviceAddress)
}
}
private fun scheduleMeshLogCleanup() {
val cleanupRequest =
PeriodicWorkRequestBuilder<MeshLogCleanupWorker>(
repeatInterval = 1,
repeatIntervalTimeUnit = TimeUnit.HOURS,
)
.build()
WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(
MeshLogCleanupWorker.WORK_NAME,
ExistingPeriodicWorkPolicy.UPDATE,
cleanupRequest,
)
}
private fun enqueueImmediateCleanupIfNeeded() {
// Use entry point to access prefs outside of Hilt graph
val entryPoint = EntryPointAccessors.fromApplication(this, AppEntryPoint::class.java)
val meshLogPrefs = entryPoint.meshLogPrefs()
val retentionDays = meshLogPrefs.retentionDays
if (!meshLogPrefs.loggingEnabled || retentionDays == MeshLogPrefs.NEVER_CLEAR_RETENTION_DAYS) {
Logger.i {
"Skipping immediate MeshLog cleanup; " +
"loggingEnabled=${meshLogPrefs.loggingEnabled}, retention=$retentionDays"
}
return
}
Logger.i { "Enqueuing immediate MeshLog cleanup with retentionDays=$retentionDays" }
WorkManager.getInstance(this)
.enqueueUniqueWork(
"${MeshLogCleanupWorker.WORK_NAME}_immediate",
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequestBuilder<MeshLogCleanupWorker>().build(),
)
}
override val workManagerConfiguration: Configuration
get() = Configuration.Builder().setWorkerFactory(workerFactory).build()
}
@EntryPoint
@ -56,6 +116,8 @@ interface AppEntryPoint {
fun databaseManager(): DatabaseManager
fun meshPrefs(): MeshPrefs
fun meshLogPrefs(): MeshLogPrefs
}
fun logAssert(executeReliableWrite: Boolean) {

View file

@ -0,0 +1,95 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.geeksville.mesh.worker
import android.content.Context
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import co.touchlab.kermit.Logger
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import org.meshtastic.core.data.repository.MeshLogRepository
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
@HiltWorker
class MeshLogCleanupWorker
@AssistedInject
constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
private val meshLogRepository: MeshLogRepository,
private val meshLogPrefs: MeshLogPrefs,
) : CoroutineWorker(appContext, workerParams) {
// Fallback constructor for cases where HiltWorkerFactory is not used (e.g., some WorkManager initializations)
constructor(
appContext: Context,
workerParams: WorkerParameters,
) : this(
appContext,
workerParams,
entryPoint(appContext).meshLogRepository(),
entryPoint(appContext).meshLogPrefs(),
)
@Suppress("TooGenericExceptionCaught")
override suspend fun doWork(): Result = try {
val retentionDays = meshLogPrefs.retentionDays
if (!meshLogPrefs.loggingEnabled) {
logger.i { "Skipping cleanup because mesh log storage is disabled" }
} else if (retentionDays == MeshLogPrefs.NEVER_CLEAR_RETENTION_DAYS) {
logger.i { "Skipping cleanup because retention is set to never delete" }
} else {
val retentionLabel =
if (retentionDays == MeshLogPrefs.ONE_HOUR_RETENTION_DAYS) {
"1 hour"
} else {
"$retentionDays days"
}
logger.d { "Cleaning logs older than $retentionLabel" }
meshLogRepository.deleteLogsOlderThan(retentionDays)
logger.i { "Successfully cleaned old MeshLog entries" }
}
Result.success()
} catch (e: Exception) {
logger.e(e) { "Failed to clean MeshLog entries" }
Result.failure()
}
companion object {
const val WORK_NAME = "meshlog_cleanup_worker"
private fun entryPoint(context: Context): WorkerEntryPoint =
EntryPointAccessors.fromApplication(context.applicationContext, WorkerEntryPoint::class.java)
}
private val logger = Logger.withTag(WORK_NAME)
}
@EntryPoint
@InstallIn(SingletonComponent::class)
interface WorkerEntryPoint {
fun meshLogRepository(): MeshLogRepository
fun meshLogPrefs(): MeshLogPrefs
}