<commit_before>require 'resolv'

class DomainLogVisitor < DomainModel
   belongs_to :end_user
 
   before_create :generate_hash

   has_many :domain_log_sessions, :order => 'domain_log_sessions.created_at DESC'
   has_many :experiment_users

   def last_session
     self.domain_log_sessions[0]
   end


   def ip_address_name
     Resolv.new.getname(self.ip_address)
   rescue Exception => e
     "No DNS Lookup"
   end

   def generate_hash
      self.visitor_hash = DomainModel.generate_hash 
   end

   def session_details(session_limit = 10)
     self.domain_log_sessions.find(:all,:include => :domain_log_entries,:limit => 10)
   end

   def has_location?
     self.country && self.country != 'UN'
   end

   def location_display
     [ city, region, country ].reject(&:blank?).join(", ")
   end


   def self.log_visitor(cookies,user,session,request)
      
      if cookies[:v] && !session[:domain_log_visitor]
        dlv = DomainLogVisitor.find_by_visitor_hash(cookies[:v])
        if dlv 
          if user.id && dlv.end_user_id != user.id
            if dlv.end_user_id.blank?
              dlv.end_user_id = user.id
            else
              cookies[:v] = nil
            end
          end

          if cookies[:v]
            dlv.update_attributes(:updated_at => Time.now)
            session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id  }
          end
        else
          cookies[:v] = nil
        end
      elsif cookies[:v] && session[:domain_log_visitor].is_a?(Integer)
        div = DomainLogVisitor.find_by_id session[:domain_log_visitor]
        session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id } if div
      end

      if  !cookies[:v]
        dlv = DomainLogVisitor.create(:ip_address => request.remote_ip, :end_user_id => user.id)
        cookies[:v] = {:value => dlv.visitor_hash, :expires => 20.years.from_now.utc }
        session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id }
      end

      DomainLogVisitor.log_user(cookies,session,user)

      # Need to capture the country and the location
      session[:domain_log_visitor][:loc] ? false : true
   end

   def self.log_user(cookies,session,user)
     if cookies[:v] && user.id && session[:domain_log_visitor][:id] && session[:domain_log_visitor][:end_user_id].blank?
       dlv =  DomainLogVisitor.find_by_visitor_hash(cookies[:v])
       dlv.update_attribute(:end_user_id,user.id) if dlv
     end
   end

   def self.log_location(cookies,session,location = {})
     if cookies[:v] && session[:domain_log_visitor][:id] && session[:domain_log_visitor][:loc].blank?
       dlv =  DomainLogVisitor.find_by_visitor_hash(cookies[:v])
       if dlv
         location[:country] = 'UN' if location[:country].blank?
         dlv.update_attributes(location.slice(:latitude,:longitude,:country,:region,:city))
         session[:domain_log_visitor][:loc] = location[:country]
       end
     end
   end
end<commit_msg>Fix for missing session entry race condition<commit_after>require 'resolv'

class DomainLogVisitor < DomainModel
   belongs_to :end_user
 
   before_create :generate_hash

   has_many :domain_log_sessions, :order => 'domain_log_sessions.created_at DESC'
   has_many :experiment_users

   def last_session
     self.domain_log_sessions[0]
   end


   def ip_address_name
     Resolv.new.getname(self.ip_address)
   rescue Exception => e
     "No DNS Lookup"
   end

   def generate_hash
      self.visitor_hash = DomainModel.generate_hash 
   end

   def session_details(session_limit = 10)
     self.domain_log_sessions.find(:all,:include => :domain_log_entries,:limit => 10)
   end

   def has_location?
     self.country && self.country != 'UN'
   end

   def location_display
     [ city, region, country ].reject(&:blank?).join(", ")
   end


   def self.log_visitor(cookies,user,session,request)
      
      if cookies[:v] && !session[:domain_log_visitor]
        dlv = DomainLogVisitor.find_by_visitor_hash(cookies[:v])
        if dlv 
          if user.id && dlv.end_user_id != user.id
            if dlv.end_user_id.blank?
              dlv.end_user_id = user.id
            else
              cookies[:v] = nil
            end
          end

          if cookies[:v]
            dlv.update_attributes(:updated_at => Time.now)
            session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id  }
          end
        else
          cookies[:v] = nil
        end
      elsif cookies[:v] && session[:domain_log_visitor].is_a?(Integer)
        div = DomainLogVisitor.find_by_id session[:domain_log_visitor]
        session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id } if div
      end

      if  !cookies[:v]
        dlv = DomainLogVisitor.create(:ip_address => request.remote_ip, :end_user_id => user.id)
        cookies[:v] = {:value => dlv.visitor_hash, :expires => 20.years.from_now.utc }
        session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country, :end_user_id => user.id }
      end

      DomainLogVisitor.log_user(cookies,session,user)

      # Need to capture the country and the location
      session[:domain_log_visitor][:loc] ? false : true
   end

   def self.log_user(cookies,session,user)
     if cookies[:v] && user.id && session[:domain_log_visitor][:id] && session[:domain_log_visitor][:end_user_id].blank?
       dlv =  DomainLogVisitor.find_by_visitor_hash(cookies[:v])
       dlv.update_attribute(:end_user_id,user.id) if dlv
     end
   end

   def self.log_location(cookies,session,location = {})
     if cookies[:v] && session[:domain_log_visitor] && session[:domain_log_visitor][:id] && session[:domain_log_visitor][:loc].blank?
       dlv =  DomainLogVisitor.find_by_visitor_hash(cookies[:v])
       if dlv
         location[:country] = 'UN' if location[:country].blank?
         dlv.update_attributes(location.slice(:latitude,:longitude,:country,:region,:city))
         session[:domain_log_visitor][:loc] = location[:country]
       end
     end
   end
end
{'fraction_non_alphanumeric': 0.12048389733529508, 'fraction_numerical': 0.0022887036128821318, 'mean_word_length': 2.959870550161812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6623671', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2040, 'n_words': 360}