Skip to content

Commit cc8dfaa

Browse files
committed
init changes
1 parent 6c96588 commit cc8dfaa

File tree

4 files changed

+186
-24
lines changed

4 files changed

+186
-24
lines changed

samples/kotlin-android-app-destinations/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ dependencies {
5858
implementation 'com.segment.analytics.kotlin.destinations:appsflyer:1.5.1'
5959
implementation 'com.segment.analytics.kotlin.destinations:firebase:1.5.2'
6060
implementation 'com.segment.analytics.kotlin.destinations:mixpanel:1.5.2'
61+
62+
implementation 'com.eclipsesource.j2v8:j2v8:6.2.0@aar'
6163
}
6264

6365
// Partner Dependencies
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.segment.analytics.destinations.plugins
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.segment.analytics.kotlin.core.BaseEvent
5+
import com.segment.analytics.kotlin.core.TrackEvent
6+
import kotlinx.serialization.json.buildJsonObject
7+
import kotlinx.serialization.json.put
8+
import kotlin.system.measureTimeMillis
9+
import com.eclipsesource.v8.V8
10+
import org.junit.Assert.assertNotNull
11+
import org.junit.Assert.assertNull
12+
import org.junit.Test
13+
import org.junit.runner.RunWith
14+
15+
16+
@RunWith(AndroidJUnit4::class)
17+
class Benchmark {
18+
val e1 = TrackEvent(
19+
event = "App Opened",
20+
properties = buildJsonObject { put("new", true); put("click", true) })
21+
val e2 = TrackEvent(
22+
event = "Screen Opened",
23+
properties = buildJsonObject { put("new", false); put("click", false) })
24+
val e3 = TrackEvent(
25+
event = "App Closed",
26+
properties = buildJsonObject { put("new", false); put("click", true) })
27+
28+
@Test
29+
fun usingExpressions() {
30+
val plugin = DestinationFiltersDropPlugin().apply {
31+
matchers.add(MatcherExpressionImpl())
32+
}
33+
val matcherTime = measureTimeMillis {
34+
assertNotNull(plugin.execute(e1))
35+
assertNull(plugin.execute(e2))
36+
assertNotNull(plugin.execute(e3))
37+
}
38+
println(matcherTime)
39+
}
40+
41+
@Test
42+
fun usingJsRuntime() {
43+
val plugin = DestinationFiltersDropPlugin().apply {
44+
matchers.add(MatcherJSImpl())
45+
}
46+
val matcherTime = measureTimeMillis {
47+
assertNotNull(plugin.execute(e1))
48+
assertNull(plugin.execute(e2))
49+
assertNotNull(plugin.execute(e3))
50+
}
51+
println(matcherTime)
52+
}
53+
}
54+
55+
/*
56+
{
57+
"ir": "["!",["or",["=","event",{"value":"App Opened"}],["=","event",{"value":"App Closed"}]]]",
58+
"type": "fql",
59+
"config": {
60+
"expr": "!(event = \"CP VOD - Start video\" or event = \"CP VOD - Track video\")"
61+
}
62+
}
63+
*/
64+
class MatcherExpressionImpl : Matcher {
65+
override fun match(payload: BaseEvent): Boolean {
66+
payload as TrackEvent
67+
return Not(
68+
Or(
69+
Equals(Path("event"), "App Opened"),
70+
Equals(Path("event"), "App Closed")
71+
)
72+
).evaluate(payload)
73+
}
74+
}
75+
76+
class MatcherJSImpl : Matcher {
77+
78+
val runtime = V8.createV8Runtime()
79+
override fun match(payload: BaseEvent): Boolean {
80+
payload as TrackEvent
81+
val result = runtime.executeBooleanScript(
82+
"""
83+
!("${payload.event}" === "App Opened" || "${payload.event}" === "App Closed")
84+
""".trimIndent()
85+
)
86+
// runtime.close()
87+
return result
88+
}
89+
}

samples/kotlin-android-app-destinations/src/androidTest/java/com/segment/analytics/next/ExampleInstrumentedTest.kt

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.segment.analytics.destinations.plugins
2+
3+
import com.segment.analytics.kotlin.core.Analytics
4+
import com.segment.analytics.kotlin.core.BaseEvent
5+
import com.segment.analytics.kotlin.core.platform.Plugin
6+
7+
/*
8+
{
9+
"matchers": [
10+
{
11+
"ir": "[\"!\",[\"or\",[\"=\",\"event\",{\"value\":\"CP VOD - Start video\"}],[\"=\",\"event\",{\"value\":\"CP VOD - Track video\"}]]]",
12+
"type": "fql",
13+
"config": {
14+
"expr": "!(event = \"CP VOD - Start video\" or event = \"CP VOD - Track video\")"
15+
}
16+
}
17+
],
18+
"scope": "destinations",
19+
"target_type": "workspace::project::destination::config",
20+
"transformers": [
21+
[
22+
{
23+
"type": "drop"
24+
}
25+
]
26+
],
27+
"destinationName": "Amazon Kinesis"
28+
}
29+
*/
30+
31+
class DestinationFiltersDropPlugin : Plugin {
32+
override val type: Plugin.Type = Plugin.Type.Enrichment
33+
override lateinit var analytics: Analytics
34+
35+
val matchers = mutableListOf<Matcher>()
36+
37+
// Implement only drop event
38+
override fun execute(event: BaseEvent): BaseEvent? {
39+
for (matcher in matchers) {
40+
if (matcher.match(event)) {
41+
return null
42+
}
43+
}
44+
return super.execute(event)
45+
}
46+
}
47+
48+
interface Matcher {
49+
/*
50+
{
51+
"ir": "["!",["or",["=","event",{"value":"CP VOD - Start video"}],["=","event",{"value":"CP VOD - Track video"}]]]",
52+
"type": "fql",
53+
"config": {
54+
"expr": "!(event = \"CP VOD - Start video\" or event = \"CP VOD - Track video\")"
55+
}
56+
}
57+
*/
58+
fun match(payload: BaseEvent): Boolean
59+
}
60+
61+
interface Expression {
62+
fun evaluate(event: BaseEvent): Boolean
63+
}
64+
65+
class Not(private val e: Expression) : Expression {
66+
override fun evaluate(event: BaseEvent): Boolean {
67+
return !e.evaluate(event)
68+
}
69+
}
70+
71+
class Or(private val e1: Expression, private val e2: Expression) : Expression {
72+
override fun evaluate(event: BaseEvent): Boolean {
73+
return e1.evaluate(event) || e2.evaluate(event)
74+
}
75+
}
76+
77+
class And(private val e1: Expression, private val e2: Expression) : Expression {
78+
override fun evaluate(event: BaseEvent): Boolean {
79+
return e1.evaluate(event) && e2.evaluate(event)
80+
}
81+
}
82+
83+
class Equals(private val t1: Any, private val t2: Any): Expression {
84+
override fun evaluate(event: BaseEvent): Boolean {
85+
return t1 == t2
86+
}
87+
}
88+
89+
class Path(private val path: String): Expression {
90+
override fun evaluate(event: BaseEvent): Boolean {
91+
return
92+
}
93+
}
94+
95+
// Not(Or(Equals(payload.event, "CP VOD - Start video"), Equals(payload.event, "CP VOD - Track video")))

0 commit comments

Comments
 (0)