<commit_before>snappy_stats
============

Simple Ruby time series statistics backed by Redis gem <commit_msg>Add detail to Readme<commit_after># snappy_stats
============

This is a simple Ruby time series statistics gem that uses Redis for storage. Retrieval is optimised for graphing engines by nil filling empty time periods. I graph it with [Rickshaw] (code.shutterstock.com/rickshaw/).

## Installation
Install the gem:

    gem install snappy_stats

Add it to your Gemfile:

    gem "snappy_stats"

## Background
SnappyStats is a Ruby implementation of a simple time series statistics tool based on [ApiAxle's Javascript statistics lib] (http://blog.apiaxle.com/post/storing-near-realtime-stats-in-redis/) which in turn was heavily influenced by [RRDTool] (http://en.wikipedia.org/wiki/RRDtool).

It allows you to store and retrieve simple metrics (think hit counters). The metrics are returned in a set optimised for graphing engines as each time period is represented in the results even if no hit was recorded in that period.

## Usage
    # Record a hit now
    SnappyStats.recordHitNow("test:counter1")
    # Get all results between given timestamp range
    results = SnappyStats.get(:day,Time.now.midnight.to_i,(Time.now.midnight + 1.days).to_i,"test:counter1")

## Detailed Example
    require 'snappy_stats'

    #Using Timecop to simulate changing time, not required in production implementation
    Timecop.freeze
    now = Time.now        

    # Simulate 5 hits for today
    5.times do 
        SnappyStats.recordHitNow("test:counter1")
    end

    # Simulate 5 hits for 2 days from now
    Timecop.freeze(now + 2.days) 
    2.times do 
        SnappyStats.recordHitNow("test:counter1")
    end

    # Decide what range we want to search for
    from = Time.now.midnight.to_i
    to = (Time.now.midnight + 2.days).to_i

    # Get daily results between the from and to timestamps for given key, filling missing days with nil
    daily_stats = SnappyStats.get(:day,from,to,"test:counter1")
    p daily_stats  # {1382054400=>"5", 1382140800=>nil,1382227200=>"2"}

## Configuration
By default SnappyStats uses the current vesion of Redis defined in your project. To use a different Redis use a config initializer.

    # config/initializers/snappy_stats.rb
    SnappyStats.configure do |config|
        # set the Redis connection to a new Redis connection
        config.redis = Redis.new
        # Change Redis key prefix away from default 'stats'
        config.namnespace = "custom_stats"
    end

## Contributing to SnappyStats

* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


{'fraction_non_alphanumeric': 0.07288082577416327, 'fraction_numerical': 0.014075695964967157, 'mean_word_length': 3.723781388478582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4833818', 'n_tokens_mistral': 937, 'n_tokens_neox': 880, 'n_words': 388}