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
Legend

@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>
---
What goes around comes around. If it helps, hit it with Karma 🙂

View solution in original post

0 Karma

renjith_nair
Legend

@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>
---
What goes around comes around. If it helps, hit it with Karma 🙂
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!

Fastest way to demo Observability

I’ve been having a lot of fun learning about Kubernetes and Observability. I set myself an interesting ...

September Community Champions: A Shoutout to Our Contributors!

As we close the books on another fantastic month, we want to take a moment to celebrate the people who are the ...

Splunk Decoded: Service Maps vs Service Analyzer Tree View vs Flow Maps

It’s Monday morning, and your phone is buzzing with alert escalations – your customer-facing portal is running ...