<commit_before>require 'rubygems'
require 'socket'

require 'opbeat/error'
require 'opbeat/linecache'

module Opbeat

  class Event

    LOG_LEVELS = {
      "debug" => "debug",
      "info" => "info",
      "warn" => "warn",
      "warning" => "warn",
      "error" => "error",
    }

    BACKTRACE_RE = /^(.+?):(\d+)(?::in `(.+?)')?$/

    attr_reader :id
    attr_accessor :organization, :app, :message, :timestamp, :level
    attr_accessor :logger, :culprit, :hostname, :modules, :extra, :user
    attr_accessor :environment

    def initialize(options={}, configuration=nil, &block)
      @configuration = configuration || Opbeat.configuration
      @interfaces = {}

      @id = options[:id]
      @message = options[:message]
      @timestamp = options[:timestamp] || Time.now.utc
      @level = options[:level] || :error
      @logger = options[:logger] || 'root'
      @culprit = options[:culprit]
      @environment = @configuration[:current_environment]
      @extra = options[:extra]
      @user = options[:user]

      # Try to resolve the hostname to an FQDN, but fall back to whatever the load name is
      hostname = Socket.gethostname
      hostname = Socket.gethostbyname(hostname).first rescue hostname
      @hostname = options[:hostname] || hostname

      block.call(self) if block

      # Some type coercion
      @timestamp = @timestamp.strftime('%Y-%m-%dT%H:%M:%S') if @timestamp.is_a?(Time)
      @level = LOG_LEVELS[@level.to_s.downcase] if @level.is_a?(String) || @level.is_a?(Symbol)

      # Basic sanity checking
      raise Error.new('A message is required for all events') unless @message && !@message.empty?
      raise Error.new('A timestamp is required for all events') unless @timestamp
    end

    def interface(name, value=nil, &block)
      int = Opbeat::find_interface(name)
      Opbeat.logger.info "Unknown interface: #{name}" unless int
      raise Error.new("Unknown interface: #{name}") unless int
      @interfaces[int.name] = int.new(value, &block) if value || block
      @interfaces[int.name]
    end

    def [](key)
      interface(key)
    end

    def []=(key, value)
      interface(key, value)
    end

    def to_hash
      data = {
        'message' => self.message,
        'timestamp' => self.timestamp,
        'level' => self.level,
        'logger' => self.logger,
      }
      data['client_supplied_id'] = self.id if self.id
      data['culprit'] = self.culprit if self.culprit
      data['machine'] = {'hostname' => self.hostname } if self.hostname
      data['environment'] = self.environment if self.environment
      data['extra'] = self.extra if self.extra
      if self.user
        data['user'] = self.user
        if self.user[:id] or self.user[:email] or self.user[:username]
          data['user'][:is_authenticated] = true
        end<commit_msg>Explicitly set user.is_authenticated to false if so<commit_after>require 'rubygems'
require 'socket'

require 'opbeat/error'
require 'opbeat/linecache'

module Opbeat

  class Event

    LOG_LEVELS = {
      "debug" => "debug",
      "info" => "info",
      "warn" => "warn",
      "warning" => "warn",
      "error" => "error",
    }

    BACKTRACE_RE = /^(.+?):(\d+)(?::in `(.+?)')?$/

    attr_reader :id
    attr_accessor :organization, :app, :message, :timestamp, :level
    attr_accessor :logger, :culprit, :hostname, :modules, :extra, :user
    attr_accessor :environment

    def initialize(options={}, configuration=nil, &block)
      @configuration = configuration || Opbeat.configuration
      @interfaces = {}

      @id = options[:id]
      @message = options[:message]
      @timestamp = options[:timestamp] || Time.now.utc
      @level = options[:level] || :error
      @logger = options[:logger] || 'root'
      @culprit = options[:culprit]
      @environment = @configuration[:current_environment]
      @extra = options[:extra]
      @user = options[:user]

      # Try to resolve the hostname to an FQDN, but fall back to whatever the load name is
      hostname = Socket.gethostname
      hostname = Socket.gethostbyname(hostname).first rescue hostname
      @hostname = options[:hostname] || hostname

      block.call(self) if block

      # Some type coercion
      @timestamp = @timestamp.strftime('%Y-%m-%dT%H:%M:%S') if @timestamp.is_a?(Time)
      @level = LOG_LEVELS[@level.to_s.downcase] if @level.is_a?(String) || @level.is_a?(Symbol)

      # Basic sanity checking
      raise Error.new('A message is required for all events') unless @message && !@message.empty?
      raise Error.new('A timestamp is required for all events') unless @timestamp
    end

    def interface(name, value=nil, &block)
      int = Opbeat::find_interface(name)
      Opbeat.logger.info "Unknown interface: #{name}" unless int
      raise Error.new("Unknown interface: #{name}") unless int
      @interfaces[int.name] = int.new(value, &block) if value || block
      @interfaces[int.name]
    end

    def [](key)
      interface(key)
    end

    def []=(key, value)
      interface(key, value)
    end

    def to_hash
      data = {
        'message' => self.message,
        'timestamp' => self.timestamp,
        'level' => self.level,
        'logger' => self.logger,
      }
      data['client_supplied_id'] = self.id if self.id
      data['culprit'] = self.culprit if self.culprit
      data['machine'] = {'hostname' => self.hostname } if self.hostname
      data['environment'] = self.environment if self.environment
      data['extra'] = self.extra if self.extra
      if self.user
        data['user'] = self.user
        if self.user[:id] or self.user[:email] or self.user[:username]
          data['user'][:is_authenticated] = true
        end
        data['user'][:is_authenticated] = false if !data['user'][:is_authenticated]
{'fraction_non_alphanumeric': 0.14535689535689536, 'fraction_numerical': 0.0, 'mean_word_length': 2.9352419904567144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1026981', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1775, 'n_words': 445}