Code Monkey home page Code Monkey logo

abacus's Introduction

Abacus

Abacus is a simple counting server. It counts al kind of things as triples of strings on dates.

The three parts of this service are gathered in a rails project:

  1. A resque worker using a redis queue which work as the data input interface.
  2. A single mysql table on which stored amounts of that triple are incremented and one Model to access the table.
  3. A Controller to provide an API interface to gather the stored counts with diferent sort of requests.

Installation

  • create database abacus_statistics_development;
  • create database abacus_statistics_test;
  • adjust varchar width in migration
  • bundle install
  • rake db:migrate
  • rake db:test:prepare
  • bundle exec spec spec
  • rake resque:work

Intregrate this lines into the code where you want to count things:

require 'resque'
require 'ostruct'
worker = module Worker class StatisticsRecorder; @queue = 'abacus_statistics' end; end

Counting

After running this initial installation steps you can start pushing data into the redis queue

Resque.enqueue(worker, {:ns1 => 'search', :ns2 => 'horse', :ns3 => 'website', :year => 2011, :month => 1, :day => 31})
Resque.enqueue(worker, {:ns1 => 'sale', :ns2 => 'horse', :ns3 => 'iphone', :year => 2011, :month => 1 , :day => 31})
Resque.enqueue(worker, {:ns1 => 'comment', :ns2 => 'horse', :ns3 => 'iphone', :year => 2011, :month => 1 ,:day => 31})

Save an amount > 1

Resque.enqueue(worker, {:ns1 => 'sales_price', :ns2 => 'horse', :ns3 => 'iphone',:amount => 2 :year => 2011, :month => 1 ,:day => 31})

Of course you can also decrement, for example if a sale is cancelled

Resque.enqueue(worker, {:ns1 => 'sales_price', :ns2 => 'horse', :ns3 => 'iphone',:amount => -2 :year => 2011, :month => 1 ,:day => 31})

day, month, year can be skipped then the actual date is used data is return as array of jason hashes

Count by post

You can also count by posting to /statistics with curl this looks like this: curl -d 'ns1=test_ns1&ns2=test_ns2&ns3=test_ns3' localhost:3000/statistics.json

Other languages

Everything that adds correctly formatted data to a redis queue the resque worker is listening on will work for counting.

Collecting the data

You can receive the counts by sending http request with triples and date ranges as parameters to the webserver. The webserver return an Array of json hashes containing the Amount.

Get a single data set

curl "localhost:3000/statistics?ns1=comment&ns2=horse&ns3=iphone&date_since=2011-01-31&date_till=2011-01-31&granularity=day"

[{"day":31,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":3.0,"ns3":"iphone"}]

Get data for a date range, 0 amounts are returned for dates without data

curl "localhost:3000/statistics?ns1=comment&ns2=horse&ns3=iphone&date_since=2011-01-29&date_till=2011-01-31&granularity=day"

[{"day":29,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":0.0,"ns3":"iphone"},
 {"day":30,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":0.0,"ns3":"iphone"},
 {"day":31,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":1.0,"ns3":"iphone"}]

Data for multi n1, n2 and dates the cartesian product will be returned filling empty triple with 0 amount

curl "http://localhost:3000/statistics?ns1\[\]=comment&ns1\[\]=sale&ns2\[\]=horse&ns2\[\]=camel&ns3=iphone&date_since=2011-01-30&date_till=2011-01-31&granularity=day"

[{"day":30,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":0.0,"ns3":"iphone"},
{"day":31,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":1.0,"ns3":"iphone"},
{"day":30,"ns1":"comment","month":1,"ns2":"camel","year":2011,"amount":0.0,"ns3":"iphone"},
{"day":31,"ns1":"comment","month":1,"ns2":"camel","year":2011,"amount":0.0,"ns3":"iphone"},
{"day":30,"ns1":"sale","month":1,"ns2":"horse","year":2011,"amount":0.0,"ns3":"iphone"},
{"day":31,"ns1":"sale","month":1,"ns2":"horse","year":2011,"amount":1.0,"ns3":"iphone"},
{"day":30,"ns1":"sale","month":1,"ns2":"camel","year":2011,"amount":0.0,"ns3":"iphone"},
{"day":31,"ns1":"sale","month":1,"ns2":"camel","year":2011,"amount":0.0,"ns3":"iphone"}]

Get data sums for month and year

curl 'http://localhost:3000/statistics?ns1=comment&ns2=horse&ns3=iphone&date_since=2011-01-30&date_till=2011-01-31&granularity=month'

[{"day":null,"ns1":"comment","month":1,"ns2":"horse","year":2011,"amount":1.0,"ns3":"iphone"}]

curl 'http://localhost:3000/statistics?ns1=comment&ns2=horse&ns3=iphone&date_since=2011-01-30&date_till=2011-01-31&granularity=year'

[{"day":null,"ns1":"comment","month":null,"ns2":"horse","year":2011,"amount":1.0,"ns3":"iphone"}]

Get sums of all ns3 by not using the param

curl 'http://localhost:3000/statistics?ns1=comment&ns2=horse&date_since=2011-01-30&date_till=2011-01-31&granularity=year'

[{"day":null,"ns1":"comment","month":null,"ns2":"horse","year":2011,"amount":1.0,"ns3":null}]

abacus's People

Contributors

hpdev avatar

Stargazers

 avatar Xi avatar Gustavo Barron avatar

Watchers

Martin Tepper avatar Frank Brandewiede avatar James Cloos avatar

Forkers

hpdev dkaxm

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.