example.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python
  2. from redistimeseries.client import Client as RedisTimeSeries
  3. import redis
  4. import time
  5. redis_host = "localhost"
  6. redis_port = 6379
  7. rts = RedisTimeSeries(host=redis_host, port=redis_port)
  8. pool = redis.ConnectionPool(host=redis_host, port=redis_port)
  9. r = redis.Redis(connection_pool=pool)
  10. try:
  11. r.xadd("mystream", {'event_type': 'startup', 'user': 'root'})
  12. r.xgroup_create("mystream", "consumerGroup", '$')
  13. except:
  14. print("group already exists")
  15. while True:
  16. msgs = r.xreadgroup("consumerGroup", "consumerName", streams={"mystream": '>'}, count=10, block=1000, noack=False)
  17. for msg in msgs:
  18. for m in msg[1]:
  19. evnt = m[1]['event_type']
  20. try:
  21. rts.info(evnt)
  22. except:
  23. rts.create(evnt, retentionSecs=60, labels={'event_type': evnt})
  24. rts.create(evnt+"_minute", retentionSecs=0, labels={'event_type': evnt})
  25. rts.createrule(evnt, evnt+"_minute", 'count', 60)
  26. rts.incrby(evnt,1)