Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove heartbeat subscription and minor code cleanup #94

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DittoHeartbeat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ plugins {
id "org.jetbrains.kotlin.android"
}

ext.libraryArtifactId = "dittoheartbeat"

apply from: "$rootProject.projectDir/gradle/android-common.gradle"
apply from: "$rootProject.projectDir/gradle/deploy.gradle"

ext.libraryArtifactId = "dittoheartbeat"

android {
namespace 'live.ditto.dittoheartbeat'
}
Expand Down
67 changes: 28 additions & 39 deletions DittoHeartbeat/src/main/java/live.ditto.dittoheartbeat/Heartbeat.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package live.ditto.dittoheartbeat

import android.os.Build
import android.provider.SyncStateContract.Constants
import androidx.annotation.RequiresApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import live.ditto.Ditto
import live.ditto.DittoConnectionType
import live.ditto.DittoPeer
import live.ditto.DittoSyncSubscription
import live.ditto.dittohealthmetrics.HealthMetric
import live.ditto.dittohealthmetrics.HealthMetricProvider
import org.joda.time.DateTime
Expand Down Expand Up @@ -43,7 +38,6 @@ data class DittoHeartbeatInfo(
var healthMetrics: MutableMap<String, HealthMetric> = mutableMapOf()
)

var heartbeatSubscription: DittoSyncSubscription? = null
lateinit var info: DittoHeartbeatInfo

private fun updateHealthMetrics(config: DittoHeartbeatConfig) {
Expand All @@ -58,37 +52,30 @@ private fun updateHealthMetrics(config: DittoHeartbeatConfig) {
fun startHeartbeat(ditto: Ditto, config: DittoHeartbeatConfig): Flow<DittoHeartbeatInfo> = flow {
val cancelable = AtomicBoolean(false)

if (heartbeatSubscription == null) {
heartbeatSubscription = ditto.sync.registerSubscription("SELECT * FROM $HEARTBEAT_COLLECTION_COLLECTION_NAME")
}
while (!cancelable.get()) {
delay((config.secondsInterval * 1000L))
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)
)

try {
while (!cancelable.get()) {
delay((config.secondsInterval * 1000L))
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)
)

updateHealthMetrics(config)

addToCollection(info, config, ditto)
emit(info)
}
} finally {
heartbeatSubscription!!.close()
updateHealthMetrics(config)

addToCollection(info, config, ditto)
emit(info)
}

}

@RequiresApi(Build.VERSION_CODES.O)
Expand All @@ -102,9 +89,8 @@ fun observePeers(ditto: Ditto): List<DittoPeer> {
return presenceGraph.remotePeers
}

val myCoroutineScope = CoroutineScope(Dispatchers.Main)
fun addToCollection(info: DittoHeartbeatInfo, config: DittoHeartbeatConfig, ditto: Ditto) {
if(!config.publishToDittoCollection) return
if (!config.publishToDittoCollection) return
val metaData = config.metaData ?: emptyMap()
val doc = mapOf(
"_id" to info.id,
Expand All @@ -122,7 +108,10 @@ fun addToCollection(info: DittoHeartbeatInfo, config: DittoHeartbeatConfig, ditt
}

@RequiresApi(Build.VERSION_CODES.O)
fun getConnections(presenceSnapshotDirectlyConnectedPeers: List<DittoPeer>?, ditto: Ditto): Map<String, Any> {
fun getConnections(
presenceSnapshotDirectlyConnectedPeers: List<DittoPeer>?,
ditto: Ditto
): Map<String, Any> {

val connectionsMap: MutableMap<String, Any> = mutableMapOf()

Expand All @@ -136,7 +125,7 @@ fun getConnections(presenceSnapshotDirectlyConnectedPeers: List<DittoPeer>?, dit
"bluetooth" to connectionsTypeMap["bt"],
"p2pWifi" to connectionsTypeMap["p2pWifi"],
"lan" to connectionsTypeMap["lan"],
)
)

connectionsMap[byteArrayToHash(connection.peerKey)] = connectionMap
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/live/ditto/dittotoolsapp/HeartbeatView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import live.ditto.dittoheartbeat.startHeartbeat
import java.util.*


@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun ShowHeartbeatData(ditto: Ditto) {

var heartbeatInfo by remember { mutableStateOf<DittoHeartbeatInfo?>(null) }
var healthMetricProviders: MutableList<HealthMetricProvider> = mutableListOf()
val diskUsageViewModel = DiskUsageViewModel()
diskUsageViewModel.isHealthyMBSizeLimit = 2048 //2GB worth of data
healthMetricProviders.add(diskUsageViewModel)
// healthMetricProviders.add(diskUsageViewModel)
bplattenburg marked this conversation as resolved.
Show resolved Hide resolved

val config = DittoHeartbeatConfig(
//id for testing only. Unique id will not persist
Expand Down