Implemented collecting projectId from Gradle

#KT-45337 Fixed
This commit is contained in:
Andrey Uskov
2021-02-21 11:38:38 +03:00
parent 4401589974
commit 63925ee018
5 changed files with 43 additions and 2 deletions
@@ -5,12 +5,17 @@
package org.jetbrains.kotlin.idea.statistics
import com.intellij.ide.highlighter.ProjectFileType
import com.intellij.ide.util.PropertiesComponent
import com.intellij.internal.statistic.eventLog.EventLogConfiguration
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.util.io.FileUtil
import com.intellij.util.PathUtilRt
import com.intellij.util.concurrency.AppExecutorUtil
import com.intellij.util.text.trimMiddle
import org.jetbrains.kotlin.statistics.BuildSessionLogger
import java.io.File
import java.util.concurrent.TimeUnit
@@ -20,6 +25,9 @@ import org.jetbrains.kotlin.statistics.fileloggers.MetricsContainer
import org.jetbrains.kotlin.statistics.metrics.BooleanMetrics
import org.jetbrains.kotlin.statistics.metrics.NumericalMetrics
import org.jetbrains.kotlin.statistics.metrics.StringMetrics
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
@@ -34,6 +42,27 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
companion object {
private val IDE_STRING_ANONYMIZERS = lazy {
mapOf(
StringMetrics.PROJECT_PATH to { path: String ->
// This code duplicated logics of StatisticsUtil.getProjectId, which could not be directly reused:
// 1. the path of gradle project may not have corresponding project
// 2. the projectId should be stable and independent on IDE version
val presentableUrl = FileUtil.toSystemIndependentName(path)
val name =
PathUtilRt.getFileName(presentableUrl).toLowerCase(Locale.US).removeSuffix(ProjectFileType.DOT_DEFAULT_EXTENSION)
val locationHash = Integer.toHexString((presentableUrl).hashCode())
val projectHash =
"${name.trimMiddle(name.length.coerceAtMost(254 - locationHash.length), useEllipsisSymbol = false)}.$locationHash"
EventLogConfiguration.anonymize(projectHash)
})
}
private fun String.anonymizeIdeString(metric: StringMetrics) = if (metric.anonymization.anonymizeOnIdeSize())
IDE_STRING_ANONYMIZERS.value[metric]?.invoke(this)
else
this
/**
* Maximum amount of directories which were reported as gradle user dirs
* These directories should be monitored for reported gradle statistics.
@@ -63,7 +92,10 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
for (metric in metrics) {
when (metric) {
is BooleanMetrics -> putIfNotNull(metric.name, this.getMetric(metric)?.toStringRepresentation())
is StringMetrics -> putIfNotNull(metric.name, this.getMetric(metric)?.toStringRepresentation())
is StringMetrics -> putIfNotNull(
metric.name,
this.getMetric(metric)?.toStringRepresentation()?.anonymizeIdeString(metric)
)
is NumericalMetrics -> putIfNotNull(metric.name, this.getMetric(metric)?.toStringRepresentation())
is Pair<*, *> -> putIfNotNull(metric.first.toString(), metric.second?.toString())
}
@@ -80,7 +112,8 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
StringMetrics.GRADLE_VERSION,
NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED,
StringMetrics.IDES_INSTALLED,
BooleanMetrics.EXECUTED_FROM_IDEA
BooleanMetrics.EXECUTED_FROM_IDEA,
StringMetrics.PROJECT_PATH
)
container.log(
GradleStatisticsEvents.Kapt,
@@ -69,6 +69,7 @@ class KotlinBuildStatHandler {
}
internal fun reportGlobalMetrics(gradle: Gradle, sessionLogger: BuildSessionLogger) {
sessionLogger.report(StringMetrics.PROJECT_PATH, gradle.rootProject.projectDir.absolutePath)
System.getProperty("os.name")?.also {
sessionLogger.report(StringMetrics.OS_TYPE, System.getProperty("os.name"))
}
@@ -11,6 +11,8 @@ internal interface ValueAnonymizer<T> {
fun anonymize(t: T): T
fun anonymizeOnIdeSize(): Boolean = false
}
internal val salt: String by lazy {
@@ -88,6 +88,10 @@ enum class StringAnonymizationPolicy : ValueAnonymizer<String> {
SAFE {
override fun anonymize(t: String) = t
},
ANONYMIZE_IN_IDE {
override fun anonymize(t: String) = t
override fun anonymizeOnIdeSize() = true
},
SHA_256 {
override fun anonymize(t: String) = sha256(t)
},
@@ -13,6 +13,7 @@ enum class StringMetrics(val type: StringOverridePolicy, val anonymization: Stri
// User environment
GRADLE_VERSION(OVERRIDE, COMPONENT_VERSION),
PROJECT_PATH(OVERRIDE, ANONYMIZE_IN_IDE),
OS_TYPE(OVERRIDE, SAFE),