Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.telemetry

import com.intellij.openapi.Disposable
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.Alarm
import software.aws.toolkits.telemetry.IdeTelemetry

class OpenedFileTypesMetrics : ProjectActivity, Disposable {

Check warning on line 15 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/OpenedFileTypesMetrics.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Extension class should be final and non-public

Extension class should not be public
private val currentOpenedFileTypes = mutableSetOf<String>()
private val alarm = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this)
override suspend fun execute(project: Project) {
// add already open file extensions
FileEditorManager.getInstance(project).openFiles.forEach {
it.extension?.let { openFileExtension -> currentOpenedFileTypes.add(openFileExtension) }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to filter this to project files? (i.e no library code)


// add newly opened file extensions
project.messageBus.connect().subscribe(
FileEditorManagerListener.FILE_EDITOR_MANAGER,
object : FileEditorManagerListener {
override fun fileOpened(source: FileEditorManager, file: VirtualFile) {

file.extension?.let { currentOpenedFileTypes.add(it) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did they want an 'unknown'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file types is now from a specified list of file extensions sourced from the ticket

}
}
)
scheduleNextMetricEvent()
}

private fun scheduleNextMetricEvent() {
alarm.addRequest(this::emitFileTypeMetric, INTERVAL_BETWEEN_METRICS)
}

override fun dispose() {}

private fun emitFileTypeMetric() {
currentOpenedFileTypes.forEach {
IdeTelemetry.editCodeFile(project = null, filenameExt = it)
}
flush()
scheduleNextMetricEvent()
}

private fun flush() {
currentOpenedFileTypes.clear()
}

companion object {
const val INTERVAL_BETWEEN_METRICS = 30 * 60 * 1000
}
}
1 change: 1 addition & 0 deletions plugins/core/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<!-- each plugin needs its own instance of these -->
<applicationService serviceImplementation="migration.software.aws.toolkits.jetbrains.core.coroutines.PluginCoroutineScopeTracker"/>
<projectService serviceImplementation="migration.software.aws.toolkits.jetbrains.core.coroutines.PluginCoroutineScopeTracker"/>
<postStartupActivity implementation="software.aws.toolkits.jetbrains.services.telemetry.OpenedFileTypesMetrics" />
</extensions>
</idea-plugin>
Loading