Skip to content

Commit 1973210

Browse files
Fixed backend connection monitor (#406)
1 parent 8b790f8 commit 1973210

File tree

5 files changed

+96
-63
lines changed

5 files changed

+96
-63
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/refreshInsightsTask/RefreshService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import com.intellij.openapi.vfs.VirtualFile
1010
import com.intellij.util.RunnableCallable
1111
import com.intellij.util.concurrency.NonUrgentExecutor
1212
import org.apache.commons.collections4.CollectionUtils
13+
import org.digma.intellij.plugin.analytics.BackendConnectionUtil
1314
import org.digma.intellij.plugin.common.Backgroundable
1415
import org.digma.intellij.plugin.document.DocumentInfoContainer
1516
import org.digma.intellij.plugin.document.DocumentInfoService
1617
import org.digma.intellij.plugin.log.Log
1718
import org.digma.intellij.plugin.model.rest.insights.CodeObjectInsight
19+
import org.digma.intellij.plugin.ui.model.DocumentScope
20+
import org.digma.intellij.plugin.ui.model.EmptyScope
1821
import org.digma.intellij.plugin.ui.model.MethodScope
1922
import org.digma.intellij.plugin.ui.service.ErrorsViewService
2023
import org.digma.intellij.plugin.ui.service.InsightsViewService
@@ -27,6 +30,7 @@ class RefreshService(private val project: Project) {
2730
private val errorsViewService: ErrorsViewService = project.getService(ErrorsViewService::class.java)
2831
private val insightsViewService: InsightsViewService = project.getService(InsightsViewService::class.java)
2932
private val documentInfoService: DocumentInfoService = project.getService(DocumentInfoService::class.java)
33+
private val backendConnectionUtil = project.getService(BackendConnectionUtil::class.java)
3034
private val refreshInsightsTaskScheduledLock: ReentrantLock = ReentrantLock()
3135
private val isGeneralRefreshButtonEnabled = AtomicBoolean(true)
3236

@@ -49,6 +53,9 @@ class RefreshService(private val project: Project) {
4953
Log.log(logger::debug, "updateInsightsCacheForActiveDocument starts for file = {}", file.name)
5054
updateInsightsCacheForActiveDocument(selectedTextEditor, documentInfoContainer, scope)
5155
Log.log(logger::debug, "updateInsightsCacheForActiveDocument finished for file = {}", file.name)
56+
} else if (scope is DocumentScope || scope is EmptyScope) {
57+
Log.log(logger::debug, "testConnectionToBackend was triggered")
58+
backendConnectionUtil.testConnectionToBackend()
5259
}
5360
}
5461

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ class BackendConnectionMonitor(val project: Project) : Disposable, AnalyticsServ
5151
connectionOk()
5252
}
5353

54-
5554
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.digma.intellij.plugin.analytics
2+
3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.intellij.openapi.project.Project
5+
import org.digma.intellij.plugin.log.Log
6+
import org.digma.intellij.plugin.refreshInsightsTask.RefreshService
7+
import org.digma.intellij.plugin.ui.model.environment.EnvironmentsSupplier
8+
import org.digma.intellij.plugin.ui.service.ErrorsViewService
9+
import org.digma.intellij.plugin.ui.service.InsightsViewService
10+
import org.digma.intellij.plugin.ui.service.SummaryViewService
11+
12+
class BackendConnectionUtil(project: Project) {
13+
private val logger: Logger = Logger.getInstance(BackendConnectionUtil::class.java)
14+
15+
private val backendConnectionMonitor: BackendConnectionMonitor
16+
private var insightsViewService: InsightsViewService
17+
private var errorsViewService: ErrorsViewService
18+
private var summaryViewService: SummaryViewService
19+
private var environmentsSupplier: EnvironmentsSupplier
20+
21+
companion object {
22+
fun getInstance(project: Project): RefreshService {
23+
return project.getService(RefreshService::class.java)
24+
}
25+
}
26+
27+
init {
28+
backendConnectionMonitor = project.getService(BackendConnectionMonitor::class.java)
29+
insightsViewService = project.getService(InsightsViewService::class.java)
30+
errorsViewService = project.getService(ErrorsViewService::class.java)
31+
summaryViewService = project.getService(SummaryViewService::class.java)
32+
val analyticsService = project.getService(AnalyticsService::class.java)
33+
environmentsSupplier = analyticsService.environment
34+
}
35+
36+
private var hadConnectionError = false
37+
38+
fun testConnectionToBackend(): Boolean {
39+
40+
//refresh will run in the background.
41+
//if there is currently no connection, but connection will recover during this refresh call then
42+
//not sure backendConnectionMonitor will catch it so the contextChange flow may still block.
43+
//the next contextChange will pass.
44+
//but anyway if the connection will recover an environmentChanged event will fire and that should have some kind
45+
//of hook to intentionally cause a contextChange event.
46+
environmentsSupplier.refresh()
47+
48+
//hadConnectionError helps to call contextEmptyNoConnection() only once on the first time that a connection error is discovered.
49+
//and there is no need to empty again or change the models and ui until the connection is back.
50+
return if (backendConnectionMonitor.isConnectionError()) {
51+
if (hadConnectionError) {
52+
Log.log(logger::debug, "Not Executing contextChanged because there is no connection to backend service")
53+
return false
54+
}
55+
contextEmptyNoConnection()
56+
hadConnectionError = true
57+
false
58+
} else {
59+
hadConnectionError = false
60+
true
61+
}
62+
}
63+
64+
private fun contextEmptyNoConnection() {
65+
Log.log(logger::debug, "contextEmptyNoConnection called")
66+
insightsViewService.empty()
67+
errorsViewService.empty()
68+
summaryViewService.empty()
69+
}
70+
71+
}

src/main/java/org/digma/intellij/plugin/service/EditorInteractionService.java

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
import com.intellij.openapi.diagnostic.Logger;
55
import com.intellij.openapi.project.Project;
66
import org.apache.commons.lang3.time.StopWatch;
7-
import org.digma.intellij.plugin.analytics.AnalyticsService;
8-
import org.digma.intellij.plugin.analytics.BackendConnectionMonitor;
7+
import org.digma.intellij.plugin.analytics.BackendConnectionUtil;
98
import org.digma.intellij.plugin.common.Backgroundable;
109
import org.digma.intellij.plugin.document.DocumentInfoContainer;
1110
import org.digma.intellij.plugin.document.DocumentInfoService;
1211
import org.digma.intellij.plugin.log.Log;
1312
import org.digma.intellij.plugin.model.discovery.MethodInfo;
1413
import org.digma.intellij.plugin.model.discovery.MethodUnderCaret;
1514
import org.digma.intellij.plugin.ui.CaretContextService;
16-
import org.digma.intellij.plugin.ui.model.environment.EnvironmentsSupplier;
1715
import org.digma.intellij.plugin.ui.service.ErrorsViewService;
1816
import org.digma.intellij.plugin.ui.service.InsightsViewService;
19-
import org.digma.intellij.plugin.ui.service.SummaryViewService;
2017

2118
import java.util.ArrayList;
2219

@@ -32,10 +29,8 @@ public class EditorInteractionService implements CaretContextService, Disposable
3229

3330
private final InsightsViewService insightsViewService;
3431
private final ErrorsViewService errorsViewService;
35-
private final SummaryViewService summaryViewService;
3632
private final DocumentInfoService documentInfoService;
37-
private final EnvironmentsSupplier environmentsSupplier;
38-
private final BackendConnectionMonitor backendConnectionMonitor;
33+
private final BackendConnectionUtil backendConnectionUtil;
3934

4035

4136
/*
@@ -47,61 +42,28 @@ public EditorInteractionService(Project project) {
4742
this.project = project;
4843
insightsViewService = project.getService(InsightsViewService.class);
4944
errorsViewService = project.getService(ErrorsViewService.class);
50-
summaryViewService = project.getService(SummaryViewService.class);
5145
documentInfoService = project.getService(DocumentInfoService.class);
52-
backendConnectionMonitor = project.getService(BackendConnectionMonitor.class);
53-
var analyticsService = project.getService(AnalyticsService.class);
54-
environmentsSupplier = analyticsService.getEnvironment();
46+
backendConnectionUtil = project.getService(BackendConnectionUtil.class);
5547
}
5648

5749
public static CaretContextService getInstance(Project project) {
5850
return project.getService(CaretContextService.class);
5951
}
6052

61-
62-
boolean hadConnectionError = false;
63-
64-
private boolean testConnectionToBackend() {
65-
66-
//refresh will run in the background.
67-
//if there is currently no connection, but connection will recover during this refresh call then
68-
//not sure backendConnectionMonitor will catch it so the contextChange flow may still block.
69-
//the next contextChange will pass.
70-
//but anyway if the connection will recover an environmentChanged event will fire and that should have some kind
71-
//of hook to intentionally cause a contextChange event.
72-
environmentsSupplier.refresh();
73-
74-
//hadConnectionError helps to call contextEmptyNoConnection() only once on the first time that a connection error is discovered.
75-
//and there is no need to empty again or change the models and ui until the connection is back.
76-
if (backendConnectionMonitor.isConnectionError()) {
77-
if (hadConnectionError) {
78-
Log.log(logger::debug, "Not Executing contextChanged because there is no connection to backend service");
79-
return false;
80-
}
81-
contextEmptyNoConnection();
82-
hadConnectionError = true;
83-
return false;
84-
} else {
85-
hadConnectionError = false;
86-
return true;
87-
}
88-
}
89-
90-
9153
@Override
9254
public void contextChanged(MethodUnderCaret methodUnderCaret) {
9355

9456
Log.log(logger::debug, "contextChanged called for '{}'", methodUnderCaret);
9557

96-
if (project.isDisposed()){
58+
if (project.isDisposed()) {
9759
Log.log(logger::debug, "project is disposed in contextChanged for '{}'", methodUnderCaret.getId());
9860
return;
9961
}
10062

10163
//There is no need to execute the contextChanged flow if there is no connection to the backend.
10264
// so testConnectionToBackend will detect a backend connection error , call contextEmptyNoConnection once
10365
// to clean the views, and will return. and will keep blocking until the connection is regained.
104-
if (!testConnectionToBackend()) {
66+
if (!backendConnectionUtil.testConnectionToBackend()) {
10567
Log.log(logger::debug, "No connection to backend, not executing contextChanged for '{}'", methodUnderCaret.getId());
10668
return;
10769
}
@@ -120,7 +82,6 @@ public void contextChanged(MethodUnderCaret methodUnderCaret) {
12082
}
12183

12284

123-
12485
private void contextChangedImpl(MethodUnderCaret methodUnderCaret) {
12586

12687
/*
@@ -130,28 +91,28 @@ private void contextChangedImpl(MethodUnderCaret methodUnderCaret) {
13091

13192
Log.log(logger::debug, "contextChangedImpl invoked for '{}'", methodUnderCaret);
13293

133-
if (!methodUnderCaret.isSupportedFile()){
94+
if (!methodUnderCaret.isSupportedFile()) {
13495
Log.log(logger::debug, "methodUnderCaret is non supported file {}. ", methodUnderCaret);
13596
contextEmptyNonSupportedFile(methodUnderCaret.getFileUri());
136-
}else if (methodUnderCaret.getId().isBlank()) {
97+
} else if (methodUnderCaret.getId().isBlank()) {
13798
Log.log(logger::debug, "No id in methodUnderCaret,trying fileUri {}. ", methodUnderCaret);
13899
//if no id then try to show a preview for the document
139-
if (methodUnderCaret.getFileUri().isBlank()){
100+
if (methodUnderCaret.getFileUri().isBlank()) {
140101
Log.log(logger::debug, "No id and no fileUri in methodUnderCaret,clearing context {}. ", methodUnderCaret);
141102
contextEmpty();
142-
}else{
103+
} else {
143104
Log.log(logger::debug, "Showing document preview for {}. ", methodUnderCaret);
144-
DocumentInfoContainer documentInfoContainer = documentInfoService.getDocumentInfo(methodUnderCaret);
145-
if (documentInfoContainer == null){
105+
DocumentInfoContainer documentInfoContainer = documentInfoService.getDocumentInfo(methodUnderCaret);
106+
if (documentInfoContainer == null) {
146107
Log.log(logger::debug, "Could not find document info for {}, Showing empty preview.", methodUnderCaret);
147-
}else{
148-
Log.log(logger::debug, "Found document info for {}. document: {}", methodUnderCaret,documentInfoContainer.getPsiFile());
108+
} else {
109+
Log.log(logger::debug, "Found document info for {}. document: {}", methodUnderCaret, documentInfoContainer.getPsiFile());
149110
}
150111

151-
insightsViewService.showDocumentPreviewList(documentInfoContainer,methodUnderCaret.getFileUri());
152-
errorsViewService.showDocumentPreviewList(documentInfoContainer,methodUnderCaret.getFileUri());
112+
insightsViewService.showDocumentPreviewList(documentInfoContainer, methodUnderCaret.getFileUri());
113+
errorsViewService.showDocumentPreviewList(documentInfoContainer, methodUnderCaret.getFileUri());
153114
}
154-
}else{
115+
} else {
155116
MethodInfo methodInfo = documentInfoService.getMethodInfo(methodUnderCaret);
156117
if (methodInfo == null) {
157118
Log.log(logger::warn, "Could not find MethodInfo for MethodUnderCaret {}. ", methodUnderCaret);
@@ -186,13 +147,6 @@ public void contextEmpty() {
186147
errorsViewService.empty();
187148
}
188149

189-
private void contextEmptyNoConnection() {
190-
Log.log(logger::debug, "contextEmptyNoConnection called");
191-
insightsViewService.empty();
192-
errorsViewService.empty();
193-
summaryViewService.empty();
194-
}
195-
196150
@Override
197151
public void dispose() {
198152
Log.log(logger::debug, "disposing..");

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474

7575
<projectService serviceImplementation="org.digma.intellij.plugin.analytics.BackendConnectionMonitor"/>
7676

77+
<projectService serviceImplementation="org.digma.intellij.plugin.analytics.BackendConnectionUtil"/>
78+
7779
<projectService serviceImplementation="org.digma.intellij.plugin.psi.LanguageServiceLocator"/>
7880

7981
<projectService serviceImplementation="org.digma.intellij.plugin.common.IDEUtilsService"/>

0 commit comments

Comments
 (0)