Skip to content

Commit

Permalink
chart: add default theme to JsonCodec parsing. (#1693)
Browse files Browse the repository at this point in the history
The old scala 2.12 code did not serialize the theme in the v2.json graph metadata. For Core,
they're using Spark to emit data in V2 then parsing it with the latest Atlas code. This will
fix the issue for them.
  • Loading branch information
manolama authored Sep 20, 2024
1 parent a2647bd commit b02bc9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.atlas.chart.graphics.ChartSettings
import com.netflix.atlas.chart.model.*
import com.netflix.atlas.chart.util.PngImage
import com.netflix.atlas.core.model.ArrayTimeSeq
Expand Down Expand Up @@ -423,7 +424,7 @@ object JsonCodec {
loadTime = Option(node.get("loadTime")).fold(-1L)(_.asLong()),
stats = Option(node.get("stats")).fold(CollectorStats.unknown)(toCollectorStats),
warnings = node.get("warnings").elements.asScala.map(_.asText()).toList,
themeName = node.get("theme").asText(),
themeName = Option(node.get("theme")).fold(ChartSettings.defaultTheme)(_.asText()),
renderingHints = processRenderingHints(node.get("renderingHints"))
)
// format: on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package com.netflix.atlas.chart

import com.netflix.atlas.chart.model.GraphDef
import com.netflix.atlas.chart.model.Layout
import com.netflix.atlas.chart.model.LineDef
import com.netflix.atlas.chart.model.PlotDef
import com.netflix.atlas.core.model.TimeSeries
import munit.FunSuite

import java.io.ByteArrayInputStream
import java.time.Instant
import java.time.ZoneId

class JsonCodecSuite extends FunSuite {

Expand All @@ -35,9 +38,51 @@ class JsonCodecSuite extends FunSuite {
)
}

private def fromJson(json: String): GraphDef = {
JsonCodec.decode(new ByteArrayInputStream(json.getBytes("UTF-8")))
}

test("decode - graph-metadata: required only") {
val json =
"""
|[
| {
| "type": "graph-metadata",
| "startTime": 0,
| "endTime": 60000,
| "timezones": [
| "Z"
| ],
| "step": 60000,
| "width": 400,
| "height": 200,
| "layout": "CANVAS",
| "zoom": 1,
| "legendType": "LABELS_WITH_STATS",
| "onlyGraph": false,
| "warnings": []
| }
|]
|""".stripMargin
val gdef = fromJson(json)
assertEquals(gdef.startTime.toEpochMilli, 0L)
assertEquals(gdef.endTime.toEpochMilli, 60_000L)
assertEquals(gdef.timezones, List(ZoneId.of("Z")))
assertEquals(gdef.step, 60_000L)
assertEquals(gdef.width, 400)
assertEquals(gdef.height, 200)
assertEquals(gdef.layout, Layout.CANVAS)
assertEquals(gdef.zoom, 1.0)
assertEquals(gdef.legendType.toString, "LABELS_WITH_STATS")
assertEquals(gdef.onlyGraph, false)
assertEquals(gdef.warnings, List.empty)
assertEquals(gdef.themeName, "light")
}

test("rendering hint: none") {
val gdef = graphDef(Set.empty)
val str = JsonCodec.encode(gdef)
System.out.println(str)
assert(str.contains(""""type":"graph-image""""))
}

Expand Down

0 comments on commit b02bc9d

Please sign in to comment.