-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It behaves like a log scale for powers of 10 with linear behavior in between. This helps spread out the smaller values. This can be useful for things like a heatmap view of percentile distributions.
- Loading branch information
1 parent
49ea9b8
commit 3e5fe07
Showing
22 changed files
with
326 additions
and
9 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
atlas-chart/src/main/scala/com/netflix/atlas/chart/graphics/LogLinear.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2014-2023 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.chart.graphics | ||
|
||
/** | ||
* Helper functions for log-linear scale. This scale does powers of 10 with a linear scale | ||
* between each power of 10. This helps emphasize some of the smaller values when there is | ||
* a larger overall range. | ||
*/ | ||
private[graphics] object LogLinear { | ||
|
||
/** | ||
* Find the max value for a given bucket. If `i` is negative it will select the negative | ||
* bucket value. | ||
*/ | ||
def bucket(i: Int): Double = { | ||
if (i < 0) | ||
-bucket(-i - 1) | ||
else | ||
bucketSpan(i) * (i % 9 + 1) | ||
} | ||
|
||
/** | ||
* Find the span for a given bucket. This should be the previous power of 10. | ||
*/ | ||
def bucketSpan(i: Int): Double = { | ||
val idx = if (i < 0) -i - 1 else i | ||
val exp = idx / 9 - 9 | ||
math.pow(10, exp) | ||
} | ||
|
||
/** Power of 10 for a long value. */ | ||
private def pow(exp: Int): Long = { | ||
var result = 1L | ||
var i = 0 | ||
while (i < exp) { | ||
result *= 10 | ||
i += 1 | ||
} | ||
result | ||
} | ||
|
||
/** | ||
* Find the bucket index for a given value. Negative values will return a negative index | ||
* that reflect the positive scale. | ||
*/ | ||
def bucketIndex(v: Double): Int = { | ||
if (v < 0.0) { | ||
-bucketIndex(-v) - 1 | ||
} else if (v == 0.0) { | ||
0 | ||
} else { | ||
val lg = math.max(-9.0, math.floor(math.log10(v))) | ||
val prevBuckets = (lg.toInt + 9) * 9 | ||
val E = 6.0 - lg | ||
if (E >= 0.0) { | ||
// For values in this range convert to Long and use integer math | ||
// to avoid some problems with floating point | ||
val n = (v * math.pow(10, E)).toLong | ||
val exp = lg.toInt + E.toInt | ||
val p10 = pow(exp) | ||
((n - 1) / p10).toInt + prevBuckets | ||
} else { | ||
val p10 = math.pow(10, lg) | ||
val delta = v - p10 | ||
math.ceil(delta / p10).toInt + prevBuckets | ||
} | ||
} | ||
} | ||
|
||
private def ratio(v: Double, i: Int): Double = { | ||
if (v < 0.0) { | ||
1.0 - ratio(-v, -i - 1) | ||
} else { | ||
val span = bucketSpan(i) | ||
val boundary = bucket(i) - span | ||
(v - boundary) / span | ||
} | ||
} | ||
|
||
/** Determine the pixel position for a given value. */ | ||
def position(v: Double, min: Int, pixelsPerBucket: Double): Double = { | ||
val i = bucketIndex(v) | ||
val offset = math.max(0.0, i - min - 1) * pixelsPerBucket | ||
ratio(v, i) * pixelsPerBucket + offset | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+48.4 KB
...ces/graphengine/DefaultGraphEngineSuite/dark_heatmap_basic_color_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.3 KB
...resources/graphengine/DefaultGraphEngineSuite/dark_heatmap_basic_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.5 KB
.../resources/graphengine/DefaultGraphEngineSuite/dark_heatmap_dist_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.2 KB
...esources/graphengine/DefaultGraphEngineSuite/dark_heatmap_timer2_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.3 KB
...resources/graphengine/DefaultGraphEngineSuite/dark_heatmap_timer_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+48.7 KB
...esources/graphengine/DefaultGraphEngineSuite/heatmap_basic_color_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.1 KB
...test/resources/graphengine/DefaultGraphEngineSuite/heatmap_basic_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.7 KB
.../test/resources/graphengine/DefaultGraphEngineSuite/heatmap_dist_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33 KB
...est/resources/graphengine/DefaultGraphEngineSuite/heatmap_timer2_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.4 KB
...test/resources/graphengine/DefaultGraphEngineSuite/heatmap_timer_log_linear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
atlas-chart/src/test/scala/com/netflix/atlas/chart/graphics/LogLinearSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2014-2023 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.chart.graphics | ||
|
||
import munit.FunSuite | ||
|
||
import scala.collection.immutable.ArraySeq | ||
|
||
class LogLinearSuite extends FunSuite { | ||
|
||
private val expectedBuckets = { | ||
val builder = ArraySeq.newBuilder[Double] | ||
var v = 1L | ||
var delta = 1L | ||
while (v < 9_000_000_000_000_000_000L) { | ||
builder += v / 1e9 | ||
v += delta | ||
if (v % (10 * delta) == 0) | ||
delta *= 10 | ||
} | ||
builder.result() | ||
} | ||
|
||
test("bucket") { | ||
var i = 0 | ||
while (i < expectedBuckets.length) { | ||
val v = expectedBuckets(i) | ||
assertEqualsDouble(LogLinear.bucket(i), v, 1e-12) | ||
i += 1 | ||
} | ||
} | ||
|
||
test("bucketIndex") { | ||
var i = 0 | ||
while (i < expectedBuckets.length) { | ||
val v = expectedBuckets(i) | ||
assertEquals(LogLinear.bucketIndex(v), i, s"$v") | ||
val v2 = v - v / 20.0 | ||
assertEquals(LogLinear.bucketIndex(v2), i, s"$v2") | ||
i += 1 | ||
} | ||
} | ||
|
||
test("bucketIndex near boundary") { | ||
var i = 0 | ||
while (i < expectedBuckets.length) { | ||
val b = expectedBuckets(i) | ||
val delta = math.pow(10, math.log10(b) - 3.0) | ||
val v = b + delta | ||
assertEquals(LogLinear.bucketIndex(v), i + 1, s"$v") | ||
val v2 = b - delta | ||
assertEquals(LogLinear.bucketIndex(v2), i, s"$v2") | ||
i += 1 | ||
} | ||
} | ||
|
||
test("bucketIndex near boundary negative") { | ||
var i = 0 | ||
while (i < expectedBuckets.length) { | ||
val b = -expectedBuckets(i) | ||
val delta = math.pow(10, math.log10(-b) - 3.0) | ||
val v = b + delta | ||
assertEquals(LogLinear.bucketIndex(v), -i - 1, s"$v") | ||
val v2 = b - delta | ||
assertEquals(LogLinear.bucketIndex(v2), -i - 2, s"$v2") | ||
i += 1 | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.