ActiveMedian

Median and percentile for Active Record, Mongoid, arrays, and hashes

Supports:

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

:fire: Uses native functions for blazing performance

Build Status

Getting Started

Add this line to your application’s Gemfile:

gem 'active_median'

For MySQL, also follow these instructions.

Models

Median

Item.median(:price)

Percentile

Request.percentile(:response_time, 0.95)

Works with grouping, too

Order.group(:store_id).median(:total)

Arrays and Hashes

Median

[1, 2, 3].median

Percentile

[1, 2, 3].percentile(0.95)

You can also pass a block

{a: 1, b: 2, c: 3}.median { |k, v| v }

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

SQLite

Improve performance with SQLite with an extension. Download percentile.c and follow the instructions for compiling loadable extensions for your platform.

On Linux, use:

gcc -g -fPIC -shared -lsqlite3 percentile.c -o percentile.so

On Mac, use:

gcc -g -fPIC -dynamiclib -L/usr/local/opt/sqlite/lib -lsqlite3 percentile.c -o percentile.dylib

To load it in Rails, create an initializer with:

db = ActiveRecord::Base.connection.raw_connection
db.enable_load_extension(1)
db.load_extension("percentile.so") # or percentile.dylib
db.enable_load_extension(0)

Upgrading

0.3.0

ActiveMedian 0.3.0 protects against unsafe input by default. For non-attribute arguments, use:

Item.median(Arel.sql(known_safe_value))

Also, percentiles are now supported with SQLite. Use the percentile extension instead of extension-functions.

Contributing

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

To get started with development and testing:

git clone https://github.com/ankane/active_median.git
cd active_median
createdb active_median_test
bundle install
bundle exec rake test