Migrates per-relay stats from event counter to bytes transmitted.

This commit is contained in:
Vitor Pamplona
2023-03-23 17:52:26 -04:00
parent d9c0b8cd20
commit 3030d474e2
5 changed files with 36 additions and 28 deletions
@@ -7,8 +7,8 @@ data class RelaySetupInfo(
val read: Boolean, val read: Boolean,
val write: Boolean, val write: Boolean,
val errorCount: Int = 0, val errorCount: Int = 0,
val downloadCount: Int = 0, val downloadCountInBytes: Int = 0,
val uploadCount: Int = 0, val uploadCountInBytes: Int = 0,
val spamCount: Int = 0, val spamCount: Int = 0,
val feedTypes: Set<FeedType> val feedTypes: Set<FeedType>
) )
@@ -31,9 +31,10 @@ class Relay(
private var socket: WebSocket? = null private var socket: WebSocket? = null
private var isReady: Boolean = false private var isReady: Boolean = false
var eventDownloadCounter = 0 var eventDownloadCounterInBytes = 0
var eventUploadCounterInBytes = 0
var spamCounter = 0 var spamCounter = 0
var eventUploadCounter = 0
var errorCounter = 0 var errorCounter = 0
var ping: Long? = null var ping: Long? = null
@@ -89,7 +90,7 @@ class Relay(
when (type) { when (type) {
"EVENT" -> { "EVENT" -> {
// Log.w("Relay", "Relay onEVENT $url, $channel") // Log.w("Relay", "Relay onEVENT $url, $channel")
eventDownloadCounter++ eventDownloadCounterInBytes += text.bytesUsedInMemory()
val event = Event.fromJson(msg[2], Client.lenient) val event = Event.fromJson(msg[2], Client.lenient)
listeners.forEach { it.onEvent(this@Relay, channel, event) } listeners.forEach { it.onEvent(this@Relay, channel, event) }
} }
@@ -185,6 +186,7 @@ class Relay(
"""["REQ","$requestId",${filters.take(10).joinToString(",") { it.filter.toJson() }}]""" """["REQ","$requestId",${filters.take(10).joinToString(",") { it.filter.toJson() }}]"""
// println("FILTERSSENT ${url} ${request}") // println("FILTERSSENT ${url} ${request}")
socket?.send(request) socket?.send(request)
eventUploadCounterInBytes += request.bytesUsedInMemory()
} }
} }
} else { } else {
@@ -209,8 +211,9 @@ class Relay(
fun send(signedEvent: EventInterface) { fun send(signedEvent: EventInterface) {
if (write) { if (write) {
socket?.send("""["EVENT",${signedEvent.toJson()}]""") val event = """["EVENT",${signedEvent.toJson()}]"""
eventUploadCounter++ socket?.send(event)
eventUploadCounterInBytes += event.bytesUsedInMemory()
} }
} }
@@ -257,3 +260,7 @@ class Relay(
fun onRelayStateChange(relay: Relay, type: Type, channel: String?) fun onRelayStateChange(relay: Relay, type: Type, channel: String?)
} }
} }
fun String.bytesUsedInMemory(): Int {
return (8 * ((((this.length) * 2) + 45) / 8))
}
@@ -150,27 +150,27 @@ fun ServerConfigHeader() {
Column(Modifier.weight(1.4f)) { Column(Modifier.weight(1.4f)) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Spacer(modifier = Modifier.size(25.dp)) Spacer(modifier = Modifier.size(30.dp))
Text( Text(
text = stringResource(R.string.posts), text = stringResource(R.string.bytes),
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 14.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1.2f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
Spacer(modifier = Modifier.size(10.dp)) Spacer(modifier = Modifier.size(5.dp))
Text( Text(
text = stringResource(id = R.string.posts), text = stringResource(id = R.string.bytes),
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 14.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1.2f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
Spacer(modifier = Modifier.size(10.dp)) Spacer(modifier = Modifier.size(5.dp))
Text( Text(
text = stringResource(R.string.errors), text = stringResource(R.string.errors),
@@ -371,10 +371,10 @@ fun ServerConfig(
} }
Text( Text(
text = "${countToHumanReadable(item.downloadCount)}", text = "${countToHumanReadable(item.downloadCountInBytes)}",
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 12.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1.2f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
@@ -399,10 +399,10 @@ fun ServerConfig(
} }
Text( Text(
text = "${countToHumanReadable(item.uploadCount)}", text = "${countToHumanReadable(item.uploadCountInBytes)}",
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 12.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1.2f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
@@ -418,7 +418,7 @@ fun ServerConfig(
Text( Text(
text = "${countToHumanReadable(item.errorCount)}", text = "${countToHumanReadable(item.errorCount)}",
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 12.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
@@ -433,7 +433,7 @@ fun ServerConfig(
Text( Text(
text = "${countToHumanReadable(item.spamCount)}", text = "${countToHumanReadable(item.spamCount)}",
maxLines = 1, maxLines = 1,
fontSize = 14.sp, fontSize = 12.sp,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
) )
@@ -49,23 +49,23 @@ class NewRelayListViewModel : ViewModel() {
val localInfoFeedTypes = account.localRelays.filter { localRelay -> localRelay.url == it.key }.firstOrNull()?.feedTypes ?: FeedType.values().toSet() val localInfoFeedTypes = account.localRelays.filter { localRelay -> localRelay.url == it.key }.firstOrNull()?.feedTypes ?: FeedType.values().toSet()
val errorCounter = liveRelay?.errorCounter ?: 0 val errorCounter = liveRelay?.errorCounter ?: 0
val eventDownloadCounter = liveRelay?.eventDownloadCounter ?: 0 val eventDownloadCounter = liveRelay?.eventDownloadCounterInBytes ?: 0
val eventUploadCounter = liveRelay?.eventUploadCounter ?: 0 val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0
val spamCounter = liveRelay?.spamCounter ?: 0 val spamCounter = liveRelay?.spamCounter ?: 0
RelaySetupInfo(it.key, it.value.read, it.value.write, errorCounter, eventDownloadCounter, eventUploadCounter, spamCounter, localInfoFeedTypes) RelaySetupInfo(it.key, it.value.read, it.value.write, errorCounter, eventDownloadCounter, eventUploadCounter, spamCounter, localInfoFeedTypes)
}.sortedBy { it.downloadCount }.reversed() }.sortedBy { it.downloadCountInBytes }.reversed()
} else { } else {
account.localRelays.map { account.localRelays.map {
val liveRelay = RelayPool.getRelay(it.url) val liveRelay = RelayPool.getRelay(it.url)
val errorCounter = liveRelay?.errorCounter ?: 0 val errorCounter = liveRelay?.errorCounter ?: 0
val eventDownloadCounter = liveRelay?.eventDownloadCounter ?: 0 val eventDownloadCounter = liveRelay?.eventDownloadCounterInBytes ?: 0
val eventUploadCounter = liveRelay?.eventUploadCounter ?: 0 val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0
val spamCounter = liveRelay?.spamCounter ?: 0 val spamCounter = liveRelay?.spamCounter ?: 0
RelaySetupInfo(it.url, it.read, it.write, errorCounter, eventDownloadCounter, eventUploadCounter, spamCounter, it.feedTypes) RelaySetupInfo(it.url, it.read, it.write, errorCounter, eventDownloadCounter, eventUploadCounter, spamCounter, it.feedTypes)
}.sortedBy { it.downloadCount }.reversed() }.sortedBy { it.downloadCountInBytes }.reversed()
} }
} }
} }
+1
View File
@@ -74,6 +74,7 @@
<string name="failed_to_upload_the_image">Failed to upload the image</string> <string name="failed_to_upload_the_image">Failed to upload the image</string>
<string name="relay_address">Relay Address</string> <string name="relay_address">Relay Address</string>
<string name="posts">Posts</string> <string name="posts">Posts</string>
<string name="bytes">Bytes</string>
<string name="errors">Errors</string> <string name="errors">Errors</string>
<string name="home_feed">Home Feed</string> <string name="home_feed">Home Feed</string>
<string name="private_message_feed">Private Message Feed</string> <string name="private_message_feed">Private Message Feed</string>