All Apps and Add-ons

How to pass a token downstream to a value in a Pulldown?

lukeh
Contributor

Hi 🙂

I have setup a dashboard to allow users to optionally draw a trendline on a graph.

The user should be able to enter the number of days into the future for extending the trendline, but when I try to pass a token from the TextField module to a Pulldown module with a couple of staticOptions listed, the $daysFuture$ token does not get interpreted 😞

Is there a workaround that we can use to pass a token downstream to a value in a Pulldown?

We are using Sideview Utils 2.3 and Splunk 5.0.1.

Example code below:

            <module name="TextField" layoutPanel="panel_row2_col1">
              <param name="name">daysFuture</param>
              <param name="template">$value$</param>
              <param name="label">Trend Days:</param>
              <param name="width">20px</param>  
              <param name="float">left</param>
              <param name="default">30</param> 


            <module name="Pulldown">
              <param name="name">calctrendLine</param>
              <param name="label">Calculate Trendline:</param>
              <param name="float">left</param>
              <param name="staticOptions">
                <list>
                  <param name="label">Off</param>
                  <param name="value"></param>
                </list>                          
                <list>
                  <param name="label">On</param>
                  <param name="value">| `lineartrend(_time, Total)` 
                                      | `lineartrendextend(_time, Total, $daysFuture$)`</param>
                </list>
              </param>

              <module name="Checkbox">                          
               <param name="name">drawtrendLine</param>
               <param name="label">Draw Trendline:</param>
               <param name="onValue">max(newY) as Linear_Trend</param>                            

Thanks in advance,

Luke 🙂

1 Solution

sideview
SplunkTrust
SplunkTrust

Interesting. the staticOptions param, (and the legacy staticFieldsToDisplay param for those using really old versions of Utils), indeed does not do $foo$ substitution.

I suppose it could. The static options only get generated once when the page loads but there's no reason they couldn't do $foo$ substitution onContextChange. I'd have to rework some of the selected-state management code but it makes sense and it's a good change so I'll try and get it done.

In the meantime, there is a workaround you can do with the ValueSetter module. ValueSetter is a module that you don't need particularly often, but when you do it's pretty useful. maybe like Harvey Keitel from Pulp Fiction. Not always pretty but practical. Anyways, ValueSetter has a toolbox of params that help it do its practical-but-rarely-called-upon work, and one of those is called "requiredKeys". You give this optional param one or more context keys, and then unless those keys are present and non-null, the ValueSetter will not output whatever value it would normally output.

So the basic idea here, is that we make your Pulldown really just output nothing for "Off", and something for "On", then we have a ValueSetter there that outputs your complex search expression, with the daysFuture there inside, but we use requiredKeys so that this only gets output when the Pulldown's value is "on". It's pretty circuitous, kinda ugly, but it's the same end-result you're looking for and without having to write any custom JS.

<module name="TextField" layoutPanel="panel_row2_col1">
  <param name="name">daysFuture</param>
  <param name="label">Trend Days:</param>
  <param name="width">20px</param>  
  <param name="float">left</param>
  <param name="default">30</param>

  <module name="Pulldown">
    <param name="name">calctrendLine_fake</param>
    <param name="label">Calculate Trendline:</param>
    <param name="float">left</param>
    <param name="staticOptions">
      <list>
        <param name="label">Off</param>
        <param name="value"></param>
      </list>                          
      <list>
        <param name="label">On</param>
        <param name="value">ON (but it doesnt actually matter what the value is here as long as there is one)</param>
      </list>
    </param>

    <module name="ValueSetter">
      <param name="name">templatedDaysFuture</param>
      <param name="value">| `lineartrend(_time, Total)` | `lineartrendextend(_time, Total, $daysFuture$)`</param>

      <module name="ValueSetter">
        <param name="name">calctrendLine</param>
        <param name="value">$templatedDaysFuture$</param>
        <!-- because of this, the whole calcTrendLine key will only be 
        output when calctrendLine_fake i set to "on" -->
        <param name="requiredKeys">calctrendLine_fake</param>

        <module name="Checkbox">                          
         <param name="name">drawtrendLine</param>
         <param name="label">Draw Trendline:</param>
         <param name="onValue">max(newY) as Linear_Trend</param>

btw, <param name="template">$value$</param> is redundant so I removed it here.

Also, if that $daysFuture$ is only ever used inside that one lineartrendextend expression, then you can factor up that entire first ValueSetter into the "template" param of your TextField. In other words you'd have

<param name="template">| `lineartrend(_time, Total)` | `lineartrendextend(_time, Total, $value$)`</param> 

in your TextField, and then only the second ValueSetter, not the first. I hope this makes sense.

View solution in original post

0 Karma

lukeh
Contributor

FYI: here is my new Pulp Fiction certified code:

            <module name="TextField" layoutPanel="panel_row2_col1">
              <param name="name">daysFuture</param>
              <param name="template">$value$</param>
              <param name="label">Trend Days:</param>
              <param name="width">20px</param>  
              <param name="float">left</param>
              <param name="default">30</param> 

              <module name="Checkbox">                          
               <param name="name">calctrendLine</param>
               <param name="label">Draw Trendline:</param>
               <param name="float">left</param>
               <param name="onValue">| `lineartrend(_time, Total)` | `lineartrendextend(_time, Total, $daysFuture$)`</param>                         

            <module name="ValueSetter">
                      <param name="name">drawtrendLine</param>
                      <param name="value">max(newY) as Linear_Trend</param>
                      <!-- because of this, the whole drawtrendLine key will only be 
                      output when calctrendLine is set to "on" -->
                      <param name="requiredKeys">calctrendLine</param> 
0 Karma

sideview
SplunkTrust
SplunkTrust

I see. Well then you should follow my comments at the end and use only the second of the two ValueSetters, pulling that lineartrendextend text up into the template param of your TextField.

0 Karma

lukeh
Contributor

yep it only gets used in the lineartrendextend command

0 Karma

sideview
SplunkTrust
SplunkTrust

Interesting. the staticOptions param, (and the legacy staticFieldsToDisplay param for those using really old versions of Utils), indeed does not do $foo$ substitution.

I suppose it could. The static options only get generated once when the page loads but there's no reason they couldn't do $foo$ substitution onContextChange. I'd have to rework some of the selected-state management code but it makes sense and it's a good change so I'll try and get it done.

In the meantime, there is a workaround you can do with the ValueSetter module. ValueSetter is a module that you don't need particularly often, but when you do it's pretty useful. maybe like Harvey Keitel from Pulp Fiction. Not always pretty but practical. Anyways, ValueSetter has a toolbox of params that help it do its practical-but-rarely-called-upon work, and one of those is called "requiredKeys". You give this optional param one or more context keys, and then unless those keys are present and non-null, the ValueSetter will not output whatever value it would normally output.

So the basic idea here, is that we make your Pulldown really just output nothing for "Off", and something for "On", then we have a ValueSetter there that outputs your complex search expression, with the daysFuture there inside, but we use requiredKeys so that this only gets output when the Pulldown's value is "on". It's pretty circuitous, kinda ugly, but it's the same end-result you're looking for and without having to write any custom JS.

<module name="TextField" layoutPanel="panel_row2_col1">
  <param name="name">daysFuture</param>
  <param name="label">Trend Days:</param>
  <param name="width">20px</param>  
  <param name="float">left</param>
  <param name="default">30</param>

  <module name="Pulldown">
    <param name="name">calctrendLine_fake</param>
    <param name="label">Calculate Trendline:</param>
    <param name="float">left</param>
    <param name="staticOptions">
      <list>
        <param name="label">Off</param>
        <param name="value"></param>
      </list>                          
      <list>
        <param name="label">On</param>
        <param name="value">ON (but it doesnt actually matter what the value is here as long as there is one)</param>
      </list>
    </param>

    <module name="ValueSetter">
      <param name="name">templatedDaysFuture</param>
      <param name="value">| `lineartrend(_time, Total)` | `lineartrendextend(_time, Total, $daysFuture$)`</param>

      <module name="ValueSetter">
        <param name="name">calctrendLine</param>
        <param name="value">$templatedDaysFuture$</param>
        <!-- because of this, the whole calcTrendLine key will only be 
        output when calctrendLine_fake i set to "on" -->
        <param name="requiredKeys">calctrendLine_fake</param>

        <module name="Checkbox">                          
         <param name="name">drawtrendLine</param>
         <param name="label">Draw Trendline:</param>
         <param name="onValue">max(newY) as Linear_Trend</param>

btw, <param name="template">$value$</param> is redundant so I removed it here.

Also, if that $daysFuture$ is only ever used inside that one lineartrendextend expression, then you can factor up that entire first ValueSetter into the "template" param of your TextField. In other words you'd have

<param name="template">| `lineartrend(_time, Total)` | `lineartrendextend(_time, Total, $value$)`</param> 

in your TextField, and then only the second ValueSetter, not the first. I hope this makes sense.

0 Karma

lukeh
Contributor

Good idea, and I was also able to use just one Checkbox now that I know how to use the ValueSetter module, thanks Nick!

0 Karma

sideview
SplunkTrust
SplunkTrust

Another thought -- since your Pulldown only has two entries anyway, have you considered using a Checkbox module instead? This would have an added benefit that you can do your $foo$ substitution into the Checkbox module's onValue and/or offValue params just fine, without the need for this ValueSetter trick.

0 Karma

lukeh
Contributor

Awesome, the workaround is just like Harvey Keitel! 🙂 Thanks Nick!!!

0 Karma

sideview
SplunkTrust
SplunkTrust

quick question - is that $daysFuture$ key only ever used in that lineartrendextend command, or does it get used in some other place in the view as well?

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...