agoragames / kairos

Python module for time series data in Redis and Mongo
BSD 3-Clause "New" or "Revised" License
207 stars 38 forks source link

[DOC] How to count events in last (n) minutes? #4

Closed inactivist closed 12 years ago

inactivist commented 12 years ago

For example, I'd like to calculate the sum of events over the last five minutes.

I'm using Timeseries in 'count_only' mode because I never care about discrete event data.

Example:

KEY_PREFIX = 'timedata:things'

client = redis.Redis('localhost', 6379)
minute_count = Timeseries(client, {
        'minute':{
            'step':60,              # 60 seconds
            'steps':15,             # last 15 minutes
            'count_only' : True,    # store counts only.
        }
    }, key_prefix=KEY_PREFIX)

def hit(event_id):
    minute_count.insert(event_id, 1)

When events arrive, I call:

    hit('some-event')

Now, I'd like to know the sum of all events recorded in the last 5 minutes (not just the event count in each bucket.)

I think I should be able to sum the items by calling minute_count.series('some-event', 'minute', steps=5, condensed=True) -- is that correct? (I've tried that but I'm having no luck. See issue #3.)

What's the best/recommended way to do this?

awestendorf commented 12 years ago

What you're describing is how I expect it to be used. I'll look into this.

inactivist commented 12 years ago

It looks like commit d463f93570ba4feacf576f523b60f785d5fcb872 fixes this one too. I'll report back ASAP.

awestendorf commented 12 years ago

Thanks for checking on that. The beta-refactor significantly cleans up the implementation.