<commit_before>class Subject
  include Mongoid::Document
  include Mongoid::Timestamps
  include Randomizer


  scope :active_root, -> { where(type: 'root', status: 'active') }

  # This is a hash with one entry per deriv; `standard', 'thumbnail', etc
  field :location,                    type: Hash 
  field :type,                        type: String,  default: "root" #options: "root", "secondary"
  field :status,                      type: String,  default: "active" #options: "active", "inactive", "retired", "complete", "contentious"

  field :meta_data,                   type: Hash
  field :secondary_subject_count,     type: Integer, default: 0
  field :classification_count,        type: Integer, default: 0
  field :random_no,                   type: Float
  field :secondary_subject_count,     type: Integer, default: 0

  # Need to sort out relationship between these two fields. Are these two fields Is this :shj
  field :retire_count,                type: Integer


  # ROOT SUBJECT concerns:
  field :order
  field :name,                        type: String
  field :width
  field :height

  # SECONDARY SUBJECT concerns:
  field :data,                        type: Hash
  field :region,                      type: Hash

  belongs_to :workflow
  belongs_to :parent_subject, :class_name => "Subject", :foreign_key => "parent_subject_id"  
  belongs_to :subject_set, :class_name => "SubjectSet", :foreign_key => "subject_set_id"

  has_many :child_subjects, :class_name => "Subject"
  has_many :classifications
  has_many :favourites

  after_create :update_subject_set_stats, :activate! # this method before :increment_parents_subject_count_by_one
  after_create :increment_parents_subject_count_by_one, :if => :parent_subject

  def update_subject_set_stats
    subject_set.inc_subject_count_for_workflow(workflow) if ! workflow.nil?
  end

  def increment_parents_subject_count_by_one
    parent_subject.inc(secondary_subject_count: 1)
  end

  def increment_retire_count_by_one
    self.inc(retire_count: 1)
    self.retire_by_vote!
  end

  # find all the classifications for subject where task_key == compleletion_assesment_task
  # calculate the percetage vote for retirement (pvr)
  # if pvr is equal or greater than retire_limit, set self.status == retired.
  def retire_by_vote!
    assesment_classifications = classifications.where(task_key: "completion_assessment_task").to_a
    
    if assesment_classifications.length > 1
      percentage_for_retire = retire_count/assesment_classifications.length.to_f   
      if percentage_for_retire >= workflow.retire_limit
        binding.pry
        self.status = "retired" 
        subject_set.subject_completed_on_workflow(workflow) if ! workflow.nil?
        save
      end
    end

  end

  def activate!
    self.status = "active"
    self.subject_set.subject_activated_on_workflow(workflow) if ! workflow.nil?
    save
  end

end<commit_msg>updated Subject#retire_by_vote! to assess retirement by percentage of vote<commit_after>class Subject
  include Mongoid::Document
  include Mongoid::Timestamps
  include Randomizer


  scope :active_root, -> { where(type: 'root', status: 'active') }

  # This is a hash with one entry per deriv; `standard', 'thumbnail', etc
  field :location,                    type: Hash 
  field :type,                        type: String,  default: "root" #options: "root", "secondary"
  field :status,                      type: String,  default: "active" #options: "active", "inactive", "retired", "complete", "contentious"

  field :meta_data,                   type: Hash
  field :secondary_subject_count,     type: Integer, default: 0
  field :classification_count,        type: Integer, default: 0
  field :random_no,                   type: Float
  field :secondary_subject_count,     type: Integer, default: 0

  # Need to sort out relationship between these two fields. Are these two fields Is this :shj
  field :retire_count,                type: Integer


  # ROOT SUBJECT concerns:
  field :order
  field :name,                        type: String
  field :width
  field :height

  # SECONDARY SUBJECT concerns:
  field :data,                        type: Hash
  field :region,                      type: Hash

  belongs_to :workflow
  belongs_to :parent_subject, :class_name => "Subject", :foreign_key => "parent_subject_id"  
  belongs_to :subject_set, :class_name => "SubjectSet", :foreign_key => "subject_set_id"

  has_many :child_subjects, :class_name => "Subject"
  has_many :classifications
  has_many :favourites

  after_create :update_subject_set_stats, :activate! # this method before :increment_parents_subject_count_by_one
  after_create :increment_parents_subject_count_by_one, :if => :parent_subject

  def update_subject_set_stats
    subject_set.inc_subject_count_for_workflow(workflow) if ! workflow.nil?
  end

  def increment_parents_subject_count_by_one
    parent_subject.inc(secondary_subject_count: 1)
  end

  def increment_retire_count_by_one
    self.inc(retire_count: 1)
    self.retire_by_vote!
  end

  # find all the classifications for subject where task_key == compleletion_assesment_task
  # calculate the percetage vote for retirement (pvr)
  # if pvr is equal or greater than retire_limit, set self.status == retired.
  def retire_by_vote!
    assesment_classifications = classifications.where(task_key: "completion_assessment_task").to_a
    
    if assesment_classifications.length > 1
      percentage_for_retire = retire_count/assesment_classifications.length.to_f   
      if percentage_for_retire >= workflow.retire_limit
        self.status = "retired" 
        subject_set.subject_completed_on_workflow(workflow) if ! workflow.nil?
        save
      end
    end

  end

  def activate!
    self.status = "active"
    self.subject_set.subject_activated_on_workflow(workflow) if ! workflow.nil?
    save
  end

end
{'fraction_non_alphanumeric': 0.07846808510638298, 'fraction_numerical': 0.0020425531914893616, 'mean_word_length': 3.2734545454545456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5177280', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1726, 'n_words': 468}