Giter Club home page Giter Club logo

active_median's Introduction

ActiveMedian

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

Supports:

  • PostgreSQL 9.4+
  • MariaDB 10.3.3+
  • MySQL and SQL (with extensions)
  • SQL Server 2012+
  • MongoDB 2.1+

🔥 Uses native functions for blazing performance

Build Status

Getting Started

Add this line to your application’s Gemfile:

gem 'active_median'

For MySQL and SQLite, 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)

User Input

If passing user input as the column, be sure to sanitize it first like you must with other aggregate methods like sum.

column = params[:column]

# check against permitted columns
raise "Unpermitted column" unless ["column_a", "column_b"].include?(column)

User.median(column)

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

SQLite requires a community extension. Download extension-functions.c and follow the instructions for compiling loadable extensions for your platform. On Mac, use:

gcc -g -fPIC -dynamiclib extension-functions.c -o extension-functions.dylib

To load it in Rails, create an initializer with:

db = ActiveRecord::Base.connection.raw_connection
db.enable_load_extension(1)
db.load_extension("extension-functions.dylib")
db.enable_load_extension(0)

Upgrading

0.2.0

A user-defined function is no longer needed for Postgres. Create a migration with ActiveMedian.drop_function to remove it.

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

active_median's People

Contributors

ankane avatar nickelser avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.