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.
- data/Readme.markdown +3 -3
- data/lib/merry_go_round/aggregation.rb +10 -4
- data/lib/merry_go_round/aggregator.rb +11 -14
- data/lib/merry_go_round/version.rb +1 -1
- data/lib/rails/generators/merry_go_round/install/install_generator.rb +1 -0
- data/lib/rails/generators/merry_go_round/install/templates/merry_go_round.rake +6 -0
- metadata +3 -3
- data/lib/merry_go_round/cli.rb +0 -17
data/Readme.markdown
CHANGED
@@ -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
|
-
$
|
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
|
-
$
|
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
|
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 :
|
10
|
-
has_many :
|
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.
|
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
|
-
|
35
|
-
|
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
|
-
|
40
|
-
|
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
|
|
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.
|
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:
|
128
|
+
hash: 1524497332131136507
|
129
129
|
requirements: []
|
130
130
|
rubyforge_project:
|
131
131
|
rubygems_version: 1.8.23
|
data/lib/merry_go_round/cli.rb
DELETED
@@ -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
|