<?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: transaction calculate duration betweeen 2 events in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304707#M91590</link>
    <description>&lt;P&gt;In this case, what is the value of duration?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| transaction eventId startswith="START" endswith="STOP" 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 12 Jul 2017 09:36:04 GMT</pubDate>
    <dc:creator>HiroshiSatoh</dc:creator>
    <dc:date>2017-07-12T09:36:04Z</dc:date>
    <item>
      <title>transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304706#M91589</link>
      <description>&lt;P&gt;I'm trying to use  transactions to generate a timeline of events where the events are grouped by an eventId&lt;/P&gt;

&lt;P&gt;I'm recieving up to 2 events as a START and a STOP event, and have to calculate the duration between them based on actualTime.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
  "action" : "START",
  "source" : "AS_PLANNED",
  "timestamp" : "2017-07-12T10:07:14.682+02:00",
   "eventId" : "1366963140327",
  "title" : "sadfasdfasdf",
  "flowPublicationId" : "1366963137812",
  "timeAllocationType" : "Segment of program",
  "actualTime" : "2017-07-12T10:07:14.760+02:00",
  "startTimeAnnounced" : "2017-07-12T10:05:00.000+02:00",
  "startTimePlanned" : "2017-07-12T10:07:14.760+02:00",
  "stopTimePlanned" : "2017-07-12T10:12:50.360+02:00",
  "broadcastDate" : [ 2017, 7, 12 ],
  "live" : false,
  "quickReprise" : false,
  "streamingLive" : false,
  "streamingOD" : false,
  "numberOfBlocks" : "1",
  "blockPartNumber" : "1",
  "blockId" : "1366963138813"
}

{
  "action" : "STOP",
  "source" : "AS_PLANNED",
  "timestamp" : "2017-07-12T10:12:50.310+02:00",
  "eventId" : "1366963140327",
  "title" : "yyyyyy",
  "flowPublicationId" : "1366963137812",
  "timeAllocationType" : "Segment of program",
  "actualTime" : "2017-07-12T10:12:50.360+02:00",
  "startTimeAnnounced" : "2017-07-12T10:05:00.000+02:00",
  "startTimePlanned" : "2017-07-12T10:07:14.760+02:00",
  "stopTimePlanned" : "2017-07-12T10:12:50.360+02:00",
  "broadcastDate" : [ 2017, 7, 12 ],
  "live" : false,
  "quickReprise" : false,
  "streamingLive" : false,
  "streamingOD" : false,
  "numberOfBlocks" : "1",
  "blockPartNumber" : "1",
  "blockId" : "1366963138813"
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The query I'm using -&amp;gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="morpheus" 
| transaction eventId 
| eval start=if(action=="START",actualTime,startTimePlanned) 
| eval stop=if(action=="STOP",actualTime,stopTimePlanned) 
| eval duration=(strptime(stop,"%Y-%m-%dT%H:%M:%S,%3N") - strptime(start,"%Y-%m-%dT%H:%M:%S,%3N")) 
| table actualTime, action, title, start, stop, duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But it seems that I'm not getting the duration correctly calculated.&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/3191i714C8CD1F0332C50/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 09:16:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304706#M91589</guid>
      <dc:creator>preben12</dc:creator>
      <dc:date>2017-07-12T09:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304707#M91590</link>
      <description>&lt;P&gt;In this case, what is the value of duration?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| transaction eventId startswith="START" endswith="STOP" 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2017 09:36:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304707#M91590</guid>
      <dc:creator>HiroshiSatoh</dc:creator>
      <dc:date>2017-07-12T09:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304708#M91591</link>
      <description>&lt;P&gt;then it's empty&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 10:22:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304708#M91591</guid>
      <dc:creator>preben12</dc:creator>
      <dc:date>2017-07-12T10:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304709#M91592</link>
      <description>&lt;P&gt;So duration is only calculated if there is at most 1 event. When the transaction returns 2 duration is empty&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 10:25:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304709#M91592</guid>
      <dc:creator>preben12</dc:creator>
      <dc:date>2017-07-12T10:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304710#M91593</link>
      <description>&lt;P&gt;Please switch to stats instead see if following solves your issue. Transaction is using _time field for calculating duration which I feel is the &lt;STRONG&gt;timestamp&lt;/STRONG&gt; field in your case. Following stats should perform better than stats and will give you control as to how you filter required events and calculate duration:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;YourBaseSearch&amp;gt;
| stats count as eventcount list(action) as action list(actualTime) as actualTime by eventId
| search eventcount=2 action="START" AND action="STOP"
| eval startTime=strptime(mvindex(actualTime,0),"%Y-%m-%dT%H:%M:%S.%3N%:z")
| eval endTime=strptime(mvindex(actualTime,1),"%Y-%m-%dT%H:%M:%S.%3N%:z")
| eval duration=endTime-startTime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Second line in the query will give you flexibility of finding matched events, unmatched events, multiple occurrences etc as per your data and use case. Following part of search is finding only those events which have both Start and Stop for the same eventId.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| search eventcount=2 action="START" AND action="STOP"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2017 10:48:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304710#M91593</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-07-12T10:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304711#M91594</link>
      <description>&lt;P&gt;I actually need group  with only 1 event to show up since this should indicate that something is missing/wrong.&lt;/P&gt;

&lt;P&gt;But other that that the search seems to work when there is 2 events&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 11:06:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304711#M91594</guid>
      <dc:creator>preben12</dc:creator>
      <dc:date>2017-07-12T11:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304712#M91595</link>
      <description>&lt;P&gt;If you want to find only events with START and not STOP, you need to change your search filter on the second line as explained in the answer: &lt;CODE&gt;| search eventcount=1 action="START" AND action!="STOP"&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The following finds all the events grouped by eventIds where only START is present but no STOP.&lt;/P&gt;

&lt;P&gt;| stats count as eventcount list(action) as action list(actualTime) as actualTime by eventId&lt;BR /&gt;
 | search eventcount=1 action="START" AND action!="STOP"&lt;BR /&gt;
 | eval startTime=strptime(mvindex(actualTime,0),"%Y-%m-%dT%H:%M:%S.%3N%:z")&lt;BR /&gt;
 | eval endTime=now()&lt;BR /&gt;
 | eval duration=endTime-startTime&lt;/P&gt;

&lt;P&gt;Setting  &lt;CODE&gt;| eval endTime=now()&lt;/CODE&gt; will tell that no STOP event found till now.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 11:59:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304712#M91595</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-07-12T11:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304713#M91596</link>
      <description>&lt;P&gt;Thanks !! I managed to get the last bits together&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 12:37:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304713#M91596</guid>
      <dc:creator>preben12</dc:creator>
      <dc:date>2017-07-12T12:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: transaction calculate duration betweeen 2 events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304714#M91597</link>
      <description>&lt;P&gt;@preben12, I am converting to Answer. Please accept if this helped you with your issue.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:56:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/transaction-calculate-duration-betweeen-2-events/m-p/304714#M91597</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-07-12T13:56:31Z</dc:date>
    </item>
  </channel>
</rss>

