<commit_before>#
# states:
# * pending
# * downloading
# * processing
# * failed
# * done
#
module OntologyVersion::States
  extend ActiveSupport::Concern

  include StateUpdater

  TERMINAL_STATES = %w(failed done)

  included do
    after_save :after_update_state, if: :state_changed?
  end

  def state_message
    msg = [state]
    if last_error
      lines = last_error.split("\n")
      if (ind=lines.index("*** Error:")) and (out = lines[ind+1]).present?
        i = ind+2
        while lines[i] and !lines[i].include?("hets: user error") do
          out += " "+lines[i]
          i+=1
        end
        msg << out.sub(URI.regexp,"...").sub(/ \/[A-Za-z0-9\/.]*/," ...")
      elsif last_error.include?("exited with status")
        msg << last_error[0,50]+" ... "+last_error.match("exited with status.*")[0]
      else
        msg << lines.first
      end
    end
    msg.join(": ")
  end

  protected

  def after_update_state
    ontology.state = state.to_s
    ontology.save!
    if ontology.distributed?
      ontology.children.update_all state: ontology.state
    end<commit_msg>prevent ontology-state update for child-versions.<commit_after>#
# states:
# * pending
# * downloading
# * processing
# * failed
# * done
#
module OntologyVersion::States
  extend ActiveSupport::Concern

  include StateUpdater

  TERMINAL_STATES = %w(failed done)

  included do
    after_save :after_update_state, if: :change_applicable?
  end

  def change_applicable?
    state_changed? &&
    parent.nil?
  end

  def state_message
    msg = [state]
    if last_error
      lines = last_error.split("\n")
      if (ind=lines.index("*** Error:")) and (out = lines[ind+1]).present?
        i = ind+2
        while lines[i] and !lines[i].include?("hets: user error") do
          out += " "+lines[i]
          i+=1
        end
        msg << out.sub(URI.regexp,"...").sub(/ \/[A-Za-z0-9\/.]*/," ...")
      elsif last_error.include?("exited with status")
        msg << last_error[0,50]+" ... "+last_error.match("exited with status.*")[0]
      else
        msg << lines.first
      end
    end
    msg.join(": ")
  end

  protected

  def after_update_state
    ontology.state = state.to_s
    ontology.save!
    if ontology.distributed?
      ontology.children.update_all state: ontology.state
    end
{'fraction_non_alphanumeric': 0.14664926022628372, 'fraction_numerical': 0.007832898172323759, 'mean_word_length': 2.5588235294117645, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 3, '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': '3730720', 'n_tokens_mistral': 798, 'n_tokens_neox': 759, 'n_words': 170}