Skip to content

Commit

Permalink
Log more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Feb 5, 2024
1 parent 83b82e2 commit b3e0593
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
44 changes: 25 additions & 19 deletions app/src/main/kotlin/element/ElementsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.app.Application
import android.util.Log
import api.Api
import kotlinx.coroutines.*
import log.LogRecordQueries
import org.json.JSONObject
import org.osmdroid.util.BoundingBox

class ElementsRepo(
private val api: Api,
private val app: Application,
private val queries: ElementQueries,
private val logRecordQueries: LogRecordQueries,
) {

suspend fun selectById(id: Long) = queries.selectById(id)
Expand Down Expand Up @@ -130,30 +133,33 @@ class ElementsRepo(
}
}

suspend fun selectCategories() = queries.selectCategories()

suspend fun fetchBundledElements(): Result<SyncReport> {
return runCatching {
val startMillis = System.currentTimeMillis()
suspend fun selectCount(): Long {
return withContext(Dispatchers.IO) { queries.selectCount() }
}

val rows = queries.selectCount()
suspend fun hasBundledElements(): Boolean {
return withContext(Dispatchers.IO) {
app.resources.assets.list("")!!.contains("elements.json")
}
}

if (rows > 0) {
return@runCatching SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
createdOrUpdatedElements = 0,
)
}
suspend fun fetchBundledElements(): Result<Unit> {
return runCatching {
val startMs = System.currentTimeMillis()

app.assets.open("elements.json").use { bundledElements ->
withContext(Dispatchers.IO) {
val elements = bundledElements.toElementsJson()
val typedElements = elements.map { it.toElement() }
queries.insertOrReplace(typedElements)

SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
createdOrUpdatedElements = elements.size.toLong(),
val elements = bundledElements.toElementsJson().map { it.toElement() }
queries.insertOrReplace(elements)

logRecordQueries.insert(
JSONObject(
mapOf(
"message" to "fetched bundled elements",
"count" to elements.size.toLong(),
"time_ms" to System.currentTimeMillis() - startMs,
)
)
)
}
}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import log.LogRecordQueries
import org.json.JSONObject
import user.UsersRepo
import java.time.ZoneOffset
import java.time.ZonedDateTime
import kotlin.time.measureTime

class Sync(
private val areasRepo: AreasRepo,
Expand All @@ -25,6 +26,7 @@ class Sync(
private val reportsRepo: ReportsRepo,
private val usersRepo: UsersRepo,
private val eventsRepo: EventsRepo,
private val logRecordQueries: LogRecordQueries,
) {

private val _active = MutableStateFlow(false)
Expand All @@ -47,9 +49,13 @@ class Sync(
}

runCatching {
Log.d(TAG, "Fetching bundled elements")
val fetchBundledElementsDuration = measureTime { elementsRepo.fetchBundledElements() }
Log.d(TAG, "Fetched bundled elements in $fetchBundledElementsDuration")
withContext(Dispatchers.IO) {
logRecordQueries.insert(JSONObject(mapOf("message" to "started sync")))

if (elementsRepo.selectCount() == 0L && elementsRepo.hasBundledElements()) {
elementsRepo.fetchBundledElements().getOrThrow()
}
}

withContext(Dispatchers.IO) {
listOf(
Expand Down

0 comments on commit b3e0593

Please sign in to comment.