How to use variable inside a switch condition?

Hello, I am trying to build a traffic light element inside Canvas.
I am using different variables to control the expression.
However, I found that the variables are not working inside the "Switch" condition blocks.

filters
| essql 
  query={string "SELECT wlsdomain
    FROM " {var "index_wls"} "
    WHERE " {var "severity"} "
      AND wlscluster LIKE " {var "cluster_dm"} "
      AND QUERY('logMessage:" {var "keyword_stuck"} "')
      AND \"@timestamp\" > DATE_ADD('seconds', " {var "interval"} ", NOW())"}
| math "size(wlsdomain)"
| metric 
  metricFont={font underline=false weight="bold" align="center" family="'Open Sans', Helvetica, Arial, sans-serif" italic=false size=20 color={if {all {eq 0}} then="rgba(0,0,0,0)" else="#FFF"}} metricFormat="0,0.[000]"
| render 
  containerStyle={containerStyle borderRadius="100px" border="rgba(255,255,255,0)" 
  backgroundColor={filters 
  | essql query={string "SELECT wlsdomain
    FROM " {var "index_wls"} "
    WHERE " {var "severity"} "
      AND wlscluster LIKE " {var "cluster_dm"} "
      AND QUERY('logMessage:" {var "keyword_stuck"} "')
      AND \"@timestamp\" > DATE_ADD('seconds', " {var "interval"} ", NOW())"} 
      | math "size(wlsdomain)"
      | switch case={case if={all {eq 0}} then={var "color_normal"}} 
               case={case if={all {gt 0} {lte {var "threshold_warn_st"}}} then={var "color_warn"}} 
               case={case if={all {gte {var "threshold_alert_st"}}} then={var "color_alert"}} 
               default={var "color_normal"}
    }
  }

According to the attached screenshot, the value of the element is equal to 40. It is greater than or equal to 4 ( {var "threshold_alert_st"} ). So based on the condition inside the Switch, it should turn to red color ( {var "color_alert"} ). However, it still in green color.

Several foundings:

  1. The variables "threshold_warn_st" and "threshold_alert_st" are not working inside the condition.
  2. The other variables outside the Switch condition block are worked properly. (e.g. {var "color_warn"} )

So may i know how to parse the variable inside the condition correctly? Thank you so much.

Regards,
Isaac

Hey there!

From what I can tell, your expression looks correct. There was a bug however with number variables being inadvertently converted to string: [Canvas] Number variable is treated as a string · Issue #90833 · elastic/kibana · GitHub
I think you might be experiencing something like that. This bug has been fixed in 7.14.1 and 7.15!

Thank you so much!

1 Like

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