EDIT Added information
Originally this was just about a general algorithm and language/platform agnostic. However I'm going to answer this question myself and the answer is in fact specific to the tools in use.
This is for event detection on an IBM mainframe under z/OS , using the Ops/MVS automation tool running a REXX script.
So the answers posted may be applicable in Python, Perl, bash, Java etc; it's just that the product being used in this particular case has a specific function that does the trick.
End of added information
My question is very similar to this:
How to calculate continuous smooth event rate based on event times?
and this would be an answer:
This can be implemented with a moving average. Take your last N events where N is the size of your averaging window. Compute the time difference between the first and the last of these N events. If you are measuring in seconds and want the rate in event per minute you would then divide 60 seconds by your time difference expressed in seconds and you multiply by N-1.
except I'd like to avoid storing information about previous events. I'm also only interested if the moving average exceeds a threshold, so I'm not interested in keeping a trend of rate.
So for example, I want to know if I get more than 3 events/min. This was my first approach:
- When the first event comes in, I create a count of 1 and log the start time.
- When another event comes in, I increment the count and calculate the rate from the count and the elapsed time
- If rate exceed permitted value, generate alert.
I realised this wouldn’t work because if you had an event a week ago and then nothing until 10 events in the last minute, the average ‘rate’ is 11 in a week i.e. 3.6/day, rather than the current rate of 10/min.
So I'm thinking of trying the following:
- When the first event comes in, I create a count of 1 and log the start time.
- When another event comes in, if the time since the previous event exceeds the interval over which I want to measure the rate (1 min in my example), I effectively discard the previous event and record a count of 1 and the current time as the new start time (because if it's been over 1 min since the previous event, the rate can't exceed x/min right?).
- If the time since the previous event hasn't exceeded the monitoring interval, increment the count and calculate the rate from the count and the elapsed time
- If rate exceed permitted value, generate alert.
This seems simple but other posts on SO (specifically this question: Estimating rate of occurrence of an event with exponential smoothing and irregular events and it's accepted answer: https://stackoverflow.com/a/23617678/1430420) seem to imply that there's a lot more to it than I think.