# Log Model - Tracks and record user interactions with various features
# This will make it easy to write an app on top on the MOle to track a 
# particular application utilization.                     
class MoleLog < ActiveRecord::Base                                             
  belongs_to :mole_feature  
    
  class << self                                     
    # logs unchecked exception           
    def check_it( context, user_id, args )     
      if args[:boom]
        args[:trace] = dump_stack( args[:boom] )
        args[:boom]  = args[:boom].to_s
      end
      log_it( context, MoleFeature::find_exception_feature( ::Mole.application ), user_id, args ) if ::Mole::moleable?
    end             
                                       
    # logs perf occurence
    def perf_it( context, user_id, args )
      log_it( context, MoleFeature::find_performance_feature( ::Mole.application ), user_id, args ) if ::Mole::moleable?
    end
          
    # persists mole information to the database. 
    # This call will store information on the mole dedicated tables namely 
    # mole_features and mole_logs.                                  
    def mole_it(context, feature, user_id, args)   
      log_it( context, MoleFeature::find_feature( feature, ::Mole.application, context.class.name ), user_id, args ) if ::Mole::moleable?
    end               
    
    # mole the bastard - create db entry into mole logs
    def log_it( context, feature, user_id, args )
      args    ||= "no args"                           
      user_id ||= "N/A"             
      ip_addr, browser_type = log_details( context )      
      MoleLog.create( :mole_feature => feature, 
                      :user_id      => user_id, 
                      :host_name    => `hostname`,
                      :params       => args.to_yaml,
                      :ip_address   => ip_addr, 
                      :browser_type => browser_type )
    end
                   
    # dumps partial stack
    def dump_stack( boom )      
      buff = boom.backtrace[0...3].join( "-" )
    end

    # extract orginating ip address and browser type                                               
    def log_details( context ) #:nodoc:
      ip_addr, browser_type = nil
      if context.respond_to? :request                                                                     
        ip_addr, browser_type = context.request.env['REMOTE_ADDR'], context.request.env['HTTP_USER_AGENT'] 
      end
      return ip_addr, browser_type
    end        
  end
end