Skip to content

Instantly share code, notes, and snippets.

@manojbecs
Last active August 29, 2015 13:58
Show Gist options
  • Save manojbecs/10017055 to your computer and use it in GitHub Desktop.
Save manojbecs/10017055 to your computer and use it in GitHub Desktop.
# All devices with at least one heartbeat
mp_with_last_seen = {}
MediaPlayer.find_in_batches(:batch_size => 100) do |mps|
mps.each do |mp|
hb = MediaPlayerHeartbeat.where(:client_id => mp.client_id).last
mp_with_last_seen[mp.name] = hb.created_at unless hb.blank?
end
end
CSV.open("data.csv", "wb") {|csv| mp_with_last_seen.to_a.each {|elem| csv << elem} }
# Last 1 hour
mp_with_last_seen = {}
MediaPlayer.find_in_batches(:batch_size => 100) do |mps|
mps.each do |mp|
if mp.up?
hb = MediaPlayerHeartbeat.where(:client_id => mp.client_id).last
mp_with_last_seen[mp.name] = hb.created_at unless hb.blank?
end
end
end
CSV.open("data.csv", "wb") {|csv| mp_with_last_seen.to_a.each {|elem| csv << elem} }
@contextmedia
Copy link

irb(main):010:0> [mp.name] = hb.created_at unless hb.blank?
SyntaxError: (irb):10: syntax error, unexpected '=', expecting $end
[mp.name] = hb.created_at unless hb.blank?
           ^
    from /home/cont04admin/www/messaging/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /home/cont04admin/www/messaging/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /home/cont04admin/www/messaging/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment