Dashboards & Visualizations

Set default value of timepicker earliest and latest based on an init condition

dojiepreji
Path Finder

Hello,

I have a dashboard with a few timepickers. I have one timepicker that is set to default earliest: @mon latest: -1d. This is to get results from beginning of the month to current day minus one. However, if the current date is on the first day of the month, the timepicker will return an error.

First I set up an init...

<init>
    <eval token="day_today">strftime(now(), "%e")</eval>
</init>

And then I use another init to get tokens for my default earliest and latest. If today is a first day of the month, I set the earliest to the previous month instead to mitigate the error.

<init>
    <condition match="'day_today' == &quot;1&quot;">
        <eval token="token_currentMonth_earliest">relative_time(relative_time(now(), "-1mon@mon"), "+8h")</eval>
      </condition>
      <condition match="'day_today'!= &quot;1&quot;">
        <eval token="token_currentMonth_earliest">relative_time(relative_time(now(), "@mon"), "+8h")</eval>
      </condition>
</init>

Problem is, the timepicker does not recognize my token.

<input type="time" token="currentMonth">
      <label>Current Month</label>
      <default>
        <earliest>$token_currentMonth_earliest$</earliest>
        <latest>-1d</latest>
      </default>
</input>

The timepicker was not able to get an epoch value. The earliest is still set to '$token_currentMonth_earliest'.
Is there a way for the timepicker to recognize the token? Or is there another way to do this?

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@dojiepreji

You can use <search> and <done> tags also for same. Please check below sample XML.

  <search>
            <query>| makeresults 
| eval day_today=trim(strftime(now(), "%e")) 
| eval e_time=if(day_today=="1",relative_time(relative_time(now(), "-2mon@mon"), "+8h"),relative_time(relative_time(now(), "@mon"), "+8h")) 
| table e_time</query>
            <done>
              <set token="token_currentMonth_earliest">$result.e_time$</set>
            </done>
          </search>

Sample Dashboard.

<form>
  <label></label>
  <search>
    <query>| makeresults 
| eval day_today=trim(strftime(now(), "%e")) 
| eval e_time=if(day_today=="1",relative_time(relative_time(now(), "-2mon@mon"), "+8h"),relative_time(relative_time(now(), "@mon"), "+8h")) 
| table e_time</query>
    <done>
      <set token="token_currentMonth_earliest">$result.e_time$</set>
    </done>
  </search>
  <fieldset submitButton="false">
    <input type="time" token="field1">
      <label></label>
      <default>
        <earliest>$token_currentMonth_earliest$</earliest>
        <latest>now</latest>
      </default>
    </input>
  </fieldset>
  <row>
    <panel>
      <title>$token_currentMonth_earliest$</title>
      <table>
        <search>
          <query>| makeresults | eval token_currentMonth_earliest="$token_currentMonth_earliest$"</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="count">10</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
</form>

Note: Dynamic earliest & latest time must be valid else it will throw invalid time error. So make sure about logic implemented for deciding earliest & latest time.

0 Karma

dojiepreji
Path Finder

Tried it on my dashboard and didn't work. It's still showing up as '$token_currentMonth_earliest$' and not as epoch time on the timepicker.
I also tried making a new dashboard and copy pasting what you did. It shows " Invalid earliest_time." on the dashboard panel.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@dojiepreji

Can you please share full dashboard XML? Mask all confidential information from XML.

0 Karma

dojiepreji
Path Finder
<form>
<search>
     <query>| makeresults 
      | eval day_today=trim(strftime(now(), "%e")) 
      | eval e_time=if(day_today=="1",relative_time(relative_time(now(), "-2mon@mon"), "+8h"),relative_time(relative_time(now(), "@mon"), "+8h")) 
      | table e_time</query>
    <done>
      <set token="token_currentMonth_earliest">$result.e_time$</set>
    </done>
  </search>
<label>Daily Report</label>
<input type="time" token="currentMonth">
      <label>Current Month</label>
      <default>
        <earliest>$token_currentMonth_earliest$</earliest>
        <latest>-1d</latest>
      </default>
    </input>
<row>
    <panel">
      <title>Daily Metrics</title>
      <table>
        <search>
          <query> index="myindex" earliest=$token_currentMonth_earliest$ latest=now
    | search type="Incident" 
    | search status="*" 
    | stats count as new_mtd by group] </query>
          <earliest>$earliest$</earliest>
          <latest>$latest$</latest>
        </search>
        <option name="count">20</option>
        <option name="drilldown">none</option>
        <option name="totalsRow">true</option>
      </table>
    </panel>
  </row>
</form>
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@dojiepreji

Can you please add <set token="form.currentMonth.earliest">$result.e_time$</set> in <done> tag and try?

<done>
    <set token="token_currentMonth_earliest">$result.e_time$</set>
    <set token="form.currentMonth.earliest">$result.e_time$</set>
</done>
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...