-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
67 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
tool/analytics/mixpanel/src/main/kotlin/flank/tool/analytics/mixpanel/internal/Add.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package flank.tool.analytics.mixpanel.internal | ||
|
||
internal fun addToReport(key: String, reportNode: Any) { | ||
Report.data[key] = reportNode | ||
} |
29 changes: 29 additions & 0 deletions
29
tool/analytics/mixpanel/src/main/kotlin/flank/tool/analytics/mixpanel/internal/Configure.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package flank.tool.analytics.mixpanel.internal | ||
|
||
import flank.tool.analytics.AnonymizeInStatistics | ||
import flank.tool.analytics.IgnoreInStatistics | ||
import kotlin.reflect.KClass | ||
|
||
internal fun configureReport( | ||
projectName: String, | ||
blockUsageStatistics: Boolean, | ||
statisticClasses: Array<out KClass<*>> | ||
) { | ||
Report.projectName = projectName | ||
Report.blockSendUsageStatistics = blockUsageStatistics | ||
Report.keysToRemove = statisticClasses getMembersWith AnonymizeInStatistics::class | ||
Report.keysToAnonymize = statisticClasses getMembersWith IgnoreInStatistics::class | ||
} | ||
|
||
private infix fun Array<out KClass<*>>.getMembersWith( | ||
annotationType: KClass<*> | ||
): Set<String> = | ||
flatMap { type -> | ||
type.members.filter { member -> | ||
member.annotations.any { annotation -> | ||
annotation.annotationClass == annotationType | ||
} | ||
} | ||
}.map { member -> | ||
member.name | ||
}.toSet() |
59 changes: 18 additions & 41 deletions
59
tool/analytics/mixpanel/src/main/kotlin/flank/tool/analytics/mixpanel/internal/Filter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,23 @@ | ||
package flank.tool.analytics.mixpanel.internal | ||
|
||
import flank.tool.analytics.AnonymizeInStatistics | ||
import flank.tool.analytics.IgnoreInStatistics | ||
import flank.tool.analytics.mixpanel.ObjectMap | ||
import kotlin.reflect.KCallable | ||
import kotlin.reflect.KClass | ||
|
||
// ============================Remove not needed ============================ | ||
|
||
internal fun ObjectMap.removeNotNeededKeys(): ObjectMap = filterNot { (key, _) -> key in keysToRemove } | ||
|
||
private val keysToRemove by lazy { | ||
getClassesForStatisticsOrThrow() | ||
.map(findMembersWithAnnotation(IgnoreInStatistics::class)) | ||
.flatten() | ||
} | ||
|
||
// ============================ Remove sensitive ============================ | ||
|
||
internal fun ObjectMap.removeSensitiveValues(): ObjectMap = mapValues { (key, value) -> | ||
when { | ||
key !in keysToAnonymize -> value | ||
value is Map<*, *> -> value.mapValues { ANONYMIZE_VALUE } | ||
value is List<*> -> "Count: $size" | ||
else -> ANONYMIZE_VALUE | ||
internal fun ObjectMap.removeNotNeededKeys( | ||
keysToRemove: Set<String> = Report.keysToAnonymize | ||
): ObjectMap = | ||
if (keysToRemove.isEmpty()) this | ||
else filterNot { (key, _) -> key in Report.keysToRemove } | ||
|
||
internal fun ObjectMap.anonymizeSensitiveValues( | ||
keysToAnonymize: Set<String> = Report.keysToAnonymize, | ||
anonymousValue: String = "...", | ||
): ObjectMap = | ||
if (keysToAnonymize.isEmpty()) this | ||
else mapValues { (key, value) -> | ||
when { | ||
key !in keysToAnonymize -> value | ||
value is Map<*, *> -> value.mapValues { anonymousValue } | ||
value is List<*> -> "Count: ${value.size}" | ||
else -> anonymousValue | ||
} | ||
} | ||
} | ||
|
||
private val keysToAnonymize by lazy { | ||
getClassesForStatisticsOrThrow() | ||
.map(findMembersWithAnnotation(AnonymizeInStatistics::class)) | ||
.flatten() | ||
} | ||
|
||
private const val ANONYMIZE_VALUE = "..." | ||
|
||
// ============================ Common ============================ | ||
|
||
private fun getClassesForStatisticsOrThrow() = | ||
(Report.classesForStatistics ?: throw NullPointerException("Analytics client should be initialized first")) | ||
|
||
private fun findMembersWithAnnotation(annotationType: KClass<*>) = fun KClass<*>.() = members | ||
.filter { member -> member.annotations.any { annotation -> annotation.annotationClass == annotationType } } | ||
.map(KCallable<*>::name) | ||
|
34 changes: 5 additions & 29 deletions
34
tool/analytics/mixpanel/src/main/kotlin/flank/tool/analytics/mixpanel/internal/Report.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,12 @@ | ||
package flank.tool.analytics.mixpanel.internal | ||
|
||
import flank.tool.analytics.mixpanel.internal.send.sendConfiguration | ||
import kotlin.reflect.KClass | ||
|
||
internal object Report { | ||
// Configuration | ||
var projectName: String = "" | ||
var blockSendUsageStatistics: Boolean = false | ||
var classesForStatistics: List<KClass<*>>? = null | ||
val data: MutableMap<String, Any> = mutableMapOf() | ||
} | ||
|
||
internal fun initStatisticsClient(blockUsageStatistics: Boolean, statisticClasses: Array<out KClass<*>>) { | ||
// TODO Verify if condition is needed | ||
if (Report.classesForStatistics != null) return | ||
Report.blockSendUsageStatistics = blockUsageStatistics | ||
Report.classesForStatistics = statisticClasses.asList() | ||
} | ||
var keysToRemove: Set<String> = emptySet() | ||
var keysToAnonymize: Set<String> = emptySet() | ||
|
||
internal fun configureReport( | ||
projectName: String, | ||
blockUsageStatistics: Boolean, | ||
statisticClasses: Array<out KClass<*>> | ||
) { | ||
Report.projectName = projectName | ||
Report.blockSendUsageStatistics = blockUsageStatistics | ||
Report.classesForStatistics = statisticClasses.asList() | ||
} | ||
|
||
internal fun addToReport(key: String, reportNode: Any) { | ||
Report.data[key] = reportNode | ||
} | ||
|
||
internal fun sendReport(eventName: String) { | ||
sendConfiguration(Report.projectName, Report.data, eventName) | ||
// Accumulated data | ||
val data: MutableMap<String, Any> = mutableMapOf() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters