Skip to content

Commit

Permalink
Refactor DescribeNetworkProfiles logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelpasterz authored and mergify-bot committed Apr 29, 2021
1 parent f2cb52b commit 2342154
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ftl.domain

import flank.common.logLn
import ftl.environment.networkProfileDescription
import ftl.presentation.Output
import ftl.run.exception.FlankConfigurationError

interface DescribeNetworkProfiles {
interface DescribeNetworkProfiles : Output {
val profileId: String
}

operator fun DescribeNetworkProfiles.invoke() {
if (profileId.isBlank()) throw FlankConfigurationError("Argument PROFILE_ID must be specified.")
logLn(networkProfileDescription(profileId))
networkProfileDescription(profileId).out()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,19 @@ import ftl.api.fetchNetworkProfiles

fun networkProfileDescription(profile: String) = fetchNetworkProfiles()
.find { it.id.equals(profile, ignoreCase = true) }
.toNullProof()
.prepareDescription()
?: "Unable to fetch profile [$profile] description"

private fun NetworkProfile?.toNullProof() = this?.run {
NetworkConfigurationWrapper(
downRule = wrappedOrEmpty(downRule),
upRule = wrappedOrEmpty(upRule),
id = id.toStringOrUnableToFetch()
)
}

private fun wrappedOrEmpty(rule: NetworkProfile.Rule?) = rule?.let {
Rule(
bandwidth = it.bandwidth.toStringOrUnableToFetch(),
delay = it.delay.toStringOrUnableToFetch(),
packetLossRatio = it.packetLossRatio.toStringOrUnableToFetch()
)
} ?: emptyRule

private const val UNABLE = "[Unable to fetch]"

private fun Any?.toStringOrUnableToFetch() = this?.toString() ?: UNABLE

private val emptyRule: Rule
get() = Rule(UNABLE, UNABLE, UNABLE)

private fun NetworkConfigurationWrapper?.prepareDescription() = this?.run {
fun NetworkProfile.prepareDescription() =
"""
downRule:
bandwidth: ${downRule.bandwidth}
delay: ${downRule.delay}
packetLossRatio: ${downRule.packetLossRatio}
bandwidth: ${downRule.bandwidth.orUnable()}
delay: ${downRule.delay.orUnable()}
packetLossRatio: ${downRule.packetLossRatio.orUnable()}
id: $id
upRule:
bandwidth: ${upRule.bandwidth}
delay: ${upRule.delay}
packetLossRatio: ${upRule.packetLossRatio}
bandwidth: ${upRule.bandwidth.orUnable()}
delay: ${upRule.delay.orUnable()}
packetLossRatio: ${upRule.packetLossRatio.orUnable()}
""".trimIndent()
}

private data class NetworkConfigurationWrapper(val downRule: Rule, val upRule: Rule, val id: String)

private data class Rule(val bandwidth: String, val delay: String, val packetLossRatio: String)
private fun Any?.orUnable() = this ?: "[Unable to fetch]"
19 changes: 19 additions & 0 deletions test_runner/src/main/kotlin/ftl/presentation/Output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ftl.presentation

import flank.common.logLn

/**
* The abstraction which allows passing output from the domain to presentation.
* Implement in domain top-level interfaces for getting access to outputting result structures.
* @property out The reference to outputting result function.
*/
interface Output {
val out: Any.() -> Unit
}

fun outputLogger(map: Any.() -> String): Any.() -> Unit = {
logLn(map())
}

fun Any.throwUnknownType(): Nothing =
throw IllegalArgumentException(javaClass.toGenericString())
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ftl.presentation.cli.firebase.test.networkprofiles

import ftl.api.NetworkProfile
import ftl.domain.DescribeNetworkProfiles
import ftl.domain.invoke
import ftl.environment.prepareDescription
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -32,4 +36,12 @@ class NetworkProfilesDescribeCommand :
override var profileId: String = ""

override fun run() = invoke()

override val out = outputLogger {
when (this) {
is NetworkProfile -> prepareDescription()
is String -> this
else -> throwUnknownType()
}
}
}

0 comments on commit 2342154

Please sign in to comment.