bucketeer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec', ">= 2.7"
7
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'bucketeer'
4
+ require "bucketeer/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bucketeer"
8
+ s.version = Bucketeer::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Alex Sharp"]
11
+ s.email = ["ajsharp@gmail.com"]
12
+ s.homepage = "https://github.com/ajsharp/bucketeer"
13
+ s.summary = %q{Utility to assist with bucketing data}
14
+ s.description = %q{Utility to assist with bucketing data}
15
+
16
+ s.rubyforge_project = "mongo_utils"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,23 @@
1
+ module Bucketeer
2
+
3
+ # @param [Hash] data a hash of data, where they keys are Time objects
4
+ # {"timestamp": "value"}
5
+ # @param [Integer, Symbol] interval the interval by which data is bucketed
6
+ # If an integer, interval if the number of seconds.
7
+ # If a symbol, interval must be one of :day, :month, :week, :annual
8
+ # @return [Hash] the bucketed hash of data
9
+ def self.bucket(set, interval)
10
+ bucketed = Hash.new { |h, k| h[k] = 0 }
11
+
12
+ set.each_pair do |time, value|
13
+ milliseconds_since_epoch = time.to_i * 1000
14
+ interval_in_milliseconds = interval * 1000
15
+ time_since_last_interval = milliseconds_since_epoch % interval_in_milliseconds
16
+ time_until_next_interval = interval_in_milliseconds - time_since_last_interval
17
+
18
+ end_of_interval = Time.at((milliseconds_since_epoch + time_until_next_interval) / 1000)
19
+ bucketed[end_of_interval] += value
20
+ end
21
+ bucketed
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Bucketeer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bucketeer, '.bucket' do
4
+ context "1 minute intervals" do
5
+ let(:set) do
6
+ { Time.new(2011, 11, 22, 1, 1) => 1,
7
+ Time.new(2011, 11, 22, 1, 1, 59) => 1,
8
+ Time.new(2011, 11, 22, 1, 2) => 1
9
+ }
10
+ end
11
+
12
+ subject { Bucketeer.bucket(set, 60) }
13
+
14
+ it {
15
+ should == {
16
+ Time.new(2011, 11, 22, 1, 2) => 2,
17
+ Time.new(2011, 11, 22, 1, 3) => 1
18
+ }
19
+ }
20
+ end
21
+
22
+ context "5 minute intervals" do
23
+ let(:set) do
24
+ { Time.new(2011, 11, 22, 1, 1) => 1,
25
+ Time.new(2011, 11, 22, 1, 1, 59) => 1,
26
+ Time.new(2011, 11, 22, 1, 2) => 1,
27
+ Time.new(2011, 11, 22, 1, 2, 30) => 1,
28
+ Time.new(2011, 11, 22, 1, 4, 59) => 1,
29
+ Time.new(2011, 11, 22, 1, 5, 1) => 1
30
+ }
31
+ end
32
+
33
+ subject { Bucketeer.bucket(set, 300) }
34
+
35
+ it {
36
+ should == {
37
+ Time.new(2011, 11, 22, 1, 5) => 5,
38
+ Time.new(2011, 11, 22, 1, 10) => 1
39
+ }
40
+ }
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ $:.unshift(File.dirname(File.expand_path(__FILE__)))
5
+
6
+ require File.join(File.dirname(File.expand_path(__FILE__)), "..", "lib", "bucketeer")
7
+
8
+ RSpec.configure do |c|
9
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bucketeer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Sharp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-24 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Utility to assist with bucketing data
15
+ email:
16
+ - ajsharp@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .rspec
22
+ - Gemfile
23
+ - bucketeer.gemspec
24
+ - lib/bucketeer.rb
25
+ - lib/bucketeer/version.rb
26
+ - spec/bucketeer_spec.rb
27
+ - spec/spec_helper.rb
28
+ homepage: https://github.com/ajsharp/bucketeer
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project: mongo_utils
48
+ rubygems_version: 1.8.6
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Utility to assist with bucketing data
52
+ test_files:
53
+ - spec/bucketeer_spec.rb
54
+ - spec/spec_helper.rb