Sha256: 5b8a7569a23ee3d861dd46dd4eba43c28ad0d7e95970bc64a600dcea1f6387ff

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

class Repository < ActiveRecord::Base
  belongs_to :network
  six_guid

  attr_accessor :ping_value

  def self.rank(pr = false)
    repos = self.find(:all)
    repos.each do |rep|
      puts "Processing #{rep.url}" if pr
      rep.ping_value = rep.ping!
      sleep 1
    end
    repos.sort!{|a, b| a.ping_value <=> b.ping_value }
    repos.each_with_index do |rep,index|
      rep.priority = index
      rep.save 
    end
    repos
  end


  def ping!
    logger.info "Handling #{self.address}"
    out = %x[ping -n 10 #{self.address}]
    vals = []
    out.each_line do |l|
      l[/=([0-9]*)ms/]
      if $1
        vals << $1.to_i
      end
    end
    logger.debug "Out: #{out}, Vals: #{vals}"
    bla = 0
    vals.each {|v| bla += v }
    bla == 0 ? 9999 : bla / vals.size
  end

  def ping
    SixUpdaterWeb.run_program("ping", SixUpdaterWeb::BASE_PATH, "-r 9 -n 30 -t #{self.address}")
  end

  def tracert
    SixUpdaterWeb.run_program("tracert", SixUpdaterWeb::BASE_PATH, "-d #{self.address}")
  end

  def address
    self.url.sub("rsync://", "")
  end

  def to_label
    "#{self.url}"
  end

  def to_updater_yml
    self.url
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
six-updater-web-0.23.3 lib/six-updater-web/app/models/repository.rb