Skip to content

Commit

Permalink
Merge pull request #48 from HookedMediaGroup/data_cleanup
Browse files Browse the repository at this point in the history
re: Better Manage the Stats Set
  • Loading branch information
steelThread committed May 22, 2013
2 parents ee50a59 + 3fe65e3 commit a4cc10f
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $ redmon -h
Usage: /Users/sean/codez/steelThread/redmon/vendor/ruby/1.9.1/bin/redmon (options)
-a, --address ADDRESS The thin bind address for the app (default: 0.0.0.0)
-n, --namespace NAMESPACE The root Redis namespace (default: redmon)
-l, --lifespan MINUTES Lifespan(in minutes) for polled data (default: 30)
-i, --interval SECS Poll interval in secs for the worker (default: 10)
-p, --port PORT The thin bind port for the app (default: 4567)
-r, --redis URL The Redis url for monitor (default: redis://127.0.0.1:6379)
Expand Down
7 changes: 7 additions & 0 deletions bin/redmon
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class RedmonCLI
:description => 'Poll interval in secs for the worker (default: 10)',
:proc => to_i

option :data_lifespan,
:short => '-l MINUTES',
:long => '--lifespan MINUTES',
:default => 30,
:description => 'Lifespan(in minutes) for polled data (default: 30)',
:proc => to_i

option :app,
:on => :tail,
:long => '--no-app',
Expand Down
1 change: 1 addition & 0 deletions lib/redmon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def run(opts={})
rescue Exception => e
unless e.is_a?(SystemExit)
log "!!! Redmon has shit the bed, restarting... #{e.message}"
e.backtrace.each { |line| log line }
sleep(1)
run(opts)
end
Expand Down
1 change: 1 addition & 0 deletions lib/redmon/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config
:worker => true,
:web_interface => ['0.0.0.0', 4567],
:poll_interval => 10,
:data_lifespan => 30,
:secure => false
}

Expand Down
4 changes: 4 additions & 0 deletions lib/redmon/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def poll_interval
Redmon.config.poll_interval * 1000
end

def num_samples_to_request
(Redmon.config.data_lifespan * 60) / Redmon.config.poll_interval
end

def count
-(params[:count] ? params[:count].to_i : 1)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/redmon/public/redmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ var Redmon = (function() {
, events = $({});

/**
* Loads the last 100 events and starts the periodic polling for new events.
* Loads the last numSamples events and starts the periodic polling for new events.
*/
function init(opts) {
config = opts;
toolbar.init();
cli.init();
requestData(100, function(data) {
requestData(config.numSamples, function(data) {
renderDashboard(data);
poll();
});
Expand Down
1 change: 1 addition & 0 deletions lib/redmon/views/app.haml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
$(document).ready(function() {
Redmon.init({
pollInterval : #{poll_interval},
numSamples : #{num_samples_to_request},
cliPrompt : '#{prompt}',
absoluteUrl : '#{absolute_url}'
});
Expand Down
20 changes: 19 additions & 1 deletion lib/redmon/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ class Worker
include Redmon::Redis

def run!
EM::PeriodicTimer.new(interval) {record_stats}
EM::PeriodicTimer.new(interval) {
record_stats
cleanup_old_stats
}
end

def record_stats
redis.zadd stats_key, *stats
end

def cleanup_old_stats
# When indexing from the end of a sorted set, we start at -1, so we need to add 1 here or we'll be keeping one
# fewer samples than expected
redis.zremrangebyscore stats_key, '-inf', '(' + oldest_data_to_keep.to_s
end

def stats
stats = redis.info.merge! \
:dbsize => redis.dbsize,
Expand Down Expand Up @@ -37,5 +46,14 @@ def interval
Redmon.config.poll_interval
end

def data_lifespan
Redmon.config.data_lifespan
end

def oldest_data_to_keep
lifespan_seconds = data_lifespan * 60
oldest_time_to_keep = Time.now - lifespan_seconds
oldest_time_to_keep.to_i * 1000
end
end
end
12 changes: 12 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'spec_helper'
require 'redmon/helpers'

include Redmon::Helpers

describe "Helpers" do

Expand Down Expand Up @@ -83,4 +86,13 @@ def em_redis
end
end

describe "#num_samples_to_request" do
it "should return the number of samples to request based on poll interval and data lifespan" do
Redmon.config.stub(:data_lifespan).and_return(31)
Redmon.config.stub(:poll_interval).and_return(10)

num_samples_to_request.should == (31 * 60) / 10
end
end

end
25 changes: 24 additions & 1 deletion spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "worker" do

before(:all) do
before(:each) do
@worker = Redmon::Worker.new
end

Expand All @@ -24,6 +24,14 @@ def mock_timer
end
end

describe "#cleanup_old_stats" do
it "should remove old stats entries from a redis sorted set" do
redis = mock_redis
redis.should_receive(:zremrangebyscore).with(Redmon::Redis.stats_key, '-inf', '(' + @worker.oldest_data_to_keep.to_s)
@worker.cleanup_old_stats
end
end

describe "#stats" do
it "should fetch info, dbsize and slowlog from redis" do
pending
Expand Down Expand Up @@ -59,4 +67,19 @@ def mock_timer
end
end

describe "#data_lifespan" do
it "should return the data lifspan" do
@worker.data_lifespan.should == Redmon.config.data_lifespan
end
end

describe "#oldest_data_to_keep" do
it "should return the oldest data timestamp that should be kept" do
Time.stub(:now).and_return(Time.at(1366044862))
@worker.stub(:data_lifespan).and_return(30)

@worker.oldest_data_to_keep.should == 1366043062000
end
end

end

0 comments on commit a4cc10f

Please sign in to comment.