feat: implement remote database epoch sync to purge stale local data and add status tracking for synced history logs
This commit is contained in:
@@ -13,7 +13,7 @@ import com.example.data.model.UserAccess
|
|||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
entities = [MaintenanceActivity::class, OSUpdateHistory::class, SyncConflictLog::class, UserAccess::class, AppLog::class, OSFinding::class],
|
entities = [MaintenanceActivity::class, OSUpdateHistory::class, SyncConflictLog::class, UserAccess::class, AppLog::class, OSFinding::class],
|
||||||
version = 8,
|
version = 9,
|
||||||
exportSchema = false
|
exportSchema = false
|
||||||
)
|
)
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
|||||||
@@ -113,5 +113,27 @@ interface MaintenanceDao {
|
|||||||
|
|
||||||
@Query("DELETE FROM os_findings")
|
@Query("DELETE FROM os_findings")
|
||||||
suspend fun clearAllFindings()
|
suspend fun clearAllFindings()
|
||||||
|
|
||||||
|
// Additional Synced and Epoch Purge Helpers
|
||||||
|
@Query("SELECT * FROM os_update_history WHERE synced = 0")
|
||||||
|
suspend fun getUnsyncedHistory(): List<OSUpdateHistory>
|
||||||
|
|
||||||
|
@Query("UPDATE os_update_history SET synced = 1 WHERE id IN (:ids)")
|
||||||
|
suspend fun markHistorySynced(ids: List<Long>)
|
||||||
|
|
||||||
|
@Query("UPDATE os_update_history SET synced = 1 WHERE numeroOs = :numeroOs AND timestamp = :timestamp")
|
||||||
|
suspend fun markHistorySyncedByOsAndTimestamp(numeroOs: String, timestamp: Long)
|
||||||
|
|
||||||
|
@Query("DELETE FROM os_update_history WHERE synced = 1 AND numeroOs NOT IN (:activeOsNumbers)")
|
||||||
|
suspend fun deleteStaleSyncedHistory(activeOsNumbers: List<String>)
|
||||||
|
|
||||||
|
@Query("DELETE FROM os_update_history WHERE synced = 1")
|
||||||
|
suspend fun deleteAllSyncedHistory()
|
||||||
|
|
||||||
|
@Query("DELETE FROM os_findings WHERE synced = 1 AND numeroOs NOT IN (:activeOsNumbers)")
|
||||||
|
suspend fun deleteStaleSyncedFindings(activeOsNumbers: List<String>)
|
||||||
|
|
||||||
|
@Query("DELETE FROM os_findings WHERE synced = 1")
|
||||||
|
suspend fun deleteAllSyncedFindings()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,13 @@ enum class ActivityStatus(val displayName: String) {
|
|||||||
CONCLUIDO("Concluído")
|
CONCLUIDO("Concluído")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(tableName = "os_update_history")
|
@Entity(
|
||||||
|
tableName = "os_update_history",
|
||||||
|
indices = [
|
||||||
|
Index("numeroOs"),
|
||||||
|
Index("synced")
|
||||||
|
]
|
||||||
|
)
|
||||||
data class OSUpdateHistory(
|
data class OSUpdateHistory(
|
||||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||||
val numeroOs: String,
|
val numeroOs: String,
|
||||||
@@ -92,7 +98,8 @@ data class OSUpdateHistory(
|
|||||||
val observacao: String? = null,
|
val observacao: String? = null,
|
||||||
val fotoPath: String? = null,
|
val fotoPath: String? = null,
|
||||||
val recurso: String,
|
val recurso: String,
|
||||||
val emailEditor: String? = null
|
val emailEditor: String? = null,
|
||||||
|
val synced: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
data class GoogleUser(
|
data class GoogleUser(
|
||||||
|
|||||||
@@ -758,13 +758,44 @@ class GoogleSheetsSyncService(
|
|||||||
observacao = obs,
|
observacao = obs,
|
||||||
fotoPath = foto,
|
fotoPath = foto,
|
||||||
recurso = resp,
|
recurso = resp,
|
||||||
emailEditor = email.takeIf { it.isNotBlank() }
|
emailEditor = email.takeIf { it.isNotBlank() },
|
||||||
|
synced = true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return historyList
|
return historyList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads database_epoch from 'config' tab (cell B2 or key 'database_epoch').
|
||||||
|
* Defaults to 1 if tab or value is missing/invalid.
|
||||||
|
*/
|
||||||
|
fun fetchDatabaseEpoch(): Int {
|
||||||
|
return try {
|
||||||
|
val rows = fetchValues("config!A:B")
|
||||||
|
if (rows.isEmpty()) return 1
|
||||||
|
for (row in rows) {
|
||||||
|
if (row.isNotEmpty() && row[0].trim().lowercase() == "database_epoch") {
|
||||||
|
if (row.size > 1) {
|
||||||
|
val epochVal = row[1].trim().toIntOrNull()
|
||||||
|
if (epochVal != null && epochVal > 0) {
|
||||||
|
return epochVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fallback: check row 2 col 2 (B2)
|
||||||
|
if (rows.size >= 2 && rows[1].size >= 2) {
|
||||||
|
rows[1][1].trim().toIntOrNull() ?: 1
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d(tag, "Tab 'config' não encontrada ou sem chave 'database_epoch'. Usando epoch padrão = 1.")
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stubs out direct activities editing since 'project' is now read-only.
|
* Stubs out direct activities editing since 'project' is now read-only.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -468,6 +468,25 @@ class MaintenanceRepository(
|
|||||||
android.util.Log.e("MaintenanceRepository", "Falha ao carregar lista de acessos da planilha", e)
|
android.util.Log.e("MaintenanceRepository", "Falha ao carregar lista de acessos da planilha", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 0.1 Check Remote Database Epoch for reset/cleanup signals
|
||||||
|
val remoteEpoch = try { sheetsSyncService.fetchDatabaseEpoch() } catch (e: Exception) { 1 }
|
||||||
|
val prefs = context.getSharedPreferences("google_auth_prefs", Context.MODE_PRIVATE)
|
||||||
|
val localEpoch = prefs.getInt("database_epoch", 0)
|
||||||
|
|
||||||
|
if (remoteEpoch > localEpoch) {
|
||||||
|
android.util.Log.d("MaintenanceRepository", "Database epoch mudou de $localEpoch para $remoteEpoch. Executando purga seletiva de histórico e achados sincronizados...")
|
||||||
|
val (epochActivities, _) = sheetsSyncService.fetchRemoteActivities()
|
||||||
|
val activeOsList = epochActivities.map { it.numeroOs }.distinct()
|
||||||
|
if (activeOsList.isNotEmpty()) {
|
||||||
|
dao.deleteStaleSyncedHistory(activeOsList)
|
||||||
|
dao.deleteStaleSyncedFindings(activeOsList)
|
||||||
|
} else {
|
||||||
|
dao.deleteAllSyncedHistory()
|
||||||
|
dao.deleteAllSyncedFindings()
|
||||||
|
}
|
||||||
|
prefs.edit().putInt("database_epoch", remoteEpoch).apply()
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Fetch remote OS definitions from the "project" sheet
|
// 1. Fetch remote OS definitions from the "project" sheet
|
||||||
val (remoteActivities, _) = sheetsSyncService.fetchRemoteActivities()
|
val (remoteActivities, _) = sheetsSyncService.fetchRemoteActivities()
|
||||||
|
|
||||||
@@ -511,9 +530,9 @@ class MaintenanceRepository(
|
|||||||
val missingLocalHistory = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
val missingLocalHistory = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
||||||
if (missingLocalHistory.isNotEmpty()) {
|
if (missingLocalHistory.isNotEmpty()) {
|
||||||
for (hist in missingLocalHistory) {
|
for (hist in missingLocalHistory) {
|
||||||
dao.insertHistory(hist)
|
dao.insertHistory(hist.copy(synced = true))
|
||||||
}
|
}
|
||||||
android.util.Log.d("MaintenanceRepository", "Inserted ${missingLocalHistory.size} missing history logs locally.")
|
android.util.Log.d("MaintenanceRepository", "Inserted ${missingLocalHistory.size} missing history logs locally as synced.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. For each remote activity, determine its current state based on findings (achados) and history log
|
// 6. For each remote activity, determine its current state based on findings (achados) and history log
|
||||||
@@ -574,7 +593,7 @@ class MaintenanceRepository(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 8. Identify and resolve sync conflicts (LWW)
|
// 8. Identify and resolve sync conflicts (LWW)
|
||||||
val unuploadedLocalLogs = localHistory.filter { "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
val unuploadedLocalLogs = localHistory.filter { !it.synced && "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
||||||
val ossWithUnuploadedLogs = unuploadedLocalLogs.map { it.numeroOs }.toSet()
|
val ossWithUnuploadedLogs = unuploadedLocalLogs.map { it.numeroOs }.toSet()
|
||||||
|
|
||||||
val newRemoteLogs = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
val newRemoteLogs = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
||||||
@@ -621,11 +640,18 @@ class MaintenanceRepository(
|
|||||||
android.util.Log.d("MaintenanceRepository", "Inserted ${conflictLogsToInsert.size} resolved conflict logs.")
|
android.util.Log.d("MaintenanceRepository", "Inserted ${conflictLogsToInsert.size} resolved conflict logs.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. Upload any local history logs that are not on Google Sheets yet
|
// 9. Upload any local history logs that are not on Google Sheets yet (only unsynced)
|
||||||
val historyToUpload = localHistory.filter { "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
val historyToUpload = localHistory.filter { !it.synced && "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
||||||
if (historyToUpload.isNotEmpty()) {
|
if (historyToUpload.isNotEmpty()) {
|
||||||
sheetsSyncService.pushHistoryToSheet(historyToUpload)
|
sheetsSyncService.pushHistoryToSheet(historyToUpload)
|
||||||
android.util.Log.d("MaintenanceRepository", "Uploaded ${historyToUpload.size} new local history logs to Google Sheet.")
|
val uploadedIds = historyToUpload.map { it.id }.filter { it != 0L }
|
||||||
|
if (uploadedIds.isNotEmpty()) {
|
||||||
|
dao.markHistorySynced(uploadedIds)
|
||||||
|
}
|
||||||
|
for (h in historyToUpload) {
|
||||||
|
dao.markHistorySyncedByOsAndTimestamp(h.numeroOs, h.timestamp)
|
||||||
|
}
|
||||||
|
android.util.Log.d("MaintenanceRepository", "Uploaded ${historyToUpload.size} new local history logs to Google Sheet and marked as synced.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 10. Sync 'achados' sheet with local OSFinding database safely preserving local/uploaded photo paths
|
// 10. Sync 'achados' sheet with local OSFinding database safely preserving local/uploaded photo paths
|
||||||
|
|||||||
@@ -578,4 +578,99 @@ class ExampleRobolectricTest {
|
|||||||
kotlinx.coroutines.Dispatchers.resetMain()
|
kotlinx.coroutines.Dispatchers.resetMain()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
|
||||||
|
@Test
|
||||||
|
fun `test database epoch reconciliation and synced flag purge rules`() {
|
||||||
|
val testDispatcher = kotlinx.coroutines.test.UnconfinedTestDispatcher()
|
||||||
|
kotlinx.coroutines.Dispatchers.setMain(testDispatcher)
|
||||||
|
try {
|
||||||
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||||
|
val db = androidx.room.Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||||
|
.allowMainThreadQueries()
|
||||||
|
.setQueryExecutor { it.run() }
|
||||||
|
.setTransactionExecutor { it.run() }
|
||||||
|
.build()
|
||||||
|
val dao = db.maintenanceDao()
|
||||||
|
|
||||||
|
kotlinx.coroutines.runBlocking {
|
||||||
|
dao.clearAllActivities()
|
||||||
|
dao.clearAllHistory()
|
||||||
|
dao.clearAllFindings()
|
||||||
|
|
||||||
|
// 1. Create active activity OS-100 and OS-200
|
||||||
|
val act1 = com.example.data.model.MaintenanceActivity(
|
||||||
|
numeroOs = "OS-100", descricao = "Tarefa 100", setor = "Mecânica", equipe = "Mecânica",
|
||||||
|
recurso = "Eng 1", dataInicio = "01/08/2026", dataFim = "02/08/2026", percentualExecucao = 50, synced = true
|
||||||
|
)
|
||||||
|
dao.insertActivity(act1)
|
||||||
|
|
||||||
|
// 2. Insert synced history for OS-999 (which is stale/removed remotely)
|
||||||
|
val staleSyncedHistory = com.example.data.model.OSUpdateHistory(
|
||||||
|
numeroOs = "OS-999", timestamp = 1000L, percentualAnterior = 0, percentualNovo = 50,
|
||||||
|
observacao = "Histórico antigo sincronizado", recurso = "Eng 1", synced = true
|
||||||
|
)
|
||||||
|
dao.insertHistory(staleSyncedHistory)
|
||||||
|
|
||||||
|
// 3. Insert unsynced history for OS-100 (pending offline change created by user)
|
||||||
|
val offlinePendingHistory = com.example.data.model.OSUpdateHistory(
|
||||||
|
numeroOs = "OS-100", timestamp = 2000L, percentualAnterior = 50, percentualNovo = 100,
|
||||||
|
observacao = "Histórico novo offline", recurso = "Eng 1", synced = false
|
||||||
|
)
|
||||||
|
dao.insertHistory(offlinePendingHistory)
|
||||||
|
|
||||||
|
// 4. Insert synced finding for OS-999 (stale) and unsynced finding for OS-100 (pending)
|
||||||
|
val staleSyncedFinding = com.example.data.model.OSFinding(
|
||||||
|
id = "finding-stale", numeroOs = "OS-999", tipo = "OBS", descricao = "Achado antigo",
|
||||||
|
timestamp = 1000L, responsavel = "Eng 1", synced = true
|
||||||
|
)
|
||||||
|
val offlinePendingFinding = com.example.data.model.OSFinding(
|
||||||
|
id = "finding-offline", numeroOs = "OS-100", tipo = "OBS", descricao = "Achado novo offline",
|
||||||
|
timestamp = 2000L, responsavel = "Eng 1", synced = false
|
||||||
|
)
|
||||||
|
dao.insertFinding(staleSyncedFinding)
|
||||||
|
dao.insertFinding(offlinePendingFinding)
|
||||||
|
|
||||||
|
// Verify initial state before epoch purge
|
||||||
|
assertEquals(2, dao.getAllHistoryList().size)
|
||||||
|
assertEquals(2, dao.getAllFindingsList().size)
|
||||||
|
assertEquals(1, dao.getUnsyncedHistory().size)
|
||||||
|
|
||||||
|
// 5. Execute Epoch purge for active OS list ["OS-100"]
|
||||||
|
dao.deleteStaleSyncedHistory(listOf("OS-100"))
|
||||||
|
dao.deleteStaleSyncedFindings(listOf("OS-100"))
|
||||||
|
|
||||||
|
// 6. Verify that stale synced OS-999 entries were purged, while unsynced OS-100 entries were preserved
|
||||||
|
val historyAfterPurge = dao.getAllHistoryList()
|
||||||
|
assertEquals(1, historyAfterPurge.size)
|
||||||
|
assertEquals("OS-100", historyAfterPurge.first().numeroOs)
|
||||||
|
assertEquals(false, historyAfterPurge.first().synced)
|
||||||
|
|
||||||
|
val findingsAfterPurge = dao.getAllFindingsList()
|
||||||
|
assertEquals(1, findingsAfterPurge.size)
|
||||||
|
assertEquals("OS-100", findingsAfterPurge.first().numeroOs)
|
||||||
|
assertEquals(false, findingsAfterPurge.first().synced)
|
||||||
|
|
||||||
|
// 7. Test marking history as synced
|
||||||
|
val unsyncedHist = historyAfterPurge.first()
|
||||||
|
dao.markHistorySyncedByOsAndTimestamp(unsyncedHist.numeroOs, unsyncedHist.timestamp)
|
||||||
|
|
||||||
|
val historyFinal = dao.getAllHistoryList()
|
||||||
|
assertEquals(true, historyFinal.first().synced)
|
||||||
|
assertEquals(0, dao.getUnsyncedHistory().size)
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
|
} finally {
|
||||||
|
kotlinx.coroutines.Dispatchers.resetMain()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test fetchDatabaseEpoch graceful fallback`() {
|
||||||
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||||
|
val service = GoogleSheetsSyncService(context)
|
||||||
|
val epoch = service.fetchDatabaseEpoch()
|
||||||
|
assertTrue("Epoch default should be >= 1, got $epoch", epoch >= 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user