Skip to content

Commit

Permalink
Merge pull request #78 from getditto/rr/use-peerkey-string
Browse files Browse the repository at this point in the history
Update tools to use peer key string
  • Loading branch information
rajramsaroop authored Aug 14, 2024
2 parents ac554be + b779d25 commit de868da
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 44 deletions.
2 changes: 1 addition & 1 deletion DittoHeartbeat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
constraints {
implementation(libs.live.ditto.ditto) {
version {
strictly "[4.5.0,)"
strictly "[4.7.0,)"
}
}
}
Expand Down
39 changes: 16 additions & 23 deletions DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import live.ditto.DittoPeer
import live.ditto.healthmetrics.HealthMetric
import live.ditto.healthmetrics.HealthMetricProvider
import org.joda.time.DateTime
import java.util.Base64
import java.util.concurrent.atomic.AtomicBoolean

data class DittoHeartbeatConfig(
Expand Down Expand Up @@ -57,18 +56,18 @@ fun startHeartbeat(ditto: Ditto, config: DittoHeartbeatConfig): Flow<DittoHeartb
val timestamp = DateTime().toISOString()
val presenceData = observePeers(ditto)

info =
DittoHeartbeatInfo(
id = config.id,
lastUpdated = timestamp,
presenceSnapshotDirectlyConnectedPeers = getConnections(presenceData, ditto),
metaData = config.metaData,
presenceSnapshotDirectlyConnectedPeersCount = presenceData.size,
secondsInterval = config.secondsInterval,
sdk = ditto.sdkVersion,
schema = HEARTBEAT_COLLECTION_SCHEMA_VALUE,
peerKey = byteArrayToHash(ditto.presence.graph.localPeer.peerKey)
)
info =
DittoHeartbeatInfo(
id = config.id,
lastUpdated = timestamp,
presenceSnapshotDirectlyConnectedPeers = getConnections(presenceData, ditto) ,
metaData = config.metaData,
presenceSnapshotDirectlyConnectedPeersCount = presenceData.size,
secondsInterval = config.secondsInterval,
sdk = ditto.sdkVersion,
schema = HEARTBEAT_COLLECTION_SCHEMA_VALUE,
peerKey = ditto.presence.graph.localPeer.peerKeyString
)

updateHealthMetrics(config)

Expand All @@ -78,12 +77,6 @@ fun startHeartbeat(ditto: Ditto, config: DittoHeartbeatConfig): Flow<DittoHeartb

}

@RequiresApi(Build.VERSION_CODES.O)
fun byteArrayToHash(byteArray: ByteArray): String {
val base64String = Base64.getEncoder().encodeToString(byteArray)
return "pk:$base64String"
}

fun observePeers(ditto: Ditto): List<DittoPeer> {
val presenceGraph = ditto.presence.graph
return presenceGraph.remotePeers
Expand Down Expand Up @@ -116,7 +109,7 @@ fun getConnections(
val connectionsMap: MutableMap<String, Any> = mutableMapOf()

presenceSnapshotDirectlyConnectedPeers?.forEach { connection ->
val connectionsTypeMap = getConnectionTypeCount(connection = connection)
val connectionsTypeMap = getConnectionTypeCount(dittoPeer = connection)

val connectionMap: Map<String, Any?> = mapOf(
"deviceName" to connection.deviceName,
Expand All @@ -127,18 +120,18 @@ fun getConnections(
"lan" to connectionsTypeMap["lan"],
)

connectionsMap[byteArrayToHash(connection.peerKey)] = connectionMap
connectionsMap[connection.peerKeyString] = connectionMap
}

return connectionsMap
}

fun getConnectionTypeCount(connection: DittoPeer): Map<String, Int> {
fun getConnectionTypeCount(dittoPeer: DittoPeer): Map<String, Int> {
var bt = 0
var p2pWifi = 0
var lan = 0

connection.connections.forEach { connection ->
dittoPeer.connections.forEach { connection ->
when (connection.connectionType) {
DittoConnectionType.Bluetooth -> bt += 1
DittoConnectionType.P2PWiFi -> p2pWifi += 1
Expand Down
2 changes: 1 addition & 1 deletion DittoPresenceDegradationReporter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
constraints {
implementation(libs.live.ditto.ditto) {
version {
strictly "[4.5.0,)"
strictly "[4.7.0,)"
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import live.ditto.presencedegradationreporter.model.PeerTransportInfo


class PresenceFlowUseCase(
private val ditto: Ditto,
private val hashPeerKeyUseCase: HashPeerKeyUseCase = HashPeerKeyUseCase()
private val ditto: Ditto
) {
operator fun invoke(bufferCapacity: Int = Channel.UNLIMITED) = callbackFlow {
val observer = ditto.presence.observe { graph ->
Expand All @@ -26,7 +25,7 @@ class PresenceFlowUseCase(
transportInfo = localPeerTransportInfo,
connected = true,
lastSeen = seenAt,
key = hashPeerKeyUseCase(graph.localPeer.peerKey),
key = graph.localPeer.peerKeyString,
)

val remotePeers = graph.remotePeers.map {
Expand All @@ -37,7 +36,7 @@ class PresenceFlowUseCase(
transportInfo = peerTransportInfo,
connected = true,
lastSeen = seenAt,
key = hashPeerKeyUseCase(it.peerKey),
key = it.peerKeyString,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ fun HeartbeatInfoCard(heartbeatInfo: DittoHeartbeatInfo) {
@Suppress("UNCHECKED_CAST")
val typedConnection = connection as Map<String, Any> // Type cast connection to Map<String, Any>
ConnectionInfo(connection = typedConnection)
} else {
}
}
}
Expand All @@ -102,6 +101,7 @@ fun HeartbeatHeader(heartbeatInfo: DittoHeartbeatInfo) {
Text("SDK: ${heartbeatInfo.sdk}")
Text("Last Updated: ${heartbeatInfo.lastUpdated}")
Text("remotePeersCount: ${heartbeatInfo.presenceSnapshotDirectlyConnectedPeersCount}", color = Color.Black)
Text("Peer key: ${heartbeatInfo.peerKey}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ androidx-navigation = "2.5.3"
androidx-test-ext = "1.1.5"
androidx-webkit = "1.7.0"
datastorePreferences = "1.0.0"
ditto = "4.5.0"
ditto = "4.7.4"
live-ditto-tools = "1.1.0"

junit = "4.13.2"
Expand Down

0 comments on commit de868da

Please sign in to comment.