Hi,
I have a table like that :
test | state_A | state_B | state_C |
1 | ok | ko- WARN | ko - ERROR |
2 | ko- WARN | ok | ok |
3 | ok | ok | ok |
I would like to create a field "global_state" with "done" value if all fields state_* value are "OK" , if not write "issue":
test | state_A | state_B | state_C | global_state |
1 | ok | ko- WARN | ko - ERROR | issue |
2 | ko- WARN | ok | ok | issue |
3 | ok | ok | ok | done |
I tried this foreach but not working :
| foreach state_* [ eval global_state= if(<<FIELD>>=="ko- WARN" OR <<FIELD>>=="ko - ERROR", "issue", "done") ]
The second condition in the if is not applied.
Can you help me please?
Your issue is that you evaluate the field with each foreach call (as per the name).
So effectively you're getting only the final value. In the first row you're gonna get "issue" properly as the result because you have a "not OK" value in the last field but in the second row the global_state gets evaluated to "issue" for state_A but is immediately overwritten with "done" from state_B and then from state_C.
In such case you'd rather want to define an initial value beforehand and then overwrite it if there is a need to do so. Like
<your search>
| eval global_state="done"
| foreach state_*
[ eval global_state=if(<<FIELD>>!="ok","issue",global_state) ]
This way you set your global_state initially to "done" and then if you encounter any value other than "ok" in any of the state_* fields, it's getting overwritten to "issue".
It looks like global_state will be set based only on the last field evaluated. See if this variation helps.
| eval ok_count = 0
| foreach state_* [ eval ok_count = ok_count + case(<<FIELD>>=="ko-WARN", 0, <<FIELD>>=="ko-ERROR", 0, 1==1, 1) ]
| eval global_state = if(ok_count==3, "done", "issue")
Your example did not work, it gave me same issue than my search.
Here are 2 ways to skin this cat.
| eval combined_state=TRIM(state_A)."-".TRIM(state_B)."-".TRIM(state_C)
| eval global_state=IF(combined_state="ok-ok-ok", "done", "issue")
| foreach state_* [| eval combined_state=MVAPPEND(combined_state, TRIM(<<FIELD>>))]
| eval combined_state=MVDEDUP(combined_state)
| eval global_state=IF(MVCOUNT(combined_state)==1 AND combined_state="ok", "done", "issue")