<commit_before>class DiskSpace

  extend ActionView::Helpers::NumberHelper

  def self.uploads_used_bytes
    # used(uploads_path)
    # temporary (on our internal setup its just too slow to iterate)
    Upload.sum(:filesize).to_i
  end

  def self.uploads_free_bytes
    free(uploads_path)
  end

  def self.backups_used_bytes
    used(backups_path)
  end

  def self.backups_free_bytes
    free(backups_path)
  end

  def self.backups_path
    Backup.base_directory
  end

  def self.uploads_path
    "#{Rails.root}/public/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
  end

  def self.stats
    {
      uploads_used: number_to_human_size(uploads_used_bytes),
      uploads_free: number_to_human_size(uploads_free_bytes),
      backups_used: number_to_human_size(backups_used_bytes),
      backups_free: number_to_human_size(backups_free_bytes)
    }
  end

  def self.cached_stats
    stats = $redis.get('disk_space_stats')
    updated_at = $redis.get('disk_space_stats_updated')

    unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
      Scheduler::Defer.later "updated stats" do
        $redis.set('disk_space_stats_updated', Time.now.to_i)
        $redis.set('disk_space_stats', self.stats.to_json)
      end
    end

    if stats
      JSON.parse(stats)
    end

  end

  protected

  def self.free(path)
    `df -Pk #{path} | awk 'NR==2 {print $4 * 1024;}'`.to_i
  end

  def self.used(path)
    `du -s #{path}`.to_i * 1024
  end

end<commit_msg>FIX: disk_space should be a BigDecimal to handle large disk (closes #3923)<commit_after>require "bigdecimal"

class DiskSpace

  extend ActionView::Helpers::NumberHelper

  def self.uploads_used_bytes
    # used(uploads_path)
    # temporary (on our internal setup its just too slow to iterate)
    Upload.sum(:filesize).to_i
  end

  def self.uploads_free_bytes
    free(uploads_path)
  end

  def self.backups_used_bytes
    used(backups_path)
  end

  def self.backups_free_bytes
    free(backups_path)
  end

  def self.backups_path
    Backup.base_directory
  end

  def self.uploads_path
    "#{Rails.root}/public/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
  end

  def self.stats
    {
      uploads_used: number_to_human_size(uploads_used_bytes),
      uploads_free: number_to_human_size(uploads_free_bytes),
      backups_used: number_to_human_size(backups_used_bytes),
      backups_free: number_to_human_size(backups_free_bytes)
    }
  end

  def self.cached_stats
    stats = $redis.get('disk_space_stats')
    updated_at = $redis.get('disk_space_stats_updated')

    unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
      Scheduler::Defer.later "updated stats" do
        $redis.set('disk_space_stats_updated', Time.now.to_i)
        $redis.set('disk_space_stats', self.stats.to_json)
      end
    end

    if stats
      JSON.parse(stats)
    end

  end

  protected

  def self.free(path)
    number = `df -Pk #{path} | awk 'NR==2 {print $4 * 1024;}'`.strip
    BigDecimal.new(number).to_i
  end

  def self.used(path)
    number = `du -s #{path}`
    BigDecimal.new(number).to_i * 1024
  end

end
{'fraction_non_alphanumeric': 0.09780184772220453, 'fraction_numerical': 0.008920038228735267, 'mean_word_length': 3.472934472934473, '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': '719143', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1100, 'n_words': 210}