Conditionals in Canvas

Hi,

I'm creating a Canvas dashboard using data from the Heartbeat plugin, and using the opportunity to learn Canvas syntax.

I'm using an SQL query to pull the data from Heartbeat like so:

filters
| essql 
  query="SELECT \"@timestamp\", \"monitor.status\" FROM \"heartbeat*\" WHERE QUERY('service_name:my_service') ORDER BY \"@timestamp\" DESC LIMIT 1 "
| table
| render

This returns a table with timestamp and monitor.status as expected.

Now I want to change the text depending on the response; instead of up I want to see Healthy. I've seen this kind of thing done in other types of queries (mostly TimeLion) with switches, but I would also expect to be able to do something like this:

filters
| essql 
  query="SELECT \"@timestamp\", \"monitor.status\" FROM \"heartbeat*\" WHERE QUERY('service_name:my_service') ORDER BY \"@timestamp\" DESC LIMIT 1 "
| if {compare \"monitor.status\" to="up"} then ="healthy"
| table
| render

This breaks the visualization though. Can anyone advise or point me to examples where switches / conditionals are used with essql queries?

Got it working as a switch! Here's the full query rendering to a markdown element:

filters
| essql 
  query="SELECT \"@timestamp\", \"monitor.status\" as status FROM \"heartbeat*\" WHERE QUERY('my_service') LIMIT 1"
| mapColumn value
  fn=${getCell status | switch 
    {case if={compare eq to="up"} then=Healthy}
    {case if={compare eq to="down"} then=Unhealthy}
    default="Unknown"
  }
| markdown "{{#each rows}}
# {{value}}
{{/each}}" 
  font={font family="'Open Sans', Helvetica, Arial, sans-serif" size=18 align="left" color="#000000" weight="bold" underline=false italic=false}
| render containerStyle={containerStyle}

(not sure the {{#each rows}} etc is necessary, happy to take advice on that.

Also FYI I got the answer from this blog post: Canvas Blog Stream

And found examples here: https://github.com/alexfrancoeur/kibana_canvas_examples

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.