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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...