bitmapist


Namebitmapist JSON
Version 3.115 PyPI version JSON
download
home_pagehttps://github.com/Doist/bitmapist
SummaryImplements a powerful analytics library using Redis bitmaps.
upload_time2024-12-19 21:27:00
maintainerNone
docs_urlNone
authorAmir Salihefendic
requires_python<3.13,>=3.9
licenseBSD
keywords redis bitmap analytics bitmaps realtime cohort
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![bitmapist](https://raw.githubusercontent.com/Doist/bitmapist/master/static/bitmapist.png "bitmapist")


[![Build Status](https://travis-ci.org/Doist/bitmapist.svg?branch=master)](https://travis-ci.org/Doist/bitmapist)

**NEW!** Try out our new standalone [bitmapist-server](https://github.com/Doist/bitmapist-server), which improves memory efficiency 443 times and makes your setup much cheaper to run (and more scaleable). It's fully compatiable with bitmapist that runs on Redis.

# bitmapist: a powerful analytics library for Redis

This Python library makes it possible to implement real-time, highly scalable analytics that can 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 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?
* What users performed action "X"?

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 as this could require larger amounts of memory. Ids should be in range [0, 2^32).

Additionally bitmapist can generate cohort graphs that can do following:
* Cohort over user retention
* How many % of users that were active last [days, weeks, months] are still active?
* How many % of users that performed action X also performed action Y (and this over time)
* And a lot of other things!

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

* http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps/
* http://redis.io/commands/setbit
* http://en.wikipedia.org/wiki/Bit_array
* http://www.slideshare.net/crashlytics/crashlytics-on-redis-analytics



# Installation

Can be installed very easily via:

    $ pip install bitmapist


# Ports

* PHP port: https://github.com/jeremyFreeAgent/Bitter


# Examples

Setting things up:

```python
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:

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

Answer if user 123 has been active this month:

```python
assert 123 in MonthEvents('active', now.year, now.month)
assert 123 in MonthEvents('song:played', now.year, now.month)
assert MonthEvents('active', now.year, now.month).has_events_marked() == True
```


How many users have been active this week?:

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

Iterate over all users active this week:

```python
for uid in WeekEvents('active'):
    print(uid)
```


If you're interested in "current events", you can omit extra `now.whatever`
arguments. Events will be populated with current time automatically.

For example, these two calls are equivalent:

```python

MonthEvents('active') == MonthEvents('active', now.year, now.month)

```

Additionally, for the sake of uniformity, you can create an event from
any datetime object with a `from_date` static method.

```python

MonthEvents('active').from_date(now) == MonthEvents('active', now.year, now.month)

```

Get the list of these users (user ids):

```python
print(list(WeekEvents('active', now.year, now.isocalendar()[1])))
```

There are special methods `prev` and `next` returning "sibling" events and
allowing you to walk through events in time without any sophisticated
iterators. A `delta` method allows you to "jump" forward or backward for
more than one step. Uniform API allows you to use all types of base events
(from hour to year) with the same code.

```python

current_month = MonthEvents()
prev_month = current_month.prev()
next_month = current_month.next()
year_ago = current_month.delta(-12)

```

Every event object has `period_start` and `period_end` methods to find a
time span of the event. This can be useful for caching values when the caching
of "events in future" is not desirable:

```python

ev = MonthEvent('active', dt)
if ev.period_end() < now:
    cache.set('active_users_<...>', len(ev))

```


As something new tracking hourly is disabled (to save memory!) To enable it as default do::

```python
import bitmapist
bitmapist.TRACK_HOURLY = True
```

Additionally you can supply an extra argument to `mark_event` to bypass the default value::

```python
mark_event('active', 123, track_hourly=False)
```


### Unique events

Sometimes the date of the event makes little or no sense, for example,
to filter out your premium accounts, or in A/B testing. There is a
`UniqueEvents` model for this purpose. The model creates only one
Redis key and doesn't depend on the date.

You can combine unique events with other types of events.

A/B testing example:

```python

active_today = DailyEvents('active')
a = UniqueEvents('signup_form:classic')
b = UniqueEvents('signup_form:new')

print("Active users, signed up with classic form", len(active & a))
print("Active users, signed up with new form", len(active & b))
```

Generic filter example

```python

def premium_up(uid):
    # called when user promoted to premium
    ...
    mark_unique('premium', uid)


def premium_down(uid):
    # called when user loses the premium status
    ...
    unmark_unique('premium', uid)

active_today = DailyEvents('active')
premium = UniqueEvents('premium')

# Add extra Karma for all premium users active today,
# just because today is a special day
for uid in premium & active_today:
    add_extra_karma(uid)
```

To get the best of two worlds you can mark unique event and regular
bitmapist events at the same time.


```python
def premium_up(uid):
    # called when user promoted to premium
    ...
    mark_event('premium', uid, track_unique=True)

```


### Perform bit operations

How many users that have been active last month are still active this month?

```python
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
```

Alternatively, you can use standard Python syntax for bitwise operations.


```python
last_month_event = MonthEvents('active', last_month.year, last_month.month)
this_month_event = MonthEvents('active', now.year, now.month)
active_two_months = last_month_event & this_month_event
```
Operators `&`, `|`, `^` and `~` supported.

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

```python
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

# Delete the temporary AND operation
active_2_months.delete()
```


### Deleting

If you want to permanently remove marked events for any time period you can use the `delete()` method:
```python
last_month_event = MonthEvents('active', last_month.year, last_month.month)
last_month_event.delete()
```

If you want to remove all bitmapist events use:
```python
bitmapist.delete_all_events()
```

When using Bit Operations (ie `BitOpAnd`) you can (and probably should) delete the results unless you want them cached. There are different ways to go about this:
```python
active_2_months = BitOpAnd(
    MonthEvents('active', last_month.year, last_month.month),
    MonthEvents('active', now.year, now.month)
)
# Delete the temporary AND operation
active_2_months.delete()

# delete all bit operations created in runtime up to this point
bitmapist.delete_runtime_bitop_keys()

# delete all bit operations (slow if you have many millions of keys in Redis)
bitmapist.delete_temporary_bitop_keys()
```


# bitmapist cohort

With bitmapist cohort you can get a form and a table rendering of the data you keep in bitmapist. If this sounds confusing [please look at Mixpanel](https://mixpanel.com/retention/).

Here's a simple example of how to generate a form and a rendering of the data you have inside bitmapist:
```python
from bitmapist import cohort

html_form = cohort.render_html_form(
    action_url='/_Cohort',
    selections1=[ ('Are Active', 'user:active'), ],
    selections2=[ ('Task completed', 'task:complete'), ]
)
print(html_form)

dates_data = cohort.get_dates_data(select1='user:active',
                                   select2='task:complete',
                                   time_group='days')

html_data = cohort.render_html_data(dates_data,
                                    time_group='days')

print(html_data)

# All the arguments should come from the FORM element (html_form)
# but to make things more clear I have filled them in directly
```

This will render something similar to this:

![bitmapist cohort screenshot](https://raw.githubusercontent.com/Doist/bitmapist/master/static/cohort_screenshot.png "bitmapist cohort screenshot")


## Contributing

Please see our guide [here](./CONTRIBUTING.md)

## Local Development

We use Poetry for dependency management & packaging.  Please see [here for setup instructions](https://python-poetry.org/docs/#installation).

Once you have Poetry installed, you can run the following to install the dependencies in a virtual environment:

```bash
poetry install
```

## Testing

To run our tests will need to ensure a local redis server is installed.

We use pytest to run unittests which you can run in a poetry shell with

```bash
poetry run pytest
```

## Releasing new versions

- Bump version in `pyproject.toml`
- Update the CHANGELOG
- Commit the changes with a commit message "Version X.X.X"
- Tag the current commit with `vX.X.X`
- Create a new release on GitHub named `vX.X.X`
- GitHub Actions will publish the new version to PIP for you

## Legal

Copyright: 2012 by Doist Ltd.

License: BSD

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Doist/bitmapist",
    "name": "bitmapist",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.9",
    "maintainer_email": null,
    "keywords": "redis, bitmap, analytics, bitmaps, realtime, cohort",
    "author": "Amir Salihefendic",
    "author_email": "dev@doist.com",
    "download_url": "https://files.pythonhosted.org/packages/07/1f/515186af197b7cee51f418896d2eb9dba983ec23f27d408c4f862e85fadd/bitmapist-3.115.tar.gz",
    "platform": null,
    "description": "![bitmapist](https://raw.githubusercontent.com/Doist/bitmapist/master/static/bitmapist.png \"bitmapist\")\n\n\n[![Build Status](https://travis-ci.org/Doist/bitmapist.svg?branch=master)](https://travis-ci.org/Doist/bitmapist)\n\n**NEW!** Try out our new standalone [bitmapist-server](https://github.com/Doist/bitmapist-server), which improves memory efficiency 443 times and makes your setup much cheaper to run (and more scaleable). It's fully compatiable with bitmapist that runs on Redis.\n\n# bitmapist: a powerful analytics library for Redis\n\nThis Python library makes it possible to implement real-time, highly scalable analytics that can answer following questions:\n\n* Has user 123 been online today? This week? This month?\n* Has user 123 performed action \"X\"?\n* How many users have been active this month? This hour?\n* How many unique users have performed action \"X\" this week?\n* How many % of users that were active last week are still active?\n* How many % of users that were active last month are still active this month?\n* What users performed action \"X\"?\n\nThis library is very easy to use and enables you to create your own reports easily.\n\nUsing Redis bitmaps you can store events for millions of users in a very little amount of memory (megabytes).\nYou should be careful about using huge ids as this could require larger amounts of memory. Ids should be in range [0, 2^32).\n\nAdditionally bitmapist can generate cohort graphs that can do following:\n* Cohort over user retention\n* How many % of users that were active last [days, weeks, months] are still active?\n* How many % of users that performed action X also performed action Y (and this over time)\n* And a lot of other things!\n\nIf you want to read more about bitmaps please read following:\n\n* http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps/\n* http://redis.io/commands/setbit\n* http://en.wikipedia.org/wiki/Bit_array\n* http://www.slideshare.net/crashlytics/crashlytics-on-redis-analytics\n\n\n\n# Installation\n\nCan be installed very easily via:\n\n    $ pip install bitmapist\n\n\n# Ports\n\n* PHP port: https://github.com/jeremyFreeAgent/Bitter\n\n\n# Examples\n\nSetting things up:\n\n```python\nfrom datetime import datetime, timedelta\nfrom bitmapist import setup_redis, delete_all_events, mark_event,\\\n                      MonthEvents, WeekEvents, DayEvents, HourEvents,\\\n                      BitOpAnd, BitOpOr\n\nnow = datetime.utcnow()\nlast_month = datetime.utcnow() - timedelta(days=30)\n```\n\nMark user 123 as active and has played a song:\n\n```python\nmark_event('active', 123)\nmark_event('song:played', 123)\n```\n\nAnswer if user 123 has been active this month:\n\n```python\nassert 123 in MonthEvents('active', now.year, now.month)\nassert 123 in MonthEvents('song:played', now.year, now.month)\nassert MonthEvents('active', now.year, now.month).has_events_marked() == True\n```\n\n\nHow many users have been active this week?:\n\n```python\nprint(len(WeekEvents('active', now.year, now.isocalendar()[1])))\n```\n\nIterate over all users active this week:\n\n```python\nfor uid in WeekEvents('active'):\n    print(uid)\n```\n\n\nIf you're interested in \"current events\", you can omit extra `now.whatever`\narguments. Events will be populated with current time automatically.\n\nFor example, these two calls are equivalent:\n\n```python\n\nMonthEvents('active') == MonthEvents('active', now.year, now.month)\n\n```\n\nAdditionally, for the sake of uniformity, you can create an event from\nany datetime object with a `from_date` static method.\n\n```python\n\nMonthEvents('active').from_date(now) == MonthEvents('active', now.year, now.month)\n\n```\n\nGet the list of these users (user ids):\n\n```python\nprint(list(WeekEvents('active', now.year, now.isocalendar()[1])))\n```\n\nThere are special methods `prev` and `next` returning \"sibling\" events and\nallowing you to walk through events in time without any sophisticated\niterators. A `delta` method allows you to \"jump\" forward or backward for\nmore than one step. Uniform API allows you to use all types of base events\n(from hour to year) with the same code.\n\n```python\n\ncurrent_month = MonthEvents()\nprev_month = current_month.prev()\nnext_month = current_month.next()\nyear_ago = current_month.delta(-12)\n\n```\n\nEvery event object has `period_start` and `period_end` methods to find a\ntime span of the event. This can be useful for caching values when the caching\nof \"events in future\" is not desirable:\n\n```python\n\nev = MonthEvent('active', dt)\nif ev.period_end() < now:\n    cache.set('active_users_<...>', len(ev))\n\n```\n\n\nAs something new tracking hourly is disabled (to save memory!) To enable it as default do::\n\n```python\nimport bitmapist\nbitmapist.TRACK_HOURLY = True\n```\n\nAdditionally you can supply an extra argument to `mark_event` to bypass the default value::\n\n```python\nmark_event('active', 123, track_hourly=False)\n```\n\n\n### Unique events\n\nSometimes the date of the event makes little or no sense, for example,\nto filter out your premium accounts, or in A/B testing. There is a\n`UniqueEvents` model for this purpose. The model creates only one\nRedis key and doesn't depend on the date.\n\nYou can combine unique events with other types of events.\n\nA/B testing example:\n\n```python\n\nactive_today = DailyEvents('active')\na = UniqueEvents('signup_form:classic')\nb = UniqueEvents('signup_form:new')\n\nprint(\"Active users, signed up with classic form\", len(active & a))\nprint(\"Active users, signed up with new form\", len(active & b))\n```\n\nGeneric filter example\n\n```python\n\ndef premium_up(uid):\n    # called when user promoted to premium\n    ...\n    mark_unique('premium', uid)\n\n\ndef premium_down(uid):\n    # called when user loses the premium status\n    ...\n    unmark_unique('premium', uid)\n\nactive_today = DailyEvents('active')\npremium = UniqueEvents('premium')\n\n# Add extra Karma for all premium users active today,\n# just because today is a special day\nfor uid in premium & active_today:\n    add_extra_karma(uid)\n```\n\nTo get the best of two worlds you can mark unique event and regular\nbitmapist events at the same time.\n\n\n```python\ndef premium_up(uid):\n    # called when user promoted to premium\n    ...\n    mark_event('premium', uid, track_unique=True)\n\n```\n\n\n### Perform bit operations\n\nHow many users that have been active last month are still active this month?\n\n```python\nactive_2_months = BitOpAnd(\n    MonthEvents('active', last_month.year, last_month.month),\n    MonthEvents('active', now.year, now.month)\n)\nprint(len(active_2_months))\n\n# Is 123 active for 2 months?\nassert 123 in active_2_months\n```\n\nAlternatively, you can use standard Python syntax for bitwise operations.\n\n\n```python\nlast_month_event = MonthEvents('active', last_month.year, last_month.month)\nthis_month_event = MonthEvents('active', now.year, now.month)\nactive_two_months = last_month_event & this_month_event\n```\nOperators `&`, `|`, `^` and `~` supported.\n\nWork with nested bit operations (imagine what you can do with this ;-))!\n\n```python\nactive_2_months = BitOpAnd(\n    BitOpAnd(\n        MonthEvents('active', last_month.year, last_month.month),\n        MonthEvents('active', now.year, now.month)\n    ),\n    MonthEvents('active', now.year, now.month)\n)\nprint(len(active_2_months))\nassert 123 in active_2_months\n\n# Delete the temporary AND operation\nactive_2_months.delete()\n```\n\n\n### Deleting\n\nIf you want to permanently remove marked events for any time period you can use the `delete()` method:\n```python\nlast_month_event = MonthEvents('active', last_month.year, last_month.month)\nlast_month_event.delete()\n```\n\nIf you want to remove all bitmapist events use:\n```python\nbitmapist.delete_all_events()\n```\n\nWhen using Bit Operations (ie `BitOpAnd`) you can (and probably should) delete the results unless you want them cached. There are different ways to go about this:\n```python\nactive_2_months = BitOpAnd(\n    MonthEvents('active', last_month.year, last_month.month),\n    MonthEvents('active', now.year, now.month)\n)\n# Delete the temporary AND operation\nactive_2_months.delete()\n\n# delete all bit operations created in runtime up to this point\nbitmapist.delete_runtime_bitop_keys()\n\n# delete all bit operations (slow if you have many millions of keys in Redis)\nbitmapist.delete_temporary_bitop_keys()\n```\n\n\n# bitmapist cohort\n\nWith bitmapist cohort you can get a form and a table rendering of the data you keep in bitmapist. If this sounds confusing [please look at Mixpanel](https://mixpanel.com/retention/).\n\nHere's a simple example of how to generate a form and a rendering of the data you have inside bitmapist:\n```python\nfrom bitmapist import cohort\n\nhtml_form = cohort.render_html_form(\n    action_url='/_Cohort',\n    selections1=[ ('Are Active', 'user:active'), ],\n    selections2=[ ('Task completed', 'task:complete'), ]\n)\nprint(html_form)\n\ndates_data = cohort.get_dates_data(select1='user:active',\n                                   select2='task:complete',\n                                   time_group='days')\n\nhtml_data = cohort.render_html_data(dates_data,\n                                    time_group='days')\n\nprint(html_data)\n\n# All the arguments should come from the FORM element (html_form)\n# but to make things more clear I have filled them in directly\n```\n\nThis will render something similar to this:\n\n![bitmapist cohort screenshot](https://raw.githubusercontent.com/Doist/bitmapist/master/static/cohort_screenshot.png \"bitmapist cohort screenshot\")\n\n\n## Contributing\n\nPlease see our guide [here](./CONTRIBUTING.md)\n\n## Local Development\n\nWe use Poetry for dependency management & packaging.  Please see [here for setup instructions](https://python-poetry.org/docs/#installation).\n\nOnce you have Poetry installed, you can run the following to install the dependencies in a virtual environment:\n\n```bash\npoetry install\n```\n\n## Testing\n\nTo run our tests will need to ensure a local redis server is installed.\n\nWe use pytest to run unittests which you can run in a poetry shell with\n\n```bash\npoetry run pytest\n```\n\n## Releasing new versions\n\n- Bump version in `pyproject.toml`\n- Update the CHANGELOG\n- Commit the changes with a commit message \"Version X.X.X\"\n- Tag the current commit with `vX.X.X`\n- Create a new release on GitHub named `vX.X.X`\n- GitHub Actions will publish the new version to PIP for you\n\n## Legal\n\nCopyright: 2012 by Doist Ltd.\n\nLicense: BSD\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Implements a powerful analytics library using Redis bitmaps.",
    "version": "3.115",
    "project_urls": {
        "Homepage": "https://github.com/Doist/bitmapist",
        "Repository": "https://github.com/Doist/bitmapist"
    },
    "split_keywords": [
        "redis",
        " bitmap",
        " analytics",
        " bitmaps",
        " realtime",
        " cohort"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "60d0a58da268db08fd9338f9bac5321cbce392652be05b71b4dde42e55f40d33",
                "md5": "a8df222d11c1972dc13f55968a698db5",
                "sha256": "99f05dad85935ba9d74b9f7c7935c11f9cd0b96af94d4141b7815f960f13e850"
            },
            "downloads": -1,
            "filename": "bitmapist-3.115-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a8df222d11c1972dc13f55968a698db5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.9",
            "size": 47680,
            "upload_time": "2024-12-19T21:26:59",
            "upload_time_iso_8601": "2024-12-19T21:26:59.555147Z",
            "url": "https://files.pythonhosted.org/packages/60/d0/a58da268db08fd9338f9bac5321cbce392652be05b71b4dde42e55f40d33/bitmapist-3.115-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "071f515186af197b7cee51f418896d2eb9dba983ec23f27d408c4f862e85fadd",
                "md5": "7b7d91e11dae059d8dbfd516c8d82cdd",
                "sha256": "87c155bf57f7c1115c4c2b7f9bd187a3929617465c2461e4df067f1bab7c9ff9"
            },
            "downloads": -1,
            "filename": "bitmapist-3.115.tar.gz",
            "has_sig": false,
            "md5_digest": "7b7d91e11dae059d8dbfd516c8d82cdd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.9",
            "size": 48358,
            "upload_time": "2024-12-19T21:27:00",
            "upload_time_iso_8601": "2024-12-19T21:27:00.799780Z",
            "url": "https://files.pythonhosted.org/packages/07/1f/515186af197b7cee51f418896d2eb9dba983ec23f27d408c4f862e85fadd/bitmapist-3.115.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-19 21:27:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Doist",
    "github_project": "bitmapist",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "bitmapist"
}
        
Elapsed time: 0.46974s