<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Making same query run for different times in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Making-same-query-run-for-different-times/m-p/369593#M108903</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a dashboard with a query that currently runs for the time range 'Today' everyday. I want the time range to be modified dynamically based on when the dashboard is viewed because data may not be available on some of the days.&lt;/P&gt;

&lt;P&gt;Viewed on Sunday: Run the query for Saturday (i.e.: if Sunday, run for -1 days)&lt;BR /&gt;
Viewed on Monday: Run the query for Saturday (i.e.: if Monday, run for -2 days)&lt;/P&gt;

&lt;P&gt;Viewed from Tuesday-Saturday:&lt;BR /&gt;
Before 9 am: Run for yesterday&lt;BR /&gt;
After 9 am: Run for today till 9 am&lt;/P&gt;

&lt;P&gt;E.g: If dashboard is loaded at 8 am on Wednesday, time range should be Tuesday&lt;BR /&gt;
If dashboard is loaded at 10 am on Wednesday, time range should be Wednesday(midnight till 9 am)&lt;/P&gt;

&lt;P&gt;The query is the same in all the cases, only the time range varies.&lt;BR /&gt;
Is it possible to implement the above in Splunk?&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Deepak&lt;/P&gt;</description>
    <pubDate>Tue, 09 May 2017 14:12:15 GMT</pubDate>
    <dc:creator>deepak02</dc:creator>
    <dc:date>2017-05-09T14:12:15Z</dc:date>
    <item>
      <title>Making same query run for different times</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Making-same-query-run-for-different-times/m-p/369593#M108903</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a dashboard with a query that currently runs for the time range 'Today' everyday. I want the time range to be modified dynamically based on when the dashboard is viewed because data may not be available on some of the days.&lt;/P&gt;

&lt;P&gt;Viewed on Sunday: Run the query for Saturday (i.e.: if Sunday, run for -1 days)&lt;BR /&gt;
Viewed on Monday: Run the query for Saturday (i.e.: if Monday, run for -2 days)&lt;/P&gt;

&lt;P&gt;Viewed from Tuesday-Saturday:&lt;BR /&gt;
Before 9 am: Run for yesterday&lt;BR /&gt;
After 9 am: Run for today till 9 am&lt;/P&gt;

&lt;P&gt;E.g: If dashboard is loaded at 8 am on Wednesday, time range should be Tuesday&lt;BR /&gt;
If dashboard is loaded at 10 am on Wednesday, time range should be Wednesday(midnight till 9 am)&lt;/P&gt;

&lt;P&gt;The query is the same in all the cases, only the time range varies.&lt;BR /&gt;
Is it possible to implement the above in Splunk?&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Deepak&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2017 14:12:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Making-same-query-run-for-different-times/m-p/369593#M108903</guid>
      <dc:creator>deepak02</dc:creator>
      <dc:date>2017-05-09T14:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Making same query run for different times</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Making-same-query-run-for-different-times/m-p/369594#M108904</link>
      <description>&lt;P&gt;Following is a run anywhere example that should work based on conditions you have provided. I am running base search to calculate earliest and latest time to be used in the Dashboard Search Query. Then &lt;STRONG&gt;progress&lt;/STRONG&gt; Search Event handler (&lt;STRONG&gt;for Splunk Enterprise version 6.5 onward&lt;/STRONG&gt;) has been used to pass on the same to Required second search query. &lt;/P&gt;

&lt;P&gt;PS: For Splunk versions prior to 6.5 you should use preview instead of progress. Refer to Splunk Search Event Handlers on Splunk Documentation specific to the version you are using.&lt;/P&gt;

&lt;P&gt;Second search runs a dummy query against Splunk's _internal index to count number of Errors in splunkd, which you can replace with your own query. &lt;/P&gt;

&lt;P&gt;Notice token &lt;STRONG&gt;$queryTimeRange$&lt;/STRONG&gt; passed from base search which is used in the second query for defining earliest and latest time instead of &lt;CODE&gt;&amp;lt;earliest&amp;gt; and &amp;lt;latest&amp;gt;&lt;/CODE&gt; search nodes.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  &amp;lt;search&amp;gt;
    &amp;lt;query&amp;gt;
      | makeresults
      | eval WeekDay=strftime(_time,"%a")
      | eval Hour=strftime(_time,"%H")
      | eval Time=case(
                        WeekDay=="Sun","earliest=-1d@d latest=now",
                        WeekDay=="Mon","earliest=-2d@d latest=now",
                        (WeekDay=="Tue" OR WeekDay=="Wed" OR WeekDay=="Thu" OR WeekDay=="Fri" OR WeekDay=="Sat") AND Hour&amp;lt;="09","earliest=-1d@d latest=-0d@d",
                        (WeekDay=="Tue" OR WeekDay=="Wed" OR WeekDay=="Thu" OR WeekDay=="Fri" OR WeekDay=="Sat") AND Hour&amp;gt;"09","earliest=-0d@d latest=-0d@d+9h",
                        true(),"earliest=-0d@d latest=now"
                      )
      | table _time WeekDay Hour Time
    &amp;lt;/query&amp;gt;
    &amp;lt;progress&amp;gt;
      &amp;lt;set token="queryTimeRange"&amp;gt;$result.Time$&amp;lt;/set&amp;gt;
    &amp;lt;/progress&amp;gt;
  &amp;lt;/search&amp;gt;
&amp;lt;row&amp;gt;
  &amp;lt;panel&amp;gt;
    &amp;lt;table&amp;gt;
      &amp;lt;title&amp;gt;Time: $queryTimeRange$&amp;lt;/title&amp;gt;
    &amp;lt;search&amp;gt;
      &amp;lt;query&amp;gt;index=_internal sourcetype=splunkd log_level="ERROR" $queryTimeRange$
| stats count as Errors
| addinfo
| fieldformat info_min_time=strftime(info_min_time,"%c")
| fieldformat info_max_time=strftime(info_max_time,"%c")
| fieldformat info_search_time=strftime(info_search_time,"%c")
| table Errors info_min_time info_max_time info_search_time
      &amp;lt;/query&amp;gt;
    &amp;lt;/search&amp;gt;
    &amp;lt;/table&amp;gt;
  &amp;lt;/panel&amp;gt;
&amp;lt;/row&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 May 2017 17:14:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Making-same-query-run-for-different-times/m-p/369594#M108904</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-05-09T17:14:35Z</dc:date>
    </item>
  </channel>
</rss>

