<?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 calculate sum of duration between events in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469854#M132212</link>
    <description>&lt;P&gt;I have events like below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2019-10-21 04:17:54.968, rev=true
2019-10-21 04:17:55.968, rev=true
2019-10-21 04:17:56.968, rev=false
2019-10-21 04:17:57.968, rev=false
2019-10-21 04:17:58.968, rev=true
2019-10-21 04:17:59.968, rev=true
2019-10-21 04:18:00.968, rev=true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here I want to calculate duration starts from &lt;CODE&gt;rev=true&lt;/CODE&gt; till &lt;CODE&gt;rev=false&lt;/CODE&gt; and then sum these duration. from above events I would like to calculate duration from &lt;CODE&gt;2019-10-21 04:17:54.968&lt;/CODE&gt; till &lt;CODE&gt;2019-10-21 04:17:56.968&lt;/CODE&gt; and again from &lt;CODE&gt;2019-10-21 04:17:58.968&lt;/CODE&gt; till &lt;BR /&gt;
&lt;CODE&gt;2019-10-21 04:18:00.968&lt;/CODE&gt;. I tried &lt;CODE&gt;transaction&lt;/CODE&gt; command but unable to succeed .&lt;BR /&gt;
kindly help.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 12:20:54 GMT</pubDate>
    <dc:creator>ips_mandar</dc:creator>
    <dc:date>2019-10-25T12:20:54Z</dc:date>
    <item>
      <title>calculate sum of duration between events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469854#M132212</link>
      <description>&lt;P&gt;I have events like below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2019-10-21 04:17:54.968, rev=true
2019-10-21 04:17:55.968, rev=true
2019-10-21 04:17:56.968, rev=false
2019-10-21 04:17:57.968, rev=false
2019-10-21 04:17:58.968, rev=true
2019-10-21 04:17:59.968, rev=true
2019-10-21 04:18:00.968, rev=true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here I want to calculate duration starts from &lt;CODE&gt;rev=true&lt;/CODE&gt; till &lt;CODE&gt;rev=false&lt;/CODE&gt; and then sum these duration. from above events I would like to calculate duration from &lt;CODE&gt;2019-10-21 04:17:54.968&lt;/CODE&gt; till &lt;CODE&gt;2019-10-21 04:17:56.968&lt;/CODE&gt; and again from &lt;CODE&gt;2019-10-21 04:17:58.968&lt;/CODE&gt; till &lt;BR /&gt;
&lt;CODE&gt;2019-10-21 04:18:00.968&lt;/CODE&gt;. I tried &lt;CODE&gt;transaction&lt;/CODE&gt; command but unable to succeed .&lt;BR /&gt;
kindly help.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 12:20:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469854#M132212</guid>
      <dc:creator>ips_mandar</dc:creator>
      <dc:date>2019-10-25T12:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of duration between events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469855#M132213</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| stats count
 | eval raw="2019-10-21 04:17:57.968, rev=false
 2019-10-21 04:17:54.968, rev=true
 2019-10-21 04:17:55.968, rev=true
 2019-10-21 04:17:56.968, rev=false
 2019-10-21 04:17:57.968, rev=false
 2019-10-21 04:17:58.968, rev=true
 2019-10-21 04:17:59.968, rev=true
 2019-10-21 04:18:00.968, rev=true"
 | makemv delim="
 " raw
 | mvexpand raw
 | rex field=raw "^(?&amp;lt;time&amp;gt;[^,]+), rev=(?&amp;lt;rev&amp;gt;\w+)"
 | eval _time=strptime(time,"%Y-%m-%d %H:%M:%S.%3Q")
 | table _time rev
 `comment("this is sample data")`
 | streamstats count(eval(rev=="true")) as count  max(_time) as new min(_time) as old  reset_after="("rev==\"false\"")"
 | eventstats count as event_count
 | streamstats count as stream_count
 | eval duration = if(rev == "false" OR event_count==stream_count , new - old , NULL)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hi, this is sample query.&lt;BR /&gt;
How about it?&lt;/P&gt;

&lt;P&gt;If you have multiple sources:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval raw="2019-10-21 04:17:54.968, rev=true
2019-10-21 04:17:55.968, rev=true
2019-10-21 04:17:56.968, rev=false
2019-10-21 04:17:57.968, rev=false
2019-10-21 04:17:58.968, rev=true
2019-10-21 04:17:59.968, rev=true
2019-10-21 04:18:00.968, rev=true"
| eval source="sourceA
sourceB
sourceC"
| makemv delim="
" source
| mvexpand source
| makemv delim="
" raw
| mvexpand raw
| rex field=raw "^(?&amp;lt;time&amp;gt;[^,]+), rev=(?&amp;lt;rev&amp;gt;\w+)"
| eval _time=strptime(time,"%Y-%m-%d %H:%M:%S.%3Q")
| table _time rev source
 `comment("this is sample data")`
| sort 0 source _time
| streamstats count(eval(rev=="true")) as count  max(_time) as new min(_time) as old  reset_after="("rev==\"false\"")" by source
| eventstats count as event_count by source
| streamstats count as stream_count by source
| eval duration = if(rev == "false" OR event_count==stream_count , new - old , NULL)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you put the &lt;CODE&gt;source&lt;/CODE&gt; on the table, it will move.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 14:51:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469855#M132213</guid>
      <dc:creator>to4kawa</dc:creator>
      <dc:date>2019-10-25T14:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of duration between events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469856#M132214</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval raw="_time=2019-10-2T04:17:54.968,rev=true _time=2019-10-21T04:17:55.968,rev=true _time=2019-10-21T04:17:56.968,rev=false _time=2019-10-21T04:17:57.968,rev=false _time=2019-10-21T04:17:58.968,rev=true _time=2019-10-21T04:17:59.968,rev=true _time=2019-10-21T04:18:00.968,rev=true" 
| makemv raw 
| mvexpand raw 
| rename raw AS _raw 
| fields - _time 
| kv 
| eval _time = strptime(time, "%Y-%m-%dT%H:%M:%S") 
| sort 0 - _time 

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution"

| streamstats count(eval(rev="false")) AS sessionID 
| eventstats last(rev) AS first_rev first(rev) AS last_rev BY sessionID
| search first_rev = "true"
| stats min(_time) AS _time range(_time) AS duration BY sessionID
| fieldformat duration = tostring(duration, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Oct 2019 00:45:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469856#M132214</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-10-26T00:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of duration between events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469857#M132215</link>
      <description>&lt;P&gt;Thanks @to4kawa. It is working if I separately mention source but I want  to work it for all source.&lt;BR /&gt;
so I tried &lt;CODE&gt;... | streamstats count(eval(rev=="true")) as count  max(_time) as new min(_time) as old  reset_after="("rev==\"false\"")" global=false by source...&lt;/CODE&gt;&lt;BR /&gt;
but it is not working as expected .Please help here.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2019 06:06:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469857#M132215</guid>
      <dc:creator>ips_mandar</dc:creator>
      <dc:date>2019-10-28T06:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of duration between events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469858#M132216</link>
      <description>&lt;P&gt;If I understand your question right, this is fairly simple, try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| sort _time
| eval row_num=1
| streamstats sum(row_num) as row_num by rev reset_on_change=true
| delta _time as timediff
| eval timediff=if(row_num==1, 0, timediff)
| streamstats sum(timediff) as timediff by rev reset_on_change=true
| streamstats max(timediff) as max_timediff by rev reset_on_change=true
| where timediff=max_timediff and rev=="true"
| stats sum(timediff)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Let me know if this works for you. If yes, please upvote and mark as answer&lt;/P&gt;

&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 10:52:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-duration-between-events/m-p/469858#M132216</guid>
      <dc:creator>arjunpkishore5</dc:creator>
      <dc:date>2019-10-29T10:52:23Z</dc:date>
    </item>
  </channel>
</rss>

