Skip to content

Commit 8b9c09d

Browse files
committed
Add trapper to telemetry
1 parent 29039b5 commit 8b9c09d

File tree

5 files changed

+124
-3
lines changed

5 files changed

+124
-3
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ buildscript {
1212

1313
configure(subprojects) {
1414
group = 'org.strykeforce.thirdcoast'
15-
version = '19.0.1'
15+
version = '19.1.0'
1616

1717
apply plugin: 'java-library'
1818
apply plugin: 'idea'
@@ -39,6 +39,8 @@ configure(subprojects) {
3939
module {
4040
downloadJavadoc = true
4141
downloadSources = true
42+
sourceDirs += files('build/generated/source/kaptKotlin/main')
43+
generatedSourceDirs += files('build/generated/source/kaptKotlin/main')
4244
}
4345
}
4446

telemetry/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ buildscript {
1515
}
1616

1717
apply plugin: "kotlin"
18+
apply plugin: "kotlin-kapt"
1819
apply plugin: 'org.jetbrains.dokka'
1920

2021
sourceCompatibility = 11
@@ -28,8 +29,10 @@ dependencies {
2829
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
2930

3031
// telemetry
31-
implementation 'org.eclipse.jetty:jetty-server:9.4.12.v20180830'
32-
implementation 'com.squareup.moshi:moshi:1.5.0'
32+
implementation "org.eclipse.jetty:jetty-server:9.4.12.v20180830"
33+
implementation "com.squareup.moshi:moshi:1.8.0"
34+
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.8.0"
35+
implementation "com.squareup.okhttp3:okhttp:3.12.0"
3336
implementation project(':swerve')
3437

3538
// Logging
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.strykeforce.thirdcoast.trapper
2+
3+
import com.squareup.moshi.JsonClass
4+
import java.time.LocalDateTime
5+
6+
internal fun actionDefaultName() = "Action ${LocalDateTime.now()}"
7+
8+
@JsonClass(generateAdapter = true)
9+
data class Action(
10+
val activity: Activity? = null,
11+
val name: String = actionDefaultName(),
12+
val meta: MutableMap<String, Any> = mutableMapOf(),
13+
val measures: List<String> = emptyList(),
14+
val data: MutableList<Double> = mutableListOf(),
15+
val traceMeasures: List<String> = emptyList(),
16+
val traceData: MutableList<List<Double>> = mutableListOf()
17+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.strykeforce.thirdcoast.trapper
2+
3+
import com.squareup.moshi.JsonClass
4+
import java.time.LocalDateTime
5+
import java.util.*
6+
7+
@JsonClass(generateAdapter = true)
8+
data class Activity(
9+
val id: UUID = UUID.randomUUID(),
10+
val name: String = "Activity ${LocalDateTime.now()}",
11+
val meta: MutableMap<String, Any> = mutableMapOf(),
12+
val measures: List<String> = emptyList(),
13+
val data: MutableList<Double> = mutableListOf()
14+
) {
15+
16+
fun newAction(
17+
name: String = actionDefaultName(),
18+
measures: List<String> = emptyList(),
19+
traceMeasures: List<String> = emptyList()
20+
): Action =
21+
Action(this, name, measures = measures, traceMeasures = traceMeasures)
22+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.strykeforce.thirdcoast.trapper
2+
3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.Moshi
5+
import com.squareup.moshi.ToJson
6+
import okhttp3.*
7+
import okio.BufferedSink
8+
import java.util.*
9+
10+
val JSON = MediaType.get("application/json; charset=utf-8")
11+
12+
object Session {
13+
val client = OkHttpClient()
14+
15+
var baseUrl = "http://example.com:5000"
16+
set(value) {
17+
field = value
18+
activityEndpoint = HttpUrl.get("$value/activities")
19+
actionEndpoint = HttpUrl.get("$value/actions")
20+
}
21+
var activityEndpoint: HttpUrl = HttpUrl.get("$baseUrl/activities")
22+
var actionEndpoint: HttpUrl = HttpUrl.get("$baseUrl/actions")
23+
24+
25+
fun post(url: HttpUrl, json: String) {
26+
val body = RequestBody.create(JSON, json)
27+
val request = Request.Builder().url(url).post(body).build()
28+
29+
client.newCall(request).execute().use { println("response: ${it.body()?.string()}") }
30+
}
31+
32+
fun post(url: HttpUrl, body: RequestBody) {
33+
val request = Request.Builder().url(url).post(body).build()
34+
client.newCall(request).execute().use { println("response: ${it.body()?.string()}") }
35+
}
36+
37+
}
38+
39+
private val moshi = Moshi.Builder().add(UUIDAdapter()).add(ActivityAdapter()).build()
40+
private val activityJsonAdapter = ActivityJsonAdapter(moshi)
41+
42+
/** Post an Activity to the web server specified in URL. */
43+
fun Activity.post() {
44+
val body = object : RequestBody() {
45+
override fun contentType() = JSON
46+
override fun writeTo(sink: BufferedSink) = activityJsonAdapter.toJson(sink, this@post)
47+
}
48+
Session.post(Session.activityEndpoint, body)
49+
}
50+
51+
private val actionJsonAdapter = ActionJsonAdapter(moshi).serializeNulls()
52+
53+
/** Post an Action to the web server specified in URL. The parent activity must be posted first or this will fail. */
54+
fun Action.post() {
55+
val body = object : RequestBody() {
56+
override fun contentType() = JSON
57+
override fun writeTo(sink: BufferedSink) = actionJsonAdapter.toJson(sink, this@post)
58+
}
59+
Session.post(Session.actionEndpoint, body)
60+
}
61+
62+
internal class UUIDAdapter {
63+
64+
@ToJson
65+
fun toJson(uuid: UUID): String = uuid.toString()
66+
67+
@FromJson
68+
fun fromJson(uuid: String) = UUID.fromString(uuid)
69+
}
70+
71+
internal class ActivityAdapter {
72+
@ToJson
73+
fun toJson(activity: Activity): UUID = activity.id
74+
75+
@FromJson
76+
fun fromJson(uuid: UUID) = Activity(id = uuid, name = "UNKNOWN")
77+
}

0 commit comments

Comments
 (0)