Skip to content

Commit a45a9b3

Browse files
committed
basic impl
1 parent 4fca6ea commit a45a9b3

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

src/main/kotlin/spp/cli/commands/view/SubscribeView.kt

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import eu.geekplace.javapinning.JavaPinning
99
import eu.geekplace.javapinning.pin.Pin
1010
import io.vertx.core.Vertx
1111
import io.vertx.core.json.Json
12+
import io.vertx.core.json.JsonArray
1213
import io.vertx.core.json.JsonObject
1314
import io.vertx.core.net.NetClientOptions
1415
import io.vertx.core.net.TrustOptions
@@ -24,15 +25,25 @@ import spp.protocol.artifact.log.LogOrderType
2425
import spp.protocol.artifact.log.LogResult
2526
import spp.protocol.extend.TCPServiceFrameParser
2627
import spp.protocol.view.LiveViewEvent
28+
import java.time.LocalDateTime
29+
import java.time.ZoneOffset
30+
import java.time.format.DateTimeFormatterBuilder
2731

2832
class SubscribeView : CliktCommand(
2933
help = "Listens for and outputs live views. Subscribes to all views by default"
3034
) {
3135

32-
val viewIds by argument(
33-
name = "View Subscription IDs",
34-
help = "Capture events from specific live views"
35-
).multiple()
36+
companion object {
37+
private val formatter = DateTimeFormatterBuilder()
38+
.appendPattern("yyyyMMddHHmm")
39+
.toFormatter()
40+
.withZone(ZoneOffset.UTC)
41+
}
42+
43+
// val viewIds by argument(
44+
// name = "View Subscription IDs",
45+
// help = "Capture events from specific live views"
46+
// ).multiple()
3647
val outputFullEvent by option(
3748
"--full",
3849
help = "Output full event data"
@@ -68,19 +79,12 @@ class SubscribeView : CliktCommand(
6879
vertx.eventBus().consumer<JsonObject>("local.$LIVE_VIEW_SUBSCRIBER.${PlatformCLI.developer.id}") {
6980
val event = Json.decodeValue(it.body().toString(), LiveViewEvent::class.java)
7081
if (outputFullEvent) {
71-
println(JsonObject(event.metricsData))
82+
println(event.metricsData)
7283
} else {
73-
val rawMetrics = JsonObject(event.metricsData)
74-
val logData = Json.decodeValue(rawMetrics.getJsonObject("log").toString(), Log::class.java)
75-
val logsResult = LogResult(
76-
event.artifactQualifiedName,
77-
LogOrderType.NEWEST_LOGS,
78-
logData.timestamp,
79-
listOf(logData),
80-
Int.MAX_VALUE
81-
)
82-
logsResult.logs.forEach {
83-
println(it.getFormattedMessage())
84+
when (event.viewConfig.viewName) {
85+
"LOG" -> outputLogEvent(event)
86+
"ACTIVITY" -> outputActivityEvent(event)
87+
else -> println(JsonObject(event.metricsData).encodePrettily())
8488
}
8589
}
8690
}
@@ -95,4 +99,35 @@ class SubscribeView : CliktCommand(
9599
println("Listening for events...")
96100
}
97101
}
102+
103+
private fun outputActivityEvent(event: LiveViewEvent) {
104+
val metrics = JsonArray(event.metricsData)
105+
for (i in 0 until metrics.size()) {
106+
val metric = metrics.getJsonObject(i)
107+
val eventTime = LocalDateTime.from(formatter.parse(metric.getString("timeBucket")))
108+
var value: String? = null
109+
if (metric.getNumber("percentage") != null) {
110+
value = (metric.getNumber("percentage").toDouble() / 100.0).toString() + "%"
111+
}
112+
if (value == null) value = metric.getNumber("value").toString()
113+
114+
val metricType = metric.getJsonObject("meta").getString("metricsName")
115+
println("$eventTime ${metric.getString("entityId")} ($metricType): $value")
116+
}
117+
}
118+
119+
private fun outputLogEvent(event: LiveViewEvent) {
120+
val rawMetrics = JsonObject(event.metricsData)
121+
val logData = Json.decodeValue(rawMetrics.getJsonObject("log").toString(), Log::class.java)
122+
val logsResult = LogResult(
123+
event.artifactQualifiedName,
124+
LogOrderType.NEWEST_LOGS,
125+
logData.timestamp,
126+
listOf(logData),
127+
Int.MAX_VALUE
128+
)
129+
logsResult.logs.forEach {
130+
println(it.getFormattedMessage())
131+
}
132+
}
98133
}

0 commit comments

Comments
 (0)