<?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: calculate duration by skiping overlapping time in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405105#M173673</link>
    <description>&lt;P&gt;A different solution if we consider your time resolution is in seconds:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; base_search
 | rename "time_of_down" as time_down, "time_of_up" as time_up 
 | sort time_down
 | eval start=strptime(time_down, "%d/%m/%Y %H:%M:%S")
 | eval end=strptime(time_up, "%d/%m/%Y %H:%M:%S")
 | eval duration=end-start
 | table start,end,duration
 |eval magic=mvrange(start,end)
 | stats dc(magic) as magic
 |eval result=toString(magic,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Jan 2019 15:19:26 GMT</pubDate>
    <dc:creator>damann</dc:creator>
    <dc:date>2019-01-15T15:19:26Z</dc:date>
    <item>
      <title>calculate duration by skiping overlapping time</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405102#M173670</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
Please help me to calculate service availability of the system. &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Method&lt;/STRONG&gt;  &lt;STRONG&gt;Time of down&lt;/STRONG&gt;           &lt;STRONG&gt;Time of up&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
   A                     01/01/2019 15:00:00    01/01/2019 15:05:00&lt;BR /&gt;&lt;BR /&gt;
   B                     01/01/2019 15:08:00    01/01/2019 15:11:00 &lt;BR /&gt;
   C                     01/01/2019 15:09:00    01/01/2019 15:12:00&lt;BR /&gt;&lt;BR /&gt;
   D                     01/01/2019 15:09:00    01/01/2019 15:10:00 &lt;BR /&gt;
   E                     01/01/2019 15:15:00    01/01/2019 15:20:00   &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Total Duration  20 Min  
Down duration   17 min  (currently)
Down duration      14 min  (required)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If any method is down, the complete service is down. Total duration will be the sum of downtime duration between each method. But there are overlapping time of methods going down. &lt;BR /&gt;
The Method C goes down before Method B comes up and method C comes up after B comes up. So downtime duration= up time of C - down time of B. Also Method D goes down and comes up between method B &amp;amp; C. So should skip that duration calculation. &lt;/P&gt;

&lt;P&gt;The normal stats command calculates downtime duration as 17 min where as I want only 14 min. Please help to build the query.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 08:46:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405102#M173670</guid>
      <dc:creator>anantdeshpande</dc:creator>
      <dc:date>2019-01-15T08:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: calculate duration by skiping overlapping time</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405103#M173671</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; your_base_search
| rename "Time of down" as time_down, "Time of up" as time_up 
| sort time_down
| eval pot_down=strptime(time_down, "%d/%m/%Y %H:%M:%S")
| eval pot_up=strptime(time_up, "%d/%m/%Y %H:%M:%S")
| eval duration=pot_up-pot_down
| streamstats window=2 first(pot_*) as prev_*
| eval duration=case(prev_up==pot_up,pot_up-pot_down,prev_up&amp;gt;pot_down,pot_up-prev_up,1==1,duration)
|streamstats sum(duration) as overlap_free_duration
| eval overlap_free_duration=toString(overlap_free_duration,"duration")
| table Method,time_down,time_up,overlap_free_duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In the field overlap_free_duration i get a result of 14 minutes.&lt;BR /&gt;
If you want a single line result you can append the following two lines:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats sum(duration) as overlap_free_duration
| eval overlap_free_duration=toString(overlap_free_duration,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:46:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405103#M173671</guid>
      <dc:creator>damann</dc:creator>
      <dc:date>2020-09-29T22:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: calculate duration by skiping overlapping time</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405104#M173672</link>
      <description>&lt;P&gt;Hi damann,&lt;BR /&gt;
Thanks for the reply.&lt;BR /&gt;
Your query works perfect on sample data.&lt;BR /&gt;
But Somehow "| streamstats window=2 first(pot_&lt;EM&gt;) as prev_&lt;/EM&gt;" is not giving the result as expected on actual data. &lt;BR /&gt;
I tested it multiple scenarios and either miss boundry events or any middle event. &lt;/P&gt;

&lt;P&gt;What I want is.....&lt;BR /&gt;
              If "Time of down" is less than any previous (not current) "Time of up" then consider the "time of down" of that event. Also if "Time of up" is less than any previous "Time of up" then consider the "Time of up" of that event.&lt;BR /&gt;
Below is from actual data. I want time_of_down (13:32:27) and time_of_up(13:24:04) from method B to E.&lt;/P&gt;

&lt;P&gt;Method   time_of_down                 time_of_up&lt;BR /&gt;
A           10/01/2019 11:25:00 10/01/2019 11:25:32&lt;BR /&gt;
B           10/01/2019 13:32:27 10/01/2019 13:33:50&lt;BR /&gt;
C           10/01/2019 13:32:30 10/01/2019 13:33:42&lt;BR /&gt;
D           10/01/2019 13:32:31 10/01/2019 13:33:46&lt;BR /&gt;
E           10/01/2019 13:32:36 10/01/2019 13:34:04&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:46:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405104#M173672</guid>
      <dc:creator>anantdeshpande</dc:creator>
      <dc:date>2020-09-29T22:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: calculate duration by skiping overlapping time</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405105#M173673</link>
      <description>&lt;P&gt;A different solution if we consider your time resolution is in seconds:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; base_search
 | rename "time_of_down" as time_down, "time_of_up" as time_up 
 | sort time_down
 | eval start=strptime(time_down, "%d/%m/%Y %H:%M:%S")
 | eval end=strptime(time_up, "%d/%m/%Y %H:%M:%S")
 | eval duration=end-start
 | table start,end,duration
 |eval magic=mvrange(start,end)
 | stats dc(magic) as magic
 |eval result=toString(magic,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jan 2019 15:19:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-duration-by-skiping-overlapping-time/m-p/405105#M173673</guid>
      <dc:creator>damann</dc:creator>
      <dc:date>2019-01-15T15:19:26Z</dc:date>
    </item>
  </channel>
</rss>

