<commit_before>class Api::ExternalGradersController < Api::BaseController

  class DeveloperAPIKeyInvalid < StandardError
    def initialize(msg="The API key did not match any participant record.")
      super
    end
  end

  class ChallengeClientNameInvalid < StandardError
    def initialize(msg="The Challenge Client Name string did not match any challenge.")
      super
    end
  end

  class GradingStatusInvalid < StandardError
    def initialize(msg="Grading status must be one of (graded|failed)")
      super
    end
  end

  def show
    message = nil
    status = nil
    participant = Participant.where(api_key: params[:id]).first
    if participant.present?
      message = "Developer API key is valid"
      status = :ok
    else
      message = "No participant could be found for this API key"
      status = :not_found
    end
    render json: { message: message }, status: status
  end

  def create
    message = nil
    status = nil
    begin
      participant = Participant.where(api_key: params[:api_key]).first
      raise Api::ExternalGradersController::DeveloperAPIKeyInvalid if participant.nil?
      challenge = Challenge.where(challenge_client_name: params[:challenge_client_name]).first
      raise Api::ExternalGradersController::ChallengeClientNameInvalid if challenge.nil?
      submission = Submission.create!(participant_id: participant.id,
                                      challenge_id: challenge.id,
                                      description_markdown: params[:comment],
                                      media_large: params[:media_large],
                                      media_thumbnail: params[:media_thumbnail],
                                      media_content_type: params[:media_content_type])
      submission.submission_grades.create!(grading_params)
      message = "Participant: #{participant.name}, submission: #{params[:id]} scored"
      status = :accepted
    rescue => e
      #Rails.logger.error e
      #Rails.logger.error params
      status = :bad_request
      message = e
    ensure
      render json: { message: message, submission_id: submission.inspect }, status: status
    end
  end

=begin
  def update
    begin
      submission = Submission.find(params[:submission_id])
      raise ActiveRecord::RecordNotFound if submission.nil?
      key_valid = validate_s3_key(params[:s3_key])
      raise InvalidS3Key.new(s3_key: params[:s3_key]) if !key_valid
      ProcessAiGymGifJob.perform_later(submission.id,params[:s3_key])
      message = "Animated GIF accepted for processing."
    rescue => e
      Rails.logger.error e<commit_msg>api for grader<commit_after>class Api::ExternalGradersController < Api::BaseController

  class DeveloperAPIKeyInvalid < StandardError
    def initialize(msg="The API key did not match any participant record.")
      super
    end
  end

  class ChallengeClientNameInvalid < StandardError
    def initialize(msg="The Challenge Client Name string did not match any challenge.")
      super
    end
  end

  class GradingStatusInvalid < StandardError
    def initialize(msg="Grading status must be one of (graded|failed)")
      super
    end
  end

  def show
    message = nil
    status = nil
    participant = Participant.where(api_key: params[:id]).first
    if participant.present?
      message = "Developer API key is valid"
      status = :ok
    else
      message = "No participant could be found for this API key"
      status = :not_found
    end
    render json: { message: message }, status: status
  end

  def create
    message = nil
    status = nil
    begin
      participant = Participant.where(api_key: params[:api_key]).first
      raise Api::ExternalGradersController::DeveloperAPIKeyInvalid if participant.nil?
      challenge = Challenge.where(challenge_client_name: params[:challenge_client_name]).first
      raise Api::ExternalGradersController::ChallengeClientNameInvalid if challenge.nil?
      submission = Submission.create!(participant_id: participant.id,
                                      challenge_id: challenge.id,
                                      description_markdown: params[:comment],
                                      media_large: params[:media_large],
                                      media_thumbnail: params[:media_thumbnail],
                                      media_content_type: params[:media_content_type])
      submission.submission_grades.create!(grading_params)
      message = "Participant: #{participant.name} scored"
      status = :accepted
    rescue => e
      #Rails.logger.error e
      #Rails.logger.error params
      status = :bad_request
      message = e
    ensure
      render json: { message: message, submission_id: submission.id }, status: status
    end
  end

=begin
  def update
    begin
      submission = Submission.find(params[:submission_id])
      raise ActiveRecord::RecordNotFound if submission.nil?
      key_valid = validate_s3_key(params[:s3_key])
      raise InvalidS3Key.new(s3_key: params[:s3_key]) if !key_valid
      ProcessAiGymGifJob.perform_later(submission.id,params[:s3_key])
      message = "Animated GIF accepted for processing."
    rescue => e
      Rails.logger.error e
{'fraction_non_alphanumeric': 0.07798693814829043, 'fraction_numerical': 0.002305032654629274, 'mean_word_length': 2.695528743789922, 'pattern_counts': {'":': 0, '<': 11, '<?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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886026', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1363, 'n_words': 363}