<commit_before># encoding: utf-8
#--
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

class Event < ActiveRecord::Base
  
  MAX_COMMIT_EVENTS = 25
  
  belongs_to :user
  belongs_to :project
  belongs_to :target, :polymorphic => true
  validates_presence_of :user_id, :unless => :user_email_set?
  
  has_many :events, :as => :target do
    def commits
      all(:limit => Event::MAX_COMMIT_EVENTS+1).select{|e|e.action == Action::COMMIT}
    end
  end

  named_scope :top, {:conditions => ['target_type != ?', 'Event']}
  
  def self.latest(count)
    find(:all, :order => "events.created_at desc", :limit => count, 
          :include => [:user, :project], 
          :conditions => ['events.target_type != ?', 'Event'])
  end
  
  def self.latest_in_projects(count, project_ids)
    find(:all, :order => "events.created_at desc", :limit => count, 
          :include => [:user, :project], 
          :conditions => ['events.target_type != ? and project_id in (?)', 
                          'Event', project_ids])
  end

  def build_commit(options={})
    e = self.class.new(options.merge({:action => Action::COMMIT, :project_id => project_id}))
    e.target = self
    return e
  end
  
  def has_commits?
    return false if self.action != Action::PUSH
    !events.blank? && !events.commits.blank?
  end
  <commit_msg>Speed up Event.latest by not filtering on events.target_type but rather on the events.action which is a binary field<commit_after># encoding: utf-8
#--
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#   Copyright (C) 2008 <NAME> <<EMAIL>>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

class Event < ActiveRecord::Base
  
  MAX_COMMIT_EVENTS = 25
  
  belongs_to :user
  belongs_to :project
  belongs_to :target, :polymorphic => true
  validates_presence_of :user_id, :unless => :user_email_set?
  
  has_many :events, :as => :target do
    def commits
      all(:limit => Event::MAX_COMMIT_EVENTS+1).select{|e|e.action == Action::COMMIT}
    end
  end

  named_scope :top, {:conditions => ['target_type != ?', 'Event']}
  
  def self.latest(count)
    find(:all, :order => "events.created_at desc", :limit => count,
          :include => [:user, :project],
          :conditions => ["events.action != ?", Action::COMMIT])
  end
  
  def self.latest_in_projects(count, project_ids)
    find(:all, :order => "events.created_at desc", :limit => count, 
          :include => [:user, :project], 
          :conditions => ['events.action != ? and project_id in (?)', 
                          Action::COMMIT, project_ids])
  end

  def build_commit(options={})
    e = self.class.new(options.merge({:action => Action::COMMIT, :project_id => project_id}))
    e.target = self
    return e
  end
  
  def has_commits?
    return false if self.action != Action::PUSH
    !events.blank? && !events.commits.blank?
  end
  
{'fraction_non_alphanumeric': 0.12303843529679327, 'fraction_numerical': 0.009551967250397999, 'mean_word_length': 3.3330049261083743, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6569221', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1345, 'n_words': 455}