• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

bitmapist: Powerful realtime analytics with Redis

mysql 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

I just released bitmapist (GitHub) – a powerful realtime analytics library that can help you answer following questions: Has user 123 been online today? This week? This month? Has user 123 performed action “X”? How many users have been act

I just released bitmapist (GitHub) – a powerful realtime analytics library that can help you answer following questions:

  • Has user 123 been online today? This week? This month?
  • Has user 123 performed action “X”?
  • How many users have been active have this month? This hour?
  • How many unique users have performed action “X” this week?
  • How many本文来源gao@daima#com搞(%代@#码@网& % of users that were active last week are still active?
  • How many % of users that were active last month are still active this month?

This library is very easy to use and enables you to create your own reports easily.

Using Redis bitmaps you can store events for millions of users in a very little amount of memory (megabytes). You should be careful about using huge ids (e.g. 2^32 or bigger) as this could require larger amounts of memory.

If you want to read more about bitmaps please read following:

  • Fast, easy, realtime metrics using Redis bitmaps
  • Redis setbit
  • Wikipedia: Bit Array
  • Crashlytics on Redis Analytics

Requires Redis 2.6+ and newest version of redis-py.

Installation

sudo pip install bitmapist

Examples

Setting things up:

from datetime import datetime, timedeltafrom bitmapist import setup_redis, delete_all_events, mark_event,\                      MonthEvents, WeekEvents, DayEvents, HourEvents,\                      BitOpAnd, BitOpOrnow = datetime.utcnow()last_month = datetime.utcnow() - timedelta(days=30)

Mark user 123 as active and has played a song:

mark_event('active', 123)mark_event('song:played', 123)

Answer if user 123 has been active this month:

assert 123 in MonthEvents('active', now.year, now.month)assert 123 in MonthEvents('song:played', now.year, now.month)

How many users have been active this week?

print len(WeekEvents('active', now.year, now.isocalendar()[1]))

Perform bit operations. How many users that have been active last month are still active this month?

active_2_months = BitOpAnd(    MonthEvents('active', last_month.year, last_month.month),    MonthEvents('active', now.year, now.month))print len(active_2_months)# Is 123 active for 2 months?assert 123 in active_2_months

Work with nested bit operations (imagine what you can do with this ;-))!

active_2_months = BitOpAnd(    BitOpAnd(        MonthEvents('active', last_month.year, last_month.month),        MonthEvents('active', now.year, now.month)    ),    MonthEvents('active', now.year, now.month))print len(active_2_months)assert 123 in active_2_months


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:bitmapist: Powerful realtime analytics with Redis

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址