@@ -14,6 +14,8 @@ import kotlinx.coroutines.launch
1414import org.digma.intellij.plugin.analytics.AnalyticsService
1515import org.digma.intellij.plugin.analytics.AnalyticsServiceException
1616import org.digma.intellij.plugin.common.Retries
17+ import org.digma.intellij.plugin.common.findActiveProject
18+ import org.digma.intellij.plugin.common.isProjectValid
1719import org.digma.intellij.plugin.errorreporting.ErrorReporter
1820import org.digma.intellij.plugin.log.Log
1921import org.digma.intellij.plugin.model.rest.version.PerformanceMetricsResponse
@@ -25,7 +27,7 @@ import kotlin.time.Duration.Companion.minutes
2527
2628class PerformanceMetricsPosthogEventStartupActivity : StartupActivity {
2729 override fun runActivity (project : Project ) {
28- service<ContinuousPerformanceMetricsReporter >().start(project )
30+ service<ContinuousPerformanceMetricsReporter >().start()
2931 }
3032}
3133
@@ -42,10 +44,10 @@ class ContinuousPerformanceMetricsReporter : Disposable {
4244 }
4345
4446
45- fun start (project : Project ) {
47+ fun start () {
4648
4749 if (started.getAndSet(true )) {
48- Log .log(logger::info, " ContinuousPerformanceMetricsReporter.start called for project {} but already started" , project.name )
50+ Log .log(logger::info, " ContinuousPerformanceMetricsReporter.start called for project {} but already started" )
4951 return
5052 }
5153
@@ -55,38 +57,47 @@ class ContinuousPerformanceMetricsReporter : Disposable {
5557 @Suppress(" UnstableApiUsage" )
5658 disposingScope().launch {
5759
58- if (! PersistenceService .getInstance().isFirstTimePerformanceMetrics()) {
59- waitForFirstTime(this , project)
60- // after first time wait 6 hours for next report
61- launchContinuousReport(6 .hours, project)
62- } else {
63- // this will happen on startup, make sure we have a report in 10 minutes and then every 6 hours
64- launchContinuousReport(10 .minutes, project)
60+ delay(2 .minutes.inWholeMilliseconds)
61+
62+ if (isActive) {
63+ if (! PersistenceService .getInstance().isFirstTimePerformanceMetrics()) {
64+ waitForFirstTime(this )
65+ if (isActive) {
66+ // after first time wait 6 hours for next report
67+ launchContinuousReport(6 .hours)
68+ }
69+ } else {
70+ // this will happen on startup, make sure we have a report in 10 minutes and then every 6 hours
71+ launchContinuousReport(10 .minutes)
72+ }
6573 }
6674 }
6775
6876 }
6977
7078
71- private suspend fun waitForFirstTime (coroutineScope : CoroutineScope , project : Project ) {
79+ private suspend fun waitForFirstTime (coroutineScope : CoroutineScope ) {
7280
7381 while (! PersistenceService .getInstance().isFirstTimePerformanceMetrics() && coroutineScope.isActive) {
7482 try {
7583
7684 delay(10 .minutes.inWholeMilliseconds)
7785
78- getAnalyticsService(project).let { analyticsService ->
79- val result: PerformanceMetricsResponse = Retries .retryWithResult({
80- analyticsService.performanceMetrics
81- }, AnalyticsServiceException ::class .java, 30000 , 20 )
82-
83- if (result.metrics.isNotEmpty()) {
84- filterMetrics(result)
85- getActivityMonitor(project).let { activityMonitor ->
86- Log .log(logger::info, " registering first time performance metrics" )
87- activityMonitor.registerPerformanceMetrics(result, true )
88- if (! PersistenceService .getInstance().isFirstTimePerformanceMetrics()) {
89- PersistenceService .getInstance().setFirstTimePerformanceMetrics()
86+ findActiveProject()?.takeIf { isProjectValid(it) }?.let { project ->
87+
88+ getAnalyticsService(project).let { analyticsService ->
89+ val result: PerformanceMetricsResponse = Retries .retryWithResult({
90+ analyticsService.performanceMetrics
91+ }, AnalyticsServiceException ::class .java, 30000 , 20 )
92+
93+ if (result.metrics.isNotEmpty()) {
94+ filterMetrics(result)
95+ getActivityMonitor(project).let { activityMonitor ->
96+ Log .log(logger::info, " registering first time performance metrics" )
97+ activityMonitor.registerPerformanceMetrics(result, true )
98+ if (! PersistenceService .getInstance().isFirstTimePerformanceMetrics()) {
99+ PersistenceService .getInstance().setFirstTimePerformanceMetrics()
100+ }
90101 }
91102 }
92103 }
@@ -95,13 +106,13 @@ class ContinuousPerformanceMetricsReporter : Disposable {
95106 } catch (e: Exception ) {
96107 Log .warnWithException(logger, e, " failed in first time registerPerformanceMetrics" )
97108 ErrorReporter .getInstance()
98- .reportError(project, " PerformanceMetricsPosthogEventStartupActivity.firstTimePerformanceMetrics" , e)
109+ .reportError(" PerformanceMetricsPosthogEventStartupActivity.firstTimePerformanceMetrics" , e)
99110 }
100111 }
101112 }
102113
103114
104- private fun launchContinuousReport (nextReport : Duration , project : Project ) {
115+ private fun launchContinuousReport (nextReport : Duration ) {
105116
106117 @Suppress(" UnstableApiUsage" )
107118 disposingScope().launch {
@@ -110,18 +121,24 @@ class ContinuousPerformanceMetricsReporter : Disposable {
110121
111122 while (isActive) {
112123 try {
113- getAnalyticsService(project).let { analyticsService ->
114- val result: PerformanceMetricsResponse = Retries .retryWithResult({
115- analyticsService.performanceMetrics
116- }, AnalyticsServiceException ::class .java, 30000 , 20 )
117124
118- if (result.metrics.isNotEmpty()) {
119- filterMetrics(result)
120- Log .log(logger::info, " registering continuous performance metrics" )
121- getActivityMonitor(project).registerPerformanceMetrics(result, false )
125+ findActiveProject()?.takeIf { isProjectValid(it) }?.let { project ->
126+
127+ getAnalyticsService(project).let { analyticsService ->
128+ val result: PerformanceMetricsResponse = Retries .retryWithResult({
129+ analyticsService.performanceMetrics
130+ }, AnalyticsServiceException ::class .java, 30000 , 20 )
131+
132+ if (result.metrics.isNotEmpty()) {
133+ filterMetrics(result)
134+ Log .log(logger::info, " registering continuous performance metrics" )
135+ getActivityMonitor(project).registerPerformanceMetrics(result, false )
136+ }
122137 }
138+
123139 }
124140
141+
125142 delay(6 .hours.inWholeMilliseconds)
126143
127144 } catch (e: Exception ) {
0 commit comments