Class: HQ::GrapherWeb::Handler

Inherits:
Object
  • Object
show all
Includes:
Tools::Escape
Defined in:
lib/hq/grapher-web/handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_elem) ⇒ Handler

Returns a new instance of Handler.



38
39
40
# File 'lib/hq/grapher-web/handler.rb', line 38

def initialize config_elem
	@config_elem = config_elem
end

Class Method Details

.get_provider(app_elem) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hq/grapher-web/handler.rb', line 9

def self.get_provider app_elem

	config_elem =
		app_elem.find_first "config"

	grapher_config_path =
		config_elem.attributes["path"]

	grapher_config_doc =
		XML::Document.file grapher_config_path

	grapher_config_elem =
		grapher_config_doc.root

	return proc do
		|env, params|

		handler =
			Handler.new \
				grapher_config_elem

		handler.handle \
			env,
			params

	end

end

Instance Method Details

#do_graph(graph_spec) ⇒ Object



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
162
163
164
165
166
# File 'lib/hq/grapher-web/handler.rb', line 115

def do_graph graph_spec

	require "RRD"

	Tempfile.open "mandar-rrd-" do
		|tmp|

		args = [
			tmp.path,
			"--start", graph_spec[:start],
			"--end", graph_spec[:end],
			"--width", graph_spec[:width],
			"--height", graph_spec[:height],
			"--slope-mode",
			"--rigid",
		]

		args += graph_spec[:data].map {
			|data|
			[
				"DEF",
				"#{data[:name]}=#{data[:source_file]}",
				data[:source_name],
				data[:source_function].upcase,
			].join(":")
		}

		args += graph_spec[:calc].map {
			|calc|
			"CDEF:#{calc[:name]}=#{calc[:rpn]}"
		}

		args += graph_spec[:outputs].map {
			|output|
			(
				[
					output[:type].upcase,
					"#{output[:data]}\##{output[:colour]}",
					output[:label],
				] + (output[:stack] ? [ "STACK" ] : [])
			).join(":")
		}

		args.map! { |arg| arg.to_s }

		RRD.graph *args

		return File.read tmp.path

	end

end

#handle(env, params) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hq/grapher-web/handler.rb', line 42

def handle env, params

	graph_name = params[:name]
	scale_name = params[:scale]

	rrd_database =
		@config_elem.attributes["database-path"]

	graph_elem =
		@config_elem.find_first("
			graph [@name = #{esc_xp graph_name}]
		")

	template_name =
		graph_elem.attributes["template"]

	template_elem =
		@config_elem.find_first("
			template [@name = #{esc_xp template_name}]
		")

	scale_elem =
		@config_elem.find_first("
			scale [@name = #{esc_xp scale_name}]
		")

	scale_steps = scale_elem.attributes["steps"].to_i
	scale_rows = scale_elem.attributes["rows"].to_i

	graph_spec = {
		:start => Time.now.to_i - scale_steps * scale_rows,
		:end => Time.now.to_i,
		:width => scale_rows,
		:height => 400,
		:data => template_elem.find("data").map { |data_elem|
			{
				:name => data_elem.attributes["name"],
				:source_file => "#{rrd_database}/#{graph_elem.attributes["source"]}.rrd",
				:source_name => data_elem.attributes["source"],
				:source_function => data_elem.attributes["function"],
			}
		},
		:calc => template_elem.find("calc").map { |calc_elem|
			{
				:name => calc_elem.attributes["name"],
				:rpn => calc_elem.attributes["rpn"],
			}
		},
		:outputs => template_elem.find("output").map { |output_elem|
			{
				:type => output_elem.attributes["type"].upcase,
				:data => output_elem.attributes["data"],
				:colour => output_elem.attributes["colour"],
				:label => output_elem.attributes["label"],
				:stack => output_elem.attributes["stack"] == "yes",
			}
		},
	}

	data = do_graph graph_spec

	headers = {
		"Content-Type" => "image/png"
	}

	body = [
		data
	]

	return [ 200, headers, body ]

end