Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
executable file 45 lines (34 sloc) 1.64 KB
#!/usr/bin/env ruby
require File.join(File.dirname(__FILE__), '..', 'config', 'environment')
require 'net/imap'
require 'net/http'
require 'rubygems'
require 'logger'
log = Logger.new(STDOUT)
log.info Time.now.to_s + " Warming up to process the clickstream..."
last_clickstream = Clickstream.last_unprocessed.last
clickstreams = Clickstream.unprocessed.all
clickstreams.each do |c|
log.info " Processing #{c.clickstreamable_type.to_s} with id #{c.clickstreamable_id.to_s}..."
unless c.clickstreamable_type.to_s =~ /(?:Home|Session)/i
object = c.clickstreamable
if object
object.viewed = object.viewed ? (object.viewed.to_i + c.cnt.to_i) : c.cnt.to_i
object.save
end
end
end
# clean up processed clickstreams ...
if last_clickstream
log.info " Cleaning the clickstream table from processed entries below id #{last_clickstream.id}..."
Clickstream.update_all('status=1', "clickstreamable_id is not null and clickstreamable_type is not null and status=0 and id<=#{last_clickstream.id}")
sql = "INSERT INTO clickstream_archives (SELECT * from clickstreams where status = 1 and id<=#{last_clickstream.id})"
ActiveRecord::Base.connection.execute(sql)
Clickstream.delete_all("status=1 and id<=#{last_clickstream.id}")
end
# clean up the rest of the table...
log.info " Cleaning table from invalid data..."
sql = "INSERT INTO clickstream_archives (SELECT * from clickstreams where clickstreamable_id is null and clickstreamable_type is null)"
ActiveRecord::Base.connection.execute(sql)
Clickstream.delete_all("clickstreamable_id is null and clickstreamable_type is null")
log.info Time.now.to_s + " Done and going to sleep..."
Something went wrong with that request. Please try again.