Created
June 10, 2011 19:31
-
-
Save timlinquist/1019588 to your computer and use it in GitHub Desktop.
SAVE THE WORLD REMIX!!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :gts do | |
desc "get source of lead for client" | |
task(:source_for_client_lead => :environment) do | |
start_date = ENV['start_date'] || Date.parse('2011-01-01') | |
client_id = ENV['client_id'] || 1003 | |
lead_types = LeadType.all(:select=>'id, name') | |
lead_types_by_id = {} | |
lead_types.each{|lead| lead_types_by_id[lead.id] = lead_types_by_id.name } | |
leads = Lead.all(:select=>'DISTINCT(session_id), lead_type_id', :conditions=>["client_id=? AND session_id IS NOT NULL AND DATE(created_at) >= ?"], client_id, start_date) | |
source = {} | |
leads.each do |session| | |
first_source = G5PageView::PageView.collection.find({:session_id => session}, :fields => ["source"]).sort([['created_at', -1]]).first | |
leads_for_source = source.fetch(first_source, 0) | |
leads_for_source += 1 | |
source[first_source] = leads_for_source | |
end | |
puts "Source for client_id #{client_id}, session #{session}: #{source}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment