Skip to content

Commit

Permalink
[IJ plugin] Telemetry: networking (#5285)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Oct 13, 2023
1 parent c269a49 commit 1a0d909
Show file tree
Hide file tree
Showing 30 changed files with 7,703 additions and 2,589 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.apollographql.ijplugin.gradle.getGradleRootPath
import com.apollographql.ijplugin.project.ApolloProjectListener
import com.apollographql.ijplugin.project.ApolloProjectService
import com.apollographql.ijplugin.project.apolloProjectService
import com.apollographql.ijplugin.settings.SettingsListener
import com.apollographql.ijplugin.settings.SettingsState
import com.apollographql.ijplugin.settings.settingsState
import com.apollographql.ijplugin.settings.ProjectSettingsListener
import com.apollographql.ijplugin.settings.ProjectSettingsState
import com.apollographql.ijplugin.settings.projectSettingsState
import com.apollographql.ijplugin.util.apolloGeneratedSourcesRoots
import com.apollographql.ijplugin.util.dispose
import com.apollographql.ijplugin.util.isNotDisposed
Expand Down Expand Up @@ -62,7 +62,7 @@ class ApolloCodegenService(
logd("project=${project.name}")
startOrStopCodegenObservers()
startObserveApolloProject()
startObservingSettings()
startObserveSettings()
}

private fun startObserveApolloProject() {
Expand All @@ -75,17 +75,17 @@ class ApolloCodegenService(
})
}

private fun startObservingSettings() {
private fun startObserveSettings() {
logd()
project.messageBus.connect(this).subscribe(SettingsListener.TOPIC, object : SettingsListener {
override fun settingsChanged(settingsState: SettingsState) {
logd("settingsState=$settingsState")
project.messageBus.connect(this).subscribe(ProjectSettingsListener.TOPIC, object : ProjectSettingsListener {
override fun settingsChanged(projectSettingsState: ProjectSettingsState) {
logd("projectSettingsState=$projectSettingsState")
startOrStopCodegenObservers()
}
})
}

private fun shouldTriggerCodegenAutomatically() = project.apolloProjectService.apolloVersion.isAtLeastV3 && project.settingsState.automaticCodegenTriggering
private fun shouldTriggerCodegenAutomatically() = project.apolloProjectService.apolloVersion.isAtLeastV3 && project.projectSettingsState.automaticCodegenTriggering

private fun startOrStopCodegenObservers() {
if (shouldTriggerCodegenAutomatically()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import com.apollographql.ijplugin.ApolloBundle
import com.apollographql.ijplugin.project.ApolloProjectListener
import com.apollographql.ijplugin.project.ApolloProjectService
import com.apollographql.ijplugin.project.apolloProjectService
import com.apollographql.ijplugin.settings.SettingsListener
import com.apollographql.ijplugin.settings.SettingsState
import com.apollographql.ijplugin.settings.settingsState
import com.apollographql.ijplugin.settings.ProjectSettingsListener
import com.apollographql.ijplugin.settings.ProjectSettingsState
import com.apollographql.ijplugin.settings.projectSettingsState
import com.apollographql.ijplugin.telemetry.telemetryService
import com.apollographql.ijplugin.util.dispose
import com.apollographql.ijplugin.util.isNotDisposed
Expand Down Expand Up @@ -53,7 +53,7 @@ class GradleToolingModelService(
startObserveApolloProject()
startOrStopObserveGradleHasSynced()
startOrAbortFetchToolingModels()
startObservingSettings()
startObserveSettings()
}

private fun startObserveApolloProject() {
Expand All @@ -68,7 +68,7 @@ class GradleToolingModelService(
}

private fun shouldFetchToolingModels() = project.apolloProjectService.apolloVersion.isAtLeastV4 &&
project.settingsState.contributeConfigurationToGraphqlPlugin
project.projectSettingsState.contributeConfigurationToGraphqlPlugin

private fun startOrStopObserveGradleHasSynced() {
logd()
Expand Down Expand Up @@ -101,14 +101,14 @@ class GradleToolingModelService(
gradleHasSyncedDisposable = null
}

private fun startObservingSettings() {
private fun startObserveSettings() {
logd()
project.messageBus.connect(this).subscribe(SettingsListener.TOPIC, object : SettingsListener {
private var contributeConfigurationToGraphqlPlugin: Boolean = project.settingsState.contributeConfigurationToGraphqlPlugin
project.messageBus.connect(this).subscribe(ProjectSettingsListener.TOPIC, object : ProjectSettingsListener {
private var contributeConfigurationToGraphqlPlugin: Boolean = project.projectSettingsState.contributeConfigurationToGraphqlPlugin

override fun settingsChanged(settingsState: SettingsState) {
val contributeConfigurationToGraphqlPluginChanged = contributeConfigurationToGraphqlPlugin != settingsState.contributeConfigurationToGraphqlPlugin
contributeConfigurationToGraphqlPlugin = settingsState.contributeConfigurationToGraphqlPlugin
override fun settingsChanged(projectSettingsState: ProjectSettingsState) {
val contributeConfigurationToGraphqlPluginChanged = contributeConfigurationToGraphqlPlugin != projectSettingsState.contributeConfigurationToGraphqlPlugin
contributeConfigurationToGraphqlPlugin = projectSettingsState.contributeConfigurationToGraphqlPlugin
logd("contributeConfigurationToGraphqlPluginChanged=$contributeConfigurationToGraphqlPluginChanged")
if (contributeConfigurationToGraphqlPluginChanged) {
startOrAbortFetchToolingModels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.apollographql.ijplugin.project
import com.apollographql.ijplugin.codegen.ApolloCodegenService
import com.apollographql.ijplugin.gradle.GradleToolingModelService
import com.apollographql.ijplugin.graphql.GraphQLConfigService
import com.apollographql.ijplugin.settings.SettingsService
import com.apollographql.ijplugin.settings.ProjectSettingsService
import com.apollographql.ijplugin.studio.fieldinsights.FieldInsightsService
import com.apollographql.ijplugin.studio.sandbox.SandboxService
import com.apollographql.ijplugin.telemetry.TelemetryService
Expand All @@ -23,7 +23,7 @@ internal class ApolloProjectManagerListener : ProjectManagerListener {
logd("apolloVersion=" + project.apolloProjectService.apolloVersion)
project.service<ApolloCodegenService>()
project.service<GradleToolingModelService>()
project.service<SettingsService>()
project.service<ProjectSettingsService>()
project.service<GraphQLConfigService>()
project.service<SandboxService>()
project.service<FieldInsightsService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.apollographql.ijplugin.refactoring.migration.v3tov4.item.UpdateMultiM
import com.apollographql.ijplugin.refactoring.migration.v3tov4.item.UpdateWebSocketReconnectWhen
import com.intellij.openapi.project.Project

private const val apollo4LatestVersion = "4.0.0-alpha.3"
private const val apollo4LatestVersion = "4.0.0-beta.1"

/**
* Migrations of Apollo Kotlin v3 to v4.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.apollographql.ijplugin.settings

import com.intellij.util.messages.Topic

interface AppSettingsListener {
companion object {
@Topic.ProjectLevel
val TOPIC: Topic<AppSettingsListener> = Topic.create("Apollo GraphQL app settings", AppSettingsListener::class.java)
}

fun settingsChanged(appSettingsState: AppSettingsState)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.apollographql.ijplugin.settings

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.util.xmlb.XmlSerializerUtil

/**
* Application level settings.
*/
@Service(Service.Level.APP)
@State(
name = "com.apollographql.ijplugin.settings.AppSettingsState",
storages = [Storage("apollo.xml")]
)
class AppSettingsStateService : PersistentStateComponent<AppSettingsStateImpl>, AppSettingsState {
private val _state = AppSettingsStateImpl()

override fun getState(): AppSettingsStateImpl {
return _state
}

override fun loadState(state: AppSettingsStateImpl) {
XmlSerializerUtil.copyBean(state, this._state)
notifySettingsChanged()
}

override var hasShownTelemetryOptOutDialog: Boolean
get() = _state.hasShownTelemetryOptOutDialog
set(value) {
_state.hasShownTelemetryOptOutDialog = value
notifySettingsChanged()
}

override var telemetryEnabled: Boolean
get() = _state.telemetryEnabled
set(value) {
_state.telemetryEnabled = value
notifySettingsChanged()
}

private var lastNotifiedState: AppSettingsState? = null
private fun notifySettingsChanged() {
if (lastNotifiedState != _state) {
lastNotifiedState = _state.copy()
ApplicationManager.getApplication().messageBus.syncPublisher(AppSettingsListener.TOPIC).settingsChanged(_state)
}
}
}

interface AppSettingsState {
var hasShownTelemetryOptOutDialog: Boolean
var telemetryEnabled: Boolean
}

data class AppSettingsStateImpl(
override var hasShownTelemetryOptOutDialog: Boolean = false,
override var telemetryEnabled: Boolean = true,
) : AppSettingsState

val appSettingsState get(): AppSettingsState = service<AppSettingsStateService>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.apollographql.ijplugin.settings

import com.intellij.util.messages.Topic

interface ProjectSettingsListener {
companion object {
@Topic.ProjectLevel
val TOPIC: Topic<ProjectSettingsListener> = Topic.create("Apollo GraphQL project settings", ProjectSettingsListener::class.java)
}

fun settingsChanged(projectSettingsState: ProjectSettingsState)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.StoragePathMacros
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil
import com.intellij.util.xmlb.annotations.Attribute
import com.intellij.util.xmlb.annotations.Transient
import java.util.UUID

/**
* Project level settings.
* These settings are not meant to be shared with the team, which is why they are stored in workspace.xml, typically ignored by VCS.
*/
@Service(Service.Level.PROJECT)
@State(
name = "com.apollographql.ijplugin.settings.SettingsState",
storages = [Storage("apollo.xml")]
name = "com.apollographql.ijplugin.settings.ProjectSettingsState",
storages = [Storage(StoragePathMacros.WORKSPACE_FILE)]
)
class SettingsService(private val project: Project) : PersistentStateComponent<SettingsStateImpl>, SettingsState {
private val _state = SettingsStateImpl()
class ProjectSettingsService(private val project: Project) : PersistentStateComponent<ProjectSettingsStateImpl>, ProjectSettingsState {
private val _state = ProjectSettingsStateImpl()

override fun getState(): SettingsStateImpl {
override fun getState(): ProjectSettingsStateImpl {
return _state
}

override fun loadState(state: SettingsStateImpl) {
override fun loadState(state: ProjectSettingsStateImpl) {
XmlSerializerUtil.copyBean(state, this._state)
notifySettingsChanged()
}
Expand Down Expand Up @@ -66,24 +71,11 @@ class SettingsService(private val project: Project) : PersistentStateComponent<S
_state.telemetryInstanceId = value
}

override var telemetryEnabled: Boolean
get() = _state.telemetryEnabled
set(value) {
_state.telemetryEnabled = value
notifySettingsChanged()
}

override var hasShownTelemetryOptOutDialog: Boolean
get() = _state.hasShownTelemetryOptOutDialog
set(value) {
_state.hasShownTelemetryOptOutDialog = value
}

private var lastNotifiedSettingsState: SettingsState? = null
private var lastNotifiedState: ProjectSettingsState? = null
private fun notifySettingsChanged() {
if (lastNotifiedSettingsState != _state) {
lastNotifiedSettingsState = _state.copy()
project.messageBus.syncPublisher(SettingsListener.TOPIC).settingsChanged(_state)
if (lastNotifiedState != _state) {
lastNotifiedState = _state.copy()
project.messageBus.syncPublisher(ProjectSettingsListener.TOPIC).settingsChanged(_state)
}
}

Expand All @@ -100,14 +92,12 @@ class SettingsService(private val project: Project) : PersistentStateComponent<S
}
}

interface SettingsState {
interface ProjectSettingsState {
var automaticCodegenTriggering: Boolean
var hasEnabledGraphQLPluginApolloKotlinSupport: Boolean
var contributeConfigurationToGraphqlPlugin: Boolean
var apolloKotlinServiceConfigurations: List<ApolloKotlinServiceConfiguration>
var telemetryInstanceId: String
var telemetryEnabled: Boolean
var hasShownTelemetryOptOutDialog: Boolean
}

data class ApolloKotlinServiceConfiguration(
Expand Down Expand Up @@ -137,15 +127,13 @@ data class ApolloKotlinServiceConfiguration(
}
}

data class SettingsStateImpl(
data class ProjectSettingsStateImpl(
override var automaticCodegenTriggering: Boolean = true,
override var hasEnabledGraphQLPluginApolloKotlinSupport: Boolean = false,
override var contributeConfigurationToGraphqlPlugin: Boolean = true,
override var apolloKotlinServiceConfigurations: List<ApolloKotlinServiceConfiguration> = emptyList(),
override var telemetryInstanceId: String = "",
override var telemetryEnabled: Boolean = true,
override var hasShownTelemetryOptOutDialog: Boolean = false,
) : SettingsState
) : ProjectSettingsState


val Project.settingsState get(): SettingsState = service<SettingsService>()
val Project.projectSettingsState get(): ProjectSettingsState = service<ProjectSettingsService>()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.apollographql.ijplugin.settings
import com.apollographql.ijplugin.ApolloBundle
import com.apollographql.ijplugin.project.apolloProjectService
import com.apollographql.ijplugin.settings.studio.ApiKeyDialog
import com.apollographql.ijplugin.telemetry.TELEMETRY_ENABLED
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.openapi.project.Project
import com.intellij.ui.AddEditRemovePanel
Expand All @@ -24,7 +23,7 @@ class SettingsComponent(private val project: Project) {
var automaticCodegenTriggering: Boolean by automaticCodegenTriggeringProperty
var contributeConfigurationToGraphqlPlugin: Boolean by contributeConfigurationToGraphqlPluginProperty
var apolloKotlinServiceConfigurations: List<ApolloKotlinServiceConfiguration>
get() = addEditRemovePanel?.data?.toList()?: emptyList()
get() = addEditRemovePanel?.data?.toList() ?: emptyList()
set(value) {
addEditRemovePanel?.data = value.toMutableList()
}
Expand Down Expand Up @@ -101,12 +100,10 @@ class SettingsComponent(private val project: Project) {
}
}
}
if (TELEMETRY_ENABLED) {
row {
checkBox(ApolloBundle.message("settings.telemetry.telemetryEnabled.text"))
.comment(ApolloBundle.message("settings.telemetry.telemetryEnabled.comment"))
.bindSelected(telemetryEnabledProperty)
}
row {
checkBox(ApolloBundle.message("settings.telemetry.telemetryEnabled.text"))
.comment(ApolloBundle.message("settings.telemetry.telemetryEnabled.comment"))
.bindSelected(telemetryEnabledProperty)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ class SettingsConfigurable(private val project: Project) : Configurable {
}

override fun isModified(): Boolean {
return settingsComponent!!.automaticCodegenTriggering != project.settingsState.automaticCodegenTriggering ||
settingsComponent!!.contributeConfigurationToGraphqlPlugin != project.settingsState.contributeConfigurationToGraphqlPlugin ||
settingsComponent!!.apolloKotlinServiceConfigurations != project.settingsState.apolloKotlinServiceConfigurations ||
settingsComponent!!.telemetryEnabled != project.settingsState.telemetryEnabled
return settingsComponent!!.automaticCodegenTriggering != project.projectSettingsState.automaticCodegenTriggering ||
settingsComponent!!.contributeConfigurationToGraphqlPlugin != project.projectSettingsState.contributeConfigurationToGraphqlPlugin ||
settingsComponent!!.apolloKotlinServiceConfigurations != project.projectSettingsState.apolloKotlinServiceConfigurations ||
settingsComponent!!.telemetryEnabled != appSettingsState.telemetryEnabled
}

override fun apply() {
project.settingsState.automaticCodegenTriggering = settingsComponent!!.automaticCodegenTriggering
project.settingsState.contributeConfigurationToGraphqlPlugin = settingsComponent!!.contributeConfigurationToGraphqlPlugin
project.settingsState.apolloKotlinServiceConfigurations = settingsComponent!!.apolloKotlinServiceConfigurations
project.settingsState.telemetryEnabled = settingsComponent!!.telemetryEnabled
project.projectSettingsState.automaticCodegenTriggering = settingsComponent!!.automaticCodegenTriggering
project.projectSettingsState.contributeConfigurationToGraphqlPlugin = settingsComponent!!.contributeConfigurationToGraphqlPlugin
project.projectSettingsState.apolloKotlinServiceConfigurations = settingsComponent!!.apolloKotlinServiceConfigurations
appSettingsState.telemetryEnabled = settingsComponent!!.telemetryEnabled
}

override fun reset() {
settingsComponent!!.automaticCodegenTriggering = project.settingsState.automaticCodegenTriggering
settingsComponent!!.contributeConfigurationToGraphqlPlugin = project.settingsState.contributeConfigurationToGraphqlPlugin
settingsComponent!!.apolloKotlinServiceConfigurations = project.settingsState.apolloKotlinServiceConfigurations
settingsComponent!!.telemetryEnabled = project.settingsState.telemetryEnabled
settingsComponent!!.automaticCodegenTriggering = project.projectSettingsState.automaticCodegenTriggering
settingsComponent!!.contributeConfigurationToGraphqlPlugin = project.projectSettingsState.contributeConfigurationToGraphqlPlugin
settingsComponent!!.apolloKotlinServiceConfigurations = project.projectSettingsState.apolloKotlinServiceConfigurations
settingsComponent!!.telemetryEnabled = appSettingsState.telemetryEnabled
}

override fun getPreferredFocusedComponent() = settingsComponent!!.preferredFocusedComponent
Expand Down
Loading

0 comments on commit 1a0d909

Please sign in to comment.