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

@ -27,6 +27,7 @@ import kotlinx.coroutines.withContext
import org.meshtastic.core.database.DatabaseManager
import org.meshtastic.core.database.entity.MeshLog
import org.meshtastic.core.di.CoroutineDispatchers
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
import org.meshtastic.proto.MeshProtos
import org.meshtastic.proto.MeshProtos.MeshPacket
import org.meshtastic.proto.Portnums
@ -39,10 +40,16 @@ class MeshLogRepository
constructor(
private val dbManager: DatabaseManager,
private val dispatchers: CoroutineDispatchers,
private val meshLogPrefs: MeshLogPrefs,
) {
fun getAllLogs(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> =
dbManager.currentDb.flatMapLatest { it.meshLogDao().getAllLogs(maxItems) }.flowOn(dispatchers.io).conflate()
fun getAllLogsUnbounded(): Flow<List<MeshLog>> = dbManager.currentDb
.flatMapLatest { it.meshLogDao().getAllLogs(Int.MAX_VALUE) }
.flowOn(dispatchers.io)
.conflate()
fun getAllLogsInReceiveOrder(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> = dbManager.currentDb
.flatMapLatest { it.meshLogDao().getAllLogsInReceiveOrder(maxItems) }
.flowOn(dispatchers.io)
@ -134,8 +141,10 @@ constructor(
.mapLatest { list -> list.firstOrNull { it.myNodeInfo != null }?.myNodeInfo }
.flowOn(dispatchers.io)
suspend fun insert(log: MeshLog) =
withContext(dispatchers.io) { dbManager.currentDb.value.meshLogDao().insert(log) }
suspend fun insert(log: MeshLog) = withContext(dispatchers.io) {
if (!meshLogPrefs.loggingEnabled) return@withContext
dbManager.currentDb.value.meshLogDao().insert(log)
}
suspend fun deleteAll() = withContext(dispatchers.io) { dbManager.currentDb.value.meshLogDao().deleteAll() }
@ -145,6 +154,19 @@ constructor(
suspend fun deleteLogs(nodeNum: Int, portNum: Int) =
withContext(dispatchers.io) { dbManager.currentDb.value.meshLogDao().deleteLogs(nodeNum, portNum) }
@Suppress("MagicNumber")
suspend fun deleteLogsOlderThan(retentionDays: Int) = withContext(dispatchers.io) {
if (retentionDays == MeshLogPrefs.NEVER_CLEAR_RETENTION_DAYS) return@withContext
val cutoffTimestamp =
if (retentionDays == MeshLogPrefs.ONE_HOUR_RETENTION_DAYS) {
System.currentTimeMillis() - (60 * 60 * 1000L)
} else {
System.currentTimeMillis() - (retentionDays * 24 * 60 * 60 * 1000L)
}
dbManager.currentDb.value.meshLogDao().deleteOlderThan(cutoffTimestamp)
}
companion object {
private const val MAX_ITEMS = 500
private const val MAX_MESH_PACKETS = 10000

View file

@ -56,4 +56,7 @@ interface MeshLogDao {
@Query("DELETE FROM log WHERE from_num = :fromNum AND port_num = :portNum")
suspend fun deleteLogs(fromNum: Int, portNum: Int)
@Query("DELETE FROM log WHERE received_date < :cutoffTimestamp")
suspend fun deleteOlderThan(cutoffTimestamp: Long)
}

View file

@ -37,6 +37,8 @@ import org.meshtastic.core.prefs.map.MapTileProviderPrefs
import org.meshtastic.core.prefs.map.MapTileProviderPrefsImpl
import org.meshtastic.core.prefs.mesh.MeshPrefs
import org.meshtastic.core.prefs.mesh.MeshPrefsImpl
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
import org.meshtastic.core.prefs.meshlog.MeshLogPrefsImpl
import org.meshtastic.core.prefs.radio.RadioPrefs
import org.meshtastic.core.prefs.radio.RadioPrefsImpl
import org.meshtastic.core.prefs.ui.UiPrefs
@ -83,6 +85,10 @@ internal annotation class RadioSharedPreferences
@Retention(AnnotationRetention.BINARY)
internal annotation class UiSharedPreferences
@Qualifier
@Retention(AnnotationRetention.BINARY)
internal annotation class MeshLogSharedPreferences
@Suppress("TooManyFunctions")
@InstallIn(SingletonComponent::class)
@Module
@ -100,6 +106,8 @@ interface PrefsModule {
@Binds fun bindMeshPrefs(meshPrefsImpl: MeshPrefsImpl): MeshPrefs
@Binds fun bindMeshLogPrefs(meshLogPrefsImpl: MeshLogPrefsImpl): MeshLogPrefs
@Binds fun bindRadioPrefs(radioPrefsImpl: RadioPrefsImpl): RadioPrefs
@Binds fun bindUiPrefs(uiPrefsImpl: UiPrefsImpl): UiPrefs
@ -159,5 +167,11 @@ interface PrefsModule {
@UiSharedPreferences
fun provideUiSharedPreferences(@ApplicationContext context: Context): SharedPreferences =
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
@Provides
@Singleton
@MeshLogSharedPreferences
fun provideMeshLogSharedPreferences(@ApplicationContext context: Context): SharedPreferences =
context.getSharedPreferences("meshlog-prefs", Context.MODE_PRIVATE)
}
}

View file

@ -0,0 +1,57 @@
/*
* 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 org.meshtastic.core.prefs.meshlog
import android.content.SharedPreferences
import org.meshtastic.core.prefs.PrefDelegate
import org.meshtastic.core.prefs.di.MeshLogSharedPreferences
import javax.inject.Inject
import javax.inject.Singleton
interface MeshLogPrefs {
var retentionDays: Int
var loggingEnabled: Boolean
companion object {
const val RETENTION_DAYS_KEY = "meshlog_retention_days"
const val LOGGING_ENABLED_KEY = "meshlog_logging_enabled"
const val DEFAULT_RETENTION_DAYS = 7
const val DEFAULT_LOGGING_ENABLED = true
const val MIN_RETENTION_DAYS = -1 // -1 == keep last hour
const val MAX_RETENTION_DAYS = 365
const val NEVER_CLEAR_RETENTION_DAYS = 0
const val ONE_HOUR_RETENTION_DAYS = -1
}
}
@Singleton
class MeshLogPrefsImpl @Inject constructor(@MeshLogSharedPreferences private val prefs: SharedPreferences) :
MeshLogPrefs {
override var retentionDays: Int by
PrefDelegate(
prefs = prefs,
key = MeshLogPrefs.RETENTION_DAYS_KEY,
defaultValue = MeshLogPrefs.DEFAULT_RETENTION_DAYS,
)
override var loggingEnabled: Boolean by
PrefDelegate(
prefs = prefs,
key = MeshLogPrefs.LOGGING_ENABLED_KEY,
defaultValue = MeshLogPrefs.DEFAULT_LOGGING_ENABLED,
)
}

View file

@ -16,7 +16,7 @@
-->
<resources>
// Language tags native names (not available via .getDisplayLanguage)
<!-- Language tags native names (not available via .getDisplayLanguage) -->
<string name="fr_HT" translatable="false">Kreyòl ayisyen</string>
<string name="pt_BR" translatable="false">Português do Brasil</string>
<string name="zh_CN" translatable="false">简体中文</string>
@ -226,6 +226,17 @@
<string name="debug_export_cancelled">Export canceled</string>
<string name="debug_export_success">%1$d logs exported</string>
<string name="debug_export_failed">Failed to write log file: %1$s</string>
<string name="debug_export_no_logs">No logs to export</string>
<plurals name="log_retention_hours">
<item quantity="one">%1$d hour</item>
<item quantity="other">%1$d hours</item>
</plurals>
<plurals name="log_retention_days_quantity">
<item quantity="one">%1$d day</item>
<item quantity="other">%1$d days</item>
</plurals>
<string name="debug_filters">Filters</string>
<string name="debug_active_filters">Active filters</string>
<string name="debug_default_search">Search in logs…</string>
@ -515,6 +526,9 @@
<string name="messages">Messages</string>
<string name="device_db_cache_limit">Device DB cache limit</string>
<string name="device_db_cache_limit_summary">Max device databases to keep on this phone</string>
<string name="log_retention_days">MeshLog retention period</string>
<string name="log_retention_days_summary">Choose how long to keep logs. Select Never to keep all logs.</string>
<string name="log_retention_never">Never delete logs</string>
<string name="detection_sensor_config">Detection Sensor Config</string>
<string name="detection_sensor_enabled">Detection Sensor enabled</string>
<string name="minimum_broadcast_seconds">Minimum broadcast (seconds)</string>