1818
1919import java .util .ArrayList ;
2020import java .util .List ;
21+ import java .util .concurrent .TimeUnit ;
2122
2223public 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 -> {
0 commit comments