11package org.digma.intellij.plugin.analytics
22
33import com.intellij.collaboration.async.disposingScope
4- import com.intellij.openapi.Disposable
54import com.intellij.openapi.components.Service
65import com.intellij.openapi.components.service
76import com.intellij.openapi.diagnostic.Logger
87import com.intellij.openapi.project.Project
98import kotlinx.coroutines.async
9+ import kotlinx.coroutines.delay
10+ import kotlinx.coroutines.isActive
1011import kotlinx.coroutines.launch
1112import kotlinx.coroutines.runBlocking
1213import kotlinx.coroutines.withTimeout
14+ import org.digma.intellij.plugin.common.DisposableAdaptor
1315import org.digma.intellij.plugin.common.ExceptionUtils
1416import org.digma.intellij.plugin.errorreporting.ErrorReporter
1517import org.digma.intellij.plugin.log.Log
1618import org.digma.intellij.plugin.model.rest.AboutResult
19+ import org.digma.intellij.plugin.posthog.ActivityMonitor
1720import java.util.concurrent.atomic.AtomicReference
21+ import kotlin.time.Duration.Companion.minutes
1822
1923/* *
2024 * keep the backend info and tracks it on connection events.
2125 * Its necessary because there is code that runs on EDT that may need the backend info. it's possible
2226 * in that case to do it on background but then the EDT will wait for the api call, and we don't want that.
2327 */
2428@Service(Service .Level .PROJECT )
25- class BackendInfoHolder (val project : Project ) : Disposable {
29+ class BackendInfoHolder (val project : Project ) : DisposableAdaptor {
2630
2731 private val logger: Logger = Logger .getInstance(BackendInfoHolder ::class .java)
2832
@@ -35,14 +39,6 @@ class BackendInfoHolder(val project: Project) : Disposable {
3539 }
3640 }
3741
38- fun loadOnStartup () {
39- updateInBackground()
40- }
41-
42-
43- override fun dispose () {
44- // nothing to do, used as parent disposable
45- }
4642
4743 init {
4844 project.messageBus.connect(this )
@@ -64,29 +60,42 @@ class BackendInfoHolder(val project: Project) : Disposable {
6460 Log .log(logger::debug, " got apiClientChanged" )
6561 updateInBackground()
6662 })
63+
64+
65+ @Suppress(" UnstableApiUsage" )
66+ disposingScope().launch {
67+ while (isActive) {
68+ update()
69+ delay(1 .minutes.inWholeMilliseconds)
70+ }
71+ }
6772 }
6873
6974
70- // updateInBackground is also called every time the analytics client is replaced
7175 private fun updateInBackground () {
7276 @Suppress(" UnstableApiUsage" )
7377 disposingScope().launch {
74- try {
75-
76- aboutRef.set( AnalyticsService .getInstance(project).about)
78+ update()
79+ }
80+ }
7781
78- } catch (e: Throwable ) {
79- val isConnectionException = ExceptionUtils .isAnyConnectionException(e)
8082
81- if (! isConnectionException) {
82- ErrorReporter .getInstance().reportError(" BackendUtilsKt.updateInBackground" , e)
83- }
83+ // must be called in background coroutine
84+ private fun update () {
85+ try {
86+ aboutRef.set(AnalyticsService .getInstance(project).about)
87+ aboutRef.get()?.let {
88+ ActivityMonitor .getInstance(project).registerServerInfo(it)
89+ }
90+ } catch (e: Throwable ) {
91+ val isConnectionException = ExceptionUtils .isAnyConnectionException(e)
92+ if (! isConnectionException) {
93+ ErrorReporter .getInstance().reportError(" BackendInfoHolder.update" , e)
8494 }
8595 }
8696 }
8797
8898
89-
9099 fun getAbout (): AboutResult ? {
91100 if (aboutRef.get() == null ) {
92101 return getAboutInBackgroundNow()
@@ -104,7 +113,6 @@ class BackendInfoHolder(val project: Project) : Disposable {
104113 }
105114
106115
107-
108116 fun isCentralized (): Boolean {
109117 return aboutRef.get()?.let {
110118 it.isCentralize ? : false
@@ -121,17 +129,9 @@ class BackendInfoHolder(val project: Project) : Disposable {
121129
122130 @Suppress(" UnstableApiUsage" )
123131 val deferred = disposingScope().async {
124- try {
125- aboutRef.set(AnalyticsService .getInstance(project).about)
126- } catch (e: Throwable ) {
127- val isConnectionException = ExceptionUtils .isAnyConnectionException(e)
128- if (! isConnectionException) {
129- ErrorReporter .getInstance().reportError(" BackendUtilsKt.getAboutInBackgroundNowWithTimeout" , e)
130- }
131- }
132+ update()
132133 }
133134
134-
135135 return runBlocking {
136136 try {
137137 withTimeout(5000 ) {
0 commit comments