Skip to content

Commit a2790b3

Browse files
authored
Merge pull request #2287 from digma-ai/backend-version-tracking
track server version, don't rely on connection gained Closes #2277
2 parents b349357 + c461d25 commit a2790b3

File tree

4 files changed

+35
-108
lines changed

4 files changed

+35
-108
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsServiceStarter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void executeProjectStartup(@NotNull Project project) {
1919

2020
AnalyticsService.getInstance(project);
2121
//make sure BackendInfoHolder is initialized after AnalyticsService
22-
BackendInfoHolder.getInstance(project).loadOnStartup();
22+
BackendInfoHolder.getInstance(project);
2323
});
2424
}
2525
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/analytics/BackendInfoHolder.kt

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
package org.digma.intellij.plugin.analytics
22

33
import com.intellij.collaboration.async.disposingScope
4-
import com.intellij.openapi.Disposable
54
import com.intellij.openapi.components.Service
65
import com.intellij.openapi.components.service
76
import com.intellij.openapi.diagnostic.Logger
87
import com.intellij.openapi.project.Project
98
import kotlinx.coroutines.async
9+
import kotlinx.coroutines.delay
10+
import kotlinx.coroutines.isActive
1011
import kotlinx.coroutines.launch
1112
import kotlinx.coroutines.runBlocking
1213
import kotlinx.coroutines.withTimeout
14+
import org.digma.intellij.plugin.common.DisposableAdaptor
1315
import org.digma.intellij.plugin.common.ExceptionUtils
1416
import org.digma.intellij.plugin.errorreporting.ErrorReporter
1517
import org.digma.intellij.plugin.log.Log
1618
import org.digma.intellij.plugin.model.rest.AboutResult
19+
import org.digma.intellij.plugin.posthog.ActivityMonitor
1720
import 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) {

ide-common/src/main/kotlin/org/digma/intellij/plugin/posthog/ActivityMonitor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.coroutines.isActive
1313
import kotlinx.coroutines.launch
1414
import kotlinx.datetime.toJavaInstant
1515
import org.digma.intellij.plugin.analytics.BackendConnectionMonitor
16+
import org.digma.intellij.plugin.analytics.BackendInfoHolder
1617
import org.digma.intellij.plugin.common.ExceptionUtils
1718
import org.digma.intellij.plugin.common.UniqueGeneratedUserId
1819
import org.digma.intellij.plugin.common.objectToJson
@@ -85,7 +86,6 @@ class ActivityMonitor(private val project: Project) : Disposable {
8586

8687
registerSessionDetails()
8788

88-
ServerVersionMonitor.getInstance(project)
8989
PluginActivityMonitor.getInstance(project)
9090

9191
settingsChangeTracker.start(this)
@@ -334,7 +334,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
334334
"ide.version" to ideVersion,
335335
"ide.build" to ideBuildNumber,
336336
"plugin.version" to pluginVersion,
337-
"server.version" to ServerVersionMonitor.getInstance(project).getServerVersion(),
337+
"server.version" to BackendInfoHolder.getInstance(project).getAbout()?.applicationVersion.toString(),
338338
"user.type" to if (isDevUser) "internal" else "external"
339339
)
340340

@@ -423,7 +423,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
423423
details["user.type"] = if (UniqueGeneratedUserId.isDevUser) "internal" else "external"
424424
details["error source"] = source
425425
details["plugin.version"] = pluginVersion
426-
details["server.version"] = ServerVersionMonitor.getInstance(project).getServerVersion()
426+
details["server.version"] = BackendInfoHolder.getInstance(project).getAbout()?.applicationVersion.toString()
427427

428428

429429
capture(
@@ -466,7 +466,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
466466
"ide.version" to ideVersion,
467467
"ide.build" to ideBuildNumber,
468468
"plugin.version" to pluginVersion,
469-
"server.version" to ServerVersionMonitor.getInstance(project).getServerVersion(),
469+
"server.version" to BackendInfoHolder.getInstance(project).getAbout()?.applicationVersion.toString(),
470470
"user.type" to if (isDevUser) "internal" else "external"
471471
)
472472
)

ide-common/src/main/kotlin/org/digma/intellij/plugin/posthog/ServerVersionMonitor.kt

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)