Skip to content

Commit

Permalink
chart: add heatmap json codec UTs.
Browse files Browse the repository at this point in the history
  • Loading branch information
manolama committed Apr 28, 2023
1 parent 7d4f98b commit b89a36c
Showing 1 changed file with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
package com.netflix.atlas.chart

import com.netflix.atlas.chart.model.GraphDef
import com.netflix.atlas.chart.model.HeatmapDef
import com.netflix.atlas.chart.model.LineDef
import com.netflix.atlas.chart.model.LineStyle
import com.netflix.atlas.chart.model.Palette
import com.netflix.atlas.chart.model.PlotDef
import com.netflix.atlas.core.model.ArrayTimeSeq
import com.netflix.atlas.core.model.DsType
import com.netflix.atlas.core.model.TimeSeries
import munit.FunSuite

import java.awt.Color
import java.time.Instant

class JsonCodecSuite extends FunSuite {
Expand All @@ -46,4 +52,94 @@ class JsonCodecSuite extends FunSuite {
val str = JsonCodec.encode(gdef)
assert(!str.contains(""""type":"graph-image""""))
}

private def generateHeatmap(
isPercentile: Boolean = false,
heatmapDef: Option[HeatmapDef]
): GraphDef = {
val dpsA = new Array[Double](60)
java.util.Arrays.fill(dpsA, 1)
val dpsB = new Array[Double](60)
java.util.Arrays.fill(dpsB, 10)
val timeseries = if (isPercentile) {
List(
LineDef(
TimeSeries(
Map("percentile" -> "T0042"),
new ArrayTimeSeq(DsType.Gauge, 0L, 60_000, dpsA)
),
lineStyle = LineStyle.HEATMAP
),
LineDef(
TimeSeries(
Map("percentile" -> "T0048"),
new ArrayTimeSeq(DsType.Gauge, 0L, 60_000, dpsB)
),
lineStyle = LineStyle.HEATMAP
)
)
} else {
List(
LineDef(
TimeSeries(
Map("series" -> "0"),
new ArrayTimeSeq(DsType.Gauge, 0L, 60_000, dpsA)
),
lineStyle = LineStyle.HEATMAP
),
LineDef(
TimeSeries(
Map("series" -> "1"),
new ArrayTimeSeq(DsType.Gauge, 0L, 60_000, dpsB)
),
lineStyle = LineStyle.HEATMAP
)
)
}

val plotDef = PlotDef(
timeseries,
scale = PlotDef(List.empty).scale,
heatmap = heatmapDef
)

GraphDef(
List(plotDef),
Instant.ofEpochMilli(0L),
Instant.ofEpochMilli(3_600_000)
)
}

test("v2 heatmap") {
val gdef = generateHeatmap(heatmapDef =
Some(
HeatmapDef(
palette = Some(Palette.fromArray("heatmap", Array(Color.RED)))
)
)
)
val str = JsonCodec.encode(gdef)
assert(str.contains(""""type":"heatmap""""))
assert(str.contains(""""data":{"type":"heatmap""""))
assert(str.contains(""""label":"10.0""""))
val obtained = JsonCodec.decode(str)
assertEquals(obtained, gdef)
}

test("v2 percentile heatmap") {
val gdef = generateHeatmap(
true,
Some(
HeatmapDef(
palette = Some(Palette.fromArray("heatmap", Array(Color.RED)))
)
)
)
val str = JsonCodec.encode(gdef)
assert(str.contains(""""type":"heatmap""""))
assert(str.contains(""""data":{"type":"heatmap""""))
assert(str.contains(""""label":"percentile=T0048""""))
val obtained = JsonCodec.decode(str)
assertEquals(obtained, gdef)
}
}

0 comments on commit b89a36c

Please sign in to comment.