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 +1 -1
- data/Gemfile.lock +1 -1
- data/bin/statsn-calls +31 -13
- data/lib/statsn/version.rb +1 -1
- data/spec/argv.example.yml +4 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/statsn_calls_spec.rb +17 -5
- metadata +4 -4
- data/spec/argv.example.txt +0 -1
data/.gitignore
CHANGED
@@ -1 +1 @@
|
|
1
|
-
spec/argv.
|
1
|
+
spec/argv.yml
|
data/Gemfile.lock
CHANGED
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
|
-
|
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(
|
18
|
-
|
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
|
-
|
93
|
+
metric_names = metric_names(namespace)
|
94
|
+
|
76
95
|
results = ["call_count", "total_call_time"].map do |field|
|
77
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
puts
|
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 #{
|
109
|
+
puts "No data found for #{metric_names.join(", ")} in last #{PERIOD} seconds"
|
92
110
|
exit 1
|
93
111
|
end
|
data/lib/statsn/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/statsn_calls_spec.rb
CHANGED
@@ -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.
|
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
|
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
|
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}
|
34
|
-
result.should include("No data found for Controller/
|
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.
|
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.
|
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:
|
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:
|
74
|
+
hash: 2968399290205698663
|
75
75
|
none: false
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
data/spec/argv.example.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
asdadsasddasads 123 123123
|