<commit_before>module HealthCheck
  class PqaApi < Component

    TIMESTAMP_FILE = "#{Rails.root}/tmp/pqa_api_healthcheck_timestamp"

    ERRS_TO_CATCH = 
    [
      Net::ReadTimeout, 
      Errno::ECONNREFUSED, 
      SocketError,
      HTTPClient::FailureResponse
     ]

    def self.time_to_run?
      interval = get_minimum_interval_in_seconds
      time_last_run = get_time_last_run
      Time.now.to_i > interval + time_last_run
    end


    def initialize
      @api = PQA::ApiClient.from_settings
      super
      record_time_ran
    end

    def available?
      res = perform_get(Settings.pq_rest_api.host)
      !!(res.code =~ /^2/)

    rescue *ERRS_TO_CATCH => e
      log_error('Access', e)
      false
    rescue => e
      log_unknown_error(e)
      false
    end

    def accessible?
      res = @api.question('1')
      !!(res.code =~ /^2/)

    rescue *ERRS_TO_CATCH => e
      log_error('Authentication', e)
      false
    rescue => e
      log_unknown_error(e)
      false
    end

    private

    def self.get_minimum_interval_in_seconds
      Settings.healthceck_pqa_api_interval * 60
    end

    def self.get_time_last_run
      if File.exist?(TIMESTAMP_FILE)
        File.open(TIMESTAMP_FILE, 'r') do |fp|
          interval = fp.gets.chomp.to_i
        end
      else
        0
      end
    end


    def record_time_ran
      tmpdir = "#{Rails.root}/tmp"
      Dir.mkdir(tmpdir) unless Dir.exist?(tmpdir
      File.open(TIMESTAMP_FILE, 'w') do |fp|
        fp.puts Time.now.to_i
      end
    end

    def log_error(type, e)
      @errors << "PQA API #{type} Error: #{e.message}"
    end

    def perform_get(uri_s)<commit_msg>fix typo<commit_after>module HealthCheck
  class PqaApi < Component

    TIMESTAMP_FILE = "#{Rails.root}/tmp/pqa_api_healthcheck_timestamp"

    ERRS_TO_CATCH = 
    [
      Net::ReadTimeout, 
      Errno::ECONNREFUSED, 
      SocketError,
      HTTPClient::FailureResponse
     ]

    def self.time_to_run?
      interval = get_minimum_interval_in_seconds
      time_last_run = get_time_last_run
      Time.now.to_i > interval + time_last_run
    end


    def initialize
      @api = PQA::ApiClient.from_settings
      super
      record_time_ran
    end

    def available?
      res = perform_get(Settings.pq_rest_api.host)
      !!(res.code =~ /^2/)

    rescue *ERRS_TO_CATCH => e
      log_error('Access', e)
      false
    rescue => e
      log_unknown_error(e)
      false
    end

    def accessible?
      res = @api.question('1')
      !!(res.code =~ /^2/)

    rescue *ERRS_TO_CATCH => e
      log_error('Authentication', e)
      false
    rescue => e
      log_unknown_error(e)
      false
    end

    private

    def self.get_minimum_interval_in_seconds
      Settings.healthceck_pqa_api_interval * 60
    end

    def self.get_time_last_run
      if File.exist?(TIMESTAMP_FILE)
        File.open(TIMESTAMP_FILE, 'r') do |fp|
          interval = fp.gets.chomp.to_i
        end
      else
        0
      end
    end


    def record_time_ran
      tmpdir = "#{Rails.root}/tmp"
      Dir.mkdir(tmpdir) unless Dir.exist?(tmpdir)
      File.open(TIMESTAMP_FILE, 'w') do |fp|
        fp.puts Time.now.to_i
      end
    end

    def log_error(type, e)
      @errors << "PQA API #{type} Error: #{e.message}"
    end

    def perform_get(uri_s)
{'fraction_non_alphanumeric': 0.09608433734939759, 'fraction_numerical': 0.0036144578313253013, 'mean_word_length': 2.1932692307692307, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '92718', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1152, 'n_words': 222}