Skip to content

Instantly share code, notes, and snippets.

@plotti
Created June 22, 2012 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plotti/2972028 to your computer and use it in GitHub Desktop.
Save plotti/2972028 to your computer and use it in GitHub Desktop.
delayed Jobs
# A version that used delayed jobs to split the work among a lot of workers
# Deprecated because the bottleneck was querying the DB
# Same as find all valued connections only trying to make it faster
def find_delayed_at_connections(friend = true, follower = false, category = false)
usernames = persons.collect{|p| p.username}
persons.each do |person|
Delayed::Job.enqueue(AggregateAtConnectionsJob.new(person.id,self.id,usernames))
end
wait_for_jobs("AggregateAtConnectionsJob")
self.return_delayed_at_connections
end
def return_delayed_at_connections
values = []
persons.each do |person|
filename = "#{RAILS_ROOT}/analysis/data/tmp/person_#{person.id}_project_#{self.id}_AT.edges"
puts "Working on #{filename}"
values += FasterCSV.read(filename)
end
#Merge counted pairs
hash = values.group_by { |first, second, third| [first,second] }
return hash.map{|k,v| [k,v.count].flatten}
end
# A version that used delayed jobs to split the work among a lot of workers
# Deprecated because the bottleneck was querying the DB
def self.find_delayed_at_connections_for_person_and_project(person_id,project_id,usernames)
person = Person.find(person_id)
project = Project.find(project_id)
filename = "#{RAILS_ROOT}/analysis/data/tmp/person_#{person.id}_project_#{project.id}_AT.edges"
if File.exists? filename
puts "Skipping person #{person.username}"
else
outfile = File.open(filename, "w+")
person.feed_entries.each do |tweet|
usernames.each do |tmp_user|
if tmp_user != person.username && tweet.retweet_ids == [] && tweet.text.include?("@#{tmp_user} ") && !tweet.text.include?("RT")
puts tweet.text
outfile.puts "#{person.username},#{tmp_user},#{1},#{tweet.id}"
end
end
end
outfile.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment