Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Commit 9263d1f

Browse files
committed
Merge branch 'master' into placeholders_graph_properties
Conflicts: README.md
2 parents f4bc9a1 + e6bca76 commit 9263d1f

File tree

6 files changed

+147
-12
lines changed

6 files changed

+147
-12
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ A external properties files can be also loaded from the url:
238238

239239
http://graphite.example.net:3000/category_name/dash_name/?include_properties=white-theme.yml
240240

241+
Special properties when printing?
242+
---------------------------------
243+
244+
When printing these properties will be overrided:
245+
246+
:graph_properties:
247+
:background_color: white
248+
:foreground_color: black
249+
250+
You can create an optional YAML file _templatedir/print.yml_ that will be loaded when printing.
251+
This way you can override additional properties or use custom colors for printing.
252+
241253
Placeholder Parameters
242254
----------------------
243255

lib/gdash/dashboard.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ def initialize(short_name, graph_templates, category, options={}, graphite_rende
4040
yaml_file = File.join(graph_templates, property_file)
4141
if File.exist?(yaml_file)
4242
@properties.rmerge!(YAML.load_file(yaml_file))
43-
else
44-
raise "Missing file #{yaml_file}' for include_properties in #{File.join(directory, 'dash.yaml')}"
4543
end
4644
end
47-
45+
4846
# Properties defined in dashboard config file are overridden when given on initialization
47+
@properties.rmerge!(options)
4948
@properties[:graph_width] = options.delete(:width) || graph_width
5049
@properties[:graph_height] = options.delete(:height) || graph_height
5150
@properties[:graph_from] = options.delete(:from) || graph_from

lib/gdash/sinatra_app.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ def initialize(graphite_base, graph_templates, options = {})
7373

7474
get '/:category/:dash/details/:name/?*' do
7575
options = {}
76-
76+
if query_params[:print]
77+
options[:include_properties] = "print.yml"
78+
options[:graph_properties] = {
79+
:background_color => "white",
80+
:foreground_color => "black"
81+
}
82+
end
7783
options.merge!(query_params)
7884

7985
if @top_level["#{params[:category]}"].list.include?(params[:dash])
@@ -98,7 +104,11 @@ def initialize(graphite_base, graph_templates, options = {})
98104
@error = "No such graph available"
99105
end
100106

101-
erb :detailed_dashboard
107+
if !query_params[:print]
108+
erb :detailed_dashboard
109+
else
110+
erb :print_detailed_dashboard, :layout => false
111+
end
102112
end
103113

104114
get '/:category/:dash/full/?*' do
@@ -115,8 +125,6 @@ def initialize(graphite_base, graph_templates, options = {})
115125
options[:height] = @graph_height
116126
end
117127

118-
options.merge!(query_params)
119-
120128
if @top_level["#{params[:category]}"].list.include?(params[:dash])
121129
@dashboard = @top_level[@params[:category]].dashboard(params[:dash], options)
122130
else
@@ -138,6 +146,13 @@ def initialize(graphite_base, graph_templates, options = {})
138146
options[:until] = params["splat"][2] || "now"
139147
end
140148

149+
if query_params[:print]
150+
options[:include_properties] = "print.yml"
151+
options[:graph_properties] = {
152+
:background_color => "white",
153+
:foreground_color => "black"
154+
}
155+
end
141156
options.merge!(query_params)
142157

143158
if @top_level["#{params[:category]}"].list.include?(params[:dash])
@@ -148,7 +163,11 @@ def initialize(graphite_base, graph_templates, options = {})
148163

149164
@graphs = @dashboard.graphs(options)
150165

151-
erb :dashboard
166+
if !query_params[:print]
167+
erb :dashboard
168+
else
169+
erb :print_dashboard, :layout => false
170+
end
152171
end
153172

154173
get '/docs/' do
@@ -178,10 +197,19 @@ def query_params
178197

179198
hash
180199
end
200+
181201
def query_alias_map(k)
182202
q_aliases = {'p' => 'placeholders'}
183203
q_aliases[k] || k
184204
end
205+
206+
def link_to_print
207+
uri = URI.parse(request.path)
208+
new_query_ar = URI.decode_www_form(request.query_string) << ["print", "1"]
209+
uri.query = URI.encode_www_form(new_query_ar)
210+
uri.to_s
211+
end
212+
185213
end
186214
end
187215
end

views/layout.erb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@
2727
</li>
2828
<% end %>
2929
</ul>
30-
<ul class="nav pull-right">
31-
<li><a href="<%= @graphite_base %>">Data Browser</a></li>
32-
<li><a href="<%= @prefix %>/docs/">Docs</a></li>
33-
</ul>
30+
<ul class="nav secondary-nav">
31+
<li class="dropdown">
32+
<a href="#" class="dropdown-toggle">Menu</a>
33+
<ul class="dropdown-menu">
34+
<li><a href="<%= @graphite_base %>">Data Browser</a></li>
35+
<li class="divider"></li>
36+
<li><a href="<%= @prefix %>/docs/">GDash Docs</a></li>
37+
<li><a href="http://graphite.readthedocs.org/en/1.0/functions.html">Graphite Function Reference</a></li>
38+
<% if @dashboard %>
39+
<li class="divider"></li>
40+
<li><a href="<%= link_to_print %>" target="_blank">Print this page</a></li>
41+
<% end %>
42+
</ul>
43+
</li>
44+
</ul>
45+
</div>
3446
</div>
3547
</div>
3648
</div>

views/print_dashboard.erb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<html>
2+
<head>
3+
<script src="<%= @prefix %>/js/jquery-1.7.2.min.js"></script>
4+
<script language=Javascript>
5+
window.print();
6+
ClosePrint();
7+
function ClosePrint( )
8+
{
9+
setTimeout("window.close();",1000);
10+
}
11+
</script>
12+
</head>
13+
<% if @error %>
14+
<h2><%= @error %></h2>
15+
<% else %>
16+
<body bgcolor="white">
17+
<h1><%= @dashboard.name %>&nbsp;<small><%= @dashboard.description %></small></h1>
18+
19+
<table width="100%" height="100%">
20+
<% @graphs.in_groups_of(@graph_columns) do |graphrows| %>
21+
<tr>
22+
<% graphrows.each do |graph| %>
23+
<td valign="middle" align="center">
24+
<% if graph %>
25+
<img src='<%= [@top_level[@params[:category]].graphite_render, graph[:graphite][:url]].join "?" %>'>
26+
<% end %>
27+
</td>
28+
<% end %>
29+
</tr>
30+
<% end %>
31+
</table>
32+
<script>
33+
$(document).ready(function() {
34+
setInterval(reloadDash, <%= @refresh_rate * 1000 %>);
35+
});
36+
function reloadDash() {
37+
var now = new Date();
38+
$('img').each(function(index) {
39+
var url = $(this).attr('src').replace(/&\d+$/, '');
40+
$(this).attr('src', url + '&' + now.getTime());
41+
});
42+
}
43+
</script>
44+
</body>
45+
<% end %>
46+
</html>

views/print_detailed_dashboard.erb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
<head>
3+
<script src="<%= @prefix %>/js/jquery-1.7.2.min.js"></script>
4+
<script language=Javascript>
5+
window.print();
6+
ClosePrint();
7+
function ClosePrint( )
8+
{
9+
setTimeout("window.close();",1000);
10+
}
11+
</script>
12+
</head>
13+
<body bgcolor="white">
14+
<% if @error %>
15+
<h2><%= @error %></h2>
16+
<% else %>
17+
<h1><%= @dashboard.name %>&nbsp;<small><%= @dashboard.description %></small></h1>
18+
<div class="row">
19+
<table>
20+
<% row = 1 %>
21+
<% @graphs.in_groups_of(@graph_columns) do |graphrows| %>
22+
<tr>
23+
<% graphrows.each do |graph| %>
24+
<td>
25+
<% if graph %>
26+
<%= erb(:graph, :locals => { :graph => graph, :omit_link => true }) %>
27+
<% end %>
28+
</td>
29+
<% end %>
30+
</tr>
31+
<% row += 1 %>
32+
<% end %>
33+
</table>
34+
</div>
35+
<% end %>
36+
</body>
37+
</html>
38+

0 commit comments

Comments
 (0)