Skip to content

Instantly share code, notes, and snippets.

@jordanterry
Created March 25, 2021 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanterry/dd5467956dff57e02c8c27f45e512a5e to your computer and use it in GitHub Desktop.
Save jordanterry/dd5467956dff57e02c8c27f45e512a5e to your computer and use it in GitHub Desktop.
fun checkIfServerIsUnhealthy(
errorRates: DoubleArray,
time: Int,
timeRange: Int,
minimumErrorThreshold: Double
): Boolean {
if (timeRange > errorRates.size) throw IllegalArgumentException()
var left = time - timeRange + 1
var right = time + timeRange
if (left < 0) {
val difference = left * -1
left += difference
right += difference
}
var upperBound = right
if (upperBound > errorRates.size) {
val difference = upperBound - errorRates.size
upperBound -= difference
}
for (i in right until upperBound) {
var isUnhealthy = true
for (j in left until right) {
if (errorRates[j] < minimumErrorThreshold) isUnhealthy = false
}
if (isUnhealthy) return true
left++
right++
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment