<commit_before>class Cache
  
  CACHE_PATH = "#{RAILS_ROOT}/tmp/dj_cache/"

  def initialize
    @queued = {}
  end

  LABELS = %w(
    organizations
    active_organizations
  )
  
  def get(label, cache_invalid_after=3600)
    run_unless_already_queued(label) if cache_invalid?(label)
    load_from_file(label)
  end
  
  protected
  
  def cache_invalid?(label, cache_life=3600)
    f_name = filename(label)
    return true unless File.exist?(f_name)
    modified_at = File.mtime(f_name)
    (Time.now - modified_at) > cache_life
  end
  
  def run_unless_already_queued(label)
    if !@queued[label]
      self.send_later(:run_query, label)
      @queued[label] = true
    else
      puts "#{label} is already in the queue"
    end
  end
  
  def run_query(label)
    o1 = case label
    when :organizations
      puts "Loading all organizations..."
      DataCatalog::Organization.all
    when :active_organizations
      puts "Loading organizations with at least one source..."
      DataCatalog::Organization.all('source_count > 0')
    else
      raise RuntimeError, "label not found : #{label}"
    end
    puts "Building array with name and id..."
    o2 = o1.map { |o| [o.name, o.id] }
    puts "Sorting array..."
    o3 = o2.sort_by { |x| x[0] }
    puts "Saving..."
    write_to_file(label, o3)
    puts "Done."
    @queued[label] = false
  end
  
  def filename(label)
    if LABELS.include?(label.to_s)
      File.join(CACHE_PATH, "#{label}.yaml")
    else
      raise "label not found : #{label}"
    end<commit_msg>regenerate dj_cache files after delete [#2322447]<commit_after>class Cache
  
  CACHE_PATH = "#{RAILS_ROOT}/tmp/dj_cache/"

  LABELS = %w(
    organizations
    active_organizations
  )
  
  def get(label, cache_invalid_after=3600)
    if cache_invalid?(label)
      self.send_later(:run_query, label)
    end
    load_from_file(label)
  end
  
  protected
  
  def cache_invalid?(label, cache_life=3600)
    f_name = filename(label)
    return true unless File.exist?(f_name)
    modified_at = File.mtime(f_name)
    (Time.now - modified_at) > cache_life
  end
  
  def run_query(label)
    logger = Delayed::Worker.logger
    o1 = case label
    when :organizations
      logger.info "Loading all organizations..."
      DataCatalog::Organization.all
    when :active_organizations
      logger.info "Loading organizations with at least one source..."
      DataCatalog::Organization.all('source_count > 0')
    else
      raise RuntimeError, "label not found : #{label}"
    end
    # logger.info "Building array with name and id..."
    o2 = o1.map { |o| [o.name, o.id] }
    # logger.info "Sorting array..."
    o3 = o2.sort_by { |x| x[0] }
    # logger.info "Saving..."
    write_to_file(label, o3)
    logger.info "Done."
  end
  
  def filename(label)
    if LABELS.include?(label.to_s)
      File.join(CACHE_PATH, "#{label}.yaml")
    else
      raise "label not found : #{label}"
    end
{'fraction_non_alphanumeric': 0.11437350631614886, 'fraction_numerical': 0.013315124615909868, 'mean_word_length': 2.8101430429128738, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7166744', 'n_tokens_mistral': 1032, 'n_tokens_neox': 1016, 'n_words': 238}