All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.tracing.renderer.PumlInteractionDiagram.kt Maven / Gradle / Ivy

There is a newer version: 5.41.0.0
Show newest version
package org.http4k.tracing.renderer

import org.http4k.tracing.Actor
import org.http4k.tracing.ActorType.Database
import org.http4k.tracing.ActorType.Human
import org.http4k.tracing.Trace
import org.http4k.tracing.TraceRender
import org.http4k.tracing.TraceRenderer
import org.http4k.tracing.TraceStep

object PumlInteractionDiagram : TraceRenderer {
    override fun render(scenarioName: String, steps: List): TraceRender {
        val traces = steps.filterIsInstance()

        val relations = traces.flatMap { it.relations() }.toSet()

        return TraceRender(
            "$scenarioName - Interactions",
            "PUML",
            """@startuml
title $scenarioName

!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml


${traces.chronologicalActors().toPumlActor().joinToString("\n")}    
${relations.joinToString("\n") { "Rel_D(${it.origin.identifier()}, ${it.target.identifier()}, \" \") " }}    
@enduml""".trimMargin()
        )
    }

    private fun Iterable.toPumlActor() =
        fold(emptyList()) { acc, it ->
            val nextVal = when (it.type) {
                Database -> "ContainerDb"
                Human -> "Person"
                else -> "System"
            } + "(${it.name.identifier()}, \"${it.name}\")"

            if (acc.contains(nextVal)) acc else acc + nextVal
        }

    private fun Trace.relations(): List =
        listOf(Call(origin.name, target.name)) + children.flatMap { it.relations() }

    private data class Call(val origin: String, val target: String)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy