<commit_before>class CreateNewVersion < ActiveJob::Base
  def perform
    ActiveRecord::Base.transaction do
      old_version = Version.current_version
      new_version = Version.create_new_version(:major)
      process_existing_dsvs(old_version, new_version)
      process_new_dsvs(new_version)
    end
  end

  private
  def process_existing_dsvs(old_version, new_version)
    old_version.disease_source_variants.find_each do |old_dsv|
      new_dsv = old_dsv.dup
      new_dsv.version = new_version
      new_dsv.save
    end
  end

  def process_new_dsvs(new_version)
    SubmittedVariant.where(status: 'accepted').find_each do |submitted_variant|
      dsv_tags = extract_tags(submitted_variant.tags)
      batch_tags = extract_tags(submitted_variant.batch.tags)
      all_tags = (dsv_tags + batch_tags).uniq.map do |t|
        Tag.where(name: t).first_or_create
      end
     dsv =  DiseaseSourceVariant.where(
        variant: get_variant(submitted_variant),
        source: Source.find_by(pubmed_id: submitted_variant.pubmed_id),
        disease: Disease.find_by(doid: submitted_variant.doid.gsub('DOID:', '')),
        version: new_version,
      ).first_or_create do |v|
        v.batch = submitted_variant.batch
      end

      current_tags = dsv.tags
      dsv.tags = (current_tags + all_tags).uniq
      current_meta = dsv.meta || {}
      dsv.meta = current_meta.merge(JSON.parse(submitted_variant.meta) || {})<commit_msg>handle nil meta<commit_after>class CreateNewVersion < ActiveJob::Base
  def perform
    ActiveRecord::Base.transaction do
      old_version = Version.current_version
      new_version = Version.create_new_version(:major)
      process_existing_dsvs(old_version, new_version)
      process_new_dsvs(new_version)
    end
  end

  private
  def process_existing_dsvs(old_version, new_version)
    old_version.disease_source_variants.find_each do |old_dsv|
      new_dsv = old_dsv.dup
      new_dsv.version = new_version
      new_dsv.save
    end
  end

  def process_new_dsvs(new_version)
    SubmittedVariant.where(status: 'accepted').find_each do |submitted_variant|
      dsv_tags = extract_tags(submitted_variant.tags)
      batch_tags = extract_tags(submitted_variant.batch.tags)
      all_tags = (dsv_tags + batch_tags).uniq.map do |t|
        Tag.where(name: t).first_or_create
      end
     dsv =  DiseaseSourceVariant.where(
        variant: get_variant(submitted_variant),
        source: Source.find_by(pubmed_id: submitted_variant.pubmed_id),
        disease: Disease.find_by(doid: submitted_variant.doid.gsub('DOID:', '')),
        version: new_version,
      ).first_or_create do |v|
        v.batch = submitted_variant.batch
      end

      current_tags = dsv.tags
      dsv.tags = (current_tags + all_tags).uniq
      if submitted_variant.meta.present?
        current_meta = dsv.meta || {}
        dsv.meta = current_meta.merge(JSON.parse(submitted_variant.meta) || {})
      end
{'fraction_non_alphanumeric': 0.09097103918228279, 'fraction_numerical': 0.0, 'mean_word_length': 3.4216867469879517, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5604344', 'n_tokens_mistral': 973, 'n_tokens_neox': 964, 'n_words': 155}