Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Mixpanel module API & implementation #2107

Merged
merged 9 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flank_wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.2.1"
version = "1.2.2"
group = "com.github.flank"

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.flank.wrapper.internal

import flank.common.config.isTest
import flank.tool.analytics.mixpanel.sessionId
import flank.tool.analytics.mixpanel.Mixpanel
import io.sentry.Sentry

private const val SESSION_ID = "session.id"
Expand All @@ -17,7 +17,7 @@ fun setupCrashReporter() {
}

logTags(
SESSION_ID to sessionId,
SESSION_ID to Mixpanel.sessionId,
OS_NAME to osName,
FLANK_WRAPPER_VERSION to flankWrapperVersion,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.github.flank.wrapper.internal

import flank.tool.analytics.mixpanel.send
import flank.tool.analytics.mixpanel.toEvent
import flank.tool.analytics.mixpanel.Mixpanel

private const val FLANK_WRAPPER = "flank_wrapper"
private const val EVENT_RUN = "flank run"
private const val EVENT_NEW_FLANK_VERSION_DOWNLOADED = "new_version_downloaded"

internal fun sendAnalyticsNewFlankVersionDownloaded() {
eventWithoutProperties(EVENT_NEW_FLANK_VERSION_DOWNLOADED).send()
Mixpanel.configure(FLANK_WRAPPER)
Mixpanel.add(FLANK_WRAPPER, EVENT_NEW_FLANK_VERSION_DOWNLOADED)
Mixpanel.send(FLANK_WRAPPER)
}

internal fun sendAnalyticsFlankRun() {
eventWithoutProperties(EVENT_RUN).send()
Mixpanel.configure(FLANK_WRAPPER)
Mixpanel.add(FLANK_WRAPPER, EVENT_RUN)
Mixpanel.send(FLANK_WRAPPER)
}

private fun eventWithoutProperties(
eventName: String
) = emptyMap<String, Any>().toEvent(FLANK_WRAPPER, eventName)
2 changes: 1 addition & 1 deletion flank_wrapper/src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
11 changes: 4 additions & 7 deletions test_runner/src/main/kotlin/ftl/analytics/InitUsageStatistics.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package ftl.analytics

import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.configure
import flank.tool.analytics.mixpanel.initializeStatisticsClient
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.args.AndroidArgs
import ftl.args.IArgs
import ftl.args.IosArgs
import ftl.util.isGoogleAnalyticsDisabled

internal fun IArgs.initUsageStatistics() {
analyticsReport.configure(project)
initializeStatisticsClient(
disableUsageStatistics || isGoogleAnalyticsDisabled(flank.common.userHome),
Mixpanel.configure(
projectName = project,
blockUsageStatistics = disableUsageStatistics,
AndroidArgs::class,
IosArgs::class,
IArgs::class
Expand Down
53 changes: 17 additions & 36 deletions test_runner/src/main/kotlin/ftl/analytics/SendConfiguration.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package ftl.analytics

import com.google.common.annotations.VisibleForTesting
import flank.tool.analytics.mixpanel.FIREBASE
import flank.tool.analytics.mixpanel.FLANK_VERSION
import flank.tool.analytics.mixpanel.SESSION_ID
import flank.tool.analytics.mixpanel.TEST_PLATFORM
import flank.tool.analytics.mixpanel.add
import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.filterSensitiveValues
import flank.tool.analytics.mixpanel.objectToMap
import flank.tool.analytics.mixpanel.removeNotNeededKeys
import flank.tool.analytics.mixpanel.schemaVersion
import flank.tool.analytics.mixpanel.sessionId
import flank.tool.analytics.mixpanel.Mixpanel
import flank.tool.analytics.mixpanel.Mixpanel.CONFIGURATION
import flank.tool.analytics.mixpanel.Mixpanel.DEVICE_TYPES
import flank.tool.analytics.mixpanel.Mixpanel.add
import flank.tool.analytics.mixpanel.Mixpanel.removeSensitiveValues
import flank.tool.analytics.mixpanel.toMap
import ftl.args.AndroidArgs
import ftl.args.IArgs
import ftl.args.IosArgs
Expand All @@ -20,36 +14,23 @@ import ftl.environment.VIRTUAL_DEVICE
import ftl.util.readVersion

fun AndroidArgs.reportConfiguration() {
initUsageStatistics()
addCommonData()
.add("configuration", createEventMap())
.add("device_types", devices.map { if (it.isVirtual) VIRTUAL_DEVICE else PHYSICAL_DEVICE }.distinct())
add(CONFIGURATION, removeSensitiveValues(toMap() + commonArgs.toMap()))
add(DEVICE_TYPES, devices.map { if (it.isVirtual) VIRTUAL_DEVICE else PHYSICAL_DEVICE }.distinct())
}

internal fun AndroidArgs.createEventMap() =
toArgsMap().plus(commonArgs.toArgsMap()).removeNotNeededKeys().filterSensitiveValues()

fun IosArgs.reportConfiguration() {
initUsageStatistics()
addCommonData()
.add("configuration", createEventMap())
.add("device_types", listOf(PHYSICAL_DEVICE))
add(CONFIGURATION, removeSensitiveValues(toMap() + commonArgs.toMap()))
add(DEVICE_TYPES, listOf(PHYSICAL_DEVICE))
}

fun IArgs.addCommonData() = let {
initUsageStatistics()
analyticsReport
.add("schema_version", schemaVersion)
.add(FLANK_VERSION, readVersion())
.add(SESSION_ID, sessionId)
.add(TEST_PLATFORM, FIREBASE)
.add("project_id", project)
add(Mixpanel.SCHEMA_VERSION, Mixpanel.schemaVersion)
add(Mixpanel.FLANK_VERSION, readVersion())
add(Mixpanel.SESSION_ID, Mixpanel.sessionId)
add(Mixpanel.TEST_PLATFORM, Mixpanel.Platform.FIREBASE)
add(Mixpanel.PROJECT_ID, project)
}

private fun IosArgs.createEventMap() =
toArgsMap().plus(commonArgs.toArgsMap()).removeNotNeededKeys().filterSensitiveValues()

private fun IArgs.toArgsMap() = objectToMap().filterNonCommonArgs()

@VisibleForTesting
internal fun Map<String, Any>.filterNonCommonArgs() = filter { it.key != COMMON_ARGS }

private const val COMMON_ARGS = "commonArgs"
2 changes: 2 additions & 0 deletions test_runner/src/main/kotlin/ftl/args/AndroidArgs.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ftl.args

import com.fasterxml.jackson.annotation.JsonIgnore
import flank.tool.analytics.AnonymizeInStatistics
import flank.tool.analytics.IgnoreInStatistics
import ftl.api.ShardChunks
Expand All @@ -10,6 +11,7 @@ import ftl.run.model.AndroidTestShards
import java.nio.file.Paths

data class AndroidArgs(
@get:JsonIgnore
val commonArgs: CommonArgs,

@property:AnonymizeInStatistics
Expand Down
1 change: 1 addition & 0 deletions test_runner/src/main/kotlin/ftl/args/IosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ftl.run.exception.FlankConfigurationError
import java.nio.file.Paths

data class IosArgs(
@get:JsonIgnore
val commonArgs: CommonArgs,

@property:AnonymizeInStatistics
Expand Down
6 changes: 2 additions & 4 deletions test_runner/src/main/kotlin/ftl/ios/xctest/XcTestData.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ftl.ios.xctest

import com.dd.plist.NSDictionary
import flank.tool.analytics.mixpanel.APP_ID
import flank.tool.analytics.mixpanel.add
import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.args.ArgsHelper.calculateShards
import ftl.args.IosArgs
import ftl.args.isXcTest
Expand Down Expand Up @@ -61,7 +59,7 @@ private fun IosArgs.calculateXcTest(): XcTestRunData {
)
}

private fun IosArgs.reportBundleId() = analyticsReport.add(APP_ID, getBundleId())
private fun IosArgs.reportBundleId() = Mixpanel.add(Mixpanel.APP_ID, getBundleId())

private inline fun <reified T> createCustomSharding(shardingJsonPath: String) =
fromJson<T>(Paths.get(shardingJsonPath).toFile().readText())
Expand Down
8 changes: 7 additions & 1 deletion test_runner/src/main/kotlin/ftl/mock/MockServer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ftl.mock

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.api.services.toolresults.model.AppStartTime
import com.google.api.services.toolresults.model.CPUInfo
Expand Down Expand Up @@ -41,7 +44,6 @@ import com.google.testing.model.TestExecution
import com.google.testing.model.TestMatrix
import com.google.testing.model.ToolResultsExecution
import com.google.testing.model.ToolResultsStep
import flank.tool.analytics.mixpanel.objectToMap
import ftl.client.google.run.toClientInfoDetailList
import ftl.config.FtlConstants
import ftl.config.FtlConstants.JSON_FACTORY
Expand Down Expand Up @@ -405,6 +407,10 @@ object MockServer {
.setClientInfoDetails(allClientDetails.toClientInfoDetailList())
}

private fun Any.objectToMap(): Map<String, Any> = objectMapper.convertValue(this, object : TypeReference<Map<String, Any>>() {})

private val objectMapper by lazy { jsonMapper { addModule(kotlinModule()) } }

fun start() {
if (isStarted) return
val loggingEnabled = LogbackLogger.Root.isEnabled
Expand Down
7 changes: 3 additions & 4 deletions test_runner/src/main/kotlin/ftl/reports/CostReport.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ftl.reports

import flank.common.println
import flank.tool.analytics.mixpanel.add
import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.api.JUnitTest
import ftl.args.IArgs
import ftl.config.FtlConstants.indent
Expand Down Expand Up @@ -32,7 +31,7 @@ object CostReport : IReport {
val physicalCost = calculatePhysicalCost(totalBillablePhysicalMinutes.toBigDecimal())
val total = calculateTotalCost(virtualCost, physicalCost)

analyticsReport.add(
Mixpanel.add(
"cost",
mapOf(
"virtual" to virtualCost,
Expand All @@ -41,7 +40,7 @@ object CostReport : IReport {
)
)

analyticsReport.add(
Mixpanel.add(
"test_duration",
mapOf(
"virtual" to totalBillableVirtualMinutes,
Expand Down
14 changes: 7 additions & 7 deletions test_runner/src/main/kotlin/ftl/run/NewTestRun.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ftl.run

import flank.tool.analytics.mixpanel.add
import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.send
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.api.TestMatrix
import ftl.args.AndroidArgs
import ftl.args.IArgs
Expand Down Expand Up @@ -47,12 +45,14 @@ suspend fun IArgs.newTestRun() = withTimeoutOrNull(parsedTimeout) {
matrixMap.printMatricesWebLinks(project)
outputReport.log(matrixMap)
matrixMap.reportTestResults()
analyticsReport.send()
Mixpanel.send(FIREBASE_TEST_LAB_RUN)
matrixMap.validate(ignoreFailedTests)
addStepTime("Generating reports", duration)
}
}

private const val FIREBASE_TEST_LAB_RUN = "firebase test lab run"

private fun MatrixMap.reportTestResults() {
val outcomes = map.flatMap { it.value.axes }
val testsSummary = outcomes.map { it.suiteOverview }.fold(TestMatrix.SuiteOverview()) { result, overview ->
Expand All @@ -66,12 +66,12 @@ private fun MatrixMap.reportTestResults() {
overheadTime = result.overheadTime + overview.overheadTime
)
}
analyticsReport.add(
Mixpanel.add(
"shards_count",
map.values.flatMap { it.testExecutions }.maxOf { testExecution -> testExecution.shardIndex ?: 0 } + 1
)
analyticsReport.add("outcome", outcomes)
analyticsReport.add(
Mixpanel.add("outcome", outcomes)
Mixpanel.add(
"tests",
mapOf(
"total" to testsSummary.total,
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/run/common/SaveSessionId.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ftl.run.common

import flank.tool.analytics.mixpanel.sessionId
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.args.IArgs
import java.nio.file.Paths

const val SESSION_ID_FILE = "session_id.txt"

fun IArgs.saveSessionId() = Paths.get(localResultDir, SESSION_ID_FILE).toFile().writeText(sessionId)
fun IArgs.saveSessionId() = Paths.get(localResultDir, SESSION_ID_FILE).toFile().writeText(Mixpanel.sessionId)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package ftl.run.platform

import flank.common.join
import flank.common.logLn
import flank.tool.analytics.mixpanel.APP_ID
import flank.tool.analytics.mixpanel.add
import flank.tool.analytics.mixpanel.analyticsReport
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.api.RemoteStorage
import ftl.api.TestMatrixAndroid
import ftl.api.executeTestMatrixAndroid
Expand Down Expand Up @@ -105,4 +103,4 @@ private fun AndroidTestContext.reportPackageName() = when (this) {
is GameLoopContext -> getAndroidAppDetails(app.remote)
}.sendPackageName()

private fun String.sendPackageName() = analyticsReport.add(APP_ID, this)
private fun String.sendPackageName() = Mixpanel.add(Mixpanel.APP_ID, this)
5 changes: 2 additions & 3 deletions test_runner/src/main/kotlin/ftl/util/CrashReporter.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ftl.util

import flank.common.config.isTest
import flank.tool.analytics.mixpanel.SESSION_ID
import flank.tool.analytics.mixpanel.sessionId
import flank.tool.analytics.mixpanel.Mixpanel
import io.sentry.Sentry
import io.sentry.SentryLevel
import java.io.File
Expand Down Expand Up @@ -53,7 +52,7 @@ private fun initializeCrashReportWrapper() {
it.release = readRevision()
}
setCrashReportTag(
SESSION_ID to sessionId,
Mixpanel.SESSION_ID to Mixpanel.sessionId,
OS_NAME to System.getProperty("os.name"),
FLANK_VERSION to readVersion(),
FLANK_REVISION to readRevision()
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package ftl.util

import com.fasterxml.jackson.annotation.JsonProperty
import flank.common.logLn
import flank.tool.analytics.mixpanel.sessionId
import flank.tool.analytics.mixpanel.Mixpanel
import ftl.run.exception.FlankGeneralError
import java.io.File
import java.io.InputStream
Expand Down Expand Up @@ -74,7 +74,7 @@ private fun getResource(name: String): InputStream {
fun printVersionInfo() {
logLn("version: ${readVersion()}")
logLn("revision: ${readRevision()}")
logLn("session id: $sessionId")
logLn("session id: ${Mixpanel.sessionId}")
logLn()
}

Expand Down
Loading