Splunk Search

how to tweak streamstats function to get sum of differences from hand picked events rather than regular streamstats functions ?

nittalasub
Explorer

Streamstats can produce sum of differences like

(fieldB- fieldA)+ (fieldC-fieldB)+(fieldD - fieldC) = a total of 30 min.

Can we achieve this evaluation through streamstats ? (fieldB - fieldA)+(fieldD - fieldC) ==> (10 + 10 = 20 min.) ???

(PFA Image which poses the question in a understandable manner)

Tags (1)
0 Karma
1 Solution

DalJeanis
SplunkTrust
SplunkTrust

Okay, so, first, mathematically, because of the associative, commutative and identity properties of numbers, (B-A) + (C-B) + (D-C) is always going to equal (D-A).

  (B - A) + (C - B) + (D - C) 
= (B + -A) + (C + -B) + (D + -C) 
= (-A + B) + (-B + C) + (-C + D)  
=  -A +  B + -B  +  C + -C  + D
=  -A + (B + -B) + (C + -C) + D
=  -A +    0     +    0     + D
= -A + D
=  D + -A
=  D - A 

Second, assuming that your events have an ID in common, and you want total duration (B-A + D-C), you can achieve this. You don't actually need streamstats for this, though.

 your base search
| eval duration=case(event="A",-fieldA, event="B",fieldB, event="C",-fieldC, event="D",fieldD, true(),null())
| stats sum(duration) as duration by CommonID

or, assuming you wanted to keep event D

| eventstats sum(duration) as duration by CommonID
| where event="D"

Finally, if you need something more complex, then we can help you with that as well, but we would need specifications for what you were doing, and to know the exact fields for reference.

View solution in original post

DalJeanis
SplunkTrust
SplunkTrust

Okay, so, first, mathematically, because of the associative, commutative and identity properties of numbers, (B-A) + (C-B) + (D-C) is always going to equal (D-A).

  (B - A) + (C - B) + (D - C) 
= (B + -A) + (C + -B) + (D + -C) 
= (-A + B) + (-B + C) + (-C + D)  
=  -A +  B + -B  +  C + -C  + D
=  -A + (B + -B) + (C + -C) + D
=  -A +    0     +    0     + D
= -A + D
=  D + -A
=  D - A 

Second, assuming that your events have an ID in common, and you want total duration (B-A + D-C), you can achieve this. You don't actually need streamstats for this, though.

 your base search
| eval duration=case(event="A",-fieldA, event="B",fieldB, event="C",-fieldC, event="D",fieldD, true(),null())
| stats sum(duration) as duration by CommonID

or, assuming you wanted to keep event D

| eventstats sum(duration) as duration by CommonID
| where event="D"

Finally, if you need something more complex, then we can help you with that as well, but we would need specifications for what you were doing, and to know the exact fields for reference.

nittalasub
Explorer

Thank You
that was an awesome explanation!

Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...