Search code examples
vega-lite

Vegalite Keep Most Recent Data (Filter Transform)


I have the following dashboard:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Domain Breakout Breakout",
  "width": "container",
  "title": {
    "text": ["Build number by service name"],
    "align": "center",
    "dy": -10,
    "fontWeight": "bold",
    "color": "#4f597a",
    "fontSize": 13,
    "font": "Montserrat,sans-serif"
  },
  "config": {"axis": {"grid": true, "tickBand": "extent"}},
  "data": {
    "values": [
      {
        "service": "service1",
        "build": 5555,
        "env": "dev"
      },
      {
        "service": "service2",
        "build": 5555,
        "env": "test"
      },
      {
        "service": "service3",
        "build": 5555,
        "env": "staging"
      },
      {
        "service": "service4",
        "build": 5555,
        "env": "prod"
      },
      {
        "service": "service4",
        "build": 5225,
        "env": "prod"
      }
    ]
  },
  "transform": [
    {"pivot": "env", "value": "build", "groupby": ["env", "service","build"]}
  ],
  "mark": "text",
  "encoding": {
    "x": {
      "field": "env",
      "type": "ordinal",
      "sort": "descending",
      "axis": {
        "title": null,
        "labelAngle": 0,
        "labelFontWeight": "bold",
        "labelColor": "#4f597a",
        "labelFontSize": 20,
        "labelPadding": 20,
        "orient": "top"
      }
    },
    "y": {
      "field": "service",
      "type": "ordinal",
      "sort": {"field": "service", "order": "descending", "op": "sum"},
      "axis": {
        "title": null,
        "labelAngle": 0,
        "labelFontWeight": "bold",
        "labelColor": "#4f597a",
        "labelFontSize": 10,
        "labelPadding": 5
      }
    }
  },
  "layer": [
    {
      "mark": "rect",
      "width": 1000,
      "encoding": {
        "fill": {
          "legend": null,
          "field": "build",
          "type": "quantitative",
          "scale": {"range": ["#ecf9ff", "#c6efff", "#7ad9ff", "#42caff"]}
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "fontSize": 8,
        "font": "Montserrat,sans-serif",
        "align": "center"
      },
      "encoding": {"text": {"field": "build"}}
    }
  ]
}

I have a use case in which new data is inserted with the same service name and env name but with a different build number. In this case:

  {
    "service": "service4",
    "build": 5555,
    "env": "prod"
  },
  {
    "service": "service4",
    "build": 5558,
    "env": "prod"
  }

The issue is the cell [service4][prod] has to layer the "old" value and the new value. Is there an option in vega to get the last inseted value or something similar?

Link the dash: Link


Solution

  • You can use a combination of joinaggregate and filter transform to get the max of build number. i.e. it will filter out the lower values. e.g.

     {
          "joinaggregate": [{"op": "max", "field": "build", "as": "MaxBuild"}],
          "groupby": ["service"]
        },
        {
          "filter":"datum.build >= datum.MaxBuild"
        }
    

    enter image description here

    enter image description here

    Editor