Skip to content

Commit 923a312

Browse files
Add global messages to get insight stats and change environment (#1992)
* Add `GLOBAL/GET_INSIGHT_STATS`/`GLOBAL/SET_INSIGHT_STATS` messages * Update Main UI * Update Navigation UI * Fix message * Update Navigation UI * Publish insight stats event to all messages Deprecate obsolete messages for Navigation app Add global message to change environment --------- Co-authored-by: Kyrylo Shmidt <[email protected]>
1 parent 3b22711 commit 923a312

File tree

11 files changed

+122
-46
lines changed

11 files changed

+122
-46
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.digma.intellij.plugin.analytics
2+
3+
import com.fasterxml.jackson.databind.JsonNode
4+
import com.intellij.util.messages.Topic
5+
import org.digma.intellij.plugin.model.rest.navigation.CodeLocation
6+
import org.digma.intellij.plugin.scope.SpanScope
7+
8+
interface InsightStatsChangedEvent {
9+
companion object {
10+
@JvmStatic
11+
@Topic.ProjectLevel
12+
val INSIGHT_STATS_CHANGED_TOPIC: Topic<InsightStatsChangedEvent> = Topic.create(
13+
"INSIGHT STATS CHANGED",
14+
InsightStatsChangedEvent::class.java
15+
)
16+
}
17+
18+
fun insightStatsChanged(scope: JsonNode?, analyticsInsightsCount: Int, issuesInsightsCount: Int, unreadInsightsCount: Int)
19+
}

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/BaseMessageRouterHandler.kt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.cef.browser.CefFrame
1212
import org.cef.callback.CefQueryCallback
1313
import org.cef.handler.CefMessageRouterHandlerAdapter
1414
import org.digma.intellij.plugin.analytics.AnalyticsService
15+
import org.digma.intellij.plugin.analytics.InsightStatsChangedEvent
1516
import org.digma.intellij.plugin.common.Backgroundable
1617
import org.digma.intellij.plugin.common.EDT
1718
import org.digma.intellij.plugin.common.createObjectMapper
@@ -24,6 +25,7 @@ import org.digma.intellij.plugin.log.Log
2425
import org.digma.intellij.plugin.model.rest.navigation.CodeLocation
2526
import org.digma.intellij.plugin.navigation.MainContentViewSwitcher
2627
import org.digma.intellij.plugin.posthog.ActivityMonitor
28+
import org.digma.intellij.plugin.scope.ScopeChangedEvent
2729
import org.digma.intellij.plugin.scope.ScopeManager
2830
import org.digma.intellij.plugin.scope.SpanScope
2931
import org.digma.intellij.plugin.ui.MainToolWindowCardsController
@@ -187,7 +189,6 @@ abstract class BaseMessageRouterHandler(protected val project: Project) : Common
187189
ToolWindowShower.getInstance(project).showToolWindow()
188190
}
189191
}
190-
191192
}
192193

193194
JCEFGlobalConstants.GLOBAL_CHANGE_SCOPE -> {
@@ -207,6 +208,29 @@ abstract class BaseMessageRouterHandler(protected val project: Project) : Common
207208
getState(browser)
208209
}
209210

211+
JCEFGlobalConstants.GLOBAL_GET_INSIGHT_STATS -> {
212+
val payload = getPayloadFromRequest(requestJsonNode)
213+
payload?.let {
214+
val scopeNode = payload.get("scope");
215+
if (scopeNode is NullNode){
216+
var stats = AnalyticsService.getInstance(project).getInsightsStats(null);
217+
project.messageBus.syncPublisher(InsightStatsChangedEvent.INSIGHT_STATS_CHANGED_TOPIC)
218+
.insightStatsChanged(null, stats.analyticsInsightsCount, stats.issuesInsightsCount, stats.unreadInsightsCount)
219+
} else {
220+
var spanCodeObjectId = scopeNode.get("span").get("spanCodeObjectId").asText()
221+
var stats = AnalyticsService.getInstance(project).getInsightsStats(spanCodeObjectId);
222+
project.messageBus.syncPublisher(InsightStatsChangedEvent.INSIGHT_STATS_CHANGED_TOPIC)
223+
.insightStatsChanged(scopeNode, stats.analyticsInsightsCount, stats.issuesInsightsCount, stats.unreadInsightsCount)
224+
}
225+
226+
227+
}
228+
}
229+
230+
JCEFGlobalConstants.GLOBAL_CHANGE_ENVIRONMENT -> {
231+
changeEnvironment(requestJsonNode)
232+
}
233+
210234

211235
else -> {
212236
val handled = doOnQuery(project, browser, requestJsonNode, request, action)
@@ -235,7 +259,7 @@ abstract class BaseMessageRouterHandler(protected val project: Project) : Common
235259
return true
236260
}
237261

238-
protected fun changeView(requestJsonNode: JsonNode) {
262+
private fun changeView(requestJsonNode: JsonNode) {
239263
val payload = getPayloadFromRequest(requestJsonNode)
240264
payload?.let {
241265
val viewId = payload.get("view")?.asText()
@@ -290,7 +314,7 @@ abstract class BaseMessageRouterHandler(protected val project: Project) : Common
290314
)
291315
}
292316

293-
fun changeScope(requestJsonNode: JsonNode) {
317+
private fun changeScope(requestJsonNode: JsonNode) {
294318
val payload = getPayloadFromRequest(requestJsonNode)
295319
payload?.let { pl ->
296320
val span = pl.get("span")
@@ -308,5 +332,11 @@ abstract class BaseMessageRouterHandler(protected val project: Project) : Common
308332
}
309333
}
310334

335+
private fun changeEnvironment(requestJsonNode: JsonNode) {
336+
val environment = getEnvironmentFromPayload(requestJsonNode)
337+
environment?.let { env ->
338+
AnalyticsService.getInstance(project).environment.setCurrent(env)
339+
}
340+
}
311341
}
312342

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/JCEFGlobalConstants.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ object JCEFGlobalConstants {
4545
const val GLOBAL_SET_STATE: String = "GLOBAL/SET_STATE"
4646
const val GLOBAL_UPDATE_STATE: String = "GLOBAL/UPDATE_STATE"
4747
const val GLOBAL_GET_STATE: String = "GLOBAL/GET_STATE"
48+
const val GLOBAL_GET_INSIGHT_STATS: String = "GLOBAL/GET_INSIGHT_STATS"
49+
const val GLOBAL_SET_INSIGHT_STATS: String = "GLOBAL/SET_INSIGHT_STATS"
50+
const val GLOBAL_CHANGE_ENVIRONMENT: String = "GLOBAL/CHANGE_ENVIRONMENT"
4851
}

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/JCefComponent.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.cef.handler.CefLifeSpanHandlerAdapter
1717
import org.digma.intellij.plugin.analytics.AnalyticsService
1818
import org.digma.intellij.plugin.analytics.AnalyticsServiceConnectionEvent
1919
import org.digma.intellij.plugin.analytics.EnvironmentChanged
20+
import org.digma.intellij.plugin.analytics.InsightStatsChangedEvent
2021
import org.digma.intellij.plugin.common.Backgroundable
2122
import org.digma.intellij.plugin.docker.DockerService
2223
import org.digma.intellij.plugin.env.Env
@@ -55,6 +56,7 @@ private constructor(
5556
private val observabilityChangeParentDisposable = Disposer.newDisposable()
5657
private val scopeChangeParentDisposable = Disposer.newDisposable()
5758
private val stateChangeParentDisposable = Disposer.newDisposable()
59+
private val insightStatsChangeParentDisposable = Disposer.newDisposable()
5860

5961

6062
init {
@@ -214,6 +216,18 @@ private constructor(
214216
}
215217
)
216218

219+
project.messageBus.connect(insightStatsChangeParentDisposable).subscribe(
220+
InsightStatsChangedEvent.INSIGHT_STATS_CHANGED_TOPIC, object : InsightStatsChangedEvent {
221+
override fun insightStatsChanged(scope: JsonNode?, analyticsInsightsCount: Int, issuesInsightsCount: Int, unreadInsightsCount: Int) {
222+
try {
223+
sendSetInsightStatsMessage(jbCefBrowser.cefBrowser, scope, analyticsInsightsCount, issuesInsightsCount, unreadInsightsCount)
224+
} catch (e: Throwable) {
225+
ErrorReporter.getInstance().reportError("JCefComponent.insightStatsChanged", e)
226+
}
227+
}
228+
}
229+
)
230+
217231
}
218232

219233

@@ -227,6 +241,7 @@ private constructor(
227241
Disposer.dispose(observabilityChangeParentDisposable)
228242
Disposer.dispose(scopeChangeParentDisposable)
229243
Disposer.dispose(stateChangeParentDisposable)
244+
Disposer.dispose(insightStatsChangeParentDisposable)
230245
jbCefBrowser.jbCefClient.removeLifeSpanHandler(lifeSpanHandler, jbCefBrowser.cefBrowser)
231246
jbCefBrowser.dispose()
232247
cefMessageRouter.dispose()

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/JCefMessagesUtils.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import org.digma.intellij.plugin.ui.jcef.model.SetEnvironmentMessage
2727
import org.digma.intellij.plugin.ui.jcef.model.SetEnvironmentMessagePayload
2828
import org.digma.intellij.plugin.ui.jcef.model.SetEnvironmentsMessage
2929
import org.digma.intellij.plugin.ui.jcef.model.SetEnvironmentsMessagePayload
30+
import org.digma.intellij.plugin.ui.jcef.model.SetInsightStatsMessage
31+
import org.digma.intellij.plugin.ui.jcef.model.SetInsightStatsMessagePayload
3032
import org.digma.intellij.plugin.ui.jcef.model.SetIsMicrometerMessage
3133
import org.digma.intellij.plugin.ui.jcef.model.SetScopeMessage
3234
import org.digma.intellij.plugin.ui.jcef.model.SetScopeMessagePayload
@@ -187,4 +189,16 @@ fun sendCurrentViewsState(cefBrowser: CefBrowser, action: String, views: List<Vi
187189
cefBrowser,
188190
SetViewMessage(action, SetViewMessagePayload(views, isTriggeredByJcef))
189191
)
192+
}
193+
194+
fun sendSetInsightStatsMessage(
195+
cefBrowser: CefBrowser,
196+
scope: JsonNode?,
197+
analyticsInsightsCount: Number,
198+
issuesInsightsCount: Number,
199+
unreadInsightsCount: Number
200+
) {
201+
serializeAndExecuteWindowPostMessageJavaScript(
202+
cefBrowser, SetInsightStatsMessage(SetInsightStatsMessagePayload(scope, analyticsInsightsCount, issuesInsightsCount, unreadInsightsCount))
203+
)
190204
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.digma.intellij.plugin.ui.jcef.model
2+
3+
import com.fasterxml.jackson.databind.JsonNode
4+
import org.digma.intellij.plugin.ui.jcef.JCEFGlobalConstants
5+
6+
7+
data class SetInsightStatsMessage(val payload: SetInsightStatsMessagePayload) {
8+
val type = JCEFGlobalConstants.REQUEST_MESSAGE_TYPE
9+
val action = JCEFGlobalConstants.GLOBAL_SET_INSIGHT_STATS
10+
}
11+
12+
data class SetInsightStatsMessagePayload(
13+
val scope: JsonNode?,
14+
val analyticsInsightsCount: Number,
15+
val issuesInsightsCount: Number,
16+
val unreadInsightsCount: Number
17+
)

src/main/kotlin/org/digma/intellij/plugin/ui/navigation/NavigationMessageRouterHandler.kt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ class NavigationMessageRouterHandler(project: Project) : BaseMessageRouterHandle
3030

3131
"NAVIGATION/INITIALIZE" -> onInitialize(browser)
3232

33-
"NAVIGATION/CHANGE_VIEW" -> changeView(requestJsonNode)
34-
35-
"NAVIGATION/CHANGE_ENVIRONMENT" -> {
36-
changeEnvironment(requestJsonNode)
37-
}
38-
3933
"NAVIGATION/AUTOFIX_MISSING_DEPENDENCY" -> {
4034
fixMissingDependencies(requestJsonNode)
4135
}
@@ -44,10 +38,6 @@ class NavigationMessageRouterHandler(project: Project) : BaseMessageRouterHandle
4438
addAnnotation(requestJsonNode)
4539
}
4640

47-
"NAVIGATION/CHANGE_SCOPE" -> {
48-
changeScope(requestJsonNode)
49-
}
50-
5141
"NAVIGATION/GO_TO_CODE_LOCATION" -> {
5242
goToCode(requestJsonNode)
5343
}
@@ -92,15 +82,6 @@ class NavigationMessageRouterHandler(project: Project) : BaseMessageRouterHandle
9282
}
9383
}
9484

95-
96-
private fun changeEnvironment(requestJsonNode: JsonNode) {
97-
val environment = getEnvironmentFromPayload(requestJsonNode)
98-
environment?.let { env ->
99-
AnalyticsService.getInstance(project).environment.setCurrent(env)
100-
}
101-
}
102-
103-
10485
private fun onInitialize(browser: CefBrowser) {
10586
try {
10687
doCommonInitialize(browser)

src/main/kotlin/org/digma/intellij/plugin/ui/recentactivity/RecentActivityService.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class RecentActivityService(val project: Project) : Disposable {
9999

100100
val spanId = payload.span.spanCodeObjectId
101101
spanId?.let {
102-
val environmentsSupplier: EnvironmentsSupplier = project.service<AnalyticsService>().environment
103-
environmentsSupplier.setCurrent(payload.environment) {
104-
ScopeManager.getInstance(project).changeScope(SpanScope(spanId))
105-
}
102+
ScopeManager.getInstance(project).changeScope(SpanScope(spanId))
106103
project.service<ActivityMonitor>().registerSpanLinkClicked(MonitoredPanel.RecentActivity)
107104
}
108105

src/main/resources/webview/main/index.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/webview/navigation/index.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)