slate 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Slate
2
2
  [![Build Status](https://secure.travis-ci.org/trobrock/slate.png)](http://travis-ci.org/trobrock/slate)
3
+ [![Dependency Status](https://gemnasium.com/trobrock/slate.png)](https://gemnasium.com/trobrock/slate)
3
4
 
4
5
  Simple wrapper to the Graphite render api
5
6
 
@@ -1,6 +1,7 @@
1
1
  require "slate/version"
2
2
  require "slate/configuration"
3
- require "slate/render"
3
+ require "slate/target"
4
+ require "slate/graph"
4
5
  require "slate/calculation"
5
6
 
6
7
  require "slate/calculation/mean"
@@ -0,0 +1,43 @@
1
+ require 'cgi'
2
+ require 'rest_client'
3
+
4
+ module Slate
5
+ class Graph
6
+ def initialize(options={})
7
+ @from = options[:from]
8
+ @until = options[:until]
9
+ end
10
+
11
+ def <<(target)
12
+ @target = target
13
+ end
14
+
15
+ def url(format=:png)
16
+ options = url_options.push(["format", format.to_s])
17
+ "#{Configuration.instance.endpoint}/render?#{params(options)}"
18
+ end
19
+
20
+ def download(format=:png)
21
+ RestClient.get url(format)
22
+ end
23
+
24
+ private
25
+
26
+ def url_options
27
+ options = []
28
+ options << ["target", @target.to_s]
29
+ options << ["from", @from] if @from
30
+ options << ["until", @until] if @until
31
+
32
+ options
33
+ end
34
+
35
+ def params(options={})
36
+ options.map do |param|
37
+ key = param.first
38
+ value = param.last
39
+ "#{CGI.escape(key)}=#{CGI.escape(value)}"
40
+ end.join("&")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ # TODO: Tests
2
+ module Slate
3
+ class Target
4
+ def self.build(metric, &block)
5
+ target = new(metric)
6
+ yield target if block_given?
7
+ target
8
+ end
9
+
10
+ def initialize(metric)
11
+ @metric = metric
12
+ @functions = []
13
+ end
14
+
15
+ def to_s
16
+ target = @metric
17
+ @functions.each do |function|
18
+ if function.is_a? Symbol
19
+ target = %Q{#{function}(#{target})}
20
+ else
21
+ args = arguments(function.last).join(",")
22
+ target = %Q{#{function.first}(#{target},#{args})}
23
+ end
24
+ end
25
+
26
+ target
27
+ end
28
+
29
+ def add_function(*function)
30
+ if function.size > 1
31
+ arguments = function[1..-1]
32
+ @functions << [function.first.to_sym, arguments]
33
+ else
34
+ @functions << function.first.to_sym
35
+ end
36
+
37
+ to_s
38
+ end
39
+
40
+ private
41
+
42
+ def arguments(args=[])
43
+ args.map do |arg|
44
+ if arg.is_a?(Numeric)
45
+ arg.to_s
46
+ elsif arg.is_a?(Slate::Graph)
47
+ arg.send(:target)
48
+ elsif arg.is_a? Slate::Target
49
+ arg.to_s
50
+ else
51
+ %Q{"#{arg}"}
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Slate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
2
 
3
3
  describe Slate::Calculation::Mean do
4
4
  before do
5
- @graph = Slate::Render.new(:target => "some.stat")
5
+ @graph = Slate::Graph.new(:target => "some.stat")
6
6
  data = [
7
7
  {
8
8
  "target" => "some.stat",
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
2
 
3
- describe Slate::Render do
3
+ describe Slate::Graph do
4
4
  before(:each) do
5
5
  Slate.configure { |c| c.endpoint = "http://graphite" }
6
6
 
@@ -18,54 +18,77 @@ describe Slate::Render do
18
18
  end
19
19
 
20
20
  it "should be able to get a single target" do
21
- graph = Slate::Render.new(:target => "app.server01.load")
21
+ target = Slate::Target.build("app.server01.load")
22
+ graph = Slate::Graph.new
23
+ graph << target
22
24
  query(graph.url).should include("target" => "app.server01.load", "format" => "png")
23
25
  end
24
26
 
25
27
  it "should be able to apply functions" do
26
- graph = Slate::Render.new(:target => "app.server01.load")
27
- graph.add_function :cumulative
28
+ target = Slate::Target.build("app.server01.load") do |t|
29
+ t.add_function :cumulative
30
+ end
31
+ graph = Slate::Graph.new
32
+ graph << target
28
33
  graph.url.should include(CGI.escape("cumulative(app.server01.load)"))
29
34
 
30
- graph = Slate::Render.new(:target => "app.server01.load")
31
- graph.add_function :cumulative
32
- graph.add_function :alias, "load"
35
+ target = Slate::Target.build("app.server01.load") do |t|
36
+ t.add_function :cumulative
37
+ t.add_function :alias, "load"
38
+ end
39
+ graph = Slate::Graph.new
40
+ graph << target
33
41
  graph.url.should include(CGI.escape("alias(cumulative(app.server01.load),\"load\""))
34
42
 
35
- graph = Slate::Render.new(:target => "app.server01.load")
36
- graph.add_function :summarize, "1s", "sum"
43
+ target = Slate::Target.build("app.server01.load") do |t|
44
+ t.add_function :summarize, "1s", "sum"
45
+ end
46
+ graph = Slate::Graph.new
47
+ graph << target
37
48
  graph.url.should include(CGI.escape("summarize(app.server01.load,\"1s\",\"sum\")"))
38
49
 
39
- graph = Slate::Render.new(:target => "app.server01.load")
40
- graph.add_function :movingAverage, 10
50
+ target = Slate::Target.build("app.server01.load") do |t|
51
+ t.add_function :movingAverage, 10
52
+ end
53
+ graph = Slate::Graph.new
54
+ graph << target
41
55
  graph.url.should include(CGI.escape("movingAverage(app.server01.load,10)"))
42
56
  end
43
57
 
44
58
  it "should be able to accept other graphs as options to a function" do
45
- graph = Slate::Render.new(:target => "app.server01.load")
59
+ first_target = Slate::Target.build("app.server*.load") do |t|
60
+ t.add_function :sum
61
+ end
62
+ second_target = Slate::Target.build("app.server01.load") do |t|
63
+ t.add_function :asPercent, first_target
64
+ end
46
65
 
47
- other_graph = Slate::Render.new(:target => "app.server*.load")
48
- other_graph.add_function :sum
49
-
50
- graph.add_function :asPercent, other_graph
66
+ graph = Slate::Graph.new
67
+ graph << second_target
51
68
 
52
69
  graph.url.should include(CGI.escape("asPercent(app.server01.load,sum(app.server*.load))"))
53
70
  end
54
71
 
55
72
  it "should be able to specify start and end times" do
56
- graph = Slate::Render.new(:target => "app.server01.load", :from => "-1d")
73
+ target = Slate::Target.build("app.server01.load")
74
+ graph = Slate::Graph.new(:from => "-1d")
75
+ graph << target
57
76
  graph.url.should match(/from=-1d/)
58
77
 
59
- graph = Slate::Render.new(:target => "app.server01.load", :until => "-1d")
78
+ graph = Slate::Graph.new(:until => "-1d")
79
+ graph << target
60
80
  graph.url.should match(/until=-1d/)
61
81
 
62
- graph = Slate::Render.new(:target => "app.server01.load", :from => "-1d", :until => "-6h")
82
+ graph = Slate::Graph.new(:from => "-1d", :until => "-6h")
83
+ graph << target
63
84
  graph.url.should match(/from=-1d/)
64
85
  graph.url.should match(/until=-6h/)
65
86
  end
66
87
 
67
88
  it "should provide methods for retrieving formats" do
68
- graph = Slate::Render.new(:target => "app.server01.load")
89
+ target = Slate::Target.build("app.server01.load")
90
+ graph = Slate::Graph.new
91
+ graph << target
69
92
  graph.url(:png).should match(/format=png/)
70
93
  graph.url(:raw).should match(/format=raw/)
71
94
  graph.url(:csv).should match(/format=csv/)
@@ -74,7 +97,9 @@ describe Slate::Render do
74
97
  end
75
98
 
76
99
  it "should retrieve the graph" do
77
- graph = Slate::Render.new(:target => "app.server01.load")
100
+ target = Slate::Target.build("app.server01.load")
101
+ graph = Slate::Graph.new
102
+ graph << target
78
103
  graph.download(:png).should eq(@png_stub)
79
104
  graph.download(:raw).should eq(@raw_stub)
80
105
  graph.download(:csv).should eq(@csv_stub)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-07 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -124,12 +124,12 @@ files:
124
124
  - lib/slate/calculation.rb
125
125
  - lib/slate/calculation/mean.rb
126
126
  - lib/slate/configuration.rb
127
- - lib/slate/render.rb
127
+ - lib/slate/graph.rb
128
+ - lib/slate/target.rb
128
129
  - lib/slate/version.rb
129
- - script.rb
130
130
  - slate.gemspec
131
131
  - spec/slate/calculation/mean_spec.rb
132
- - spec/slate/render_spec.rb
132
+ - spec/slate/graph_spec.rb
133
133
  - spec/slate_spec.rb
134
134
  - spec/spec_helper.rb
135
135
  homepage: https://github.com/trobrock/slate
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  segments:
148
148
  - 0
149
- hash: -1369003764169049016
149
+ hash: -3823959702397249819
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  none: false
152
152
  requirements:
@@ -155,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  segments:
157
157
  - 0
158
- hash: -1369003764169049016
158
+ hash: -3823959702397249819
159
159
  requirements: []
160
160
  rubyforge_project:
161
161
  rubygems_version: 1.8.24
@@ -164,6 +164,6 @@ specification_version: 3
164
164
  summary: Simple wrapper on top of the graphite render api
165
165
  test_files:
166
166
  - spec/slate/calculation/mean_spec.rb
167
- - spec/slate/render_spec.rb
167
+ - spec/slate/graph_spec.rb
168
168
  - spec/slate_spec.rb
169
169
  - spec/spec_helper.rb
@@ -1,77 +0,0 @@
1
- require 'cgi'
2
- require 'rest_client'
3
-
4
- module Slate
5
- class Render
6
- def initialize(options={})
7
- @target = options[:target]
8
- @from = options[:from]
9
- @until = options[:until]
10
- @functions = []
11
- end
12
-
13
- def url(format=:png)
14
- options = url_options.merge("format" => format.to_s)
15
- "#{Configuration.instance.endpoint}/render?#{params(options)}"
16
- end
17
-
18
- def download(format=:png)
19
- RestClient.get url(format)
20
- end
21
-
22
- def add_function(*function)
23
- if function.size > 1
24
- arguments = function[1..-1]
25
- @functions << [function.first.to_sym, arguments]
26
- else
27
- @functions << function.first.to_sym
28
- end
29
-
30
- target
31
- end
32
-
33
- def target
34
- target = @target
35
- @functions.each do |function|
36
- if function.is_a? Symbol
37
- target = %Q{#{function}(#{target})}
38
- else
39
- args = arguments(function.last).join(",")
40
- target = %Q{#{function.first}(#{target},#{args})}
41
- end
42
- end
43
-
44
- target
45
- end
46
-
47
- private
48
-
49
- def arguments(args=[])
50
- args.map do |arg|
51
- if arg.is_a?(Numeric)
52
- arg.to_s
53
- elsif arg.is_a?(Slate::Render)
54
- arg.send(:target)
55
- else
56
- %Q{"#{arg}"}
57
- end
58
- end
59
- end
60
-
61
- def url_options
62
- options = {
63
- "target" => target
64
- }
65
- options["from"] = @from if @from
66
- options["until"] = @until if @until
67
-
68
- options
69
- end
70
-
71
- def params(options={})
72
- options.map do |key,value|
73
- "#{CGI.escape(key)}=#{CGI.escape(value)}"
74
- end.join("&")
75
- end
76
- end
77
- end
data/script.rb DELETED
@@ -1,61 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'bundler/setup'
5
- require 'slate'
6
-
7
- WINDOW = ARGV[0] || "-1w"
8
-
9
- def log(title, value)
10
- format = "[%17s] %01.2f\n"
11
- printf format, title, value
12
- end
13
-
14
- Slate.configure do |c|
15
- c.endpoint = "http://statsd.outright.com"
16
- end
17
-
18
- graph = Slate::Render.new(target: "stats.timers.rack.*.response_time.upper_90", from: WINDOW)
19
- graph.add_function :avg
20
- graph.add_function :summarize, "1h", "avg"
21
-
22
- app_response_time = Slate::Calculation::Mean.new(graph).result
23
-
24
- graph = Slate::Render.new(target: "stats.rack.*.status_code.success", from: WINDOW)
25
- graph.add_function :sum
26
-
27
- total_graph = Slate::Render.new(target: "stats.rack.*.status_code.*", from: WINDOW)
28
- total_graph.add_function :exclude, "missing"
29
- total_graph.add_function :sum
30
-
31
- graph.add_function :asPercent, total_graph
32
-
33
- app_success_rate = Slate::Calculation::Mean.new(graph).result
34
-
35
-
36
-
37
- graph = Slate::Render.new(target: "stats.timers.agg.thrift.*.response_time.upper_90", from: WINDOW)
38
- graph.add_function :avg
39
- graph.add_function :summarize, "1h", "avg"
40
-
41
- agg_response_time = Slate::Calculation::Mean.new(graph).result
42
-
43
-
44
- graph = Slate::Render.new(target: "stats.agg.thrift.*.status_code.success", from: WINDOW)
45
- graph.add_function :sum
46
-
47
- total_graph = Slate::Render.new(target: "stats.agg.thrift.*.status_code.*", from: WINDOW)
48
- total_graph.add_function :exclude, "missing"
49
- total_graph.add_function :sum
50
-
51
- graph.add_function :asPercent, total_graph
52
-
53
- agg_success_rate = Slate::Calculation::Mean.new(graph).result
54
-
55
- log "App Response Time", app_response_time
56
- log "App Success Rate" , app_success_rate
57
- log "App Uptime" , 0
58
- print "\n"
59
- log "Agg Response Time", agg_response_time
60
- log "Agg Success Rate" , agg_success_rate
61
- log "Agg Uptime" , 0