statsn 0.1.0 → 0.1.1

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/.gitignore CHANGED
@@ -1 +1 @@
1
- spec/argv.txt
1
+ spec/argv.yml
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- statsn (0.1.0)
4
+ statsn (0.1.1)
5
5
  newrelic_rpm (~> 3.5)
6
6
 
7
7
  GEM
data/bin/statsn-calls CHANGED
@@ -3,6 +3,8 @@
3
3
  raise "Needs 1.9+" if RUBY_VERSION < "1.9.0"
4
4
  PERIOD = 24*60*60
5
5
  API_METRICS_LIMIT = 100
6
+ class MissingFieldError < Exception
7
+ end
6
8
 
7
9
  def stats(options)
8
10
  response = Faraday.new(
@@ -10,12 +12,20 @@ def stats(options)
10
12
  :params => options,
11
13
  :headers => {"x-api-key" => API_KEY}
12
14
  ).get
13
- raise response.body unless response.status == 200
15
+
16
+ if response.status != 200
17
+ if response.body.include?("The field you use must be valid for at least one metric.")
18
+ raise MissingFieldError
19
+ else
20
+ raise response.body
21
+ end
22
+ end
23
+
14
24
  JSON.parse(response.body)
15
25
  end
16
26
 
17
- def aggregate(names, field)
18
- names.each_slice(API_METRICS_LIMIT).map do |group|
27
+ def aggregate(metric_names, field)
28
+ metric_names.each_slice(API_METRICS_LIMIT).map do |group|
19
29
  stats(:metrics => group, :field => field, :begin => Time.now - PERIOD, :end => Time.now)
20
30
  end.flatten(1)
21
31
  end
@@ -55,6 +65,14 @@ def metric_names(namespace)
55
65
  end
56
66
  end
57
67
 
68
+ def data_for_field(metric_names, field)
69
+ metric_data = aggregate(metric_names, field)
70
+ metric_data = metric_data.group_by { |x| x["name"] }
71
+ metric_data.map { |_, values| values.inject(0) { |sum, x| sum + x[field] } }.inject(:+)
72
+ rescue MissingFieldError
73
+ :missing_field
74
+ end
75
+
58
76
  # Dependencies
59
77
  gem "json"
60
78
  gem "faraday"
@@ -72,22 +90,22 @@ end
72
90
  API_KEY, ACCOUNT_ID, APP_ID, namespace = ARGV
73
91
 
74
92
  # Data
75
- names = metric_names(namespace)
93
+ metric_names = metric_names(namespace)
94
+
76
95
  results = ["call_count", "total_call_time"].map do |field|
77
- metric_data = aggregate(names, field).group_by{ |x| x["name"] }
78
- sums = metric_data.map{ |_, values| values.inject(0){ |sum,x| sum + x[field] } }.inject(:+)
79
- [field, sums]
96
+ [field, data_for_field(metric_names, field)]
80
97
  end
81
98
 
82
99
  # Display results
83
100
  results = Hash[results]
84
- if results.values.first
85
- call_time = results["total_call_time"]
86
- call_count = results["call_count"]
87
- puts results.map{|x|x.join(": ")}
88
- puts "response time: #{call_time / call_count}"
101
+ call_time = results["total_call_time"]
102
+ call_count = results["call_count"]
103
+
104
+ if call_time && call_count
105
+ puts results.reject{ |name,value| value == :missing_field }.map{ |x| x.join(": ") }
106
+ puts "response time: #{call_time / call_count}" unless call_time == :missing_field
89
107
  puts "call per second: #{call_count / PERIOD}"
90
108
  else
91
- puts "No data found for #{names.join(", ")} in last #{PERIOD} seconds"
109
+ puts "No data found for #{metric_names.join(", ")} in last #{PERIOD} seconds"
92
110
  exit 1
93
111
  end
@@ -1,3 +1,3 @@
1
1
  module Statsn
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,4 @@
1
+ # keep exmaple up to date when chaning this!
2
+ single: asdadsasddasads 123 123123 Controller/users/update
3
+ multi: asdadsasddasads 123 123123 Controller/users/*
4
+ count: asdadsasddasads 123 123123 Custom/xxx # must only have counts, no response time
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "bundler/setup"
2
2
  require "statsn"
3
+ require "yaml"
@@ -1,10 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "statsn-calls" do
4
- let(:argv){ File.read("#{Bundler.root}/spec/argv.txt").strip }
4
+ let(:argv){ YAML.load File.read("#{Bundler.root}/spec/argv.yml") }
5
+
6
+ before do
7
+ pending "create a spec/argv.yml" unless (argv rescue nil)
8
+ end
5
9
 
6
10
  it "gives us some numbers" do
7
- result = run("#{argv} Controller/api/v2/users/update")
11
+ result = run argv["single"]
8
12
  result.should =~ /^call_count: \d+/
9
13
  result.should =~ /^total_call_time: \d+/
10
14
  result.should =~ /^response time: \d+/
@@ -12,7 +16,7 @@ describe "statsn-calls" do
12
16
  end
13
17
 
14
18
  it "gives us some numbers for a prefix call" do
15
- result = run("#{argv} Controller/api/v2/users/*")
19
+ result = run argv["multi"]
16
20
  result.should =~ /^call_count: \d+/
17
21
  result.should =~ /^total_call_time: \d+/
18
22
  result.should =~ /^response time: \d+/
@@ -29,9 +33,17 @@ describe "statsn-calls" do
29
33
  result.should include("statsn-calls API-KEY")
30
34
  end
31
35
 
36
+ it "show data for count-only metrics" do
37
+ result = run argv["count"]
38
+ result.should =~ /^call_count: \d+/
39
+ result.should_not =~ /^total_call_time: \d+/
40
+ result.should_not =~ /^response time: \d+/
41
+ result.should =~ /^call per second: \d+/
42
+ end
43
+
32
44
  it "tell us if the metric is missing" do
33
- result = run("#{argv} Controller/xxxxxxx", :fail => true)
34
- result.should include("No data found for Controller/xxxxxxx")
45
+ result = run("#{argv["single"]}/xxxx", :fail => true)
46
+ result.should include("No data found for Controller/")
35
47
  end
36
48
 
37
49
  private
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: statsn
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Michael Grosser
@@ -43,7 +43,7 @@ files:
43
43
  - bin/statsn-calls
44
44
  - lib/statsn.rb
45
45
  - lib/statsn/version.rb
46
- - spec/argv.example.txt
46
+ - spec/argv.example.yml
47
47
  - spec/spec_helper.rb
48
48
  - spec/statsn_calls_spec.rb
49
49
  - spec/statsn_spec.rb
@@ -62,7 +62,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  segments:
64
64
  - 0
65
- hash: -3608192158582391302
65
+ hash: 2968399290205698663
66
66
  none: false
67
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements:
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  version: '0'
72
72
  segments:
73
73
  - 0
74
- hash: -3608192158582391302
74
+ hash: 2968399290205698663
75
75
  none: false
76
76
  requirements: []
77
77
  rubyforge_project:
@@ -1 +0,0 @@
1
- asdadsasddasads 123 123123