Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 45 lines (34 sloc) 1.678 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#!/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.