Skip to content

Commit e9dd928

Browse files
Configure Insights caching #232
1 parent 6536255 commit e9dd928

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/document/DocumentInfoContainer.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ public void refresh() {
7272
loadAllInsightsForCurrentDocument();
7373
}
7474

75-
public List<CodeObjectInsight> getMethodInsightsFromCache(@NotNull MethodInfo methodInfo) {
76-
Log.log(LOGGER::debug, "Requesting insights for MethodInfo {}", methodInfo.getId());
77-
78-
return insights.stream().filter(codeObjectInsight -> {
79-
String codeObjectId = codeObjectInsight.getCodeObjectId();
80-
return methodInfo.getId().equals(codeObjectId)
81-
|| methodInfo.idWithType().equals(codeObjectId)
82-
|| methodInfo.getRelatedCodeObjectIds().contains(codeObjectId)
83-
|| methodInfo.getRelatedCodeObjectIdsWithType().contains(codeObjectId);
84-
}).collect(Collectors.toList());
85-
}
86-
8775
private void loadAllInsightsForCurrentDocument() {
8876
List<String> objectIds = getObjectIdsForCurrentDocument();
8977
try {

ide-common/src/main/java/org/digma/intellij/plugin/document/DocumentInfoService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import org.digma.intellij.plugin.model.discovery.DocumentInfo;
1111
import org.digma.intellij.plugin.model.discovery.MethodInfo;
1212
import org.digma.intellij.plugin.model.discovery.MethodUnderCaret;
13+
import org.digma.intellij.plugin.model.rest.insights.CodeObjectInsight;
1314
import org.digma.intellij.plugin.psi.PsiUtils;
1415
import org.jetbrains.annotations.NotNull;
1516
import org.jetbrains.annotations.Nullable;
1617

1718
import java.util.*;
19+
import java.util.stream.Collectors;
1820

1921
/**
2022
* DocumentInfoService holds the open documents containers and has various services to query the documents containers.
@@ -107,6 +109,23 @@ public void removeDocumentInfo(@NotNull PsiFile psiFile) {
107109
documents.remove(PsiUtils.psiFileToUri(psiFile));
108110
}
109111

112+
@NotNull
113+
public List<CodeObjectInsight> getCachedMethodInsights(@NotNull MethodInfo methodInfo) {
114+
Log.log(LOGGER::debug, "Requesting cached insights for MethodInfo {}", methodInfo.getId());
115+
116+
DocumentInfoContainer documentInfoContainer = documents.get(methodInfo.getContainingFileUri());
117+
if (documentInfoContainer != null) {
118+
return documentInfoContainer.getAllInsights().stream().filter(codeObjectInsight -> {
119+
String codeObjectId = codeObjectInsight.getCodeObjectId();
120+
return methodInfo.getId().equals(codeObjectId)
121+
|| methodInfo.idWithType().equals(codeObjectId)
122+
|| methodInfo.getRelatedCodeObjectIds().contains(codeObjectId)
123+
|| methodInfo.getRelatedCodeObjectIdsWithType().contains(codeObjectId);
124+
}).collect(Collectors.toList());
125+
}
126+
return new ArrayList<>();
127+
}
128+
110129
@Nullable
111130
public DocumentInfoContainer getDocumentInfo(PsiFile psiFile) {
112131
return documents.get(PsiUtils.psiFileToUri(psiFile));

ide-common/src/main/java/org/digma/intellij/plugin/insights/InsightsProvider.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.concurrent.TimeUnit;
2122

2223
public class InsightsProvider {
2324

@@ -36,15 +37,38 @@ public InsightsProvider(Project project) {
3637
}
3738

3839
public InsightsListContainer getInsights(@NotNull MethodInfo methodInfo) {
40+
List<? extends CodeObjectInsight> upToDateInsightsList;
41+
List<String> objectIds = getObjectIds(methodInfo);
42+
Log.log(LOGGER::debug, "Got following code object ids for method {}: {}", methodInfo.getId(), objectIds);
43+
var stopWatch = StopWatch.createStarted();
44+
try {
45+
upToDateInsightsList = analyticsService.getInsights(objectIds);
46+
} catch (AnalyticsServiceException e) {
47+
//if analyticsService.getInsights throws exception it means insights could not be loaded, usually when
48+
//the backend is not available. return an empty InsightsListContainer to keep everything running and don't
49+
//crash the plugin. don't log the exception, it was logged in AnalyticsService, keep the log quite because
50+
//it may happen many times.
51+
Log.log(LOGGER::debug, "AnalyticsServiceException for getInsights for {}: {}", methodInfo.getId(), e.getMessage());
52+
return new InsightsListContainer();
53+
} finally {
54+
stopWatch.stop();
55+
Log.log(LOGGER::debug, "getInsights time took {} milliseconds", stopWatch.getTime(TimeUnit.MILLISECONDS));
56+
}
57+
return getInsightsListContainer(methodInfo, upToDateInsightsList);
58+
}
3959

40-
List<String> objectIds = new ArrayList<>();
41-
objectIds.add(methodInfo.idWithType());
42-
objectIds.addAll(methodInfo.getRelatedCodeObjectIdsWithType());
60+
public InsightsListContainer getCachedInsights(@NotNull MethodInfo methodInfo) {
61+
List<? extends CodeObjectInsight> cachedMethodInsights = documentInfoService.getCachedMethodInsights(methodInfo);
62+
return getInsightsListContainer(methodInfo, cachedMethodInsights);
63+
}
64+
65+
public InsightsListContainer getInsightsListContainer(@NotNull MethodInfo methodInfo, List<? extends CodeObjectInsight> insightsList) {
66+
List<String> objectIds = getObjectIds(methodInfo);
4367
Log.log(LOGGER::debug, "Got following code object ids for method {}: {}",methodInfo.getId(), objectIds);
4468
var stopWatch = StopWatch.createStarted();
4569

4670
try {
47-
List<? extends CodeObjectInsight> codeObjectInsights = analyticsService.getInsights(objectIds);
71+
List<? extends CodeObjectInsight> codeObjectInsights = insightsList;
4872
codeObjectInsights = filterUnmapped(codeObjectInsights);
4973
Log.log(LOGGER::debug, "CodeObjectInsights for {}: {}", methodInfo.getId(), codeObjectInsights);
5074
final UsageStatusResult usageStatus = analyticsService.getUsageStatus(objectIds);
@@ -53,18 +77,25 @@ public InsightsListContainer getInsights(@NotNull MethodInfo methodInfo) {
5377
Log.log(LOGGER::debug, "ListViewItems for {}: {}", methodInfo.getId(), listViewItems);
5478
return new InsightsListContainer(listViewItems, codeObjectInsights.size(), usageStatus);
5579
} catch (AnalyticsServiceException e) {
56-
//if analyticsService.getInsights throws exception it means insights could not be loaded, usually when
80+
//if analyticsService.getUsageStatus throws exception it means usageStatus could not be loaded, usually when
5781
//the backend is not available. return an empty InsightsListContainer to keep everything running and don't
5882
//crash the plugin. don't log the exception, it was logged in AnalyticsService, keep the log quite because
5983
//it may happen many times.
60-
Log.log(LOGGER::debug, "AnalyticsServiceException for getInsights for {}: {}", methodInfo.getId(), e.getMessage());
84+
Log.log(LOGGER::debug, "AnalyticsServiceException for getUsageStatus for {}: {}", methodInfo.getId(), e.getMessage());
6185
return new InsightsListContainer();
6286
} finally {
6387
stopWatch.stop();
64-
Log.log(LOGGER::debug, "getInsights time took {} milliseconds", stopWatch.getTime(java.util.concurrent.TimeUnit.MILLISECONDS));
88+
Log.log(LOGGER::debug, "getUsageStatus time took {} milliseconds", stopWatch.getTime(TimeUnit.MILLISECONDS));
6589
}
6690
}
6791

92+
private List<String> getObjectIds(@NotNull MethodInfo methodInfo) {
93+
List<String> objectIds = new ArrayList<>();
94+
objectIds.add(methodInfo.idWithType());
95+
objectIds.addAll(methodInfo.getRelatedCodeObjectIdsWithType());
96+
return objectIds;
97+
}
98+
6899
private List<? extends CodeObjectInsight> filterUnmapped(List<? extends CodeObjectInsight> codeObjectInsights) {
69100
var filteredInsights = new ArrayList<CodeObjectInsight>();
70101
codeObjectInsights.forEach(codeObjectInsight -> {

ide-common/src/main/kotlin/org/digma/intellij/plugin/ui/service/InsightsViewService.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ class InsightsViewService(project: Project) : AbstractViewService(project) {
4646
fun contextChanged(
4747
methodInfo: MethodInfo
4848
) {
49+
updateInsightsModel(methodInfo, insightsProvider.getCachedInsights(methodInfo))
50+
}
4951

52+
private fun updateInsightsModel(
53+
methodInfo: MethodInfo,
54+
insightsListContainer: InsightsListContainer
55+
) {
5056
lock.lock()
51-
Log.log(logger::debug, "Lock acquired for contextChanged to {}. ", methodInfo)
57+
Log.log(logger::debug, "Lock acquired for updateInsightsModel to {}. ", methodInfo)
5258
try {
53-
Log.log(logger::debug, "contextChanged to {}. ", methodInfo)
54-
55-
val insightsListContainer: InsightsListContainer = insightsProvider.getInsights(methodInfo)
59+
Log.log(logger::debug, "updateInsightsModel to {}. ", methodInfo)
5660

5761
model.listViewItems = insightsListContainer.listViewItems
5862
model.previewListViewItems = ArrayList()
@@ -65,7 +69,7 @@ class InsightsViewService(project: Project) : AbstractViewService(project) {
6569
} finally {
6670
if (lock.isHeldByCurrentThread) {
6771
lock.unlock()
68-
Log.log(logger::debug, "Lock released for contextChanged to {}. ", methodInfo)
72+
Log.log(logger::debug, "Lock released for updateInsightsModel to {}. ", methodInfo)
6973
}
7074
}
7175
}
@@ -169,7 +173,7 @@ class InsightsViewService(project: Project) : AbstractViewService(project) {
169173
val scope = model.scope
170174
if (scope is MethodScope) {
171175
Backgroundable.ensureBackground(project, "Refresh insights list") {
172-
contextChanged(scope.getMethodInfo())
176+
updateInsightsModel(scope.getMethodInfo(), insightsProvider.getInsights(scope.getMethodInfo()))
173177
}
174178
}
175179
}

0 commit comments

Comments
 (0)