mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-02 18:02:32 +02:00
Refactoring base EOSE managers to provide better logging
This commit is contained in:
@@ -141,10 +141,10 @@ fun debugState(context: Context) {
|
||||
LocalCache.addressables
|
||||
.sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory() ?: 0L })
|
||||
|
||||
qttNotes.toList().sortedByDescending { bytesNotes.get(it.first) }.forEach { (kind, qtt) ->
|
||||
qttNotes.toList().sortedByDescending { bytesNotes[it.first] }.forEach { (kind, qtt) ->
|
||||
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesNotes.get(kind)?.div((1024 * 1024))}MB ")
|
||||
}
|
||||
qttAddressables.toList().sortedByDescending { bytesNotes.get(it.first) }.forEach { (kind, qtt) ->
|
||||
qttAddressables.toList().sortedByDescending { bytesNotes[it.first] }.forEach { (kind, qtt) ->
|
||||
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesAddressables.get(kind)?.div((1024 * 1024))}MB ")
|
||||
}
|
||||
}
|
||||
|
@@ -25,17 +25,29 @@ import com.vitorpamplona.amethyst.isDebug
|
||||
import com.vitorpamplona.ammolite.relays.BundledUpdate
|
||||
import com.vitorpamplona.ammolite.relays.datasources.SubscriptionController
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.single.newSubId
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
abstract class BaseEoseManager<T>(
|
||||
val client: NostrClient,
|
||||
val allKeys: () -> Set<T>,
|
||||
) {
|
||||
val orchestrator = SubscriptionController(client)
|
||||
protected val logTag: String = this.javaClass.simpleName
|
||||
|
||||
private val orchestrator = SubscriptionController(client)
|
||||
|
||||
abstract fun updateSubscriptions(keys: Set<T>)
|
||||
|
||||
fun printStats() = orchestrator.printStats(this.javaClass.simpleName)
|
||||
fun printStats() = orchestrator.printStats(logTag)
|
||||
|
||||
fun newSubscriptionId() = if (isDebug) logTag + newSubId() else newSubId()
|
||||
|
||||
fun getSubscription(subId: String) = orchestrator.getSub(subId)
|
||||
|
||||
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null) = orchestrator.requestNewSubscription(newSubscriptionId(), onEOSE)
|
||||
|
||||
fun dismissSubscription(subId: String) = orchestrator.dismissSubscription(subId)
|
||||
|
||||
// Refreshes observers in batches.
|
||||
private val bundler = BundledUpdate(300, Dispatchers.Default)
|
||||
@@ -56,7 +68,7 @@ abstract class BaseEoseManager<T>(
|
||||
bundler.cancel()
|
||||
orchestrator.destroy()
|
||||
if (isDebug) {
|
||||
Log.d("${this.javaClass.simpleName}", "Destroy, Unsubscribe")
|
||||
Log.d(logTag, "Destroy, Unsubscribe")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -62,7 +62,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
||||
}
|
||||
|
||||
open fun newSub(key: T): Subscription =
|
||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
||||
requestNewSubscription { time, relayUrl ->
|
||||
newEose(key, relayUrl, time)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
||||
key: String,
|
||||
subId: String,
|
||||
) {
|
||||
orchestrator.dismissSubscription(subId)
|
||||
dismissSubscription(subId)
|
||||
userSubscriptionMap.remove(key)
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
||||
return if (subId == null) {
|
||||
newSub(key).also { userSubscriptionMap[id] = it.id }
|
||||
} else {
|
||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[id] = it.id }
|
||||
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[id] = it.id }
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.groupByRelay
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import kotlin.collections.distinctBy
|
||||
|
||||
/**
|
||||
* This query type creates a new relay subscription for every logged-in
|
||||
@@ -57,7 +58,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
||||
) = latestEOSEs.newEose(user(key), list(key), relay, time)
|
||||
|
||||
open fun newSub(key: T): Subscription =
|
||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
||||
requestNewSubscription { time, relayUrl ->
|
||||
newEose(key, relayUrl, time)
|
||||
if (invalidateAfterEose) {
|
||||
invalidateFilters()
|
||||
@@ -68,7 +69,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
||||
key: User,
|
||||
subId: String,
|
||||
) {
|
||||
orchestrator.dismissSubscription(subId)
|
||||
dismissSubscription(subId)
|
||||
userSubscriptionMap.remove(key)
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
||||
return if (subId == null) {
|
||||
newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
} else {
|
||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -56,7 +56,7 @@ abstract class PerUserEoseManager<T>(
|
||||
) = latestEOSEs.newEose(user(key), relay, time)
|
||||
|
||||
open fun newSub(key: T): Subscription =
|
||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
||||
requestNewSubscription { time, relayUrl ->
|
||||
newEose(key, relayUrl, time)
|
||||
if (invalidateAfterEose) {
|
||||
invalidateFilters()
|
||||
@@ -67,7 +67,7 @@ abstract class PerUserEoseManager<T>(
|
||||
key: User,
|
||||
subId: String,
|
||||
) {
|
||||
orchestrator.dismissSubscription(subId)
|
||||
dismissSubscription(subId)
|
||||
userSubscriptionMap.remove(key)
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ abstract class PerUserEoseManager<T>(
|
||||
return if (subId == null) {
|
||||
newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
} else {
|
||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -56,7 +56,7 @@ abstract class SingleSubEoseManager<T>(
|
||||
) = latestEOSEs.newEose(relay, time)
|
||||
|
||||
val sub =
|
||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
||||
requestNewSubscription { time, relayUrl ->
|
||||
newEose(relayUrl, time)
|
||||
if (invalidateAfterEose) {
|
||||
invalidateFilters()
|
||||
|
@@ -36,7 +36,7 @@ abstract class SingleSubNoEoseCacheEoseManager<T>(
|
||||
val invalidateAfterEose: Boolean = false,
|
||||
) : BaseEoseManager<T>(client, allKeys) {
|
||||
val sub =
|
||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
||||
requestNewSubscription { time, relayUrl ->
|
||||
if (invalidateAfterEose) {
|
||||
invalidateFilters()
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ class AccountGiftWrapsEoseManager(
|
||||
client: NostrClient,
|
||||
allKeys: () -> Set<AccountQueryState>,
|
||||
) : PerUserEoseManager<AccountQueryState>(client, allKeys) {
|
||||
override fun user(query: AccountQueryState) = query.account.userProfile()
|
||||
override fun user(key: AccountQueryState) = key.account.userProfile()
|
||||
|
||||
override fun updateFilter(
|
||||
key: AccountQueryState,
|
||||
|
@@ -78,7 +78,10 @@ class SubscriptionController(
|
||||
|
||||
fun getSub(subId: String) = subscriptions.get(subId)
|
||||
|
||||
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null): Subscription = Subscription(onEose = onEOSE).also { subscriptions.put(it.id, it) }
|
||||
fun requestNewSubscription(
|
||||
subId: String,
|
||||
onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null,
|
||||
): Subscription = Subscription(subId, onEose = onEOSE).also { subscriptions.put(it.id, it) }
|
||||
|
||||
fun dismissSubscription(subId: String) = getSub(subId)?.let { dismissSubscription(it) }
|
||||
|
||||
|
Reference in New Issue
Block a user