graphite_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "minitest", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ minitest (2.6.1)
10
+ rake (0.9.2)
11
+ rcov (0.9.10)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ minitest
20
+ rcov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Edward Muller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = graphite_builder
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to graphite_builder
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Edward Muller. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "graphite_builder"
18
+ gem.homepage = "http://github.com/freeformz/graphite_builder"
19
+ gem.license = "MIT"
20
+ gem.summary = %q{A DSL: for generating graphite graph urls.}
21
+ gem.description = %q{A DSL for generating graphite graph urls. Meant to enable copy-and-paste from the graphite UI + simple substitution.}
22
+ gem.email = "edward@heroku.com"
23
+ gem.authors = ["Edward Muller"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "graphite_builder #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{graphite_builder}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Edward Muller}]
12
+ s.date = %q{2011-10-11}
13
+ s.description = %q{A DSL for generating graphite graph urls. Meant to enable copy-and-paste from the graphite UI + simple substitution.}
14
+ s.email = %q{edward@heroku.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "graphite_builder.gemspec",
28
+ "lib/graphite_builder.rb",
29
+ "test/helper.rb",
30
+ "test/test_graphite_builder.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/freeformz/graphite_builder}
33
+ s.licenses = [%q{MIT}]
34
+ s.require_paths = [%q{lib}]
35
+ s.rubygems_version = %q{1.8.6}
36
+ s.summary = %q{A DSL: for generating graphite graph urls.}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<minitest>, [">= 0"])
43
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
44
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
45
+ s.add_development_dependency(%q<rcov>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<minitest>, [">= 0"])
48
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
50
+ s.add_dependency(%q<rcov>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<minitest>, [">= 0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,107 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+
4
+ module Graphite
5
+
6
+ # Encapsulates a small DSL for enabling the cut-n-paste of data from the graphite UI and interpolating/
7
+ # modifying them for inclusion in a server side template
8
+ class Builder
9
+
10
+ # Raised when no targets are defined for a graph and #render is called
11
+ class NoTargetsDefined < RuntimeError; end
12
+
13
+ # Raised when we don't understand the function signature
14
+ # i.e. the user did something wrong or we need to implement the
15
+ # specific function signature
16
+ class UnknownFunctionSignature < RuntimeError; end
17
+
18
+ def initialize(opts=nil, &block)
19
+ @args = {}
20
+ @targets = []
21
+ data opts
22
+ @args[:base_url] = @data.delete(:base_url) if @data[:base_url]
23
+ if block
24
+ self.instance_eval(&block)
25
+ end
26
+ end
27
+
28
+ def target(value)
29
+ @targets << value
30
+ end
31
+
32
+ def data(opts=nil)
33
+ case opts
34
+ when Hash, Array
35
+ @data = opts
36
+ when String, Symbol, Fixnum
37
+ @data[opts]
38
+ when NilClass
39
+ @data
40
+ else
41
+ raise RuntimeError.new("Unknown options: #{opts}")
42
+ end
43
+ end
44
+
45
+ def sumSeries(*args)
46
+ array_argument_wrapper('sumSeries', args)
47
+ end
48
+
49
+ def asPercent(is, of)
50
+ array_argument_wrapper('asPercent', is, of)
51
+ end
52
+
53
+ def secondYAxis(arg)
54
+ single_argument_wrapper('secondYAxis',arg)
55
+ end
56
+
57
+ def stacked(arg)
58
+ single_argument_wrapper('stacked',arg)
59
+ end
60
+
61
+ def method_missing(meth,*args,&block)
62
+ if args.length == 2
63
+ if meth == :legend
64
+ meth = :alias
65
+ end
66
+ quoted_array_argument_wrapper(meth, args)
67
+ elsif args.length == 1
68
+ @args[meth.to_sym] = args.first
69
+ else
70
+ raise ::Graphite::Builder::UnknownFunctionSignature.new("#{meth}(#{args.join(',')})")
71
+ end
72
+ end
73
+
74
+ def render
75
+ raise ::Graphite::Builder::NoTargetsDefined.new if @targets.empty?
76
+ '<img src="' <<
77
+ @args.delete(:base_url) << '?' <<
78
+ (@args.map do |k ,v|
79
+ if v.is_a?(Array)
80
+ v.map { |item| url_param(k, item) }.join("&")
81
+ else
82
+ url_param(k, v)
83
+ end
84
+ end + @targets.map {|target| "target=#{target}" }).join('&') << '"/>'
85
+ end
86
+
87
+ private
88
+
89
+ def single_argument_wrapper(meth, arg)
90
+ "#{meth}(#{arg})"
91
+ end
92
+
93
+ def array_argument_wrapper(meth, *args)
94
+ "#{meth}(#{args.join(",")})"
95
+ end
96
+
97
+ def quoted_array_argument_wrapper(meth, *args)
98
+ args = args[0] if args[0].is_a? Array
99
+ "#{meth}(#{args.shift},#{args.map { |a| "'#{CGI::escape(a.to_s)}'"}.join(',')})"
100
+ end
101
+
102
+ def url_param(param, value)
103
+ "#{URI.escape(param.to_s)}=#{CGI::escape(value.to_s)}"
104
+ end
105
+
106
+ end
107
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'minitest/spec'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'graphite_builder'
15
+
16
+ class MiniTest::Unit::TestCase
17
+ end
18
+
19
+ MiniTest::Unit.autorun
@@ -0,0 +1,182 @@
1
+ require 'helper'
2
+
3
+ describe Graphite::Builder do
4
+
5
+ describe 'when initialized with a block' do
6
+
7
+ describe '#render' do
8
+
9
+ describe 'when no targets are defined' do
10
+ it 'should raise a NoTargetsDefined error' do
11
+ Proc.new do
12
+ Graphite::Builder.new(base_url: 'http://foo.bar/render', foo: :bar) do
13
+ width 800
14
+ end.render
15
+ end.must_raise Graphite::Builder::NoTargetsDefined
16
+ end
17
+ end
18
+
19
+ describe 'when a target is defined' do
20
+
21
+ describe 'whithout using data' do
22
+ it 'should render the correct <img/> tag' do
23
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
24
+ target '1.2.3'
25
+ end.render.must_equal '<img src="http://localhost/render?target=1.2.3"/>'
26
+ end
27
+ end
28
+
29
+ describe 'using data' do
30
+ it 'should render the correct <img/> tag' do
31
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
32
+ target data :foo
33
+ target "#{data :foo}.stuff"
34
+ end.render.must_equal '<img src="http://localhost/render?target=bar&target=bar.stuff"/>'
35
+ end
36
+
37
+ describe 'setting params' do
38
+
39
+ it 'should render the correct <img/> tag' do
40
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
41
+ width 800
42
+ height 200
43
+ target 'a.b.c'
44
+ end.render.must_equal "<img src=\"http://localhost/render?width=800&height=200&target=a.b.c\"/>"
45
+ end
46
+ end
47
+
48
+ describe 'and applying a "function"' do
49
+ it 'should render the correct <img/> tag' do
50
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
51
+ target data :foo
52
+ target color("#{data :foo}.stuff", 'red')
53
+ end.render.must_equal "<img src=\"http://localhost/render?target=bar&target=color(bar.stuff,'red')\"/>"
54
+ end
55
+ end
56
+
57
+ describe 'and nesting functions' do
58
+ it 'should render the correct <img/> tag' do
59
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
60
+ target data :foo
61
+ target legend(color("#{data :foo}.stuff", 'red'), 'foozle')
62
+ end.render.must_equal "<img src=\"http://localhost/render?target=bar&target=alias(color(bar.stuff,'red'),'foozle')\"/>"
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe 'DSL functions' do
71
+
72
+ describe 'sumSeries' do
73
+
74
+ describe 'with a single data point' do
75
+ it 'should render the correct <img/> tag' do
76
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
77
+ target sumSeries('a.b.*')
78
+ end.render.must_equal "<img src=\"http://localhost/render?target=sumSeries(a.b.*)\"/>"
79
+ end
80
+ end
81
+
82
+ describe 'with multiple data points' do
83
+ it 'should render the correct <img/> tag' do
84
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
85
+ target sumSeries('a.b.*','a.c.*','a.d.*')
86
+ end.render.must_equal "<img src=\"http://localhost/render?target=sumSeries(a.b.*,a.c.*,a.d.*)\"/>"
87
+ end
88
+ end
89
+ end
90
+
91
+ describe 'asPercent' do
92
+
93
+ describe 'with a single argument' do
94
+ it 'should raise an ArgumentError' do
95
+ Proc.new do
96
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
97
+ target asPercent(1)
98
+ end.render
99
+ end.must_raise ArgumentError
100
+ end
101
+ end
102
+
103
+ describe 'with two arguments' do
104
+ it 'should render the correct <img/> tag' do
105
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
106
+ target asPercent('a.b.c','a.c.b')
107
+ end.render.must_equal "<img src=\"http://localhost/render?target=asPercent(a.b.c,a.c.b)\"/>"
108
+ end
109
+ end
110
+ end
111
+
112
+ describe 'secondYAxis' do
113
+
114
+ describe 'with a single argument' do
115
+ it 'should render the correct <img/> tag' do
116
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
117
+ target secondYAxis('a.b.c')
118
+ end.render.must_equal "<img src=\"http://localhost/render?target=secondYAxis(a.b.c)\"/>"
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ describe 'stacked' do
125
+
126
+ describe 'with a single argument' do
127
+ it 'should render the correct <img/> tag' do
128
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
129
+ target stacked('a.b.c')
130
+ end.render.must_equal "<img src=\"http://localhost/render?target=stacked(a.b.c)\"/>"
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ describe 'legend' do
137
+
138
+ describe 'with a single argument' do
139
+ it 'should render the correct <img/> tag' do
140
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
141
+ target legend('a.b.c','A B C')
142
+ end.render.must_equal "<img src=\"http://localhost/render?target=alias(a.b.c,'A+B+C')\"/>"
143
+ end
144
+ end
145
+
146
+ end
147
+
148
+ describe 'an unknown function' do
149
+
150
+ it 'should raise an UnknownFunctionSignature exception' do
151
+ Proc.new do
152
+ Graphite::Builder.new(base_url: 'http://localhost/render', foo: :bar) do
153
+ target blargen(1,2,3)
154
+ end.render
155
+ end.must_raise Graphite::Builder::UnknownFunctionSignature
156
+ end
157
+
158
+ end
159
+
160
+ end
161
+
162
+ describe "some complex graphs" do
163
+
164
+ it "should render the correct <img/> tag" do
165
+
166
+ Graphite::Builder.new(:hostname => 'foo') do
167
+ base_url 'http://my_graphite.host/render/'
168
+ width 800
169
+ height 200
170
+ areaMode :stacked
171
+ from '-2hours'
172
+ target(legend(color(sumSeries("#{data :hostname}.cpu-*.cpu-steal.value"), :red), 'Steal'))
173
+ target(legend(color(sumSeries("#{data :hostname}.cpu-*.cpu-steal.value"), :green), 'Idle'))
174
+ end.render.must_equal "<img src=\"http://my_graphite.host/render/?width=800&height=200&areaMode=stacked&from=-2hours&target=alias(color(sumSeries(foo.cpu-*.cpu-steal.value),'red'),'Steal')&target=alias(color(sumSeries(foo.cpu-*.cpu-steal.value),'green'),'Idle')\"/>"
175
+ end
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphite_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Edward Muller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &70338624977220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70338624977220
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70338624976320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70338624976320
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70338624975440 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70338624975440
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &70338624974560 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70338624974560
58
+ description: A DSL for generating graphite graph urls. Meant to enable copy-and-paste
59
+ from the graphite UI + simple substitution.
60
+ email: edward@heroku.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .document
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.rdoc
72
+ - Rakefile
73
+ - VERSION
74
+ - graphite_builder.gemspec
75
+ - lib/graphite_builder.rb
76
+ - test/helper.rb
77
+ - test/test_graphite_builder.rb
78
+ homepage: http://github.com/freeformz/graphite_builder
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: 372793677429185885
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.6
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: ! 'A DSL: for generating graphite graph urls.'
106
+ test_files: []