cube-evaluator 0.0.1.alpha → 0.0.1.beta

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,12 +7,12 @@ Obtain data from cube evaluators in a nice format
7
7
  Add this to your Gemfile
8
8
 
9
9
  ```ruby
10
- gem cube-evaluator
10
+ gem 'cube-evaluator'
11
11
  ```
12
12
 
13
13
  and then run
14
14
 
15
- `bundle`
15
+ $ bundle
16
16
 
17
17
  ## Usage
18
18
 
@@ -29,7 +29,13 @@ $cube_evaluator = Cube::Evaluator.new 'cube.example.com', 2280
29
29
  Ask for some metrics
30
30
 
31
31
  ```ruby
32
- $cube_evaluator.metric(:expression => 'sum(request)', :start => Time.now - 2592000, :stop => Time.now, :limit => 10, :step => '1minute')
32
+ $cube_evaluator.metric(
33
+ :expression => 'sum(request)',
34
+ :start => Time.now - 2592000,
35
+ :stop => Time.now,
36
+ :limit => 10,
37
+ :step => '1minute'
38
+ )
33
39
  ```
34
40
 
35
41
  The result will be a json encoded Hash with an array of 'times' and the corresponding 'values'
@@ -48,5 +54,4 @@ https://github.com/square/cube/wiki/Evaluator
48
54
 
49
55
  # TODO
50
56
 
51
- * Add support for evaluator events and types
52
- * Add tests
57
+ * Add support for evaluator events and types
@@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Cube::Evaluator::VERSION
17
17
 
18
18
  gem.add_development_dependency 'rspec'
19
+ gem.add_development_dependency 'webmock'
19
20
  end
@@ -1,5 +1,5 @@
1
1
  module Cube
2
- module Evaluator
3
- VERSION = "0.0.1.alpha"
2
+ class Evaluator
3
+ VERSION = "0.0.1.beta"
4
4
  end
5
5
  end
@@ -1,16 +1,21 @@
1
1
  require "cube-evaluator/version"
2
+ require 'json'
2
3
 
3
4
  module Cube
4
5
  class Evaluator
5
- def initialize(url = 'localhost', port = 1081)
6
- @url = "http://#{url}:#{port}/1.0/metric/get"
6
+ def initialize(host = 'localhost', port = 1081)
7
+ @url = "http://#{host}:#{port}/"
8
+ end
9
+
10
+ def url
11
+ @url || ""
7
12
  end
8
13
 
9
14
  def metric(options = {})
10
- uri = URI(@url)
15
+ uri = URI(@url) + "1.0/metric/get"
11
16
 
12
17
  params = options.merge(options) do |k,v|
13
- if v.is_a?(DateTime) || v.is_a?(Date) || v.is_a?(Time)
18
+ if [DateTime, Date, Time].any?{ |klass| v.is_a?(klass) }
14
19
  v.to_time.utc.iso8601
15
20
  elsif k == :step
16
21
  to_step(v)
@@ -42,6 +47,8 @@ module Cube
42
47
  result
43
48
  end
44
49
 
50
+ private
51
+
45
52
  def to_step(step)
46
53
  case step
47
54
  when '10seconds'
@@ -1,5 +1,66 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Cube::Evaluator do
4
-
4
+ before(:all) do
5
+ @cube = Cube::Evaluator.new
6
+ end
7
+
8
+ describe "#initialize" do
9
+ it "should set the url" do
10
+ @cube = Cube::Evaluator.new "cube.example.org", 1185
11
+ @cube.url.should == "http://cube.example.org:1185/"
12
+ end
13
+
14
+ it "should default the host and port to localhost:1081" do
15
+ @cube.url.should == "http://localhost:1081/"
16
+ end
17
+ end
18
+
19
+ describe "#metric" do
20
+ before(:all) do
21
+ @fake_response = [
22
+ {"time" => "2012-07-16T11:40:00.000Z", "value" => 1},
23
+ {"time" => "2012-07-16T11:41:00.000Z", "value" => 2},
24
+ {"time" => "2012-07-16T11:42:00.000Z", "value" => 3},
25
+ {"time" => "2012-07-16T11:43:00.000Z", "value" => 4},
26
+ {"time" => "2012-07-16T11:44:00.000Z", "value" => 5},
27
+ {"time" => "2012-07-16T11:45:00.000Z", "value" => 6},
28
+ {"time" => "2012-07-16T11:46:00.000Z", "value" => 7}
29
+ ]
30
+
31
+ stub_request(:get, "http://localhost:1081/1.0/metric/get?expression=sum(cube_requests)&limit=10&start=2012-04-16T16:00:00Z&step=6e4&stop=2012-04-16T17:00:00Z").
32
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
33
+ to_return(:status => 200, :body => @fake_response.to_json, :headers => {})
34
+ end
35
+
36
+ it "should return the response with the times and values hash" do
37
+ @metric = {
38
+ :times => [],
39
+ :values => []
40
+ }
41
+
42
+ @fake_response.each do |metric|
43
+ @metric[:times] << Time.parse(metric['time'])
44
+ @metric[:values] << metric['value']
45
+ end
46
+
47
+ @cube.metric(
48
+ :expression => 'sum(cube_requests)',
49
+ :start => Time.parse('2012-04-16T16:00Z'),
50
+ :stop => Time.parse('2012-04-16T17:00Z'),
51
+ :step => '1minute',
52
+ :limit => '10'
53
+ ).should == @metric
54
+ end
55
+ end
56
+
57
+ describe "#to_step" do
58
+ it "should return the supported step" do
59
+ @cube.send(:to_step, "10seconds").should == '1e4'
60
+ @cube.send(:to_step, "1minute").should == '6e4'
61
+ @cube.send(:to_step, "5minutes").should == '3e5'
62
+ @cube.send(:to_step, "1hour").should == '36e5'
63
+ @cube.send(:to_step, "1day").should == '864e5'
64
+ end
65
+ end
5
66
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,5 @@
1
- require 'cube-evaluator'
1
+ require 'cube-evaluator'
2
+ require 'webmock/rspec'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cube-evaluator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.1.beta
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: webmock
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  description: Square Cube evaluator gem
31
47
  email:
32
48
  - matteodepalo@gmail.com