<commit_before>class Evaluation < ActiveRecord::Base

  belongs_to :endpoint

  def self.lookup(endpoint_id, evaluation_id)
    if evaluation_id.nil?
      evaluation = Evaluation.where({endpoint_id: endpoint_id, latest: true}).first
    else
      evaluation = Evaluation.find(evaluation_id)
      return nil if evaluation.nil?
      return nil if evaluation[:endpoint_id].to_i != endpoint_id.to_i
    end
    return evaluation
  end

  def self.previous(endpoint_id, evaluation_id)
    self.where('id < ?', evaluation_id).where(endpoint_id: endpoint_id).order('id DESC').first
  end

  def self.next(endpoint_id, evaluation_id)
    self.where('id > ?', evaluation_id).where(endpoint_id: endpoint_id).order('id ASC').first
  end

  def self.record(endpoint, retriever)
    self.transaction do
      self.where(endpoint_id: endpoint.id).update_all("latest = false")
      self.retrieve_and_record endpoint, retriever
    end
  end

  def self.retrieve_and_record(endpoint, retriever)
    eval = Evaluation.new
    eval.endpoint_id = endpoint.id

    eval.latest = true

    logger = Umakadata::Logging::Log.new
    eval.alive = retriever.alive?(logger: logger)
    eval.alive_error_reason = logger.as_json

    if eval.alive
      self.retrieve_service_description(retriever, eval)
      self.retrieve_void(retriever, eval)
      self.retrieve_linked_data_rules(retriever, eval)

      logger = Umakadata::Logging::Log.new
      eval.execution_time = retriever.execution_time(logger: logger)
      eval.execution_time_error_reason = logger.as_json

      eval.cool_uri_rate = retriever.cool_uri_rate

      eval.support_turtle_format = retriever.check_content_negotiation(Umakadata::DataFormat::TURTLE)
      eval.support_xml_format    = retriever.check_content_negotiation(Umakadata::DataFormat::RDFXML)
      eval.support_html_format   = retriever.check_content_negotiation(Umakadata::DataFormat::HTML)
      eval.support_content_negotiation = eval.support_turtle_format ||
                                         eval.support_xml_format ||
                                         eval.support_html_format

      eval.support_content_negotiation_error_reason = retriever.get_error if !eval.support_content_negotiation


      metadata = retriever.metadata
      eval.metadata_error_reason = retriever.get_error
      eval.metadata_score = retriever.score_metadata(metadata)
      eval.ontology_score = retriever.score_ontologies(metadata)
      eval.vocabulary_score = retriever.score_vocabularies(metadata)

      self.check_update(retriever, eval)

      eval.number_of_statements = retriever.number_of_statements
      eval.number_of_statements_error_reason = retriever.get_error if eval.number_of_statements.nil?
    end

    eval.alive_rate = Evaluation.calc_alive_rate(eval)
    eval.score = Evaluation.calc_score(eval)
    eval.rank  = Evaluation.calc_rank(eval.score)
    eval.update_interval = Evaluation.calc_update_interval(eval)

    return eval if eval.save!
  end

  def self.retrieve_service_description(retriever, eval)
    logger = Umakadata::Logging::Log.new
    service_description = retriever.service_description(logger: logger)
    eval.service_description_error_reason = logger.as_json
    return if service_description.nil?
    eval.response_header     = service_description.response_header
    eval.service_description = service_description.text
  end

  def self.retrieve_void(retriever, eval)
    eval.void_uri = retriever.well_known_uri
    void = retriever.void_on_well_known_uri
    if void.nil?
      eval.void_ttl = nil
      eval.void_ttl_error_reason = retriever.get_error
      return
    end
    eval.void_ttl_error_reason = retriever.get_error if void.text.nil?
    eval.void_ttl = void.text
  end

  def self.retrieve_linked_data_rules(retriever, eval)
    eval.subject_is_uri = retriever.uri_subject?
    eval.subject_is_uri_error_reason = retriever.get_error if !eval.subject_is_uri<commit_msg>Set log in retrieved from void_well_known_uri<commit_after>class Evaluation < ActiveRecord::Base

  belongs_to :endpoint

  def self.lookup(endpoint_id, evaluation_id)
    if evaluation_id.nil?
      evaluation = Evaluation.where({endpoint_id: endpoint_id, latest: true}).first
    else
      evaluation = Evaluation.find(evaluation_id)
      return nil if evaluation.nil?
      return nil if evaluation[:endpoint_id].to_i != endpoint_id.to_i
    end
    return evaluation
  end

  def self.previous(endpoint_id, evaluation_id)
    self.where('id < ?', evaluation_id).where(endpoint_id: endpoint_id).order('id DESC').first
  end

  def self.next(endpoint_id, evaluation_id)
    self.where('id > ?', evaluation_id).where(endpoint_id: endpoint_id).order('id ASC').first
  end

  def self.record(endpoint, retriever)
    self.transaction do
      self.where(endpoint_id: endpoint.id).update_all("latest = false")
      self.retrieve_and_record endpoint, retriever
    end
  end

  def self.retrieve_and_record(endpoint, retriever)
    eval = Evaluation.new
    eval.endpoint_id = endpoint.id

    eval.latest = true

    logger = Umakadata::Logging::Log.new
    eval.alive = retriever.alive?(logger: logger)
    eval.alive_error_reason = logger.as_json

    if eval.alive
      self.retrieve_service_description(retriever, eval)
      logger = Umakadata::Logging::Log.new
      self.retrieve_void(retriever, eval, logger: logger)
      self.retrieve_linked_data_rules(retriever, eval)

      logger = Umakadata::Logging::Log.new
      eval.execution_time = retriever.execution_time(logger: logger)
      eval.execution_time_error_reason = logger.as_json

      eval.cool_uri_rate = retriever.cool_uri_rate

      eval.support_turtle_format = retriever.check_content_negotiation(Umakadata::DataFormat::TURTLE)
      eval.support_xml_format    = retriever.check_content_negotiation(Umakadata::DataFormat::RDFXML)
      eval.support_html_format   = retriever.check_content_negotiation(Umakadata::DataFormat::HTML)
      eval.support_content_negotiation = eval.support_turtle_format ||
                                         eval.support_xml_format ||
                                         eval.support_html_format

      eval.support_content_negotiation_error_reason = retriever.get_error if !eval.support_content_negotiation


      metadata = retriever.metadata
      eval.metadata_error_reason = retriever.get_error
      eval.metadata_score = retriever.score_metadata(metadata)
      eval.ontology_score = retriever.score_ontologies(metadata)
      eval.vocabulary_score = retriever.score_vocabularies(metadata)

      self.check_update(retriever, eval)

      eval.number_of_statements = retriever.number_of_statements
      eval.number_of_statements_error_reason = retriever.get_error if eval.number_of_statements.nil?
    end

    eval.alive_rate = Evaluation.calc_alive_rate(eval)
    eval.score = Evaluation.calc_score(eval)
    eval.rank  = Evaluation.calc_rank(eval.score)
    eval.update_interval = Evaluation.calc_update_interval(eval)

    return eval if eval.save!
  end

  def self.retrieve_service_description(retriever, eval)
    logger = Umakadata::Logging::Log.new
    service_description = retriever.service_description(logger: logger)
    eval.service_description_error_reason = logger.as_json
    return if service_description.nil?
    eval.response_header     = service_description.response_header
    eval.service_description = service_description.text
  end

  def self.retrieve_void(retriever, eval, logger: nil)
    eval.void_uri = retriever.well_known_uri
    void = retriever.void_on_well_known_uri(logger: logger)
    eval.void_ttl_error_reason = logger.as_json
    if void.nil?
      eval.void_ttl = nil
      return
    end
    eval.void_ttl = void.text
  end

  def self.retrieve_linked_data_rules(retriever, eval)
    eval.subject_is_uri = retriever.uri_subject?
    eval.subject_is_uri_error_reason = retriever.get_error if !eval.subject_is_uri
{'fraction_non_alphanumeric': 0.08097524002021222, 'fraction_numerical': 0.0, 'mean_word_length': 3.863022113022113, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4485381', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2453, 'n_words': 383}