merry_go_round 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  You'll need to add the migrations to your app. Simply run:
30
30
 
31
- $ merry install
31
+ $ rails generate merry_go_round:install
32
32
 
33
33
  Merry Go Round will automatically detect most Redis servers. If you need to configure it, add an initializer with:
34
34
 
@@ -42,11 +42,11 @@ end
42
42
 
43
43
  To run the aggregator, run:
44
44
 
45
- $ merry go
45
+ $ rake merry_go_round:aggregate
46
46
 
47
47
  By default, it will aggregate everything at 1 minute, 1 hour, 1 day, 1 week, 1 month, and 1 year. All you need to do is run the command and it will automatically aggregate for each time period. If that time period is not complete, it will not aggregate them (i.e. if you have been running Merry Go Round for 5 days, it won't generate week and higher aggregations).
48
48
 
49
- It is recommened to run the aggregator via cron (or [Heroku Scheduler](https://devcenter.heroku.com/articles/scheduler)) every few minutes. Since it uses ActiveRecord to store the aggregations, it will have to load your environment.
49
+ It is recommened to run the aggregator via cron (or [Heroku Scheduler](https://devcenter.heroku.com/articles/scheduler)) every 5 minutes. Since it uses ActiveRecord to store the aggregations, it will have to load your environment.
50
50
 
51
51
  ## Contributing
52
52
 
@@ -4,13 +4,19 @@ module MerryGoRound
4
4
  # Aggregation is the ActiveRecord store for aggregated data points. You should
5
5
  # never create aggregations directly unless you know what you're doing.
6
6
  class Aggregation < ActiveRecord::Base
7
+ GRANULARITIES = [:minute, :hour, :day, :week, :year]
8
+
7
9
  attr_accessible :key, :value, :timestamp, :granularity, :parent_id
8
10
 
9
- belongs_to :aggregation, foreign_key: :parent_id
10
- has_many :aggregations, foreign_key: :parent_id
11
+ belongs_to :parent, class_name: 'MerryGoRound::Aggregation'
12
+ has_many :children, class_name: 'MerryGoRound::Aggregation', foreign_key: :parent_id
13
+
14
+ def self.table_name
15
+ 'merry_go_round_aggregations'
16
+ end
11
17
 
12
- def self.values_for_key(key, granularity = 60)
13
- where('key = ? AND granularity = ?', granularity)
18
+ def self.for_key(key, granularity = GRANULARITIES.first)
19
+ where('key = ? AND granularity = ?', key, granularity)
14
20
  end
15
21
  end
16
22
  end
@@ -1,10 +1,8 @@
1
1
  module MerryGoRound
2
2
  class Aggregator
3
- GRANULARITIES = [:minute, :hour, :day, :week, :year] # :month, :quarter
4
-
5
3
  def aggregate!
6
4
  from_redis
7
- existing_slivers
5
+ # existing_slivers
8
6
  end
9
7
 
10
8
  private
@@ -16,7 +14,7 @@ module MerryGoRound
16
14
  # Aggregate stuff since the last aggregation in Redis
17
15
  def from_redis
18
16
  # Aggregate to the first granularity
19
- redis_granularity = GRANULARITIES.first
17
+ redis_granularity = Aggregation::GRANULARITIES.first
20
18
 
21
19
  # Get entry keys from Redis
22
20
  redis_keys = redis.keys 'entry-*'
@@ -26,19 +24,18 @@ module MerryGoRound
26
24
  # Extract the timestamp
27
25
  timestamp = Time.utc(redis_key.sub('entry-', '').to_i)
28
26
 
29
- # Make sure this timestamp hasn't already been aggregated
30
- redis.multi do
31
- # Get the hash of Merry Go Round keys and values
32
- hash = redis.hgetall redis_key
27
+ # TODO: Make sure this timestamp hasn't already been aggregated
33
28
 
34
- # Loop through each key/value in the hash
35
- hash.each do |key, value|
36
- Sliver.create(key: key, value: value, timestamp: timestamp, granularity: redis_granularity)
37
- end
29
+ # Get the hash of Merry Go Round keys and values
30
+ hash = redis.hgetall redis_key
38
31
 
39
- # Delete the key
40
- redis.del redis_key
32
+ # Loop through each key/value in the hash
33
+ hash.each do |key, value|
34
+ Aggregation.create(key: key, value: value, timestamp: timestamp, granularity: redis_granularity)
41
35
  end
36
+
37
+ # Delete the key
38
+ redis.del redis_key
42
39
  end
43
40
  end
44
41
 
@@ -1,3 +1,3 @@
1
1
  module MerryGoRound
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -17,6 +17,7 @@ module MerryGoRound
17
17
 
18
18
  def copy_migrations
19
19
  migration_template 'add_aggregations.rb', 'db/migrate/add_merry_go_round_aggregations.rb'
20
+ copy_file 'merry_go_round.rake', 'lib/tasks/merry_go_round.rake'
20
21
  end
21
22
  end
22
23
  end
@@ -0,0 +1,6 @@
1
+ namespace :merry_go_round do
2
+ desc 'Run the aggregations'
3
+ task aggregate: :environment do
4
+ MerryGoRound.aggregate!
5
+ end
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merry_go_round
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -94,13 +94,13 @@ files:
94
94
  - lib/merry_go_round.rb
95
95
  - lib/merry_go_round/aggregation.rb
96
96
  - lib/merry_go_round/aggregator.rb
97
- - lib/merry_go_round/cli.rb
98
97
  - lib/merry_go_round/entry.rb
99
98
  - lib/merry_go_round/railtie.rb
100
99
  - lib/merry_go_round/version.rb
101
100
  - lib/rails/generators/merry_go_round/install/USAGE
102
101
  - lib/rails/generators/merry_go_round/install/install_generator.rb
103
102
  - lib/rails/generators/merry_go_round/install/templates/add_aggregations.rb
103
+ - lib/rails/generators/merry_go_round/install/templates/merry_go_round.rake
104
104
  - merry_go_round.gemspec
105
105
  - test/merry_go_round_test.rb
106
106
  - test/test_helper.rb
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: 3503042711735793145
128
+ hash: 1524497332131136507
129
129
  requirements: []
130
130
  rubyforge_project:
131
131
  rubygems_version: 1.8.23
@@ -1,17 +0,0 @@
1
- require 'thor'
2
- require 'merry_go_round'
3
-
4
- module MerryGoRound
5
- class Cli < Thor
6
- desc 'go', 'Aggregate collected data in Redis into ActiveRecord Slivers.'
7
- def go
8
- MerryGoRound.aggregate!
9
- end
10
-
11
- desc 'install', 'Install the Merry Go Round migration'
12
- def install
13
- system 'rails generate merry_go_round:install'
14
- puts "\n\nThe migratation has been installed. Don't forget to run `rake db:migrate`.\n"
15
- end
16
- end
17
- end