Dashboards & Visualizations

Conditional Switcher always picks first tree

kmugglet
Communicator

I've been dabbling with ConditionalSwitcher, but no matter what I use as the condition it always picks the first tree.

It got to the point where I stripped out all the rest of my searches and advanced xml and just tried a ConditionalSwitcher on it's own.

<view onunloadCancelJobs="true" template="dashboard.html">
<label>ConditionalSwitcher Test</label>
<module name="AccountBar" layoutPanel="appHeader" />
<module name="SideviewUtils" layoutPanel="appHeader" />
<module name="AppBar" layoutPanel="appHeader" />

<module name="Message" layoutPanel="messaging">
    <param name="filter">*</param>
    <param name="maxSize">2</param>
    <param name="clearOnJobDispatch">False</param>
</module>

<!-- if 1 = 0  -->
<module name="ConditionalSwitcher" layoutPanel="panel_row1_col1">
    <param name="mode">independent</param>
    <param name="condition">'("1"=="0")'</param>
    <!-- then  true -->
    <module name="HTML" layoutPanel="panel_row1_col1">
        <param name="html">
            First
        </param>
    </module>
    <!-- else false -->
    <module name="HTML" layoutPanel="panel_row1_col1">
        <param name="html">
            Second                                              
      </param>
    </module>
</module>
<!-- end ifs -->

As you can hopefully tell, the condition for switching is 1=0, if true it prints First, if false it prints Second.

It always prints First - no matter what.

HELP!

What am I doing wrong?

Cheers, Keith

Tags (1)
1 Solution

sideview
SplunkTrust
SplunkTrust

I think it's just because you're wrapping the condition in single quotes? So the "condition" you're asking it to evaluate is just a big string. Strings evaluate to true in a boolean context in JS, so it's doing so here and thus always picking the first child. Remove the single quotes and it should be fine.

Since you're using Sideview Utils in this view, you may want to look into replacing Splunk's ConditionalSwitcher module with the simple conditional abilities of the Sideview ValueSetter module in combination with the Sideview Switcher module. This can give you more flexibility than the old ConditionalSwitcher, which could only switch between one or two branches. http://sideviewapps.com/apps/sideview-utils

UPDATE: Aha!. The other problem is that the way ConditionalSwitcher is implemented, it will only ever check the condition in two cases 1) When the module receives a push from upstream, and 2) when the currently running job it has access to has made some progress towards completion - (in this case it will only check if the condition param has "job" anywhere in it)

In your example there is no push from above (eg via an autoRun="True" attribute upstream), and so the ConditionalSwitcher actually never checks it's condition so the first child remains selected simply because it's selected by default.

You can rework the example a bit to have an autoRun="True" on a module upstream somewhere and it will start working. Here is a modified view.

<view onunloadCancelJobs="true" template="dashboard.html">
<label>ConditionalSwitcher Test</label>
<module name="AccountBar" layoutPanel="appHeader" />
<module name="SideviewUtils" layoutPanel="appHeader" />
<module name="AppBar" layoutPanel="appHeader" />

<module name="Message" layoutPanel="messaging">
    <param name="filter">*</param>
    <param name="maxSize">2</param>
    <param name="clearOnJobDispatch">False</param>
</module>

<!-- if 1 = 0  -->
<module name="URLLoader" layoutPanel="panel_row1_col1" autoRun="True">
  <module name="ConditionalSwitcher">
    <param name="mode">independent</param>
    <param name="condition">"1"=="0"</param>
    <!-- then  true -->
    <module name="HTML">
      <param name="html">
        First
      </param>
    </module>
    <!-- else false -->
    <module name="HTML">
      <param name="html">
        Second
      </param>
    </module>
  </module>
</module>
<!-- end ifs -->
</view>

View solution in original post

0 Karma

sideview
SplunkTrust
SplunkTrust

I think it's just because you're wrapping the condition in single quotes? So the "condition" you're asking it to evaluate is just a big string. Strings evaluate to true in a boolean context in JS, so it's doing so here and thus always picking the first child. Remove the single quotes and it should be fine.

Since you're using Sideview Utils in this view, you may want to look into replacing Splunk's ConditionalSwitcher module with the simple conditional abilities of the Sideview ValueSetter module in combination with the Sideview Switcher module. This can give you more flexibility than the old ConditionalSwitcher, which could only switch between one or two branches. http://sideviewapps.com/apps/sideview-utils

UPDATE: Aha!. The other problem is that the way ConditionalSwitcher is implemented, it will only ever check the condition in two cases 1) When the module receives a push from upstream, and 2) when the currently running job it has access to has made some progress towards completion - (in this case it will only check if the condition param has "job" anywhere in it)

In your example there is no push from above (eg via an autoRun="True" attribute upstream), and so the ConditionalSwitcher actually never checks it's condition so the first child remains selected simply because it's selected by default.

You can rework the example a bit to have an autoRun="True" on a module upstream somewhere and it will start working. Here is a modified view.

<view onunloadCancelJobs="true" template="dashboard.html">
<label>ConditionalSwitcher Test</label>
<module name="AccountBar" layoutPanel="appHeader" />
<module name="SideviewUtils" layoutPanel="appHeader" />
<module name="AppBar" layoutPanel="appHeader" />

<module name="Message" layoutPanel="messaging">
    <param name="filter">*</param>
    <param name="maxSize">2</param>
    <param name="clearOnJobDispatch">False</param>
</module>

<!-- if 1 = 0  -->
<module name="URLLoader" layoutPanel="panel_row1_col1" autoRun="True">
  <module name="ConditionalSwitcher">
    <param name="mode">independent</param>
    <param name="condition">"1"=="0"</param>
    <!-- then  true -->
    <module name="HTML">
      <param name="html">
        First
      </param>
    </module>
    <!-- else false -->
    <module name="HTML">
      <param name="html">
        Second
      </param>
    </module>
  </module>
</module>
<!-- end ifs -->
</view>
0 Karma

sideview
SplunkTrust
SplunkTrust

hehe. yep that's it. no worries. ConditionalSwitcher is pretty weird and seldom used. I do recommend spending your cycles getting to know ValueSetter and Switcher better.

0 Karma

kmugglet
Communicator

Never mind, found it.
Double quotes around the token name without dollar signs


(context.get("rowTitle")=="env")

Which looks oddly like your previous answer
http://answers.splunk.com/answers/58424/how-to-pass-a-token-valuedrop-down-value-to-a-conditionalswi...

RTFM

0 Karma

kmugglet
Communicator

Thanks for that, I've got it past the true/false bit now...
All I have to do now is get it to recognise my token
that I set upstream.
In my "real" XML with a token of $rowTitle$


independent
(context.get(rowTitle)=="env")


From Firebug I'm getting the follow errors :-


the condition threw an exception ReferenceError: rowTitle is not defined module....min.js (line 2549)

The token definitely has value as I use it in the upstream search.

0 Karma

sideview
SplunkTrust
SplunkTrust

I see. The ConditionalSwitcher has a bug in it where if it never receives a push from upstream, it will never check your condition. I updated my answer above.

0 Karma

kmugglet
Communicator

Thanks for the pointer on the single quotes, however this still doesn't work.
I've gone as far as putting 0, false , and "Not flipping true" in the condition param, all to no avail.
It always comes up true and picks the first tree.
Can someone post a simple example where it definitely picks the second tree.

0 Karma
Get Updates on the Splunk Community!

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...