-
Notifications
You must be signed in to change notification settings - Fork 1
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
Enforce valid KpiResult scores #30
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import de.fraunhofer.iem.spha.core.hierarchy.KpiHierarchyEdge | |
import de.fraunhofer.iem.spha.model.kpi.KpiStrategyId | ||
import de.fraunhofer.iem.spha.model.kpi.hierarchy.KpiCalculationResult | ||
import de.fraunhofer.iem.spha.model.kpi.hierarchy.KpiNode | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
|
||
internal fun getKpiCalculationStrategy(strategyId: KpiStrategyId): KpiCalculationStrategy { | ||
return when (strategyId) { | ||
|
@@ -60,6 +61,8 @@ internal interface KpiCalculationStrategy { | |
fun isValid(node: KpiNode, strict: Boolean = false): Boolean | ||
} | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
internal abstract class BaseKpiCalculationStrategy : KpiCalculationStrategy { | ||
|
||
abstract val kpiStrategyId: KpiStrategyId | ||
|
@@ -82,7 +85,7 @@ internal abstract class BaseKpiCalculationStrategy : KpiCalculationStrategy { | |
|
||
updateEdgeWeights(edges = hierarchyEdges, strict) | ||
|
||
val result = internalCalculateKpi(hierarchyEdges) | ||
val result = getResultInValidRange(internalCalculateKpi(hierarchyEdges)) | ||
|
||
if ( | ||
hierarchyEdges.any { it.to.result !is KpiCalculationResult.Success } && | ||
|
@@ -149,4 +152,44 @@ internal abstract class BaseKpiCalculationStrategy : KpiCalculationStrategy { | |
} | ||
|
||
protected abstract fun internalIsValid(node: KpiNode, strict: Boolean): Boolean | ||
|
||
companion object { | ||
/** | ||
* Checks if `result.score` is in a valid range (0..100). If the score is lower 0 or higher | ||
* than 100, we return 0 or 100 respectively. | ||
* | ||
* @param result | ||
*/ | ||
fun getResultInValidRange(result: KpiCalculationResult): KpiCalculationResult { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a point where we should throw in strict mode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in strict mode we would fail even earlier as we then enforce the validity of the given KPI hierarchy and a valid hierarchy should not result in invalid values. |
||
return when (result) { | ||
is KpiCalculationResult.Success -> | ||
createValidScore(result, result.score) { score -> | ||
KpiCalculationResult.Success(score) | ||
} | ||
is KpiCalculationResult.Incomplete -> | ||
createValidScore(result, result.score) { score -> | ||
KpiCalculationResult.Incomplete(score, result.reason) | ||
} | ||
else -> result | ||
} | ||
} | ||
|
||
private fun <T : KpiCalculationResult> createValidScore( | ||
result: T, | ||
score: Int, | ||
createResult: (Int) -> T, | ||
): T { | ||
return when { | ||
score < 0 -> { | ||
logger.warn { "Calculation result score $result is out of bounds." } | ||
createResult(0) | ||
} | ||
score > 100 -> { | ||
logger.warn { "Calculation result score $result is out of bounds." } | ||
createResult(100) | ||
} | ||
else -> result | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intented to be on global scope? How does the private modifier work in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logging library says to use it this way, I didn't check why and trusted them.
private makes it accessible in the whole file and it should be initialized first.