Class: Wobble::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/wobble/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Tracker

Returns a new instance of Tracker.



11
12
13
14
# File 'lib/wobble/tracker.rb', line 11

def initialize(host)
  @host = Nokogiri::HTML(Net::HTTP.get(URI(host)))
  @xpath_prefix = "//html/body/table[@class='datatable']"
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/wobble/tracker.rb', line 7

def host
  @host
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/wobble/tracker.rb', line 8

def status
  @status
end

#xpath_prefixObject

Returns the value of attribute xpath_prefix.



9
10
11
# File 'lib/wobble/tracker.rb', line 9

def xpath_prefix
  @xpath_prefix
end

Instance Method Details

#completed_jobsObject



47
48
49
# File 'lib/wobble/tracker.rb', line 47

def completed_jobs
  find_by_table_index 2
end

#edit_id(id) ⇒ Object



93
94
95
# File 'lib/wobble/tracker.rb', line 93

def edit_id(id)
  id.gsub(/job_\d+_/, '')
end

#edit_name(name) ⇒ Object



97
98
99
# File 'lib/wobble/tracker.rb', line 97

def edit_name(name)
  name.gsub(/\[.*\]|\/\(\d+\/\d+\)/, '').strip[0...40]
end

#edit_timestamp(id) ⇒ Object



87
88
89
90
91
# File 'lib/wobble/tracker.rb', line 87

def edit_timestamp(id)
  DateTime.parse(id.gsub(/job_/, '').gsub(/_\d+/, ''))
          .to_time
          .to_s
end

#failed_jobsObject



51
52
53
# File 'lib/wobble/tracker.rb', line 51

def failed_jobs
  find_by_table_index 3
end

#find_by_column_index(index) ⇒ Object



55
56
57
# File 'lib/wobble/tracker.rb', line 55

def find_by_column_index(index)
  find_jobs.xpath("td[#{index}]").map { |c| c.content }.reject(&:empty?)
end

#find_by_table_index(table_index = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/wobble/tracker.rb', line 33

def find_by_table_index(table_index=nil)
  if table_index
    table = host.xpath("#{xpath_prefix}[#{table_index}]/tbody")
  else
    table = host.xpath("#{xpath_prefix}/tbody")
  end

  table.first.search('tr')
end

#find_jobsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wobble/tracker.rb', line 20

def find_jobs
  case status
  when 'running'
    running_jobs
  when 'completed'
    completed_jobs
  when 'failed'
    failed_jobs
  else
    find_by_table_index
  end
end

#idsObject



63
64
65
# File 'lib/wobble/tracker.rb', line 63

def ids
  find_by_column_index(1).map { |name| edit_id name }
end

#jobsObject



16
17
18
# File 'lib/wobble/tracker.rb', line 16

def jobs
  ids.zip(timestamps, priorities, users, names, mapped, reduced)
end

#mappedObject



79
80
81
# File 'lib/wobble/tracker.rb', line 79

def mapped
  find_by_column_index 5
end

#namesObject



75
76
77
# File 'lib/wobble/tracker.rb', line 75

def names
  find_by_column_index(4).map { |name| edit_name name }
end

#prioritiesObject



67
68
69
# File 'lib/wobble/tracker.rb', line 67

def priorities
  find_by_column_index 2
end

#reducedObject



83
84
85
# File 'lib/wobble/tracker.rb', line 83

def reduced
  find_by_column_index 8
end

#running_jobsObject



43
44
45
# File 'lib/wobble/tracker.rb', line 43

def running_jobs
  find_by_table_index 1
end

#timestampsObject



59
60
61
# File 'lib/wobble/tracker.rb', line 59

def timestamps
  find_by_column_index(1).map { |timestamp|  edit_timestamp timestamp }
end

#usersObject



71
72
73
# File 'lib/wobble/tracker.rb', line 71

def users
  find_by_column_index 3
end