Skip to content

Commit

Permalink
Generate more detailed user sync reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent 5900f57 commit cef9da4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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 @@ -57,7 +57,7 @@ class SyncNotificationController(
|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.newAreas} new, ${report.areasReport.updatedAreas} updated in ${report.areasReport.duration.toMillis()} ms
|Users: ${report.usersReport.createdOrUpdatedUsers} in ${report.usersReport.timeMillis}
|Users: ${report.usersReport.newUsers} new, ${report.usersReport.updatedUsers} updated in ${report.usersReport.duration.toMillis()} ms
""".trimMargin()
)
)
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/kotlin/user/UsersRepo.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 UsersRepo(
private val api: Api,
Expand Down Expand Up @@ -33,28 +36,42 @@ class UsersRepo(
}

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

while (true) {
val users = api.getUsers(queries.selectMaxUpdatedAt(), BATCH_SIZE)
count += users.size
queries.insertOrReplace(users.map { it.toUser() })
val users = api.getUsers(queries.selectMaxUpdatedAt(), BATCH_SIZE).map { it.toUser() }

users.forEach {
if (maxUpdatedAtBeforeSync == null
|| it.createdAt.isAfter(maxUpdatedAtBeforeSync)
) {
newUsers += 1
} else {
updatedUsers += 1
}
}

queries.insertOrReplace(users)

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

return SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
createdOrUpdatedUsers = count,
duration = Duration.between(startedAt, ZonedDateTime.now(ZoneOffset.UTC)),
newUsers = newUsers,
updatedUsers = updatedUsers,
)
}

data class SyncReport(
val timeMillis: Long,
val createdOrUpdatedUsers: Long,
val duration: Duration,
val newUsers: Long,
val updatedUsers: Long,
)

companion object {
Expand Down

0 comments on commit cef9da4

Please sign in to comment.