Dashboards & Visualizations

How do you make a search that would return timerange depending on dropdown values?

jvmerilla
Path Finder

Hi Everyone,

I have a dashboard with 2 dropdown filters, From Date and To Date. Below this is a table. It looks something like the image below:

alt text

Now, what should happen is that when both of the dropdowns are set to the date today, the search should return results from "date today" and "the last 30 mins". Then, if the dropdown is set to a different date, (for example FROM DATE=9/9/2018, TO DATE=9/11/2018), the search should return results from 9//9/2018 to 9/11/2018.

What could be the possible solution for this? I am trying to use |where and |eval but I still can't figure out how to do this.

I hope anyone can help me solve this.

Thanks in advance!

Have a nice day. 🙂

0 Karma
1 Solution

renjith_nair
SplunkTrust
SplunkTrust

@jvmerilla ,

Try this

<form>
  <label>Time Token</label>
  <search>
    <query>|makeresults|eval earliest=if("$from$"=="$to$","-30m",strptime("$from$","%m/%d/%Y"))|eval latest=if("$from$"=="$to$",now(),strptime("$to$","%m/%d/%Y"))|fields earliest,latest</query>
    <done>
      <set token="earliest_time">$result.earliest$</set>
      <set token="latest_time">$result.latest$</set>
    </done>
  </search>
  <fieldset submitButton="false">
    <input type="dropdown" token="from" searchWhenChanged="true">
      <label>FROM</label>
      <choice value="9/9/2018">9/9/2018</choice>
      <choice value="9/10/2018">9/10/2018</choice>
      <choice value="911/2018">9/11/2018</choice>
      <default>9/9/2018</default>
    </input>
    <input type="dropdown" token="to" searchWhenChanged="true">
      <label>TO</label>
      <choice value="9/9/2018">9/9/2018</choice>
      <choice value="9/10/2018">9/10/2018</choice>
      <choice value="911/2018">9/11/2018</choice>
      <default>9/10/2018</default>
    </input>
  </fieldset>
  <row>
    <panel>
      <chart>
        <search>
          <query>index=_internal|timechart span=10m count by sourcetype</query>
          <earliest>$earliest_time$</earliest>
          <latest>$latest_time$</latest>
        </search>
        <option name="charting.chart">line</option>
        <option name="charting.drilldown">none</option>
      </chart>
    </panel>
  </row>
</form>
Happy Splunking!

View solution in original post

0 Karma

renjith_nair
SplunkTrust
SplunkTrust

@jvmerilla ,

Try this

<form>
  <label>Time Token</label>
  <search>
    <query>|makeresults|eval earliest=if("$from$"=="$to$","-30m",strptime("$from$","%m/%d/%Y"))|eval latest=if("$from$"=="$to$",now(),strptime("$to$","%m/%d/%Y"))|fields earliest,latest</query>
    <done>
      <set token="earliest_time">$result.earliest$</set>
      <set token="latest_time">$result.latest$</set>
    </done>
  </search>
  <fieldset submitButton="false">
    <input type="dropdown" token="from" searchWhenChanged="true">
      <label>FROM</label>
      <choice value="9/9/2018">9/9/2018</choice>
      <choice value="9/10/2018">9/10/2018</choice>
      <choice value="911/2018">9/11/2018</choice>
      <default>9/9/2018</default>
    </input>
    <input type="dropdown" token="to" searchWhenChanged="true">
      <label>TO</label>
      <choice value="9/9/2018">9/9/2018</choice>
      <choice value="9/10/2018">9/10/2018</choice>
      <choice value="911/2018">9/11/2018</choice>
      <default>9/10/2018</default>
    </input>
  </fieldset>
  <row>
    <panel>
      <chart>
        <search>
          <query>index=_internal|timechart span=10m count by sourcetype</query>
          <earliest>$earliest_time$</earliest>
          <latest>$latest_time$</latest>
        </search>
        <option name="charting.chart">line</option>
        <option name="charting.drilldown">none</option>
      </chart>
    </panel>
  </row>
</form>
Happy Splunking!
0 Karma

SathyaNarayanan
Path Finder

in the drop down set the token for earliest and latest.

In the search query add the below line

| eval earliest=case(isnum(earliest),earliest,earliest=="-1d","-30m",1=1)

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

Splunk is officially part of Cisco

Revolutionizing how our customers build resilience across their entire digital footprint.   Splunk ...

Splunk APM & RUM | Planned Maintenance March 26 - March 28, 2024

There will be planned maintenance for Splunk APM and RUM between March 26, 2024 and March 28, 2024 as ...