Package: django-qsstats-magic
The goal of django-qsstats is to be a microframework to make
repetitive tasks such as generating aggregate statistics of querysets
over time easier.  It’s probably overkill for the task at hand, but yay
microframeworks!django-qsstats-magic is a refactoring of django-qsstats app with slightly
changed API, simplified internals and faster time_series implementation.Requirementspython-dateutil> 1.4, < 2.0django1.8+DatabaseIf timezone support is enabled in Django, the database must have also timezone support installed.
For MySQL it might be needed to run:::mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysqlLicenseLiensed under a BSD-style license.ExamplesHow many users signed up today? this month? this year?from django.contrib.auth.models import User
import qsstats

qs = User.objects.all()
qss = qsstats.QuerySetStats(qs, 'date_joined')

print '%s new accounts today.' % qss.this_day()
print '%s new accounts this week.' % qss.this_week()
print '%s new accounts this month.' % qss.this_month()
print '%s new accounts this year.' % qss.this_year()
print '%s new accounts until now.' % qss.until_now()This might print something like:5 new accounts today.
11 new accounts this week.
27 new accounts this month.
377 new accounts this year.
409 new accounts until now.Aggregating time-series data suitable for graphingfrom django.contrib.auth.models import User
import datetime, qsstats

qs = User.objects.all()
qss = qsstats.QuerySetStats(qs, 'date_joined')

today = datetime.date.today()
seven_days_ago = today - datetime.timedelta(days=7)

time_series = qss.time_series(seven_days_ago, today)
print 'New users in the last 7 days: %s' % [t[1] for t in time_series]This might print something like:New users in the last 7 days: [3, 10, 7, 4, 12, 9, 11]Please see qsstats/tests.py for similar usage examples.APITheQuerySetStatsobjectIn order to provide maximum flexibility, theQuerySetStatsobject
can be instantiated with as little or as much information as you like.
All keword arguments are optional butDateFieldMissingandQuerySetMissingwill be raised if you try to useQuerySetStatswithout providing enough information.qsThe queryset to operate on.Default:Nonedate_fieldThe date field within the queryset to use.Default:NoneaggregateThe django aggregation instance. Can be set also set when
instantiating or calling one of the methods.Default:Count('id')operatorThe default operator to use for thepivotfunction.  Can be also set
when callingpivot.Default:'lte'todayThe date that will be considered as today date. Iftodayparam is None
QuerySetStats’ today will be datetime.date.today().Default:NoneAll of the documented methods take a standard set of keyword arguments
that override any information already stored within theQuerySetStatsobject.  These keyword arguments aredate_fieldandaggregate.Once you have aQuerySetStatsobject instantiated, you can receive a
single aggregate result by using the following methods:for_minutefor_hourfor_dayfor_weekfor_monthfor_yearPositional arguments:dt, adatetime.datetimeordatetime.dateobject to filter the queryset to this interval (minute, hour, day, week,
month or year).this_minutethis_hourthis_daythis_weekthis_monththis_yearWrappers aroundfor_<interval>that usesdateutil.relativedeltato
provide aggregate information for this current interval.QuerySetStatsalso provides a method for returning aggregated
time-series data which may be extremely using in plotting data:time_seriesPositional arguments:startandend, each adatetime.dateordatetime.datetimeobject used in marking
the start and stop of the time series data.Keyword arguments: In addition to the standarddate_fieldandaggregatekeyword argument,time_seriestakes an optionalintervalkeyword argument used to mark which interval to use while
calculating aggregate data betweenstartandend.  This argument
defaults to'days'and can accept'years','months','weeks','days','hours'or'minutes'.
It will raiseInvalidIntervalotherwise.This methods returns a list of tuples.  The first item in each
tuple is adatetime.datetimeobject for the current inverval.  The
second item is the result of the aggregate operation.  For
example:[(datetime.datetime(2010, 3, 28, 0, 0), 12), (datetime.datetime(2010, 3, 29, 0, 0), 0), ...]Formatting of date information is left as an exercise to the user and may
vary depending on interval used.untilProvide aggregate information until a given date or time, filtering the
queryset usinglte.Positional arguments:dtadatetime.dateordatetime.datetimeobject to be used for filtering the queryset since.Keyword arguments:date_field,aggregate.until_nowAggregate information until now.Positional arguments:dtadatetime.dateordatetime.datetimeobject to be used for filtering the queryset since (usinglte).Keyword arguments:date_field,aggregate.afterAggregate information after a given date or time, filtering the queryset
usinggte.Positional arguments:dtadatetime.dateordatetime.datetimeobject to be used for filtering the queryset since.Keyword arguments:date_field,aggregate.after_nowAggregate information after now.Positional arguments:dtadatetime.dateordatetime.datetimeobject to be used for filtering the queryset since (usinggte).Keyword arguments:date_field,aggregate.pivotUsed bysince,after, anduntil_nowbut potentially useful if
you would like to specify your own operator instead of the defaults.Positional arguments:dtadatetime.dateordatetime.datetimeobject to be used for filtering the queryset since (usinglte).Keyword arguments:operator,date_field,aggregate.RaisesInvalidOperatorif the operator provided is not one of'lt','lte',gtorgte.TestingIf you’d like to testdjango-qsstats-magicagainst your local configuration, addqsstatsto yourINSTALLED_APPSand run./manage.py test qsstats.
The test suite assumes thatdjango.contrib.authis installed.For testing against different python, DB and django versions install tox
(pip install tox) and run ‘tox’ from the source checkout:$ toxDb user ‘qsstats_test’ with password ‘qsstats_test’ and a DB ‘qsstats_test’
should exist.Difference from django-qsstatsFaster time_series method using 1 sql query (currently works for MySQL and
PostgreSQL, with a fallback to the old method for other DB backends).Singleaggregateparameter instead ofaggregate_fieldandaggregate_class. Default value is alwaysCount('id')and can’t be
specified in settings.py.QUERYSETSTATS_DEFAULT_OPERATORoption is also
unsupported now.Support for minute and hour aggregates.start_dateandend_datearguments are renamed tostartandendbecause of 3.Internals are changed.I don’t know if original author (Matt Croydon) would like my changes so
I renamed a project for now. If the changes will be merged then
django-qsstats-magic will become obsolete.