Class: GRI::DSList

Inherits:
Object show all
Includes:
FormatHelper, Utils
Defined in:
lib/gri/ds_list.rb

Constant Summary collapse

SORT_PROC =
{
  :by_name=>Proc.new {|a, b|
    (cmp = a.first.to_s.gsub(/(\d+)/) {"%06d"%$1.to_i} <=>
     b.first.to_s.gsub(/(\d+)/) {"%06d"%$1.to_i}).zero? ?
    a.first <=> b.first : cmp
  }
}
CMNAME =
{'s'=>'stack', 'v'=>'overlay', ''=>'sum', 't'=>'tile'}

Constants included from FormatHelper

FormatHelper::HTML_ESCAPE, FormatHelper::HTML_ESCAPE_ONCE_REGEXP

Instance Method Summary collapse

Methods included from FormatHelper

#check_box, #escape_once, #h, #hidden, #mk_query, #mk_tag, #popup_menu, #radio_button, #render, #td, #text_field, #to_scalestr, #u, #url_to

Methods included from Utils

get_prop, key_encode, load_records, parse_host_key, parse_key, search_records, update_ltsv_file, url_encode

Constructor Details

#initialize(options = {}) ⇒ DSList

Returns a new instance of DSList.



17
18
19
# File 'lib/gri/ds_list.rb', line 17

def initialize options={}
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gri/ds_list.rb', line 21

def call env
  req = Rack::Request.new env
  dirs = @options[:dirs]

  list_item = env['PATH_INFO'] || ''
  host = File.basename list_item
  gra_dir, records = search_records dirs, host
  return [404, {}, ['Not Found']] unless gra_dir
  dir = gra_dir + list_item
  sysinfo = records['SYS'] || {}
  data_hash = get_data_hash records
  data_names = data_hash.keys.sort

  @title = host
  body = render(Grapher.layout) {render template, binding}
  [200, {'Content-type' => 'text/html'}, [body]]
end

#format_cell(f, prop, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gri/ds_list.rb', line 106

def format_cell f, prop, options={}
  str = f.gsub(/%([\.\/a-z\d][\.\/A-Za-z_\d]*)?([%A-Z])/) {
    case $2
    when 'D'
      prop[:description]
    when 'N'
      name = prop[:name]
      (href = options[:href]) ? "<a href=\"#{h href}\">#{h name}</a>" : name
    when 'I'
      addrs = (prop[:ipaddr] || '').split(',').map {|item|
        if item =~ %r{^(\d+\.\d+\.\d+\.\d+)/(\d+\.\d+\.\d+\.\d+)}
          ipaddr = $1
          int, = $2.split('.').map {|s|s.to_i}.pack('C4').unpack('N')
          mask = 32 - Integer(Math.log((int ^ 0xffffffff) + 1) / Math.log(2))
          ipaddr + '/' + mask.to_s
        else
          item
        end
      }.join('</br>')
    when 'L'
      if $1
        base, mag = $1.split('/')
        base = base.to_i
      end
      #mag = (mag || 1).to_f
      to_scalestr(prop[:lastvalue], base)
    when 'S'
      (prop[:astatus].to_i == 2) ? 'AdminDown' :
        ((prop[:ostatus].to_i == 2) ? 'Down' : '')
    when 'U'
      to_scalestr(prop[:ub], $1.to_i)
    else
      ''
    end
  }
  td str, :class=>options[:class]
end

#format_data_list(dir, data_name, items) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gri/ds_list.rb', line 59

def format_data_list dir, data_name, items
  host = File.basename dir
  specs = DEFS.get_specs data_name
  hdstr, pat = specs[:list] || specs[:list_text]
  hds = (Array === hdstr) ? hdstr : hdstr.split(',')
  formats = (pat || '%N').split(',')

  p0 = ((hds.size == formats.size) ?
        [hds.map {|hd| td(h(hd), :head=>true)}] :
        [[td(h(hds.join('.')), :head=>true, :colspan=>formats.size)]])
  ckeys = []
  p1 = items.sort(&SORT_PROC[:by_name]).map {|key, prop|
    host_key = "#{host}_#{key}"
    href = File.exist?("#{dir}/#{host_key}.rrd") ?
    (ckeys.push key; url_to("?r=#{host_key}")) : nil
    format_tr formats, prop, :href=>href
  }
  if specs[:composite] and p1.size > 1
    links = mk_comp_links specs[:composite], host, ckeys
    p1.push [td(links.join(' | '), :colspan=>formats.size)]
  end
  p0 + p1
end

#format_tr(formats, prop, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gri/ds_list.rb', line 94

def format_tr formats, prop, options={}
  formats.map {|format|
    format = format.gsub(/\\(\w)/) {
      case $1
      when 'r'; options[:class] = 'text-right'
      end
      ''
    }
    format_cell format, prop, options
  }
end

#get_data_hash(records) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gri/ds_list.rb', line 39

def get_data_hash records
  h = {}
  for key, record in records
    next if !key or key == 'SYS'
    data_name, index = parse_key key
    next unless data_name
    specs = DEFS.get_specs data_name
    next unless specs and (specs[:list] or specs[:list_text])
    next if specs[:hidden?] and specs[:hidden?].call(record)
    record['_index'] = index; record[:key] = index
    if specs[:prop]
      specs[:prop].each {|k1, k2| record[k1] = record[k2] if record[k2]}
      record[:name] ||= specs[:prop][:name]
    end
    h[data_name] ||= []
    h[data_name] << [key, record, nil]
  end
  h
end


84
85
86
87
88
89
90
91
92
# File 'lib/gri/ds_list.rb', line 84

def mk_comp_links comps, host, ckeys
  links = []
  for cm in comps
    href_fp = url_to "?p=#{cm}&"
    href = href_fp + ckeys.map {|k| "r=#{host}_#{k}"}.join('&')
    links.push mk_tag('a', {:href=>href}, CMNAME[cm])
  end
  links
end

#templateObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gri/ds_list.rb', line 144

def template
  <<'EOS'
<span class="large"><%=h host %></span>
<small><%=h sysinfo['_sysdescr'] || '' %></small><br/>
<hr/>

<table>
<% for data_name in data_names -%>
<tr><td>
<table class="table ds" width="100%">
<% for row in format_data_list dir, data_name, data_hash[data_name] -%>
  <tr>
<%= row.join('') %>
  </tr>
<% end -%>
</table>
</td></tr>
<% end -%>
</table>
EOS
end