One extra nuance, is that if an event has a duration greater than 10 seconds, it will be entirely missing from at least one of your 5second buckets, so you have to somehow avoid it not being counted for that bucket.
This sort of thing is fixable by using mvrange() and mvexpand, to fabricate extra copies of the events to fill all the spaces.
Skipping to the end, here's a full search that I think will do what you need. Again I'm assuming that you have start_time, end_time, duration and name on each event. Also that start_time and end_time are epochtime values, and duration is measured in seconds.
<your search terms>
| eval bucket_start=mvrange(start_time,end_time,5)
| mvexpand bucket_start
| bin bucket_start span=5s
| eval duration_correction1=max(bucket_start-start_time,0)
| eval duration_correction2=max(end_time-bucket_start-5,0)
| eval duration_within_bucket=duration - duration_correction1 - duration_correction2
| stats max(duration_within_bucket) as duration by name
Walking through this, we make a field called bucket_start , and use mvrange() to assign it a multivalue value. If start_time were 1002, and end_time were 1018, this would be 1002,1007,1012,1017. Now say for a given event, it has N multivalue values for start_time. We then immediately use mvexpand to turn each of our events into N copies of the event, each with one particular value for start_time. (Weird stuff but useful for a variety of advanced concurrency cases.)
Next up, at this point we've created lots of little sets of events, but they're not all aligned on the same 0,5,10,15 fenceposts. So we can use the bin command to round our bucket_start values down to the nearest 5 seconds.
The next challenge is that for each of our 5 second buckets, we have to calculate, for that value of name, how much of the total duration was actually in that given 5 second bucket. This is easy to work out on pencil and paper and it amounts to snipping off the components of the duration that fall outside our bucket. The one that falls earlier than our bucket is duration_correction1 and the seconds that fall later than our bucket are in duration_correction2. Once we subtract those off we'll have "duration_within_bucket" that is actually accurate.
And then we're on the homestretch cause | stats max(duration_within_bucket) as duration by name will roll it all up and give you what you need. I think.
... View more