Skip to content

Commit

Permalink
structure response body for route of pattern detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jooaodanieel committed Apr 24, 2022
1 parent eaac946 commit 4ddf960
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sortinghat.pattern_detector.api

import com.sortinghat.pattern_detector.domain.model.DatabasePerService
import kotlinx.serialization.Serializable

@Serializable
data class PatternsInSystemPayload(
val system: String,
val patterns: PatternsPresent
) {
companion object {
fun create(system: String, databasePerServices: Set<DatabasePerService>): PatternsInSystemPayload {
return PatternsInSystemPayload(
system = system,
patterns = PatternsPresent(
databasePerService = databasePerServices
)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sortinghat.pattern_detector.api

import com.sortinghat.pattern_detector.domain.model.DatabasePerService
import kotlinx.serialization.Serializable

@Serializable
data class PatternsPresent(
val databasePerService: Set<DatabasePerService>
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sortinghat.pattern_detector.api.plugins

import com.sortinghat.pattern_detector.api.PatternsInSystemPayload
import com.sortinghat.pattern_detector.domain.model.ServiceRepository
import com.sortinghat.pattern_detector.domain.services.DatabasePerServiceDetector
import com.sortinghat.pattern_detector.domain.services.MetricCollector
Expand All @@ -12,26 +13,27 @@ fun Application.configureRouting(serviceRepository: ServiceRepository) {
routing {
get("/systems/{slug}/patterns") {
val slugParam = call.parameters["slug"]
?: call.respond(
?: return@get call.respond(
status = HttpStatusCode.BadRequest,
message = mapOf("error" to "slug must be provided")
)

val services = serviceRepository.findAllOfSystem(slugParam as String)
val services = serviceRepository.findAllOfSystem(slugParam)

val metricCollector = MetricCollector()
val databasePerServiceDetector = DatabasePerServiceDetector()
val collector = MetricCollector()
val dbpsDetector = DatabasePerServiceDetector()

services.forEach { it.accept(metricCollector) }
services.forEach { it.accept(databasePerServiceDetector) }
services.forEach { it.accept(collector) }
services.forEach { it.accept(dbpsDetector) }

val dbps = databasePerServiceDetector.getResults()

val patterns = mapOf("DatabasePerService" to dbps)
val body = PatternsInSystemPayload.create(
system = slugParam,
databasePerServices = dbpsDetector.getResults()
)

call.respond(
status = HttpStatusCode.OK,
message = mapOf("patterns" to patterns)
message = body
)
}
}
Expand Down

0 comments on commit 4ddf960

Please sign in to comment.