Skip to content

Commit

Permalink
Bundle reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 30, 2024
1 parent aedc877 commit 1d7a085
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ build/
.gradle

# App data
app/src/main/assets/elements.json
app/src/main/assets/elements.json
app/src/main/assets/reports.json
9 changes: 6 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ android {

tasks.register("bundleData") {
doLast {
val src = URL("https://static.btcmap.org/api/v3/elements.json")
val destDir = File(projectDir, "src/main/assets")
destDir.mkdirs()
val destFile = File(destDir, "elements.json")
destFile.writeText(src.readText())

val elementsSrc = URL("https://static.btcmap.org/api/v3/elements.json")
File(destDir, "elements.json").writeText(elementsSrc.readText())

val reportsSrc = URL("https://static.btcmap.org/api/v2/reports.json")
File(destDir, "reports.json").writeText(reportsSrc.readText())
}
}

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/kotlin/reports/ReportQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ class ReportQueries(private val db: SQLiteOpenHelper) {
}

}

suspend fun selectCount(): Long {
return withContext(Dispatchers.IO) {
val cursor = db.readableDatabase.query(
"""
SELECT count(*)
FROM report;
"""
)

cursor.moveToNext()
cursor.getLong(0)
}
}
}
69 changes: 53 additions & 16 deletions app/src/main/kotlin/reports/ReportsRepo.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,76 @@
package reports

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 ReportsRepo(
private val api: Api,
private val queries: ReportQueries,
private val context: Context,
) {

suspend fun selectByAreaId(areaId: String) = queries.selectByAreaId(areaId)

suspend fun sync(): Result<SyncReport> {
return runCatching {
val startMillis = System.currentTimeMillis()
var count = 0L
suspend fun selectCount() = queries.selectCount()

while (true) {
val reports = api.getReports(queries.selectMaxUpdatedAt(), BATCH_SIZE)
count += reports.size
queries.insertOrReplace(reports.map { it.toReport() })
suspend fun hasBundledReports(): Boolean {
return withContext(Dispatchers.IO) {
context.resources.assets.list("")!!.contains("reports.json")
}
}

suspend fun fetchBundledReports() {
withContext(Dispatchers.IO) {
context.assets.open("reports.json").use { bundledReports ->
val reports = bundledReports.toReportsJson().map { it.toReport() }
queries.insertOrReplace(reports)
}
}
}

if (reports.size < BATCH_SIZE) {
break
suspend fun sync(): SyncReport {
val startedAt = ZonedDateTime.now(ZoneOffset.UTC)
val newReports = mutableListOf<Report>()
val updatedReports = mutableListOf<Report>()
val maxUpdatedAtBeforeSync = queries.selectMaxUpdatedAt()

while (true) {
val reports =
api.getReports(queries.selectMaxUpdatedAt(), BATCH_SIZE).map { it.toReport() }

reports.forEach {
if (maxUpdatedAtBeforeSync == null
|| it.createdAt.isAfter(maxUpdatedAtBeforeSync)
) {
newReports += it
} else {
updatedReports += it
}
}

SyncReport(
timeMillis = System.currentTimeMillis() - startMillis,
createdOrUpdatedReports = count,
)
queries.insertOrReplace(reports)

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

return SyncReport(
duration = Duration.between(startedAt, ZonedDateTime.now(ZoneOffset.UTC)),
newReports = newReports,
updatedReports = updatedReports,
)
}

data class SyncReport(
val timeMillis: Long,
val createdOrUpdatedReports: Long,
val duration: Duration,
val newReports: List<Report>,
val updatedReports: List<Report>,
)

companion object {
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import reports.Report
import time.now
import user.UsersRepo
import java.time.ZoneOffset
Expand Down Expand Up @@ -43,8 +44,12 @@ class Sync(
elementsRepo.fetchBundledElements().getOrThrow()
}

if (reportsRepo.selectCount() == 0L && reportsRepo.hasBundledReports()) {
reportsRepo.fetchBundledReports()
}

val elementsReport = async { elementsRepo.sync().getOrThrow() }
val reportsReport = async { reportsRepo.sync().getOrThrow() }
val reportsReport = async { reportsRepo.sync() }
val areasReport = async { areasRepo.sync().getOrThrow() }
val usersReport = async { usersRepo.sync().getOrThrow() }
val eventsReport = async { eventsRepo.sync().getOrThrow() }
Expand All @@ -62,6 +67,8 @@ class Sync(
finishedAt = ZonedDateTime.now(ZoneOffset.UTC),
newElements = elementsReport.await().newElements,
updatedElements = elementsReport.await().updatedElements,
newReports = reportsReport.await().newReports,
updatedReports = reportsReport.await().updatedReports,
newEvents = eventsReport.await().newEvents,
updatedEvents = eventsReport.await().updatedEvents,
)
Expand All @@ -85,6 +92,8 @@ data class SyncReport(
val finishedAt: ZonedDateTime,
val newElements: Long,
val updatedElements: Long,
val newReports: List<Report>,
val updatedReports: List<Report>,
val newEvents: List<Event>,
val updatedEvents: List<Event>,
)
7 changes: 3 additions & 4 deletions app/src/main/kotlin/sync/SyncNotificationController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class SyncNotificationController(
.bigText(
"""
|Time: $syncTimeMillis ms
|New elements: ${report.newElements}
|Updated elements: ${report.updatedElements}
|New events: ${report.newEvents.size}
|Updated events: ${report.updatedEvents.size}
|Elements: ${report.newElements} new, ${report.updatedElements} updated
|Events: ${report.newEvents.size} new, ${report.updatedEvents.size} updated
|Reports: ${report.newReports.size} new, ${report.updatedReports.size} updated
""".trimMargin()
)
)
Expand Down

0 comments on commit 1d7a085

Please sign in to comment.