Class: GRI::Page
- Includes:
- FormatHelper, Utils
- Defined in:
- lib/gri/page.rb
Constant Summary collapse
- TZS =
[['', 'localtime'], ['JST-9', 'JST-9'], ['EST5EDT', 'EST5EDT'], ['PST8PDT', 'PST8PDT'], ['Europe/London', 'Europe/London'], ['Europe/Amsterdam', 'Europe/Amsterdam'], ['Singapore', 'Singapore']]
Constants included from FormatHelper
FormatHelper::HTML_ESCAPE, FormatHelper::HTML_ESCAPE_ONCE_REGEXP
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(options = {}) ⇒ Page
constructor
A new instance of Page.
- #mk_graph_tag(stime, etime, rs, params) ⇒ Object
- #mk_graph_tag_r(stime, etime, rs, params) ⇒ Object
- #mk_graph_tag_s(param_str, gidx = nil) ⇒ Object
- #mk_page_title(dirs, rs, params) ⇒ Object
- #mk_param_str(stime, etime, rs, ds, params) ⇒ Object
- #parse_request(req) ⇒ Object
- #parse_tstr(tstr) ⇒ Object
- #template ⇒ Object
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 = {}) ⇒ Page
Returns a new instance of Page.
13 14 15 |
# File 'lib/gri/page.rb', line 13 def initialize ={} @options = end |
Instance Method Details
#call(env) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/gri/page.rb', line 167 def call env req = GRI::Request.new env ENV['TZ'] = Config['tz'] if Config['tz'] ENV['TZ'] = req.params['tz'].to_s unless req.params['tz'].blank? stime, etime, params = parse_request req cs = stime.strftime '%Y-%m-%d %H:%M:%S' ce = etime.strftime '%Y-%m-%d %H:%M:%S' if ( = params.getvar('tag')) and (grp = params['grp']) gra_dir, = @options[:dirs] rrdpaths = .inject([]) {|a, tag| a += Dir.glob("#{gra_dir}/#{grp}/#{tag}/*.rrd") } r = File.basename rrdpaths.first else r = params['r'] || '' end host, @data_name, = parse_host_key r rs = params.getvar('r') || [] @title, body_title, headlegend = mk_page_title @options[:dirs], rs, params defs_term = DEFS[:term] sym = params['tm'].blank? ? '' : params['tm'].intern terms = defs_term[sym] || defs_term[:default] body = render(Grapher.layout) {render template, binding} [200, {'Content-type' => 'text/html'}, [body]] end |
#mk_graph_tag(stime, etime, rs, params) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gri/page.rb', line 86 def mk_graph_tag stime, etime, rs, params if params['p'] == 't' if ( = params.getvar 'tag') .map {|tag| mk_graph_tag_r stime, etime, [], params }.join("\n") else rs.map {|rname| mk_graph_tag_r stime, etime, [rname], params }.join("\n") end else mk_graph_tag_r stime, etime, rs, params end end |
#mk_graph_tag_r(stime, etime, rs, params) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gri/page.rb', line 102 def mk_graph_tag_r stime, etime, rs, params specs = DEFS.get_specs @data_name if (gr_specs = specs[:graph]) and gr_specs.size >= 1 if params['p'] == 's' or params['p'] == 'v' (0..gr_specs.size-1).map {|gidx| gr_spec = gr_specs[gidx] pickup_re = gr_spec[3] dss = specs[:ds] || [] dss = dss.grep pickup_re if pickup_re dss = dss.map {|ds| ds.split(',', 3)[1]} dss.map {|ds| param_str = mk_param_str stime, etime, rs, ds, params mk_graph_tag_s param_str, gidx }.join '' }.join '' else param_str = mk_param_str stime, etime, rs, nil, params (0..gr_specs.size-1).map {|gidx| mk_graph_tag_s param_str, gidx}.join '' end end end |
#mk_graph_tag_s(param_str, gidx = nil) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/gri/page.rb', line 125 def mk_graph_tag_s param_str, gidx=nil param_str += "&g=#{gidx}" if gidx name = ENV['SCRIPT_NAME'] || '' "<a href=\"#{name}/#{h param_str}\">" + "<img ismap src=\"#{name}?#{h param_str}\"></a><br/>" end |
#mk_page_title(dirs, rs, params) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 62 63 64 65 66 67 68 69 70 |
# File 'lib/gri/page.rb', line 17 def mk_page_title dirs, rs, params jstr = (params['p'] == 't' or params['p'] == 'v') ? ', ' : ' + ' descrs = [] headlegend = nil if ( = params.getvar('tag')) title = .compact.join jstr return title, title, headlegend end hosthash = {} for rname in rs host, key = rname.split('_', 2) hosthash[host] = true dir, records = search_records dirs, host if records and (prop = get_prop records[key]) xhost = prop[:description] ? nil : host descr = (prop[:description] || prop[:name] || '?').to_s url = if params['grp'].blank? url_to("?r=#{rname}") else url_to("?grp=#{params['grp']}&r=#{rname}") end descrs.push [url, xhost, descr] unless headlegend or params['grp'] ub = prop[:ub] if descr or ub name = prop[:name].to_s url = url_to("#{host}") headlegend = "<a href=\"#{h url}\">#{h host}</a>" headlegend << ' ' + name if descr #headlegend << ", MAX: #{to_scalestr ub, 1000}" end end end end html_title = descrs.map {|url, xhost, descr| if xhost and hosthash.size > 1 xhost + ':' + descr else descr end }.join(jstr) body_title = descrs.map {|url, xhost, descr| if xhost and hosthash.size > 1 "<a href=\"#{h url}\">#{h xhost}:#{h descr}</a>" else "<a href=\"#{h url}\">#{h descr}</a>" end }.join(jstr) return html_title, body_title, headlegend end |
#mk_param_str(stime, etime, rs, ds, params) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gri/page.rb', line 72 def mk_param_str stime, etime, rs, ds, params res = rs.map {|r| "r=#{u r}"} res << "grp=#{u params['grp']}" if params['grp'] ( = params.getvar('tag')) && .each {|tag| res << "tag=#{u tag}"} res << "stime=#{stime.to_i}" res << "etime=#{etime.to_i}" if etime.to_i.nonzero? res << "z=#{u params['z']}" res << "tz=#{u params['tz']}" res << "y=#{u params['y']}" res << "p=#{u params['p']}" if params['p'] res << "ds=#{ds}" if ds res.join('&') end |
#parse_request(req) ⇒ Object
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 162 163 164 165 |
# File 'lib/gri/page.rb', line 136 def parse_request req now = Time.now if @options[:clicked] s = req.path_info[1..-1] req.query_string = s params = req.gparams z = params['z'] width, = GRI::Graph::SIZE[z] canvas_x_offset = (Config['canvas-x-offset'] || 75).to_i imgx = @options[:imgx].to_i - canvas_x_offset imgy = @options[:imgy].to_i stime = params['stime'].to_i etime = params['etime'].to_i etime = (now + etime).to_i if etime <= 0 stime = (etime + stime).to_i if stime <= 0 stime = Time.at stime time = stime + ((Time.at(etime) - stime) / width) * imgx stime = Time.local(time.year, time.mon, time.day) etime = stime + 24*3600 params['pt'] = 's' else params = req.gparams deftime = now - now.to_i % 60 stime = parse_tstr(params['cs']) || deftime - 7*24*3600 etime = parse_tstr(params['ce']) || deftime end return stime, etime, params end |
#parse_tstr(tstr) ⇒ Object
132 133 134 |
# File 'lib/gri/page.rb', line 132 def parse_tstr tstr (tstr and !tstr.empty?) ? Time.parse(tstr) : nil end |
#template ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/gri/page.rb', line 203 def template <<'EOS' <span class="large"><%= body_title %></span><br/> <% if params['z'] != 'll' and rs.size == 1 and headlegend and headlegend.size > 0 -%> <span class="small"><%= headlegend %></span><br/> <% end -%> <br/> <form enctype="application/x-www-form-urlencoded" method="get" action="<%= url_to '?' %>"> <% rs.each {|r| -%><%= hidden 'r', r %><% } -%> <% tags and tags.each {|tag| -%><%= hidden 'tag', tag %><% } -%> <% if params['grp'] then %><%= hidden 'grp', params['grp'] %><% end %> <% tzs = TZS -%> <% (tzs.assoc(params['tz']) || tzs[0])[2] = true -%> TIMEZONE: <%= popup_menu('tz', nil, *tzs) %> <% nflag, uflag = (params['y'] == 'u') ? [false, true] : [true, false] -%> Y-axis scale: <%= radio_button 'y', 'a', nflag %>auto <%= radio_button 'y', 'u', uflag %>upper limit<br/> <%= check_box 'pt', 's', (params['pt'] == 's') %> <nobr> from <%= text_field 'cs', cs, 20, 19, nil %> to <%= text_field 'ce', ce, 20, 19, nil %> </nobr> <% zs = [['ss', 'SS'], ['s', 'S'], ['m', 'M'], ['l', 'L'], ['ll', 'LL']] -%> <% (zs.assoc(params['z']) || zs[2])[2] = true -%> Graph size: <%= popup_menu('z', nil, *zs) %> <% tms = defs_term.sort_by {|k, v| v[0][1]}.map {|k,| [k.to_s, k.to_s]} -%> <% (tms.assoc(params['tm']) || tms[1])[2] = true -%> term: <%= popup_menu('tm', nil, *tms) %> <br/> <% if rs.size > 1 or grp -%> <% c_ary = [['', 'sum'], ['s', 'stack'], ['v', 'overlay'], ['t', 'tile']] -%> <% (c_ary.assoc(params['p']) || c_ary[0])[2] = true -%> Composite type: <%= popup_menu('p', nil, *c_ary) %><br/> <% end -%> <input class="btn btn-primary btn-sm" type="submit" value="submit"> </form> <% if params['pt'] == 's' -%> <%= mk_graph_tag stime.to_i, etime.to_i, rs, params %> <% else -%> <% for label, int in terms -%> <hr/><p> <strong><%=h label %> Graph</strong><br/> <%= mk_graph_tag -int, 0, rs, params %> </p> <% end -%> <% end -%> <hr/><!--%= RUBY_VERSION %--> EOS end |