Skip to content

Commit

Permalink
eval: use parens for simple legends (#1657)
Browse files Browse the repository at this point in the history
When generating a simple legend with variable substitutions
use the `$(var)` for instead of `$var`. This avoid issues
in some cases where the data source is using invalid key
characters.
  • Loading branch information
brharrington authored May 18, 2024
1 parent 2216b69 commit b5912b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object SimpleLegends extends StrictLogging {
}

private def withLegend(expr: StyleExpr, legend: String): StyleExpr = {
val label = if (expr.offset > 0L) s"$legend (offset=$$atlas.offset)" else legend
val label = if (expr.offset > 0L) s"$legend (offset=$$(atlas.offset))" else legend
expr.copy(settings = expr.settings + ("legend" -> label))
}

Expand All @@ -94,7 +94,7 @@ object SimpleLegends extends StrictLogging {

private def generateLegend(expr: StyleExpr, kv: Map[String, String]): StyleExpr = {
if (expr.expr.isGrouped) {
val fmt = expr.expr.finalGrouping.mkString("$", " $", "")
val fmt = expr.expr.finalGrouping.mkString("$(", ") $(", ")")
withLegend(expr, fmt)
} else if (kv.contains("name")) {
withLegend(expr, kv("name"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SimpleLegendsSuite extends FunSuite {
}

test("use group by keys") {
assertEquals(legends("name,cpu,:eq,:sum,(,app,id,),:by"), List("$app $id"))
assertEquals(legends("name,cpu,:eq,:sum,(,app,id,),:by"), List("$(app) $(id)"))
}

test("name with math") {
Expand All @@ -79,7 +79,7 @@ class SimpleLegendsSuite extends FunSuite {

test("name with offsets") {
val expr = "name,cpu,:eq,:sum,(,0h,1w,),:offset"
assertEquals(legends(expr), List("cpu", "cpu (offset=$atlas.offset)"))
assertEquals(legends(expr), List("cpu", "cpu (offset=$(atlas.offset))"))
}

test("name with avg") {
Expand All @@ -103,17 +103,21 @@ class SimpleLegendsSuite extends FunSuite {
}

test("name with node avg and grouping") {
assertEquals(legends("name,cpu,:eq,:node-avg,(,app,),:by"), List("$app"))
assertEquals(legends("name,cpu,:eq,:node-avg,(,app,),:by"), List("$(app)"))
}

test("name with node avg and grouping with special chars") {
assertEquals(legends("name,cpu,:eq,:node-avg,(,foo:bar,),:by"), List("$(foo:bar)"))
}

test("name with node avg and nested grouping") {
val expr = "name,cpu,:eq,:node-avg,(,app,region,),:by,:max,(,region,),:by"
assertEquals(legends(expr), List("$region"))
assertEquals(legends(expr), List("$(region)"))
}

test("group by with offsets") {
val expr = "name,cpu,:eq,:sum,(,id,),:by,(,0h,1w,),:offset"
assertEquals(legends(expr), List("$id", "$id (offset=$atlas.offset)"))
assertEquals(legends(expr), List("$(id)", "$(id) (offset=$(atlas.offset))"))
}

test("complex: same name and math") {
Expand Down

0 comments on commit b5912b4

Please sign in to comment.