Skip to content

Commit e2a29ea

Browse files
committed
improve db layer observability
1 parent aec7234 commit e2a29ea

3 files changed

Lines changed: 9 additions & 59 deletions

File tree

app/src/main/kotlin/org/btcmap/db/table/place/PlaceQueries.kt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,8 @@ class PlaceQueries(private val conn: SQLiteConnection) {
8181
}
8282
}
8383

84-
fun selectMerchants(): List<Marker> {
85-
conn.prepare(
86-
"""
87-
SELECT ${MarkerProjection.COLUMNS}
88-
FROM $TABLE
89-
WHERE
90-
$ICON <> 'local_atm' AND $ICON <> 'currency_exchange'
91-
ORDER BY $LAT DESC;
92-
"""
93-
).use {
94-
val rows = mutableListOf<Marker>()
95-
while (it.step()) {
96-
rows.add(MarkerProjection.fromStatement(it))
97-
}
98-
return rows
99-
}
100-
}
84+
var selectMerchantsByBoundsCallCount = 0
85+
var selectMerchantsByBoundsLastCallDurationMs = 0L
10186

10287
fun selectMerchantsByBounds(
10388
minLat: Double,
@@ -106,6 +91,8 @@ class PlaceQueries(private val conn: SQLiteConnection) {
10691
maxLon: Double,
10792
minVerifiedAt: ZonedDateTime? = null,
10893
): List<Marker> {
94+
val startTime = System.currentTimeMillis()
95+
selectMerchantsByBoundsCallCount += 1
10996
val whereClause = buildString {
11097
append("$ICON <> 'local_atm' AND $ICON <> 'currency_exchange'")
11198
append(" AND $LAT >= ?1 AND $LAT <= ?2 AND $LON >= ?3 AND $LON <= ?4")
@@ -132,6 +119,7 @@ class PlaceQueries(private val conn: SQLiteConnection) {
132119
while (it.step()) {
133120
rows.add(MarkerProjection.fromStatement(it))
134121
}
122+
selectMerchantsByBoundsLastCallDurationMs = System.currentTimeMillis() - startTime
135123
return rows
136124
}
137125
}

app/src/main/kotlin/org/btcmap/map/MapFragment.kt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ class MapFragment : Fragment() {
9797
var statusBarController: MapStatusBarController? = null
9898
var bottomSheetController: BottomSheetController? = null
9999

100-
private var dbCallCount = 0
101-
private var lastDbCallTimeMs = 0L
102100
private var lastMerchantsGeoJson: String? = null
103101
private var lastEventsGeoJson: String? = null
104102
private var lastExchangesGeoJson: String? = null
@@ -631,8 +629,6 @@ class MapFragment : Fragment() {
631629
merchantsCache = PlaceCache()
632630
exchangesCache = PlaceCache()
633631
eventsCache = EventCache()
634-
dbCallCount = 0
635-
lastDbCallTimeMs = 0L
636632

637633
when (filter) {
638634
Filter.MERCHANTS -> showMerchants()
@@ -663,8 +659,6 @@ class MapFragment : Fragment() {
663659
val expandedBounds = expandBounds(bounds)
664660
viewLifecycleOwner.lifecycleScope.launch {
665661
if (!merchantsCache.contains(expandedBounds)) {
666-
dbCallCount++
667-
val startTime = System.currentTimeMillis()
668662
val newMerchants = withContext(Dispatchers.IO) {
669663
db().place.selectMerchantsByBounds(
670664
expandedBounds.latitudeSouth,
@@ -675,7 +669,6 @@ class MapFragment : Fragment() {
675669
.minusYears(prefs.verifiedFilterYears.toLong()),
676670
)
677671
}
678-
lastDbCallTimeMs = System.currentTimeMillis() - startTime
679672
merchantsCache = merchantsCache.add(newMerchants, expandedBounds)
680673
}
681674
val geoJson = merchantsCache.features.toGeoJson()
@@ -695,8 +688,6 @@ class MapFragment : Fragment() {
695688
val expandedBounds = expandBounds(bounds)
696689
viewLifecycleOwner.lifecycleScope.launch {
697690
if (!eventsCache.contains(expandedBounds)) {
698-
dbCallCount++
699-
val startTime = System.currentTimeMillis()
700691
val newEvents = withContext(Dispatchers.IO) {
701692
db().event.selectByBounds(
702693
expandedBounds.latitudeSouth,
@@ -705,7 +696,6 @@ class MapFragment : Fragment() {
705696
expandedBounds.longitudeEast,
706697
)
707698
}
708-
lastDbCallTimeMs = System.currentTimeMillis() - startTime
709699
eventsCache = eventsCache.add(newEvents, expandedBounds)
710700
}
711701
val geoJson = eventsCache.features.toEventsGeoJson()
@@ -725,8 +715,6 @@ class MapFragment : Fragment() {
725715
val expandedBounds = expandBounds(bounds)
726716
viewLifecycleOwner.lifecycleScope.launch {
727717
if (!exchangesCache.contains(expandedBounds)) {
728-
dbCallCount++
729-
val startTime = System.currentTimeMillis()
730718
val newExchanges = withContext(Dispatchers.IO) {
731719
db().place.selectExchangesByBounds(
732720
expandedBounds.latitudeSouth,
@@ -735,7 +723,6 @@ class MapFragment : Fragment() {
735723
expandedBounds.longitudeEast,
736724
)
737725
}
738-
lastDbCallTimeMs = System.currentTimeMillis() - startTime
739726
exchangesCache = exchangesCache.add(newExchanges, expandedBounds)
740727
}
741728
val geoJson = exchangesCache.features.toGeoJson()
@@ -786,7 +773,10 @@ class MapFragment : Fragment() {
786773
binding.debugStats.apply {
787774
text =
788775
"memcache: %d items\nmemcache bounds: %s\ndb queries: %d\nlast query: %dms".format(
789-
cacheSize, viewportInfo, dbCallCount, lastDbCallTimeMs
776+
cacheSize,
777+
viewportInfo,
778+
db().place.selectMerchantsByBoundsCallCount,
779+
db().place.selectMerchantsByBoundsLastCallDurationMs,
790780
)
791781
isVisible = true
792782
}

app/src/test/kotlin/org/btcmap/db/table/place/PlaceQueriesTest.kt

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,6 @@ class PlaceQueriesTest {
121121
Assert.assertTrue(results.isEmpty())
122122
}
123123

124-
@Test
125-
fun selectMerchants_excludesAtmAndExchange() {
126-
val db = createDatabase()
127-
db.place.insert(listOf(createPlace(id = 1L, icon = "restaurant")))
128-
db.place.insert(listOf(createPlace(id = 2L, icon = "local_atm")))
129-
db.place.insert(listOf(createPlace(id = 3L, icon = "currency_exchange")))
130-
db.place.insert(listOf(createPlace(id = 4L, icon = "coffee")))
131-
132-
val results = db.place.selectMerchants()
133-
134-
Assert.assertEquals(2, results.size)
135-
Assert.assertTrue(results.all { it.icon != "local_atm" && it.icon != "currency_exchange" })
136-
}
137-
138124
@Test
139125
fun selectExchanges_includesOnlyAtmAndExchange() {
140126
val db = createDatabase()
@@ -274,20 +260,6 @@ class PlaceQueriesTest {
274260
Assert.assertEquals(1L, db.place.selectCount())
275261
}
276262

277-
@Test
278-
fun selectMerchants_withBoostedPlace() {
279-
val db = createDatabase()
280-
val boostedPlace = createPlace(id = 1L, icon = "restaurant")
281-
val normalPlace = createPlace(id = 2L, icon = "coffee")
282-
283-
db.place.insert(listOf(boostedPlace))
284-
db.place.insert(listOf(normalPlace))
285-
286-
val results = db.place.selectMerchants()
287-
288-
Assert.assertEquals(2, results.size)
289-
}
290-
291263
@Test
292264
fun selectMerchantsByBounds_withMinVerifiedAt_returnsOnlyRecentMerchants() {
293265
val db = createDatabase()

0 commit comments

Comments
 (0)