<commit_before># frozen_string_literal: true

#= Queries for articles and revisions that have interesting properties
class RevisionAnalyticsService
  ################
  # Entry points #
  ################

  DEFAULT_DYK_WP10_LIMIT = 60
  def self.dyk_eligible(opts={})
    new(opts).dyk_eligible
  end

  DEFAULT_PLAGIARISM_LIMIT = 20
  def self.suspected_plagiarism(opts={})
    new(opts).suspected_plagiarism
  end

  DEFAULT_RECENT_EDITS_LIMIT = 30
  def self.recent_edits(opts={})
    new(opts).recent_edits
  end

  #########
  # Setup #
  #########

  def initialize(opts)
    return unless opts[:scoped] == 'true' && opts[:current_user]
    @course_ids = Course.joins(:courses_users)
                        .where('courses_users.user_id = ?', opts[:current_user].id)
                        .current.pluck(:id)
  end

  #################
  # Main routines #
  #################
  def dyk_eligible
    wp10_limit = ENV['dyk_wp10_limit'] || DEFAULT_DYK_WP10_LIMIT
    good_student_revisions = Revision
                             .where(user_id: student_ids)
                             .where('wp10 >= ?', wp10_limit)
                             .where('date > ?', 2.months.ago)
    good_article_ids = good_student_revisions.pluck(:article_id)

    good_draft_ids = userspace_draft_ids(good_article_ids) + draft_space_ids(good_article_ids)
    good_drafts = articles_sorted_by_latest_revision(good_draft_ids)
    good_drafts
  end

  def suspected_plagiarism
    if @course_ids
      suspected_revisions = Revision.where.not(ithenticate_id: nil)
                                    .where(user_id: student_ids)
                                    .order(date: :desc).first(DEFAULT_PLAGIARISM_LIMIT)
    else
      suspected_revisions = Revision.where.not(ithenticate_id: nil)
                                    .order(date: :desc).first(DEFAULT_PLAGIARISM_LIMIT)
    end
    suspected_revisions
  end

  def recent_edits
    recent_revisions = if @course_ids
                         Revision.user.where(user_id: student_ids).last(DEFAULT_RECENT_EDITS_LIMIT)
                       else
                         Revision.user.last(DEFAULT_RECENT_EDITS_LIMIT)
                       end
    recent_revisions
  end

  ##################<commit_msg>refactor revision analytics to use scoped suspected_plagiarism<commit_after># frozen_string_literal: true

#= Queries for articles and revisions that have interesting properties
class RevisionAnalyticsService
  ################
  # Entry points #
  ################

  DEFAULT_DYK_WP10_LIMIT = 60
  def self.dyk_eligible(opts={})
    new(opts).dyk_eligible
  end

  DEFAULT_PLAGIARISM_LIMIT = 20
  def self.suspected_plagiarism(opts={})
    new(opts).suspected_plagiarism
  end

  DEFAULT_RECENT_EDITS_LIMIT = 30
  def self.recent_edits(opts={})
    new(opts).recent_edits
  end

  #########
  # Setup #
  #########

  def initialize(opts)
    return unless opts[:scoped] == 'true' && opts[:current_user]
    @course_ids = Course.joins(:courses_users)
                        .where('courses_users.user_id = ?', opts[:current_user].id)
                        .current.pluck(:id)
  end

  #################
  # Main routines #
  #################
  def dyk_eligible
    wp10_limit = ENV['dyk_wp10_limit'] || DEFAULT_DYK_WP10_LIMIT
    good_student_revisions = Revision
                             .where(user_id: student_ids)
                             .where('wp10 >= ?', wp10_limit)
                             .where('date > ?', 2.months.ago)
    good_article_ids = good_student_revisions.pluck(:article_id)

    good_draft_ids = userspace_draft_ids(good_article_ids) + draft_space_ids(good_article_ids)
    good_drafts = articles_sorted_by_latest_revision(good_draft_ids)
    good_drafts
  end

  def suspected_plagiarism
    if @course_ids
      suspected_revisions = Revision.suspected_plagiarism
                                    .where(user_id: student_ids)
                                    .order(date: :desc).first(DEFAULT_PLAGIARISM_LIMIT)
    else
      suspected_revisions = Revision.suspected_plagiarism
                                    .order(date: :desc).first(DEFAULT_PLAGIARISM_LIMIT)
    end
    suspected_revisions
  end

  def recent_edits
    recent_revisions = if @course_ids
                         Revision.user.where(user_id: student_ids).last(DEFAULT_RECENT_EDITS_LIMIT)
                       else
                         Revision.user.last(DEFAULT_RECENT_EDITS_LIMIT)
                       end
    recent_revisions
  end

  ##################
{'fraction_non_alphanumeric': 0.11930442438917015, 'fraction_numerical': 0.00836451683909311, 'mean_word_length': 2.460776846915461, 'pattern_counts': {'":': 0, '<': 3, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2626880', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1393, 'n_words': 212}