<commit_before>class ExitTicket < ActiveRecord::Base

  self.inheritance_column = "inheritance_type"

  enum type: [:technical, :nontechnical]

  belongs_to :developer

  def self.completion_rate_in_range(range)
    times = where("submitted_at >= ? AND submitted_at <= ?", range.begin, range.end).map(&:submitted_at)
    a = Attendance.where("timestamp in (?)", times)
    return (times.count / a.count.to_f) unless count == 0
    return -1
  end

  def self.completion_rate_on_day(day)
    a = Attendance.where("timestamp = ?", day).present
    count = where(submitted_at: day).count
    return (count / a.count.to_f) unless count == 0
    return -1
  end

  def self.accuracy_rate_in_range(range)
    tickets = where("submitted_at >= ? AND submitted_at <= ?", range.begin, range.end)
    return tickets.map(&:score).inject(0.0) { |sum, el| sum + (el || 0) }.to_f / tickets.count unless tickets.count == 0
    return -1
  end

  def self.accuracy_rate_on_day(day)
    tickets = where(submitted_at: day)
    return tickets.map(&:score).inject(0.0) { |sum, el| sum + (el || 0) }.to_f / tickets.count unless tickets.count == 0
    return -1
  end

  # Submitted as Google Form
  def self.create_from_google_form(form_data)
    json = JSON.parse(form_data["ticket"])
    tck = json["ticket"]

    ActiveRecord::Base.transaction do
      developer = Developer.where(full_name: tck["name"].downcase).first
        tck["submitted_at"] = Date.parse(tck["submitted_at"]).to_datetime
        tck.delete("name")
        tck["questions"] = tck["questions"].to_json
        if t = ExitTicket.where("developer_id = ? AND submitted_at = ?", developer.id, tck["submitted_at"]).last
          t.update_attributes(tck)
        else
          ticket = ExitTicket.new(tck)
          if ticket.save
            developer.exit_tickets << ticket
          else
            return false
          end
        end
    end

    true
  end

  # Imported as .csv
  def self.import(file)
    ActiveRecord::Base.transaction do
      ExitTicketImporter.import_from_file(file)
    end
  end

  # Submitted as exit ticket response sheet
  def self.import_from_google_form(form_data)
    ActiveRecord::Base.transaction do
      ExitTicketImporter.import_from_form(form_data)
    end<commit_msg>exit ticket repeats dont overwrite type<commit_after>class ExitTicket < ActiveRecord::Base

  self.inheritance_column = "inheritance_type"

  enum type: [:technical, :nontechnical]

  belongs_to :developer

  def self.completion_rate_in_range(range)
    times = where("submitted_at >= ? AND submitted_at <= ?", range.begin, range.end).map(&:submitted_at)
    a = Attendance.where("timestamp in (?)", times)
    return (times.count / a.count.to_f) unless count == 0
    return -1
  end

  def self.completion_rate_on_day(day)
    a = Attendance.where("timestamp = ?", day).present
    count = where(submitted_at: day).count
    return (count / a.count.to_f) unless count == 0
    return -1
  end

  def self.accuracy_rate_in_range(range)
    tickets = where("submitted_at >= ? AND submitted_at <= ?", range.begin, range.end)
    return tickets.map(&:score).inject(0.0) { |sum, el| sum + (el || 0) }.to_f / tickets.count unless tickets.count == 0
    return -1
  end

  def self.accuracy_rate_on_day(day)
    tickets = where(submitted_at: day)
    return tickets.map(&:score).inject(0.0) { |sum, el| sum + (el || 0) }.to_f / tickets.count unless tickets.count == 0
    return -1
  end

  # Submitted as Google Form
  def self.create_from_google_form(form_data)
    json = JSON.parse(form_data["ticket"])
    tck = json["ticket"]

    ActiveRecord::Base.transaction do
      developer = Developer.where(full_name: tck["name"].downcase).first
        tck["submitted_at"] = Date.parse(tck["submitted_at"]).to_datetime
        tck.delete("name")
        tck["questions"] = tck["questions"].to_json
        if t = ExitTicket.where("developer_id = ? and submitted_at = ? and type = ?", developer.id, tck["submitted_at"], (tck["type"] || 0)).last
          t.update_attributes(tck)
        else
          ticket = ExitTicket.new(tck)
          if ticket.save
            developer.exit_tickets << ticket
          else
            return false
          end
        end
    end

    true
  end

  # Imported as .csv
  def self.import(file)
    ActiveRecord::Base.transaction do
      ExitTicketImporter.import_from_file(file)
    end
  end

  # Submitted as exit ticket response sheet
  def self.import_from_google_form(form_data)
    ActiveRecord::Base.transaction do
      ExitTicketImporter.import_from_form(form_data)
    end
{'fraction_non_alphanumeric': 0.1207160008731718, 'fraction_numerical': 0.006330495524994543, 'mean_word_length': 3.1242124212421243, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4706485', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1483, 'n_words': 322}