Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/INTELLIJ-172-spr…
Browse files Browse the repository at this point in the history
…ing-match
  • Loading branch information
himanshusinghs committed Jan 14, 2025
2 parents d76a2bc + 09a1ec1 commit 1d92ebf
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ MongoDB plugin for IntelliJ IDEA.

### Added
* [INTELLIJ-172](https://jira.mongodb.org/browse/INTELLIJ-172) Add support for parsing and inspecting an aggregation and a match stage written in Spring Criteria
* [INTELLIJ-179](https://jira.mongodb.org/browse/INTELLIJ-179) Telemetry when Create Index intention is clicked.
It can be disabled in the Plugin settings.
* [INTELLIJ-178](https://jira.mongodb.org/browse/INTELLIJ-178) Telemetry when the Run Query button is clicked.
It can be disabled in the Plugin settings.
* [INTELLIJ-153](https://jira.mongodb.org/browse/INTELLIJ-153) Add support for parsing, linting and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import com.mongodb.jbplugin.linting.IndexCheckWarning
import com.mongodb.jbplugin.linting.IndexCheckingLinter
import com.mongodb.jbplugin.meta.service
import com.mongodb.jbplugin.mql.Node
import com.mongodb.jbplugin.observability.probe.CreateIndexIntentionProbe
import kotlinx.coroutines.CoroutineScope

/**
* @param coroutineScope
*/
@Suppress("MISSING_KDOC_TOP_LEVEL")
class IndexCheckInspectionBridge(coroutineScope: CoroutineScope) :
AbstractMongoDbInspectionBridge(
coroutineScope,
Expand All @@ -50,12 +50,7 @@ internal object IndexCheckLinterInspection : MongoDbInspection {
}

val readModelProvider by query.source.project.service<DataGripBasedReadModelProvider>()
val result =
IndexCheckingLinter.lintQuery(
dataSource,
readModelProvider,
query,
)
val result = IndexCheckingLinter.lintQuery(dataSource, readModelProvider, query)

result.warnings.forEach {
when (it) {
Expand Down Expand Up @@ -86,6 +81,9 @@ internal object IndexCheckLinterInspection : MongoDbInspection {
),
localDataSource
) {
val createIndexClicked by query.source.project.service<CreateIndexIntentionProbe>()
createIndexClicked.intentionClicked(query)

MongoshDialect.formatter.indexCommandForQuery(query)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ enum class TelemetryProperty(
COMMAND("command"),
CONSOLE("console"),
TRIGGER_LOCATION("trigger_location"),
SIGNAL_TYPE("signal_type"),
}

enum class SignalType(
@Pattern("[a-z_]+")
val publicName: String
) {
MISSING_INDEX("missing_index")
}

/**
Expand Down Expand Up @@ -208,4 +216,15 @@ sealed class TelemetryEvent(
CONTEXT_MENU
}
}

class CreateIndexIntentionEvent(
dialect: HasSourceDialect.DialectName
) : TelemetryEvent(
name = "CreateIndex",
properties =
mapOf(
TelemetryProperty.DIALECT to dialect.name.lowercase(),
TelemetryProperty.SIGNAL_TYPE to SignalType.MISSING_INDEX.publicName,
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mongodb.jbplugin.observability.probe

import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.psi.PsiElement
import com.mongodb.jbplugin.meta.service
import com.mongodb.jbplugin.mql.Node
import com.mongodb.jbplugin.mql.components.HasSourceDialect
import com.mongodb.jbplugin.observability.TelemetryEvent
import com.mongodb.jbplugin.observability.TelemetryService
import com.mongodb.jbplugin.observability.useLogMessage

private val logger: Logger = logger<CreateIndexIntentionProbe>()

/**
* This probe is emitted when the user clicks on the Create Index
* intention.
*/
@Service
class CreateIndexIntentionProbe {
fun intentionClicked(query: Node<PsiElement>) {
val telemetry by service<TelemetryService>()

var dialect = query.component<HasSourceDialect>() ?: return
val event = TelemetryEvent.CreateIndexIntentionEvent(dialect.name)
telemetry.sendEvent(event)

logger.info(
useLogMessage("Create index quick action clicked.")
.mergeTelemetryEventProperties(event)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mongodb.jbplugin.observability.probe

import com.intellij.openapi.application.Application
import com.intellij.psi.PsiElement
import com.mongodb.jbplugin.fixtures.IntegrationTest
import com.mongodb.jbplugin.fixtures.mockLogMessage
import com.mongodb.jbplugin.fixtures.withMockedService
import com.mongodb.jbplugin.mql.Node
import com.mongodb.jbplugin.mql.components.HasSourceDialect
import com.mongodb.jbplugin.observability.TelemetryEvent
import com.mongodb.jbplugin.observability.TelemetryService
import org.junit.jupiter.api.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

@IntegrationTest
internal class CreateIndexIntentionProbeTest {
@Test
fun `should send a CreateIndexIntentionEvent event`(application: Application) {
val telemetryService = mock<TelemetryService>()
val dialect = HasSourceDialect.DialectName.entries.toTypedArray().random()

val query = Node<PsiElement?>(null, listOf(HasSourceDialect(dialect))) as Node<PsiElement>

application.withMockedService(telemetryService)
.withMockedService(mockLogMessage())

val probe = CreateIndexIntentionProbe()

probe.intentionClicked(query)

verify(telemetryService).sendEvent(TelemetryEvent.CreateIndexIntentionEvent(dialect))
}
}

0 comments on commit 1d92ebf

Please sign in to comment.