视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
bitmapist:PowerfulrealtimeanalyticswithRedis
2020-11-09 13:28:18 责编:小采
文档

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 % 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, timedelta
    from bitmapist import setup_redis, delete_all_events, mark_event,\
     MonthEvents, WeekEvents, DayEvents, HourEvents,\
     BitOpAnd, BitOpOr
    now = 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
    

    下载本文
    显示全文
    专题