Skip to content

Commit

Permalink
feat: Move Mixpanel metrics to tools:analytics (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfilipow92 authored Jul 14, 2021
1 parent f44162e commit ace7b16
Show file tree
Hide file tree
Showing 36 changed files with 259 additions and 140 deletions.
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ object Dependencies {

const val PROGUARD = "com.guardsquare:proguard-gradle:${Versions.PROGUARD}"

const val JSON = "org.json:json:${Versions.JSON}"

//region Test Dependencies
const val JSOUP = "org.jsoup:jsoup:${Versions.JSOUP}"
const val JUNIT = "junit:junit:${Versions.JUNIT}"
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ object Versions {
// https://github.com/Guardsquare/proguard
const val PROGUARD = "7.1.0"

// https://mvnrepository.com/artifact/org.json/json
const val JSON = "20210307"

// ============== flank-scripts ==============
const val KOTLIN_SERIALIZATION = "1.2.2"
const val ARCHIVE_LIB = "1.1.0"
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "1.8" }
dependencies {
api(Dependencies.Fuel.CORE)
api(Dependencies.ARCHIVE_LIB)
api(Dependencies.JSON)

testImplementation(Dependencies.JUNIT)
testImplementation(Dependencies.MOCKK)
Expand Down
5 changes: 5 additions & 0 deletions common/src/main/kotlin/flank/common/Json.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package flank.common

import org.json.JSONObject

fun Map<*, *>.toJSONObject() = JSONObject(this)
2 changes: 2 additions & 0 deletions corellium/domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dependencies {
api(project(":tool:instrument:log"))
api(project(":tool:junit"))
api(project(":tool:log"))
api(project(":tool:analytics"))
api(project(":tool:analytics:mixpanel"))

testImplementation(Dependencies.JUNIT)
testImplementation(project(":corellium:adapter"))
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ include(
":corellium:adapter",
":corellium:client",
":corellium:sandbox",

":tool:apk",
":tool:config",
":tool:filter",
Expand All @@ -35,6 +34,9 @@ include(
":tool:log",
":tool:log:format",
":tool:execution:parallel",
":tool:execution:synchronized",
":tool:analytics",
":tool:analytics:mixpanel",
":tool:execution:linear",
)

Expand Down
3 changes: 3 additions & 0 deletions test_runner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ dependencies {

implementation(project(":corellium:cli"))

implementation(project(":tool:analytics"))
implementation(project(":tool:analytics:mixpanel"))

testImplementation(Dependencies.JUNIT)
testImplementation(Dependencies.SYSTEM_RULES)
testImplementation(Dependencies.TRUTH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ftl.adapter.google
import com.google.testing.model.FileReference
import com.google.testing.model.TestExecution
import com.google.testing.model.TestMatrix
import ftl.analytics.toJSONObject
import flank.common.toJSONObject
import ftl.api.TestMatrix.Data
import ftl.api.TestMatrix.Outcome
import ftl.api.TestMatrix.SuiteOverview
Expand Down
16 changes: 16 additions & 0 deletions test_runner/src/main/kotlin/ftl/analytics/InitUsageStatistics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ftl.analytics

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

internal fun IArgs.initUsageStatistics() {
initializeStatisticsClient(
disableUsageStatistics || isGoogleAnalyticsDisabled(flank.common.userHome),
AndroidArgs::class,
IosArgs::class,
IArgs::class
)
}
21 changes: 0 additions & 21 deletions test_runner/src/main/kotlin/ftl/analytics/PrepareConfiguration.kt

This file was deleted.

17 changes: 17 additions & 0 deletions test_runner/src/main/kotlin/ftl/analytics/SendAppId.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ftl.analytics

import flank.tool.analytics.mixpanel.ANDROID
import flank.tool.analytics.mixpanel.APP_ID
import flank.tool.analytics.mixpanel.DEVICE_TYPE
import flank.tool.analytics.mixpanel.IOS
import flank.tool.analytics.mixpanel.sendConfiguration
import ftl.args.AndroidArgs
import ftl.args.IArgs

fun IArgs.sendAppId(appId: String) {
initUsageStatistics()
when (this) {
is AndroidArgs -> sendConfiguration(project, mapOf(APP_ID to appId, DEVICE_TYPE to ANDROID), eventName = APP_ID)
else -> sendConfiguration(project, mapOf(APP_ID to appId, DEVICE_TYPE to IOS), eventName = APP_ID)
}
}
33 changes: 33 additions & 0 deletions test_runner/src/main/kotlin/ftl/analytics/SendConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ftl.analytics

import com.google.common.annotations.VisibleForTesting
import flank.tool.analytics.mixpanel.filterSensitiveValues
import flank.tool.analytics.mixpanel.objectToMap
import flank.tool.analytics.mixpanel.removeNotNeededKeys
import flank.tool.analytics.mixpanel.sendConfiguration
import ftl.args.AndroidArgs
import ftl.args.IArgs
import ftl.args.IosArgs

fun AndroidArgs.sendConfiguration() {
initUsageStatistics()
sendConfiguration(project = project, events = createEventMap())
}

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

fun IosArgs.sendConfiguration() {
initUsageStatistics()
sendConfiguration(project = project, events = createEventMap())
}

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"
37 changes: 0 additions & 37 deletions test_runner/src/main/kotlin/ftl/analytics/SendUsageStatistics.kt

This file was deleted.

15 changes: 0 additions & 15 deletions test_runner/src/main/kotlin/ftl/analytics/UsageStatistics.kt

This file was deleted.

4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/AndroidArgs.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ftl.args

import ftl.analytics.AnonymizeInStatistics
import ftl.analytics.IgnoreInStatistics
import flank.tool.analytics.AnonymizeInStatistics
import flank.tool.analytics.IgnoreInStatistics
import ftl.api.ShardChunks
import ftl.args.yml.AppTestPair
import ftl.args.yml.Type
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/IArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ftl.args
import flank.common.OutputLogLevel
import flank.common.config.isTest
import flank.common.setLogLevel
import ftl.analytics.AnonymizeInStatistics
import ftl.analytics.IgnoreInStatistics
import flank.tool.analytics.AnonymizeInStatistics
import flank.tool.analytics.IgnoreInStatistics
import ftl.args.yml.Type
import ftl.config.Device
import ftl.config.common.CommonFlankConfig.Companion.defaultLocalResultsDir
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/args/IosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ftl.args

import com.fasterxml.jackson.annotation.JsonIgnore
import com.google.common.annotations.VisibleForTesting
import ftl.analytics.AnonymizeInStatistics
import flank.tool.analytics.AnonymizeInStatistics
import ftl.args.yml.Type
import ftl.ios.xctest.XcTestRunData
import ftl.ios.xctest.calculateXcTestRunData
Expand Down
16 changes: 11 additions & 5 deletions test_runner/src/main/kotlin/ftl/domain/RunTestAndroid.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ftl.domain

import flank.common.logLn
import ftl.analytics.FIREBASE
import ftl.analytics.FLANK_VERSION
import ftl.analytics.FLANK_VERSION_PROPERTY
import ftl.analytics.TEST_PLATFORM
import flank.tool.analytics.mixpanel.FIREBASE
import flank.tool.analytics.mixpanel.FLANK_VERSION
import flank.tool.analytics.mixpanel.FLANK_VERSION_PROPERTY
import flank.tool.analytics.mixpanel.TEST_PLATFORM
import flank.tool.analytics.mixpanel.sendConfiguration
import ftl.analytics.sendConfiguration
import ftl.args.createAndroidArgs
import ftl.args.setupLogLevel
Expand Down Expand Up @@ -60,8 +61,13 @@ operator fun RunTestAndroid.invoke() {
DEVICE_SYSTEM to "android",
TEST_TYPE to type?.name.orEmpty()
)

sendConfiguration()
sendConfiguration(mapOf(FLANK_VERSION_PROPERTY to readVersion(), TEST_PLATFORM to FIREBASE), eventName = FLANK_VERSION)
sendConfiguration(
project,
mapOf(FLANK_VERSION_PROPERTY to readVersion(), TEST_PLATFORM to FIREBASE),
eventName = FLANK_VERSION
)
}.validate().also { args ->
runBlocking {
if (dumpShards)
Expand Down
15 changes: 12 additions & 3 deletions test_runner/src/main/kotlin/ftl/domain/RunTestIos.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ftl.domain

import flank.common.logLn
import ftl.analytics.FLANK_VERSION
import ftl.analytics.FLANK_VERSION_PROPERTY
import flank.tool.analytics.mixpanel.FIREBASE
import flank.tool.analytics.mixpanel.FLANK_VERSION
import flank.tool.analytics.mixpanel.FLANK_VERSION_PROPERTY
import flank.tool.analytics.mixpanel.TEST_PLATFORM
import flank.tool.analytics.mixpanel.sendConfiguration
import ftl.analytics.sendConfiguration
import ftl.args.createIosArgs
import ftl.args.setupLogLevel
Expand Down Expand Up @@ -58,8 +61,14 @@ operator fun RunIosTest.invoke() {
DEVICE_SYSTEM to "ios",
TEST_TYPE to type?.name.orEmpty()
)

sendConfiguration()
sendConfiguration(mapOf(FLANK_VERSION_PROPERTY to readVersion()), eventName = FLANK_VERSION)
sendConfiguration(
project,
mapOf(FLANK_VERSION_PROPERTY to readVersion(), TEST_PLATFORM to FIREBASE),
eventName = FLANK_VERSION
)

if (dumpShards.not()) logLn(this)
}.validate().run {
if (dumpShards) dumpShards()
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/mock/MockServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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 ftl.analytics.objectToMap
import flank.tool.analytics.mixpanel.objectToMap
import ftl.client.google.run.toClientInfoDetailList
import ftl.config.FtlConstants
import ftl.config.FtlConstants.JSON_FACTORY
Expand Down
27 changes: 4 additions & 23 deletions test_runner/src/main/kotlin/ftl/reports/CostReport.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package ftl.reports

import flank.common.println
import ftl.analytics.sendConfiguration
import ftl.api.JUnitTest
import ftl.args.IArgs
import ftl.config.FtlConstants.indent
import ftl.json.MatrixMap
import ftl.reports.util.IReport
import ftl.reports.util.ReportManager
import ftl.util.calculatePhysicalCost
import ftl.util.calculateTotalCost
import ftl.util.calculateVirtualCost
import ftl.util.estimateCosts
import java.io.StringWriter

Expand All @@ -19,7 +15,7 @@ object CostReport : IReport {

override val extension = ".txt"

private fun estimate(args: IArgs, matrices: MatrixMap): String {
private fun estimate(matrices: MatrixMap): String {
var totalBillableVirtualMinutes = 0L
var totalBillablePhysicalMinutes = 0L

Expand All @@ -28,26 +24,11 @@ object CostReport : IReport {
totalBillablePhysicalMinutes += it.billableMinutes.physical
}

val virtualCost = calculateVirtualCost(totalBillableVirtualMinutes.toBigDecimal())
val physicalCost = calculatePhysicalCost(totalBillablePhysicalMinutes.toBigDecimal())

args.sendConfiguration(
events = mapOf(
"virtual_cost" to virtualCost,
"physical_cost" to physicalCost,
"cost_total" to calculateTotalCost(
virtualCost,
physicalCost
)
),
eventName = "devices_cost"
)

return estimateCosts(totalBillableVirtualMinutes, totalBillablePhysicalMinutes)
}

private fun generate(args: IArgs, matrices: MatrixMap): String {
val cost = estimate(args, matrices)
private fun generate(matrices: MatrixMap): String {
val cost = estimate(matrices)
StringWriter().use { writer ->
writer.println(reportName())
cost.split("\n").forEach { writer.println(indent + it) }
Expand All @@ -56,7 +37,7 @@ object CostReport : IReport {
}

override fun run(matrices: MatrixMap, result: JUnitTest.Result?, printToStdout: Boolean, args: IArgs) {
val output = generate(args, matrices)
val output = generate(matrices)
if (printToStdout) print(output)
write(matrices, output, args)
ReportManager.uploadReportResult(output, args, fileName())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ftl.run.common

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

const val SESSION_ID_FILE = "session_id.txt"
Expand Down
Loading

0 comments on commit ace7b16

Please sign in to comment.