<?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 Re: Eval value based on timerange in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88633#M22691</link>
    <description>&lt;P&gt;Okay. The solution I was able to come up with give the -4h@m is simple and relatively clean but not quite as flexible as I'd like. I just take the extra seconds and subtract it when calculating search Range: &lt;/P&gt;

&lt;P&gt;"...|addinfo | eval searchRange = round( info_max_time - info_max_time%60 - info_min_time, 0) | eval..." &lt;/P&gt;

&lt;P&gt;Thanks for the help you two, let me know if you think of a better way to do this.&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 14:19:41 GMT</pubDate>
    <dc:creator>cpeteman</dc:creator>
    <dc:date>2020-09-28T14:19:41Z</dc:date>
    <item>
      <title>Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88614#M22672</link>
      <description>&lt;P&gt;Ok I'm rewriting this question as it has become much simpler than before. All I need to do is have a way the get the length of the current time range I am searching over (as a variable I hope) so that I can use it in eval. What I have right now is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search term |bucket _time span=1m | stats count by _time,punct | eval occurred=if(count!=0,1,0)| stats sum(count) AS sum,sum(occurred) AS num_of_mins_occurred,mean(count) AS mean,stdev(count) AS standard_deviation by punct |eval hourly=if((num_of_mins_occurred/4)=1,"True","False")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Jul 2013 20:29:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88614#M22672</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2013-07-08T20:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88615#M22673</link>
      <description>&lt;P&gt;To calculate the number dynamically you may want to calculate the latest(_time) and the earliest(_time) and do a diff on them. You can do this by running an eventstats after your bucketing (to round to the minute).&lt;/P&gt;

&lt;P&gt;This will give you the value in hours in the field named "diff":&lt;/P&gt;

&lt;P&gt;search_terms | bucket _time span=1m | eventstats latest(_time) as last, earliest(_time) as first | eval diff=(last-first)/60/60&lt;/P&gt;

&lt;P&gt;I should mention that if you use "last 4 hours" you will likely get decimal places in your calculation. You can just add a function to truncate or round the decimals if you want.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:16:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88615#M22673</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2020-09-28T14:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88616#M22674</link>
      <description>&lt;P&gt;I'm afraid that this seems to only be giving me falses&lt;BR /&gt;
 when I try the following "search_term |bucket _time span=1m | stats count by _time,punct | eval occurred=if(count!=0,1,0)| stats sum(count) AS sum_events,sum(occurred) AS num_of_mins_occurred,mean(count) AS mean,stdev(count) AS standard_deviation by punct | eventstats latest(_time) as last, earliest(_time) as first | eval hourly=if((num_of_mins_occurred*60*60/(last-first))==1,"True","False")"&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:16:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88616#M22674</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2020-09-28T14:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88617#M22675</link>
      <description>&lt;P&gt;Like I pointed out, if you are using a relative timerange, like "last 4 hours" or "last 24 hours", 99% of the time you'll get a value that includes seconds in it.&lt;/P&gt;

&lt;P&gt;What this means for you, is that even if you convert your "num_of_mins_occurred" to seconds, by dividing twice by 60, you will get a decimal answer. It will be something like (4 /60 /60) / (4.01 /60 /60), which is not in fact == 1.&lt;/P&gt;

&lt;P&gt;You should run a round or truncate on the answer to the diff before you try to use it in an eval.&lt;/P&gt;

&lt;P&gt;Ran out of chars, see below&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:17:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88617#M22675</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2020-09-28T14:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88618#M22676</link>
      <description>&lt;P&gt;Like:&lt;BR /&gt;
search_terms | bucket _time span=1m | eventstats latest(_time) as last, earliest(_time) as first | eval diff=round((last-first)/60/60 , 0)&lt;/P&gt;

&lt;P&gt;This will round up to the nearest interger, avoiding the problem I mentioned above.&lt;/P&gt;

&lt;P&gt;Also, I just noticed that you ran your eventstats after you ran a stats that does not contain _time as a field. This would also cause problems since the would be no _time field for the eventstats to work with. You'll need to use the eventstats prior to your second stats.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:17:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88618#M22676</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2020-09-28T14:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88619#M22677</link>
      <description>&lt;P&gt;I fixed the order of eventstats and used to the rounding. I really think what you've have been suggesting should work, Thanks for the help so far. Still not working though, any ideas?&lt;/P&gt;

&lt;P&gt;"search_terms |bucket _time span=1m | stats count by _time,punct | eval occurred=if(count!=0,1,0)| eventstats latest(_time) AS last, earliest(_time) AS first |stats sum(count) AS sum,sum(occurred) AS num_of_mins_occurred,mean(count) AS mean,stdev(count) AS standard_deviation by punct | eval diff=round((last-first)/60/60, 0) | eval hourly=if((num_of_mins_occurred/diff)==1,"True","False")"&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:17:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88619#M22677</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2020-09-28T14:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88620#M22678</link>
      <description>&lt;P&gt;So there's a couple of things that could be going on.&lt;BR /&gt;
1) When you run a stats (or any agg command) you lose fields that aren't part of that aggregation. Running an "eval diff=last-first..." after you did a stats that doesn't have last nor first, you'll get blank results for that eval. We'll need last and first in the stats somehow, maybe avg()?&lt;BR /&gt;
2) I think we'll have to use truncate rather than rounding. Why? If you run "Last 4 hours" it basically does "earliest=-4h@h". The @h snaps it to the beginning of that hour. If you run it at 13:50, you'll get earliest=9:00 til now, for a 4h50m length&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 17:59:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88620#M22678</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-09T17:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88621#M22679</link>
      <description>&lt;P&gt;Just double checked, we want to use floor(), since there is no truncate function.&lt;/P&gt;

&lt;P&gt;Also, instead of having a separate eventstats, we could include the "latest(_time) AS last, earliest(_time) AS first" as part of your last stats command. This way saving one command step.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:17:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88621#M22679</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2020-09-28T14:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88622#M22680</link>
      <description>&lt;P&gt;There is a Splunk command &lt;CODE&gt;addinfo&lt;/CODE&gt; that adds some fields to your search results. See &lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.3/SearchReference/Addinfo"&gt;addinfo&lt;/A&gt; for more info &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
To calculate the time range of a search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere
| addinfo
| eval searchRange = info_max_time - info_min_time
| eval searchRangeOutput=tostring(searchRange,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that &lt;CODE&gt;searchRange&lt;/CODE&gt; will be in seconds. Also, the solution in the comments will compute the time range of the resulting events. This answer will compute the time range of the search itself, regardless of what events are returned.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 20:26:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88622#M22680</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2013-07-09T20:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88623#M22681</link>
      <description>&lt;P&gt;That's awesome. I didn't know about addinfo.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 20:29:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88623#M22681</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-09T20:29:54Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88624#M22682</link>
      <description>&lt;P&gt;This almost almost works, but for a 4 hour time span it gives 4.0100... and when I use round(info_max_time - info_min_time, 0) it works fine but a search over the last 24 hours returns 25 hours and a week returns 169 hours not 168. Is there a clean fix for this?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:17:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88624#M22682</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2020-09-28T14:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88625#M22683</link>
      <description>&lt;P&gt;What if you &lt;CODE&gt;exact()&lt;/CODE&gt;? As in&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval searchRange = round( exact(info_max_time) - exact(info_min_time), 0)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I would also add this to the search, especially for the day and week, to see what is going on&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval searchStart=strftime(info_min_time,"%x %X")&lt;BR /&gt;
| eval searchEnd =strftime(info_max_time,"%x %X")&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I wonder if there is something weird about the times...&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2013 02:45:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88625#M22683</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2013-07-10T02:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88626#M22684</link>
      <description>&lt;P&gt;The reason that happens is as follows:&lt;/P&gt;

&lt;P&gt;When you run "Last 4 hours" it basically does "earliest=-4h@h". The @h snaps it to the beginning of that hour. If you run it at 13:50, you'll get earliest=9:00 til now, for a 4h50m length. If you round this, you'll get 5h as the answer.&lt;/P&gt;

&lt;P&gt;This applies to any relative option from the timerange picker ("Last X &lt;UNITS&gt;").&lt;/UNITS&gt;&lt;/P&gt;

&lt;P&gt;In the eval you use to convert your first and last times to hours you should run a floor on them to truncate the decimal places, rather than round which is what is giving you the extra hour.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2013 13:59:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88626#M22684</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-10T13:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88627#M22685</link>
      <description>&lt;P&gt;It looks like the problem is similar to what you are aholzer is saying. Fro the last four hours it searches from say 12:03:00 to 4:03:38 if I start the search at 4:03:38. Is there a way to make the default so that it starts so many hours ago based onf the seconds as well? Or is this an issue caused by my bucketing of time?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 16:31:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88627#M22685</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2013-07-11T16:31:56Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88628#M22686</link>
      <description>&lt;P&gt;I know I could make this happen in my search, but it would be better of it was not something I had to do every time I wanted a new search.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 16:41:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88628#M22686</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2013-07-11T16:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88629#M22687</link>
      <description>&lt;P&gt;Well you can change the definitions of the "Last 4 hours" option (and any other timerange option) to not snap.&lt;/P&gt;

&lt;P&gt;Go to: Manager » User interface » Time ranges &lt;/P&gt;

&lt;P&gt;To make one of the timerange option stop snapping you just have to remove everything after (and including) the '@'.&lt;/P&gt;

&lt;P&gt;For example "Last 4 hours" will look like -4h@h by default, you can change it to -4h, and it will do the EXACT 4 hours ago.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 18:17:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88629#M22687</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-11T18:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88630#M22688</link>
      <description>&lt;P&gt;You can also simply define your earliest and latest values in your base search.&lt;/P&gt;

&lt;P&gt;Example:&lt;BR /&gt;
index=&lt;YOUR_INDEX&gt; sourcetype=&lt;YOUR_ST&gt; earliest=-4h latest=now&lt;/YOUR_ST&gt;&lt;/YOUR_INDEX&gt;&lt;/P&gt;

&lt;P&gt;Doing it this way should override anything that was selected in the timerange picker&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 18:18:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88630#M22688</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-11T18:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88631#M22689</link>
      <description>&lt;P&gt;Thanks for the first solution I'll have to check to see, but from the results I am getting would it not seem that it is probably set for -4h@m ?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 20:22:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88631#M22689</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2013-07-11T20:22:07Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88632#M22690</link>
      <description>&lt;P&gt;You're right, I checked to confirm. I think I had the -4h@h from an older version of splunk.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 20:27:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88632#M22690</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2013-07-11T20:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Eval value based on timerange</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88633#M22691</link>
      <description>&lt;P&gt;Okay. The solution I was able to come up with give the -4h@m is simple and relatively clean but not quite as flexible as I'd like. I just take the extra seconds and subtract it when calculating search Range: &lt;/P&gt;

&lt;P&gt;"...|addinfo | eval searchRange = round( info_max_time - info_max_time%60 - info_min_time, 0) | eval..." &lt;/P&gt;

&lt;P&gt;Thanks for the help you two, let me know if you think of a better way to do this.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:19:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-value-based-on-timerange/m-p/88633#M22691</guid>
      <dc:creator>cpeteman</dc:creator>
      <dc:date>2020-09-28T14:19:41Z</dc:date>
    </item>
  </channel>
</rss>

