Skip to content

Commit

Permalink
Generate more detailed area sync reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent ff7786f commit 5900f57
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 26 additions & 9 deletions app/src/main/kotlin/area/AreasRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.content.Context
import api.Api
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.time.Duration
import java.time.ZoneOffset
import java.time.ZonedDateTime

class AreasRepo(
private val api: Api,
Expand Down Expand Up @@ -35,28 +38,42 @@ class AreasRepo(
}

suspend fun sync(): SyncReport {
val startMillis = System.currentTimeMillis()
var count = 0L
val startedAt = ZonedDateTime.now(ZoneOffset.UTC)
var newAreas = 0L
var updatedAreas = 0L
val maxUpdatedAtBeforeSync = queries.selectMaxUpdatedAt()

while (true) {
val areas = api.getAreas(queries.selectMaxUpdatedAt(), BATCH_SIZE)
count += areas.size
queries.insertOrReplace(areas.map { it.toArea() })
val areas = api.getAreas(queries.selectMaxUpdatedAt(), BATCH_SIZE).map { it.toArea() }

areas.forEach {
if (maxUpdatedAtBeforeSync == null
|| it.createdAt.isAfter(maxUpdatedAtBeforeSync)
) {
newAreas += 1
} else {
updatedAreas += 1
}
}

queries.insertOrReplace(areas)

if (areas.size < BATCH_SIZE) {
break
}
}

return SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
createdOrUpdatedAreas = count,
duration = Duration.between(startedAt, ZonedDateTime.now(ZoneOffset.UTC)),
newAreas = newAreas,
updatedAreas = updatedAreas,
)
}

data class SyncReport(
val timeMillis: Long,
val createdOrUpdatedAreas: Long,
val duration: Duration,
val newAreas: Long,
val updatedAreas: Long,
)

companion object {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/sync/SyncNotificationController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SyncNotificationController(
|Elements: ${report.elementsReport.newElements} new, ${report.elementsReport.updatedElements} updated in ${report.elementsReport.duration.toMillis()} ms
|Events: ${report.eventsReport.newEvents.size} new, ${report.eventsReport.updatedEvents} updated in ${report.eventsReport.duration.toMillis()} ms
|Reports: ${report.reportsReport.newReports} new, ${report.reportsReport.updatedReports} updated in ${report.reportsReport.duration.toMillis()} ms
|Areas: ${report.areasReport.createdOrUpdatedAreas} in ${report.areasReport.timeMillis}
|Areas: ${report.areasReport.newAreas} new, ${report.areasReport.updatedAreas} updated in ${report.areasReport.duration.toMillis()} ms
|Users: ${report.usersReport.createdOrUpdatedUsers} in ${report.usersReport.timeMillis}
""".trimMargin()
)
Expand Down

0 comments on commit 5900f57

Please sign in to comment.