Permalink
Browse files

Count status by hour of day

  • Loading branch information...
gregmoreno committed Sep 5, 2012
1 parent ccc3c0b commit 93d18d206c5ff0960c62d03cd1e2d3333a45e2fc
Showing with 34 additions and 1 deletion.
  1. +2 −1 counter.rb
  2. +32 −0 status_counter.rb
@@ -6,5 +6,6 @@
settings = YAML.load_file File.dirname(__FILE__) + '/mongo.yml'
miner = TweetMiner.new(settings)
results = miner.status_count_by_user
#results = miner.status_count_by_user
results = miner.status_count_by_hday
ap results
@@ -30,6 +30,38 @@ def status_count_by_user
statuses.map_reduce(counter.map_command, counter.reduce_command, default_mr_options)
end
class HourOfDayCounter
def map_command
'function() {
var re = /(\d{2,2}):\d{2,2}:\d{2,2}/;
var hour = re.exec(this.created_at)[1];
emit(hour, 1);
}'
end
def reduce_command
<<-EOS
function(key, values) {
var count = 0;
for(i in values) {
count += values[i]
}
return count;
}
EOS
end
end
def status_count_by_hday
counter = HourOfDayCounter.new
statuses.map_reduce(counter.map_command, counter.reduce_command, default_mr_options)
end
def default_mr_options
{:out => {:inline => 1}, :raw => true }
end

0 comments on commit 93d18d2

Please sign in to comment.