Skip to content

Commit 24562c4

Browse files
committed
Refactor LiveView implementation and Service object structure
1 parent 36c8a4d commit 24562c4

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

src/main/kotlin/spp/protocol/platform/general/Service.kt

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,24 @@ import spp.protocol.platform.general.util.IDManager
2727
*/
2828
@DataObject
2929
data class Service(
30-
val id: String,
3130
val name: String,
3231
val group: String = "",
3332
val shortName: String? = null,
3433
val layers: List<String> = emptyList(),
35-
val normal: Boolean = true
34+
val normal: Boolean = true,
35+
val environment: String? = null,
36+
val commitId: String? = null
3637
) {
38+
39+
val id by lazy {
40+
if (commitId != null) {
41+
IDManager.ServiceID.buildId("$name|$environment|$commitId", normal)
42+
} else {
43+
IDManager.ServiceID.buildId(name, normal)
44+
}
45+
}
46+
3747
constructor(json: JsonObject) : this(
38-
json.getString("id"),
3948
json.getString("name"),
4049
json.getString("group"),
4150
json.getString("shortName"),
@@ -45,7 +54,6 @@ data class Service(
4554

4655
fun toJson(): JsonObject {
4756
val json = JsonObject()
48-
json.put("id", id)
4957
json.put("name", name)
5058
json.put("group", group)
5159
json.put("shortName", shortName)
@@ -54,23 +62,59 @@ data class Service(
5462
return json
5563
}
5664

65+
fun withEnvironment(environment: String?): Service {
66+
return copy(environment = environment)
67+
}
68+
69+
fun withCommitId(commitId: String?): Service {
70+
return copy(commitId = commitId)
71+
}
72+
73+
/**
74+
* Ensures all non-null fields are equal.
75+
*/
76+
fun isSameLocation(other: Service): Boolean {
77+
if (name != other.name) return false
78+
if (group != other.group) return false
79+
if (shortName != null && shortName != other.shortName) return false
80+
if (layers != other.layers) return false
81+
if (normal != other.normal) return false
82+
if (environment != null && environment != other.environment) return false
83+
if (commitId != null && commitId != other.commitId) return false
84+
return true
85+
}
86+
87+
fun withName(name: String?): Service {
88+
if (name == null) return this
89+
if (name.contains("|")) {
90+
val parts = name.split("|")
91+
return withName(parts[0])
92+
.withEnvironment(parts[1])
93+
.withCommitId(parts[2])
94+
}
95+
return copy(name = name)
96+
}
97+
5798
companion object {
99+
100+
@JvmStatic
58101
fun fromId(id: String): Service {
59102
val definition = IDManager.ServiceID.analysisId(id)
60-
return Service(
61-
id = id,
62-
name = definition.name,
63-
normal = definition.isReal
64-
)
103+
return fromName(definition.name)
65104
}
66105

106+
@JvmStatic
67107
fun fromName(name: String): Service {
68-
return Service(
69-
id = IDManager.ServiceID.buildId(name, true),
70-
name = name
71-
)
108+
if (name.contains("|")) {
109+
val parts = name.split("|")
110+
return fromName(parts[0])
111+
.withEnvironment(parts[1])
112+
.withCommitId(parts[2])
113+
}
114+
return Service(name = name)
72115
}
73116

117+
@JvmStatic
74118
fun fromNameIfPresent(name: String?): Service? {
75119
return if (name != null) {
76120
fromName(name)

src/main/kotlin/spp/protocol/view/LiveView.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import io.vertx.codegen.annotations.DataObject
2020
import io.vertx.core.Vertx
2121
import io.vertx.core.json.JsonArray
2222
import io.vertx.core.json.JsonObject
23-
import spp.protocol.artifact.ArtifactQualifiedName
24-
import spp.protocol.instrument.location.LiveSourceLocation
23+
import spp.protocol.platform.general.Service
2524
import spp.protocol.service.SourceServices.Subscribe.toLiveViewSubscription
2625

2726
/**
@@ -32,27 +31,27 @@ import spp.protocol.service.SourceServices.Subscribe.toLiveViewSubscription
3231
*/
3332
@DataObject
3433
data class LiveView(
35-
val subscriptionId: String? = null, //todo: actual bottom
3634
val entityIds: MutableSet<String>,
37-
val artifactQualifiedName: ArtifactQualifiedName? = null, //todo: remove, use artifactLocation
38-
val artifactLocation: LiveSourceLocation? = null, //todo: bottom?
39-
val viewConfig: LiveViewConfig
35+
val viewConfig: LiveViewConfig,
36+
val service: Service? = null,
37+
val serviceInstance: String? = null,
38+
val subscriptionId: String? = null
4039
) {
4140
constructor(json: JsonObject) : this(
4241
subscriptionId = json.getString("subscriptionId"),
4342
entityIds = json.getJsonArray("entityIds").map { it.toString() }.toMutableSet(),
44-
artifactQualifiedName = json.getJsonObject("artifactQualifiedName")?.let { ArtifactQualifiedName(it) },
45-
artifactLocation = json.getJsonObject("artifactLocation")?.let { LiveSourceLocation(it) },
46-
viewConfig = LiveViewConfig(json.getJsonObject("viewConfig"))
43+
viewConfig = LiveViewConfig(json.getJsonObject("viewConfig")),
44+
service = json.getJsonObject("service")?.let { Service(it) },
45+
serviceInstance = json.getString("serviceInstance")
4746
)
4847

4948
fun toJson(): JsonObject {
5049
val json = JsonObject()
5150
json.put("subscriptionId", subscriptionId)
5251
json.put("entityIds", JsonArray().apply { entityIds.forEach { add(it) } })
53-
artifactQualifiedName?.let { json.put("artifactQualifiedName", it.toJson()) }
54-
artifactLocation?.let { json.put("artifactLocation", it.toJson()) }
5552
json.put("viewConfig", viewConfig.toJson())
53+
service?.let { json.put("service", it.toJson()) }
54+
json.put("serviceInstance", serviceInstance)
5655
return json
5756
}
5857

@@ -73,9 +72,9 @@ data class LiveView(
7372
append("LiveView(")
7473
if (subscriptionId != null) append("subscriptionId=$subscriptionId, ")
7574
append("entityIds=$entityIds, ")
76-
if (artifactQualifiedName != null) append("artifactQualifiedName=$artifactQualifiedName, ")
77-
if (artifactLocation != null) append("artifactLocation=$artifactLocation, ")
7875
append("viewConfig=$viewConfig")
76+
if (service != null) append(", service=$service")
77+
if (serviceInstance != null) append(", serviceInstance=$serviceInstance")
7978
append(")")
8079
}
8180
}

src/main/kotlin/spp/protocol/view/LiveViewEvent.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package spp.protocol.view
1818

1919
import io.vertx.codegen.annotations.DataObject
2020
import io.vertx.core.json.JsonObject
21-
import spp.protocol.artifact.ArtifactQualifiedName
2221

2322
/**
2423
* An event that occurred in a [LiveView].
@@ -30,15 +29,13 @@ import spp.protocol.artifact.ArtifactQualifiedName
3029
data class LiveViewEvent(
3130
val subscriptionId: String,
3231
val entityId: String,
33-
val artifactQualifiedName: ArtifactQualifiedName? = null,
3432
val timeBucket: String,
3533
val viewConfig: LiveViewConfig,
3634
val metricsData: String, //todo: type out
3735
) {
3836
constructor(json: JsonObject) : this(
3937
json.getString("subscriptionId"),
4038
json.getString("entityId"),
41-
json.getJsonObject("artifactQualifiedName")?.let { ArtifactQualifiedName(it) },
4239
json.getString("timeBucket"),
4340
LiveViewConfig(json.getJsonObject("viewConfig")),
4441
json.getString("metricsData")

src/test/kotlin/spp/protocol/view/LiveViewTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class LiveViewTest {
2525
@Test
2626
fun `test mutable entity ids`() {
2727
val sub = LiveView(
28-
"id",
2928
mutableSetOf("entity-1"),
30-
viewConfig = LiveViewConfig("name", listOf("metrics"))
29+
viewConfig = LiveViewConfig("name", listOf("metrics")),
30+
subscriptionId = "id"
3131
)
3232
val subJson = sub.toJson()
3333
assertEquals(JsonArray(listOf("entity-1")), subJson.getJsonArray("entityIds"))

0 commit comments

Comments
 (0)