Tailslide

Median and percentile for Django models and MongoEngine documents

Supports:

  • PostgreSQL
  • SQLite
  • MariaDB
  • MySQL (with an extension)
  • SQL Server
  • MongoDB

? Uses native functions when possible for blazing performance

Build Status

Installation

Run:

pip install tailslide

For MySQL, also follow these instructions.

Getting Started

Django

Median

from tailslide import Median

Item.objects.aggregate(Median('price'))

Percentile

from tailslide import Percentile

Request.objects.aggregate(Percentile('response_time', .95))

Works with grouping, too, with PostgreSQL, MySQL, and SQLite

Order.objects.values('store').annotate(total=Median('total')).order_by('store')

MongoEngine

Add a custom QuerySet to the models where you want to use it.

from tailslide.mongoengine import TailslideQuerySet

class Item(Document):
    meta = {'queryset_class': TailslideQuerySet}

Median

Item.objects.median('price')

Percentile

Item.objects.percentile('price', .95)

Additional Instructions

MySQL

MySQL requires the PERCENTILE_CONT function from udf_infusion. To install it, do:

git clone https://github.com/infusion/udf_infusion.git
cd udf_infusion
./configure --enable-functions="percentile_cont"
make
sudo make install
mysql <options> < load.sql

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/tailslide.git
cd tailslide
pip install -r requirements.txt

# Postgres
createdb tailslide_test
ADAPTER=postgresql pytest tests/django

# SQLite
ADAPTER=sqlite pytest tests/django

# MariaDB
mysqladmin create tailslide_test
ADAPTER=mariadb pytest tests/django

# MySQL (install the extension first)
mysqladmin create tailslide_test
ADAPTER=mysql pytest tests/django

# SQL Server
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
docker exec -it <container-id> /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P YourStrong\!Passw0rd -Q "CREATE DATABASE tailslide_test"
ADAPTER=sqlserver pytest tests/django

# MongoDB
pytest tests/mongoengine

GitHub

https://github.com/ankane/tailslide