Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refactor DescribeNetworkProfiles logging #1885

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ data class NetworkProfile(
val burst: Float?
)

data class ErrorMessage(
val message: String
)

interface Fetch : () -> List<NetworkProfile>
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package ftl.domain

import flank.common.logLn
import ftl.api.NetworkProfile
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))
val description = networkProfileDescription(profileId)
?: NetworkProfile.ErrorMessage("Unable to fetch profile [$profileId] description")

description.out()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,20 @@ package ftl.environment
import ftl.api.NetworkProfile
import ftl.api.fetchNetworkProfiles

fun networkProfileDescription(profile: String) = fetchNetworkProfiles()
fun networkProfileDescription(profile: String): NetworkProfile? = 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(): String =
"""
downRule:
bandwidth: ${downRule.bandwidth}
delay: ${downRule.delay}
packetLossRatio: ${downRule.packetLossRatio}
bandwidth: ${downRule.bandwidth ?: UNABLE_TO_FETCH}
delay: ${downRule.delay ?: UNABLE_TO_FETCH}
packetLossRatio: ${downRule.packetLossRatio ?: UNABLE_TO_FETCH}
id: $id
upRule:
bandwidth: ${upRule.bandwidth}
delay: ${upRule.delay}
packetLossRatio: ${upRule.packetLossRatio}
bandwidth: ${upRule.bandwidth ?: UNABLE_TO_FETCH}
delay: ${upRule.delay ?: UNABLE_TO_FETCH}
packetLossRatio: ${upRule.packetLossRatio ?: UNABLE_TO_FETCH}
""".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 const val UNABLE_TO_FETCH = "[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 NetworkProfile.ErrorMessage -> message
else -> throwUnknownType()
}
}
}