Class: GRI::Clist

Inherits:
Object show all
Includes:
FormatHelper
Defined in:
lib/gri/clist.rb

Constant Summary collapse

TERMS =
{'sh'=>[['sh', 's8h'], 'hour/8hours', 0],
'd'=>[['d', 'w'], 'day/week', 3],
'm'=>[['m', 'y'], 'month/year', 4]}
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

Constructor Details

#initialize(options = {}) ⇒ Clist

Returns a new instance of Clist.



12
13
14
# File 'lib/gri/clist.rb', line 12

def initialize options={}
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gri/clist.rb', line 16

def call env
  req = Rack::Request.new env
  if (path_info = env['PATH_INFO']) =~ %r{^/list}
    path_info = Regexp.last_match.post_match
  end
  if path_info
    dummy, service_name, section_name, graph_name = path_info.split /\// #/
  end

  list_item = path_info
  dir = @options[:dir] || Config['cast-dir'] ||
    (Config::ROOT_PATH + '/cast')

  pt = req.params['t'] || 'd'
  t = TERMS[pt] || TERMS['d']
  dirs = scan_dir dir, service_name, section_name, graph_name
  @title = 'list'
  body = render(Cast.layout) {render template, binding}
  [200, {'Content-type' => 'text/html'}, [body]]
end

#load_sysdb(dir) ⇒ Object



67
68
69
# File 'lib/gri/clist.rb', line 67

def load_sysdb dir
  sysdb = Marshal.load_from_file sysdb_path(dir) || {}
end


80
81
82
83
84
85
86
87
88
# File 'lib/gri/clist.rb', line 80

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

#mk_graph_tag(service_name, section_name, graph_name, t, ind) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/gri/clist.rb', line 71

def mk_graph_tag service_name, section_name, graph_name, t, ind
  cs = (Time.now - Cgraph::TERMS[t[0][ind]][1]).strftime('%Y-%m-%d %H:%M:%S')
  u = "?grp=#{service_name}&r=#{section_name}_num_#{graph_name}&pt=s"
  gu = "graph/#{service_name}/#{section_name}/#{graph_name}"
  img = "<img src=\"#{url_to gu + '?t=' + t[0][ind]}\"/>"
  "<a href=\"#{u}&cs=#{Rack::Utils.escape cs}\">#{img}</a>"
end

#scan_dir(dir, service_name, section_name, graph_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gri/clist.rb', line 37

def scan_dir dir, service_name, section_name, graph_name
  h = {}
  for f0 in Dir.glob(dir + '/' + (service_name || '*'))
    b0 = File.basename f0
    if File.directory? f0
      h[b0] = {}
      for f1 in Dir.glob(f0 + '/' + (section_name || '*'))
        b1 = File.basename f1
        if File.directory? f1
          h[b0][b1] = {}
          pat = graph_name ? "#{section_name}_num_#{graph_name}.rrd" : '*'
          for f2 in Dir.glob(f1 + '/' + pat)
            b2 = File.basename f2
            if b2  =~ /^(?:[^_]+)_(?:[^_]+)_(.+).rrd/
              h[b0][b1][$1] = true
            end
          end
        end
      end
    else
      h[b0] = true
    end
  end
  h
end

#sysdb_path(dir) ⇒ Object



63
64
65
# File 'lib/gri/clist.rb', line 63

def sysdb_path dir
  sysdb_path = dir + '/.sysdb/sysdb.dump'
end

#templateObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gri/clist.rb', line 90

def template
  <<'EOS'
<% if section_name -%>

<% d0 = dirs.keys.first -%>

<% if (ckeys = dirs[d0][section_name].keys.map {|k| "num_#{k}"}).size > 1 -%>
<div style="float: right!important;" class="pull-right"><ul class="pagination">
<% for u in mk_comp_links(DEFS['num'][:composite], section_name, ckeys, "grp=#{d0}&") -%>
<li><%= u %></li>
<% end -%>
</ul></div>
<% end -%>

<div style="float: right!important;" class="pull-right"><ul class="pagination">
<% for k, v in TERMS.sort_by {|kk, vv| vv[2]} -%>
  <li style="display: inline"<%= (pt == k) ? " class=active" : '' %>>
  <a href="<%= url_to "list/#{d0}/#{section_name}?t=#{k}" %>"><%= v[1] %></a>
  </li>
<% end -%>
</ul></div>


<section>
<h1>
<a href="<%= url_to ''%>">Home</a> &raquo;
 <a href="<%= url_to "list/#{d0}"%>"><%= d0 %></a> &raquo;
<% if graph_name -%>
 <a href="<%= url_to "list/#{d0}/#{section_name}"%>"><%= section_name%></a> &raquo;
 <%= graph_name %>
<% else -%>
 <%= section_name%>
<% end -%>
</h1>

<% for graph_name in dirs[d0][section_name].keys.sort -%>
<h2>
<a href="<%= url_to "list/#{d0}/#{section_name}/#{graph_name}"%>"><%= graph_name %></a>
</h2>
<%= mk_graph_tag service_name, section_name, graph_name, t, 0 %>
<%= mk_graph_tag service_name, section_name, graph_name, t, 1 %>
<% end %>
</section>

<% else -%><!-- if section_name -->

<section>
<h1>
<a href="<%= url_to ''%>">Home</a>
<% if service_name -%>
 &raquo; <%= service_name %>
<% end -%>
</h1>

<% for d0 in dirs.keys.sort %>
<h2><a href="<%= url_to('list') + '/' + d0 %>"><%=h d0 %>
</a></h2>
<!--a href="<%= url_to "?search=1&grp=#{d0}" %>">search</a-->
<table class="table table-striped">
<% for d1 in dirs[d0].keys.sort -%>
<tr>
  <td><a href="<%= url_to('list')+'/'+d0+'/'+d1 %>"><%= d1 %></a></td>
</tr>
<% end -%>
</table>
</section>
<% end %>

<% end -%><!-- if section_name -->
<hr/>
EOS
end