<commit_before>class BackupServer < ActiveRecord::Base
  has_many :servers
  
  validates_presence_of :hostname, :zpool, :max_backups
  attr_accessor :in_subnet
  
  def self.available_for(server)
    list = nanite_query("/info/in_subnet?", server)
    recommended = []
    list.each_pair do | server, result |
      recommended.push server if result == true
    end
    available = all
    available.each do | backup_server |
      backup_server.in_subnet = false
      backup_server.in_subnet = true if recommended.include? backup_server.hostname
    end
    available
  end
  
  def to_s
    hostname
  end
  
  def do_nanite(action, payload)
    res = 'undef'
    return [1,'backup server was offline'] unless online?
    Nanite.request(action, payload, :target => "nanite-#{hostname}") do |result |
     key = "nanite-" + hostname
     puts result
     res = result[key]
    end
    while res == 'undef'
      puts "no result yet"
      sleep 0.1
    end
    return res
  end
  
  def self.nanite_query(action, payload)
    res = 'undef'
    servers = {}
    return servers if nanites.size == 0
    Nanite.request(action, payload, :selector => :all) do | result |
      result.each_pair do | key, value |
        # Strip the nanite- prefix in the hash key
        new_key = key.sub(/nanite-/,'')
        servers[new_key] = value
      end
      res = servers
    end
    while res == 'undef'
      sleep 0.1
    end
    return res
  end
  
  def nanites
    self.class.nanites
  end
  
  def online?
    nanites.include? hostname
  end
  
  def update_disk_space
    if online?
      Nanite.request('/zfs/disk_free', nil, :target => "nanite-#{hostname}") do |result |
       key = "nanite-" + hostname
       puts result
       res = result[key]
       self.disk_free = res
       self.save
      end
    end
  end
  
  def self.nanites
    return [] if Nanite.mapper.nil? or Nanite.mapper.cluster.nil?
    Nanite.mapper.cluster.nanites.map do | nanite |
      nanite[0].sub(/nanite-/,'')
    end
  end
  
  def self.array_to_models(arr)
    find(:all, :conditions => [ "hostname IN (?)", arr])<commit_msg>make sure to send the zpool too<commit_after>class BackupServer < ActiveRecord::Base
  has_many :servers
  
  validates_presence_of :hostname, :zpool, :max_backups
  attr_accessor :in_subnet
  
  def self.available_for(server)
    list = nanite_query("/info/in_subnet?", server)
    recommended = []
    list.each_pair do | server, result |
      recommended.push server if result == true
    end
    available = all
    available.each do | backup_server |
      backup_server.in_subnet = false
      backup_server.in_subnet = true if recommended.include? backup_server.hostname
    end
    available
  end
  
  def to_s
    hostname
  end
  
  def do_nanite(action, payload)
    res = 'undef'
    return [1,'backup server was offline'] unless online?
    Nanite.request(action, payload, :target => "nanite-#{hostname}") do |result |
     key = "nanite-" + hostname
     puts result
     res = result[key]
    end
    while res == 'undef'
      puts "no result yet"
      sleep 0.1
    end
    return res
  end
  
  def self.nanite_query(action, payload)
    res = 'undef'
    servers = {}
    return servers if nanites.size == 0
    Nanite.request(action, payload, :selector => :all) do | result |
      result.each_pair do | key, value |
        # Strip the nanite- prefix in the hash key
        new_key = key.sub(/nanite-/,'')
        servers[new_key] = value
      end
      res = servers
    end
    while res == 'undef'
      sleep 0.1
    end
    return res
  end
  
  def nanites
    self.class.nanites
  end
  
  def online?
    nanites.include? hostname
  end
  
  def update_disk_space
    if online?
      Nanite.request('/zfs/disk_free', self.zpool, :target => "nanite-#{hostname}") do |result |
       key = "nanite-" + hostname
       puts result
       res = result[key]
       self.disk_free = res
       self.save
      end
    end
  end
  
  def self.nanites
    return [] if Nanite.mapper.nil? or Nanite.mapper.cluster.nil?
    Nanite.mapper.cluster.nanites.map do | nanite |
      nanite[0].sub(/nanite-/,'')
    end
  end
  
  def self.array_to_models(arr)
    find(:all, :conditions => [ "hostname IN (?)", arr])
{'fraction_non_alphanumeric': 0.10383800329644455, 'fraction_numerical': 0.0032964445490934777, 'mean_word_length': 2.4038461538461537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2982850', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1430, 'n_words': 371}