django-trackstats


Namedjango-trackstats JSON
Version 0.7.0 PyPI version JSON
download
home_pagehttp://github.com/pennersr/django-trackstats
SummaryStatistics storage for Django
upload_time2023-08-04 12:21:01
maintainer
docs_urlNone
authorRaymond Penners
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            =============================
Welcome to django-trackstats!
=============================

.. image:: https://github.com/pennersr/django-trackstats/actions/workflows/ci.yml/badge.svg
   :target: https://github.com/pennersr/django-trackstats/actions

.. image:: https://img.shields.io/pypi/v/django-trackstats.svg
   :target: https://pypi.python.org/pypi/django-trackstats

.. image:: https://coveralls.io/repos/pennersr/django-trackstats/badge.svg?branch=master
   :alt: Coverage Status
   :target: https://coveralls.io/r/pennersr/django-trackstats

.. image:: https://pennersr.github.io/img/bitcoin-badge.svg
   :target: https://blockchain.info/address/1AJXuBMPHkaDCNX2rwAy34bGgs7hmrePEr

.. image:: https://pennersr.github.io/img/emacs-badge.svg
   :target: https://www.gnu.org/software/emacs/

Keep track of your statistics.

Source code
  http://github.com/pennersr/django-trackstats


Use Case
========

- You need an elegant solution for storing statistics in a generic and structural fashion.

- You need to denormalize the results of various aggregated queries.

- You require access to the stored statistics within your application layer.

So, the focus is purely on storing statistics for use within your application later
on. Other features, such as charting, reports, OLAP, query builders, slicing &
dicing, integration with ``Datadog`` and the likes are all beyond scope.


Concepts
========

The following concepts are used:

Metric
  A piece of information to keep track of. For example, "Order count",
  or "Number of users signed up".

Domain
  Metrics are organized in groups, each group is called a domain. For
  example you can have a "shopping" domain with metrics such as "Order
  count", "Items sold", "Products viewed", and a "users" domain with
  "Login count", "Signup count". Or, in case you are tracking external
  statistics from social networks, you may introduce a "Twitter"
  domain, and metrics "Followers count".

Statistic
  Used to store the actual values by date, for a specific metric.

Period
  The time period for which the stored value holds. For example, you
  can keep track of cumulative, all-time, numbers (`Period.LIFETIME`),
  store incremental values on a daily basis (`Period.DAY`), or keep
  track of a rolling count for the last 7 days (`Period.WEEK`).

Reference IDs
  Domains and metrics must be assigned unique reference IDs (of type
  string). Rationale: Having a human readable, non PK based, reference
  is esential as soon as you are going to export statistics.


Usage
=====

First, setup your domains:

.. code:: python

    from trackstats.models import Domain

    Domain.objects.SHOPPING = Domain.objects.register(
        ref='shopping',
        name='Shopping')
    Domain.objects.USERS = Domain.objects.register(
        ref='users',
        name='Users')
    Domain.objects.TWITTER = Domain.objects.register(
        ref='twitter',
        name='Twitter')

Define a few metrics:

.. code:: python

    from trackstats.models import Domain, Metric

    Metric.objects.SHOPPING_ORDER_COUNT = Metric.objects.register(
        domain=Domain.objects.SHOPPING,
        ref='order_count',
        name='Number of orders sold')
    Metric.objects.USERS_USER_COUNT = Metric.objects.register(
        domain=Domain.objects.USERS,
        ref='user_count',
        name='Number of users signed up')
    Metric.objects.TWITTER_FOLLOWER_COUNT = Metric.objects.register(
        # Matches Twitter API
        ref='follower_count',
        domain=Domain.objects.TWITTER)

Now, let's store some one-off statistics:

.. code:: python

    from trackstats.models import StatisticByDate, Domain, Metric, Period

    # All-time, cumulative, statistic
    n = Order.objects.all().count()
    StatisticByDate.objects.record(
        metric=Metric.objects.SHOPPING_ORDER_COUNT,
        value=n,
        period=Period.LIFETIME)

    # Users signed up, at a specific date
    dt = date.today()
    n = User.objects.filter(
        date_joined__day=dt.day,
        date_joined__month=dt.month,
        date_joined__year=dt.year).count()
    StatisticByDate.objects.record(
        metric=Metric.objects.USERS_USER_COUNT,
        value=n,
        period=Period.DAY)

Creating code to store statistics yourself can be a tedious job.
Luckily, a few shortcuts are available to track statistics without
having to write any code yourself.

Consider you want to keep track of the number of comments created on a
daily basis:

.. code:: python

    from trackstats.trackers import CountObjectsByDateTracker

    CountObjectsByDateTracker(
        period=Period.DAY,
        metric=Metric.objects.COMMENT_COUNT,
        date_field='timestamp').track(Comment.objects.all())

Or, in case you want to track the number of comments, per user, on a daily
basis:

.. code:: python

    CountObjectsByDateAndObjectTracker(
        period=Period.DAY,
        metric=Metric.objects.COMMENT_COUNT,
        # comment.user points to a User
        object_model=User,
        object_field='user',
        # Comment.timestamp is used for grouping
        date_field='timestamp').track(Comment.objects.all())


Models
======

The ``StatisticByDate`` model represents statistics grouped by date --
the most common use case.

Another common use case is to group by both date and some other object
(e.g. a user, category, site).  For this, use
``StatisticByDateAndObject``. It uses a generic foreign key.

If you need to group in a different manner, e.g. by country, province
and date, you can use the ``AbstractStatistic`` base class to build just
that.


Cross-Selling
=============

If you like this, you may also like:

- django-allauth: https://github.com/pennersr/django-allauth
- netwell: https://github.com/pennersr/netwell

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/pennersr/django-trackstats",
    "name": "django-trackstats",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Raymond Penners",
    "author_email": "raymond.penners@intenct.nl",
    "download_url": "https://files.pythonhosted.org/packages/bd/f5/057c5c4e377cf4cb95a07b93f558f263232f5d907a13ff06109b751450c7/django-trackstats-0.7.0.tar.gz",
    "platform": "any",
    "description": "=============================\nWelcome to django-trackstats!\n=============================\n\n.. image:: https://github.com/pennersr/django-trackstats/actions/workflows/ci.yml/badge.svg\n   :target: https://github.com/pennersr/django-trackstats/actions\n\n.. image:: https://img.shields.io/pypi/v/django-trackstats.svg\n   :target: https://pypi.python.org/pypi/django-trackstats\n\n.. image:: https://coveralls.io/repos/pennersr/django-trackstats/badge.svg?branch=master\n   :alt: Coverage Status\n   :target: https://coveralls.io/r/pennersr/django-trackstats\n\n.. image:: https://pennersr.github.io/img/bitcoin-badge.svg\n   :target: https://blockchain.info/address/1AJXuBMPHkaDCNX2rwAy34bGgs7hmrePEr\n\n.. image:: https://pennersr.github.io/img/emacs-badge.svg\n   :target: https://www.gnu.org/software/emacs/\n\nKeep track of your statistics.\n\nSource code\n  http://github.com/pennersr/django-trackstats\n\n\nUse Case\n========\n\n- You need an elegant solution for storing statistics in a generic and structural fashion.\n\n- You need to denormalize the results of various aggregated queries.\n\n- You require access to the stored statistics within your application layer.\n\nSo, the focus is purely on storing statistics for use within your application later\non. Other features, such as charting, reports, OLAP, query builders, slicing &\ndicing, integration with ``Datadog`` and the likes are all beyond scope.\n\n\nConcepts\n========\n\nThe following concepts are used:\n\nMetric\n  A piece of information to keep track of. For example, \"Order count\",\n  or \"Number of users signed up\".\n\nDomain\n  Metrics are organized in groups, each group is called a domain. For\n  example you can have a \"shopping\" domain with metrics such as \"Order\n  count\", \"Items sold\", \"Products viewed\", and a \"users\" domain with\n  \"Login count\", \"Signup count\". Or, in case you are tracking external\n  statistics from social networks, you may introduce a \"Twitter\"\n  domain, and metrics \"Followers count\".\n\nStatistic\n  Used to store the actual values by date, for a specific metric.\n\nPeriod\n  The time period for which the stored value holds. For example, you\n  can keep track of cumulative, all-time, numbers (`Period.LIFETIME`),\n  store incremental values on a daily basis (`Period.DAY`), or keep\n  track of a rolling count for the last 7 days (`Period.WEEK`).\n\nReference IDs\n  Domains and metrics must be assigned unique reference IDs (of type\n  string). Rationale: Having a human readable, non PK based, reference\n  is esential as soon as you are going to export statistics.\n\n\nUsage\n=====\n\nFirst, setup your domains:\n\n.. code:: python\n\n    from trackstats.models import Domain\n\n    Domain.objects.SHOPPING = Domain.objects.register(\n        ref='shopping',\n        name='Shopping')\n    Domain.objects.USERS = Domain.objects.register(\n        ref='users',\n        name='Users')\n    Domain.objects.TWITTER = Domain.objects.register(\n        ref='twitter',\n        name='Twitter')\n\nDefine a few metrics:\n\n.. code:: python\n\n    from trackstats.models import Domain, Metric\n\n    Metric.objects.SHOPPING_ORDER_COUNT = Metric.objects.register(\n        domain=Domain.objects.SHOPPING,\n        ref='order_count',\n        name='Number of orders sold')\n    Metric.objects.USERS_USER_COUNT = Metric.objects.register(\n        domain=Domain.objects.USERS,\n        ref='user_count',\n        name='Number of users signed up')\n    Metric.objects.TWITTER_FOLLOWER_COUNT = Metric.objects.register(\n        # Matches Twitter API\n        ref='follower_count',\n        domain=Domain.objects.TWITTER)\n\nNow, let's store some one-off statistics:\n\n.. code:: python\n\n    from trackstats.models import StatisticByDate, Domain, Metric, Period\n\n    # All-time, cumulative, statistic\n    n = Order.objects.all().count()\n    StatisticByDate.objects.record(\n        metric=Metric.objects.SHOPPING_ORDER_COUNT,\n        value=n,\n        period=Period.LIFETIME)\n\n    # Users signed up, at a specific date\n    dt = date.today()\n    n = User.objects.filter(\n        date_joined__day=dt.day,\n        date_joined__month=dt.month,\n        date_joined__year=dt.year).count()\n    StatisticByDate.objects.record(\n        metric=Metric.objects.USERS_USER_COUNT,\n        value=n,\n        period=Period.DAY)\n\nCreating code to store statistics yourself can be a tedious job.\nLuckily, a few shortcuts are available to track statistics without\nhaving to write any code yourself.\n\nConsider you want to keep track of the number of comments created on a\ndaily basis:\n\n.. code:: python\n\n    from trackstats.trackers import CountObjectsByDateTracker\n\n    CountObjectsByDateTracker(\n        period=Period.DAY,\n        metric=Metric.objects.COMMENT_COUNT,\n        date_field='timestamp').track(Comment.objects.all())\n\nOr, in case you want to track the number of comments, per user, on a daily\nbasis:\n\n.. code:: python\n\n    CountObjectsByDateAndObjectTracker(\n        period=Period.DAY,\n        metric=Metric.objects.COMMENT_COUNT,\n        # comment.user points to a User\n        object_model=User,\n        object_field='user',\n        # Comment.timestamp is used for grouping\n        date_field='timestamp').track(Comment.objects.all())\n\n\nModels\n======\n\nThe ``StatisticByDate`` model represents statistics grouped by date --\nthe most common use case.\n\nAnother common use case is to group by both date and some other object\n(e.g. a user, category, site).  For this, use\n``StatisticByDateAndObject``. It uses a generic foreign key.\n\nIf you need to group in a different manner, e.g. by country, province\nand date, you can use the ``AbstractStatistic`` base class to build just\nthat.\n\n\nCross-Selling\n=============\n\nIf you like this, you may also like:\n\n- django-allauth: https://github.com/pennersr/django-allauth\n- netwell: https://github.com/pennersr/netwell\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Statistics storage for Django",
    "version": "0.7.0",
    "project_urls": {
        "Homepage": "http://github.com/pennersr/django-trackstats"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bdf5057c5c4e377cf4cb95a07b93f558f263232f5d907a13ff06109b751450c7",
                "md5": "8ed58b0a14f587f7e015ddb0884abb29",
                "sha256": "f3db9bd4c4cd7bb6095f32cf018cc87a6aefd124eb1bed06ec3e8d2db494d35b"
            },
            "downloads": -1,
            "filename": "django-trackstats-0.7.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8ed58b0a14f587f7e015ddb0884abb29",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15324,
            "upload_time": "2023-08-04T12:21:01",
            "upload_time_iso_8601": "2023-08-04T12:21:01.920040Z",
            "url": "https://files.pythonhosted.org/packages/bd/f5/057c5c4e377cf4cb95a07b93f558f263232f5d907a13ff06109b751450c7/django-trackstats-0.7.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-04 12:21:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pennersr",
    "github_project": "django-trackstats",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "django-trackstats"
}
        
Elapsed time: 0.34603s