<commit_before>class Disk < ActiveRecord::Base

  include ModelsWatcher
  watch_fields :cache, :free
  watch_on_destroy

  named_scope :system, :conditions => {:system => true}
  named_scope :cache, :conditions => {:cache => true}
  named_scope :free, :conditions => {:free => true}
  named_scope :in_raid, :conditions => ["disks.raid IS NOT NULL AND disks.system = FALSE AND disks.cache = TRUE"]
  named_scope :raid_is, lambda {|raid| { :conditions => ["disks.raid = ?", raid]} }
  named_scope :cache_in_raid, :conditions => ["disks.raid != 'md0' and disks.raid != 'NULL'"]
  named_scope :prepared_for_cache, :conditions => 'prepare_disk_for_cache = 1'
  before_update :clean_cache, :if => "self.free_changed? and self.free"

  def is_mounted?
    system("mount | grep '#{name}' &>/dev/null")
  end
  def self.scan
    disks = {}
    aux =`sudo /usr/bin/lshw -C disk`.strip.split("*-")
    system_disks = used_for_system
    cache_disks = used_for_cache

    aux.each do |disk|
      if disk.include?("disk")
        name = ""
        capacity = ""
        serial = ""
        attributes = disk.split("  ")
        attributes.each do |attr|
          name = attr.chomp.split(":").last.strip if attr.include?("logical name")
          capacity = attr.chomp.split(":").last.split(" ").last.strip if attr.include?("size:")
          serial = attr.chomp.split(":").last.strip if attr.include?("serial")          
        end
        is_system = system_disks[:devices].include?(name) ? true : false
        is_cache = cache_disks[:devices].include?(name) ? true : false
        #which_raid = system_disks[:raid] if is_system
        #which_raid = cache_disks[:raid] if is_cache
        is_free = is_system or is_cache ? false : true
        partitioned =  is_free ? false : true
        hash = {:name => name, :capacity => capacity, :serial => serial, :system => is_system, :cache => is_cache, :free => is_free, :partitioned => partitioned, :clean_partition => is_free}
        scan_for_other_uses(hash)
        hash[:raid] = `cat /proc/mdstat | grep "#{hash[:name].split('/').last}"`.chomp.split.first
        disks[name] = hash
      end
    end
    disks
  end

  def self.scan_for_other_uses(hash)
  end

  def self.used_for_system
    self.disk_usage("on / ")
  end

  def is_system_disk?
    system "mount | grep '#{name}.*on / '"
  end

  def self.used_for_cache
    hash = {:raid => nil, :devices => []}
    devs = self.disk_usage("/mnt/sequreisp/dev")
    if devs[:devices].empty?
      devs = self.disk_usage("/mnt/cache")
      devs = self.disk_usage("/mnt/cache/web") if devs[:devices].empty?
      hash[:devices] = devs[:devices]
    else
      devs[:devices].each do |dev|
        hash[:devices] << dev if File.directory?("/mnt/sequreisp#{dev}/squid")
      end
    end
    hash[:raid] = devs[:raid]
    hash
  end

  def self.disk_usage(command)
    hash = {:raid => nil, :devices => []}
    IO.popen("mount | grep '#{command}'", "r") do |io|
      io.each do |line|
        device = line.chomp.split(" ").first
        if device.include?("md")
          hash[:raid] = device
          IO.popen("cat /proc/mdstat | grep #{device.split("/").last}", "r") do |io|
            io.each do |line|
              _cache_disks = line.chomp.split(" ")
              _cache_disks[4.._cache_disks.count].each do |disk|
                hash[:devices] << "/dev/#{disk[0..2]}"
              end
            end
          end
        else
          hash[:devices] << device.delete(device.last)
        end
      end
    end
    hash
  end

  def assigned_for(attr)
    self.free   = attr.include?(:free) ? true : false
    self.system = attr.include?(:system) ? true : false
    self.cache  = attr.include?(:cache) ? true : false
    self.save
  end

  def name_with_partition
    "#{name}1"
  end
  def mounting_point
    "/mnt/sequreisp#{name}"
  end
  def mount_and_add_to_fstab
    fstab_line = "#{name_with_partition} #{mounting_point} ext4 defaults 0 1"
    if system "grep '#{name_with_partition}' /etc/fstab"
      system "sed -i 's/^#{name_with_partition}.*/#{fstab_line}' /etc/fstab"
    else
      system "echo #{fstab_line} >> /etc/fstab"
    end
    system "mkdir -p #{mounting_point}"
    system "mount #{name_with_partition}"
  end
  def do_prepare_disk_for_cache
    if system?
      system "mkdir -p /var/spool/squid"
      system "chown proxy.proxy -R /var/spool/squid"
    else
      system "mkdir -p #{mounting_point}/squid"
      system "chown proxy.proxy -R #{mounting_point}/squid"
    end
  end<commit_msg>changed scan method in model disk<commit_after>class Disk < ActiveRecord::Base

  include ModelsWatcher
  watch_fields :cache, :free
  watch_on_destroy

  named_scope :system, :conditions => {:system => true}
  named_scope :cache, :conditions => {:cache => true}
  named_scope :free, :conditions => {:free => true}
  named_scope :in_raid, :conditions => ["disks.raid IS NOT NULL AND disks.system = FALSE AND disks.cache = TRUE"]
  named_scope :raid_is, lambda {|raid| { :conditions => ["disks.raid = ?", raid]} }
  named_scope :cache_in_raid, :conditions => ["disks.raid != 'md0' and disks.raid != 'NULL'"]
  named_scope :prepared_for_cache, :conditions => 'prepare_disk_for_cache = 1'
  before_update :clean_cache, :if => "self.free_changed? and self.free"

  def is_mounted?
    system("mount | grep '#{name}' &>/dev/null")
  end
  def self.scan
    disks = {}
    aux =`sudo /usr/bin/lshw -C disk`.strip.split("*-")
    system_disks = used_for_system
    cache_disks = used_for_cache

    aux.each do |disk|
      if disk.include?("disk")
        name = ""
        capacity = ""
        serial = ""
        attributes = disk.split("  ")
        attributes.each do |attr|
          name = attr.chomp.split(":").last.strip if attr.include?("logical name")
          capacity = attr.chomp.split(":").last.split(" ").last.strip if attr.include?("size:")
          serial = attr.chomp.split(":").last.strip if attr.include?("serial")
        end
        is_system = system_disks[:devices].include?(name) ? true : false
        is_cache = cache_disks[:devices].include?(name) ? true : false
        #which_raid = system_disks[:raid] if is_system
        #which_raid = cache_disks[:raid] if is_cache
        is_free = is_system or is_cache ? false : true
        partitioned =  is_free ? false : true
        hash = {:name => name, :capacity => capacity, :serial => serial, :system => is_system, :cache => is_cache, :free => is_free, :partitioned => partitioned, :clean_partition => is_free}
  #      scan_for_other_uses(hash)
        hash[:raid] = `cat /proc/mdstat | grep "#{hash[:name].split('/').last}"`.chomp.split.first
        disks[name] = hash
      end
    end
    disks
  end

  def self.scaan
    command_disks = `sudo /usr/bin/lshw -C disk`.strip.split("*-")
    command_disks.each do |c_disk|
      if c_disk.include?("disk")
        attributes = c_disk.split("  ")
        attributes.each do |attr|
          name = attr.chomp.split(":").last.strip if attr.include?("logical name")
          capacity = attr.chomp.split(":").last.split(" ").last.strip if attr.include?("size:")
          # serial = attr.chomp.split(":").last.strip if attr.include?("serial")
          if name.present?
            disk = Disk.create(:name => name, :capacity => capacity)
            disk.raid = disk.which_raid
            disk.is_used_for
            disk.save
          end
        end
      end
    end
  end

  def is_used_for
    if is_system_disk?
      system = true
      free = false
    end
    if is_cache_disk?
      cache = true
      free = false
    end
    free = true if free.nil?
  end

  def which_raid
    `cat /proc/mdstat | grep "#{self.logical_name}"`.split(" ").first
  end

  def is_system_disk?
    # system "mount | grep '#{raid.nil? ? name : raid}.*on / '"
    `mount | grep '#{raid.nil? ? name : raid}.*on / '`.present?
  end

  def is_cache_disk?
    is_cache = false
    #if configuration.first.transparent_proxy
      cache_dirs = `grep "^cache_dir*" /etc/squid/squid.conf`.split("\n")
      cache_dirs = `grep "^cache_dir*" /etc/squid/sequreisp.squid.conf`.split("\n") if cache_dirs.empty?
      cache_dirs.each do |cache_dir|
        dir = cache_dir.split(' ')[2]
        link = `readlink "#{dir}"`.chomp
        if link.present?
          dir = link
          dir = dir.split("/squid").first
          is_cache = true if `mount | grep "#{dir}"`.include?("#{raid.nil? ? name : raid}")
          # else
          # is_cache = true if system
        end
      end
    #end
    is_cache
  end

  def self.used_for_system
    self.disk_usage("on / ")
  end

  def self.used_for_cache
    hash = {:raid => nil, :devices => []}
    devs = self.disk_usage("/mnt/sequreisp/dev")
    if devs[:devices].empty?
      devs = self.disk_usage("/mnt/cache")
      devs = self.disk_usage("/mnt/cache/web") if devs[:devices].empty?
      hash[:devices] = devs[:devices]
    else
      devs[:devices].each do |dev|
        hash[:devices] << dev if File.directory?("/mnt/sequreisp#{dev}/squid")
      end
    end
    hash[:raid] = devs[:raid]
    hash
  end

  def self.disk_usage(command)
    hash = {:raid => nil, :devices => []}
    IO.popen("mount | grep '#{command}'", "r") do |io|
      io.each do |line|
        device = line.chomp.split(" ").first
        if device.include?("md")
          hash[:raid] = device
          IO.popen("cat /proc/mdstat | grep #{device.split("/").last}", "r") do |io|
            io.each do |line|
              _cache_disks = line.chomp.split(" ")
              _cache_disks[4.._cache_disks.count].each do |disk|
                hash[:devices] << "/dev/#{disk[0..2]}"
              end
            end
          end
        else
          hash[:devices] << device.delete(device.last)
        end
      end
    end
    hash
  end

  def assigned_for(attr)
    self.free   = attr.include?(:free) ? true : false
    self.system = attr.include?(:system) ? true : false
    self.cache  = attr.include?(:cache) ? true : false
    self.save
  end

  def logical_name
    name.split("/").last
  end

  def name_with_partition
    "#{name}1"
  end
  def mounting_point
    "/mnt/sequreisp#{name}"
  end
  def mount_and_add_to_fstab
    fstab_line = "#{name_with_partition} #{mounting_point} ext4 defaults 0 1"
    if system "grep '#{name_with_partition}' /etc/fstab"
      system "sed -i 's/^#{name_with_partition}.*/#{fstab_line}' /etc/fstab"
    else
      system "echo #{fstab_line} >> /etc/fstab"
    end
    system "mkdir -p #{mounting_point}"
    system "mount #{name_with_partition}"
  end
  def do_prepare_disk_for_cache
    if system?
      system "mkdir -p /var/spool/squid"
      system "chown proxy.proxy -R /var/spool/squid"
    else
      system "mkdir -p #{mounting_point}/squid"
      system "chown proxy.proxy -R #{mounting_point}/squid"
    end
  end
{'fraction_non_alphanumeric': 0.1499261447562777, 'fraction_numerical': 0.0017540620384047267, 'mean_word_length': 2.563486842105263, 'pattern_counts': {'":': 9, '<': 17, '<?xml version=': 0, '>': 57, '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': '2311532', 'n_tokens_mistral': 3734, 'n_tokens_neox': 3628, 'n_words': 824}