Class: Repository

Inherits:
ActiveRecord::Base show all
Defined in:
lib/six-updater-web/app/models/repository.rb

Constant Summary collapse

PING_PACKET_SIZE =
1372

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#associated_valid?, #no_errors_in_associated?, #save_associated, #save_associated!, #save_with_unsaved_flag, #unsaved=, #unsaved?

Instance Attribute Details

#ping_valueObject

Returns the value of attribute ping_value.



5
6
7
# File 'lib/six-updater-web/app/models/repository.rb', line 5

def ping_value
  @ping_value
end

Class Method Details

.rank(pr = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/six-updater-web/app/models/repository.rb', line 9

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

Instance Method Details

#addressObject



52
53
54
# File 'lib/six-updater-web/app/models/repository.rb', line 52

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

#pingObject



44
45
46
# File 'lib/six-updater-web/app/models/repository.rb', line 44

def ping
  SixUpdaterWeb.run_program("ping", SixUpdaterWeb::BASE_PATH, "-f -l #{PING_PACKET_SIZE} -n 30 #{self.address}")
end

#ping!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/six-updater-web/app/models/repository.rb', line 28

def ping!
  logger.info "Handling #{self.address}"
  out = %x[ping -n 10 -f -l #{PING_PACKET_SIZE} #{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

#prioObject



24
25
26
# File 'lib/six-updater-web/app/models/repository.rb', line 24

def prio
  self.priority.nil? ? 9999 : self.priority
end

#to_labelObject



56
57
58
# File 'lib/six-updater-web/app/models/repository.rb', line 56

def to_label
  "#{self.url}"
end

#to_updater_ymlObject



60
61
62
# File 'lib/six-updater-web/app/models/repository.rb', line 60

def to_updater_yml
  self.url
end

#tracertObject



48
49
50
# File 'lib/six-updater-web/app/models/repository.rb', line 48

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