<commit_before>class Task < Sequel::Model

  many_to_many  :features

  one_to_many   :contributions

  plugin        :class_table_inheritance,
    key:         :type

  def tracks
    features.first.tracks
  end

  class << self
    def give(to: nil)
      raise "Task can only be given 'to' a User" unless to.is_a? User
      available_tasks = where(id: to.tasks.map(&:id)).invert.where(state: 'ready')
      available_tasks_with_highest_priority = available_tasks.where(priority: available_tasks.max(:priority))
      task = available_tasks_with_highest_priority.offset(Sequel.function(:floor, Sequel.function(:random) * available_tasks_with_highest_priority.count)).first
      task.set_running_state
    end
  end

  def register_submission(submission, from: nil)
    yield
    register_user_contribution from
    #if contributions.count == 3 # and type == 'curation'
      #set_
    #else
      increment_priority and set_ready_state and save
    #end
  end

  def auto_check
    submissions.each_cons(2) do |s1, s2|
      return false unless s1 == s2
    end
    true
  end

  def set_ready_state
    self.state = 'ready'
    self.save
    self
  end

  def set_running_state
    self.state = 'running'
    self.save
    self
  end

  def increment_priority
    self.priority += 1
    self.save
    self
  end

  def register_user_contribution(from)
    raise "Submission should come 'from' a User" unless from.is_a? User
    Contribution.create user_id: from.id, task_id: id
  end
end

class CurationTask < Task

  one_to_many   :submissions,
    class: :UserCreatedFeature

  def register_submission(submission, from: nil)
    super do
      # The id of the submissions can be ignored. And assuming the submission
      # comprises only one gene model.
      submission = submission.values.first.values.first

      feature = UserCreatedFeature.new({
        name:        submission['name'],
        ref:         submission['ref'],
        start:       submission['start'],
        end:         submission['end'],
        subfeatures: submission['subfeatures'].map do |subfeature|
          subfeature = subfeature.values.first
          {start: subfeature['start'], end: subfeature['end']}
        end,
        curation_task_id: self.id
      })
      feature.save
    end
  end
end<commit_msg>Hack to ensure user submissions aren't lost, because I am clearly not doing it right.<commit_after>require 'yaml'
require 'date'

class Task < Sequel::Model

  many_to_many  :features

  one_to_many   :contributions

  plugin        :class_table_inheritance,
    key:         :type

  def tracks
    features.first.tracks
  end

  class << self
    def give(to: nil)
      raise "Task can only be given 'to' a User" unless to.is_a? User
      available_tasks = where(id: to.tasks.map(&:id)).invert.where(state: 'ready')
      available_tasks_with_highest_priority = available_tasks.where(priority: available_tasks.max(:priority))
      task = available_tasks_with_highest_priority.offset(Sequel.function(:floor, Sequel.function(:random) * available_tasks_with_highest_priority.count)).first
      task.set_running_state
    end
  end

  def register_submission(submission, from: nil)
    yield
    register_user_contribution from
    #if contributions.count == 3 # and type == 'curation'
      #set_
    #else
      increment_priority and set_ready_state and save
    #end
  end

  def auto_check
    submissions.each_cons(2) do |s1, s2|
      return false unless s1 == s2
    end
    true
  end

  def set_ready_state
    self.state = 'ready'
    self.save
    self
  end

  def set_running_state
    self.state = 'running'
    self.save
    self
  end

  def increment_priority
    self.priority += 1
    self.save
    self
  end

  def register_user_contribution(from)
    raise "Submission should come 'from' a User" unless from.is_a? User
    Contribution.create user_id: from.id, task_id: id
  end
end

class CurationTask < Task

  one_to_many   :submissions,
    class: :UserCreatedFeature

  def register_submission(submission, from: nil)
    super do
      ### MEGA HACK!!
      File.write(".submissions/#{from.id}_#{DateTime.now}.yml", YAML.dump(submission))
      ### MEGA HACK!!

      # The id of the submissions can be ignored. And assuming the submission
      # comprises only one gene model.
      submission = submission.values.first.values.first

      feature = UserCreatedFeature.new({
        name:        submission['name'],
        ref:         submission['ref'],
        start:       submission['start'],
        end:         submission['end'],
        subfeatures: submission['subfeatures'].map do |subfeature|
          subfeature = subfeature.values.first
          {start: subfeature['start'], end: subfeature['end']}
        end,
        curation_task_id: self.id
      })
      feature.save
    end
  end
end
{'fraction_non_alphanumeric': 0.0881684906049969, 'fraction_numerical': 0.0028907701837703904, 'mean_word_length': 2.79921568627451, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '3985151', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1456, 'n_words': 385}