blackbeard 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +1 -0
- data/TODO.md +77 -0
- data/blackbeard.gemspec +30 -0
- data/lib/blackbeard/configuration.rb +23 -0
- data/lib/blackbeard/context.rb +40 -0
- data/lib/blackbeard/dashboard/helpers.rb +8 -0
- data/lib/blackbeard/dashboard/views/layout.erb +15 -0
- data/lib/blackbeard/dashboard/views/metrics/index.erb +10 -0
- data/lib/blackbeard/dashboard/views/metrics/show.erb +16 -0
- data/lib/blackbeard/dashboard.rb +30 -0
- data/lib/blackbeard/metric/total.rb +17 -0
- data/lib/blackbeard/metric/unique.rb +18 -0
- data/lib/blackbeard/metric.rb +81 -0
- data/lib/blackbeard/pirate.rb +28 -0
- data/lib/blackbeard/redis_store.rb +54 -0
- data/lib/blackbeard/storable.rb +19 -0
- data/lib/blackbeard/version.rb +3 -0
- data/lib/blackbeard.rb +24 -0
- data/spec/blackbeard_spec.rb +7 -0
- data/spec/dashboard_spec.rb +38 -0
- data/spec/metric_spec.rb +20 -0
- data/spec/pirate_spec.rb +5 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/total_metric_spec.rb +65 -0
- data/spec/unique_metric_spec.rb +60 -0
- metadata +205 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 40ecb485cbf20c68fb3c9eb801b3a4190a159d5a
|
4
|
+
data.tar.gz: ef929019e6ca00b2eb177700d661d21fc18c832c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca6c0cd39a9846cb23dbc157900d89e0164b50d5b4ccaec5f81e96349f30931f95193d5045310d6bc7ee0e4b666e50abca65cdc399f9ce9ab298a0701d64cfbd
|
7
|
+
data.tar.gz: 566519a8926091851afb1f4c7ba679a9ab71ffd750a7d0519daef8ce22476e0ff53dd4051817e542092fd6aaf2419744c22af4b62e7d967e4c39973ad7718813
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Robert Graff
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Blackbeard
|
2
|
+
|
3
|
+
Blackbeard is a Redis backed metrics collection system with a Rack dashboard.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'blackbeard'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install blackbeard
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In an initializer create your global $pirate and pass in your configuration.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
$pirate = Blackbeard.pirate do |config|
|
25
|
+
config.redis = Redis.new # required. Will automatically be namespaced.
|
26
|
+
config.namespace = "Blackbeard" # optional
|
27
|
+
config.timezone = "America/Los_Angeles" # optional
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Note that the configuration is shared by all pirates, so only create one pirate at a time.
|
32
|
+
|
33
|
+
To get the rack dashboard on Rails, mount it in your `routes.rb`. Don't forget to protect it with some constraints.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
mount Blackbeard::Dashboard => '/blackbeard', :constraints => ConstraintClassYouCreate.new
|
37
|
+
```
|
38
|
+
|
39
|
+
### Collecting Metrics
|
40
|
+
|
41
|
+
In your app, have your pirate collect the important metrics.
|
42
|
+
|
43
|
+
#### Counting Unique Metrics
|
44
|
+
|
45
|
+
Unique counts are for metrics wherein a user may trigger the same metric twice, but should only be counted once.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
$pirate.context(:user_id => user_id, :cookies => cookies).add_unique(:logged_in_user)
|
49
|
+
```
|
50
|
+
|
51
|
+
#### Counting Non-Unique Metrics
|
52
|
+
|
53
|
+
Non-unique counts are for metrics wherein a user may trigger the same metric multiple times and the amounts are summed up.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
$pirate.context(...).add(:like, +1) # increment a like
|
57
|
+
$pirate.context(...).add(:like, -1) # de-increment a like
|
58
|
+
$pirate.context(...).add(:revenue, +119.95) # can also accept floats
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
68
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/TODO.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
## TODO:
|
2
|
+
|
3
|
+
Multi-tests
|
4
|
+
* define test variants in multiple places
|
5
|
+
* allow for undefined (e.g. control) variations
|
6
|
+
* true multivariate tests with multiple AB tests
|
7
|
+
* reusable across experiments
|
8
|
+
* define goals in controllers, views, javascript
|
9
|
+
Segmentation
|
10
|
+
* true or false (e.g. premium members)
|
11
|
+
* groupings (e.g. organization size)
|
12
|
+
* experiment results viewable segmentation
|
13
|
+
* reusable across experiments
|
14
|
+
* define goals in controllers, views, javascript
|
15
|
+
Targetting
|
16
|
+
* by defined segments (e.g. staff?)
|
17
|
+
* by user_id (e.g. rollout like)
|
18
|
+
|
19
|
+
|
20
|
+
Create and manage multiple experiments in a browser/rack dashboard
|
21
|
+
|
22
|
+
### Experiments
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
$pirate.experiment(
|
26
|
+
"name of test",
|
27
|
+
:description => "Optional",
|
28
|
+
:running => true,
|
29
|
+
:tests => [:link],
|
30
|
+
:goals => [:join, :like]
|
31
|
+
)
|
32
|
+
```
|
33
|
+
|
34
|
+
### Funnel Metrics
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
$pirate.funnel(:checkout, 'Confirm') # User reached step 3 of funnel (Confirm)
|
38
|
+
```
|
39
|
+
|
40
|
+
### Segments
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
$pirate.segment(:premium_member) do
|
44
|
+
current_user.premium_member? # true or false
|
45
|
+
end
|
46
|
+
|
47
|
+
$pirate.segment(:organization_size) do
|
48
|
+
current_user.organization_size # '0-5', '6-14', '15+'
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
### Changes
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
$pirate.change(:link, :control => 'blue', :red => 'red')
|
56
|
+
$pirate.change(:link,
|
57
|
+
:control => nil, # noop
|
58
|
+
:red => Proc.new("red")
|
59
|
+
)
|
60
|
+
|
61
|
+
|
62
|
+
$pirate.variation(:link, :control) do
|
63
|
+
something.blue
|
64
|
+
end
|
65
|
+
|
66
|
+
$pirate.variation(:link, :red) do
|
67
|
+
...
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Outside a Web Session
|
72
|
+
|
73
|
+
You can run them in crons or asynchronously.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
$pirate.context().goal(...)
|
77
|
+
$pirate.context()
|
data/blackbeard.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blackbeard/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "blackbeard"
|
8
|
+
spec.version = Blackbeard::VERSION
|
9
|
+
spec.authors = ["Robert Graff"]
|
10
|
+
spec.email = ["robert_graff@yahoo.com"]
|
11
|
+
spec.description = %q{Blackbeard is a Redis backed metrics collection system with a Rack dashboard. It dreams of being a replacement for rollout and split, but is early in it's development.}
|
12
|
+
spec.summary = %q{Blackbeard is a Redis backed metrics collection system with a Rack dashboard}
|
13
|
+
spec.homepage = "https://github.com/goldstar/blackbeard"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 0"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2"
|
24
|
+
spec.add_development_dependency 'rack-test', '~> 0.6'
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "sinatra-base", "~> 1.4"
|
27
|
+
spec.add_runtime_dependency "tzinfo", "~> 1"
|
28
|
+
spec.add_runtime_dependency 'redis', '~> 3.0', '>= 3.0.4'
|
29
|
+
spec.add_runtime_dependency 'redis-namespace', '~> 1.4', '>= 1.4.1'
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'tzinfo'
|
2
|
+
require "blackbeard/redis_store"
|
3
|
+
|
4
|
+
module Blackbeard
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :timezone, :namespace, :redis
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@timezone = 'America/Los_Angeles'
|
10
|
+
@namespace = 'Blackbeard'
|
11
|
+
@redis = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def db
|
15
|
+
@db ||= RedisStore.new(@redis, @namespace)
|
16
|
+
end
|
17
|
+
|
18
|
+
def tz
|
19
|
+
@tz ||= TZInfo::Timezone.get(@timezone)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Blackbeard
|
2
|
+
class Context
|
3
|
+
|
4
|
+
def initialize(pirate, options)
|
5
|
+
@pirate = pirate
|
6
|
+
@user_id = options[:user_id]
|
7
|
+
@bot = options[:bot] || false
|
8
|
+
@blackbeard_identifier = blackbeard_visitor_id(options[:cookies] || {}) unless bot?
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_total(name, amount = 1)
|
12
|
+
@pirate.total_metric(name.to_s).add(amount) unless bot?
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_unique(name)
|
16
|
+
@pirate.unique_metric(name.to_s).add(unique_identifier) unless bot?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def bot?
|
22
|
+
@bot
|
23
|
+
end
|
24
|
+
|
25
|
+
def unique_identifier
|
26
|
+
@user_id.present? ? "a#{@user_id}" : "b#{@blackbeard_identifier}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def blackbeard_visitor_id(cookies)
|
30
|
+
cookies[:bbd] ||= generate_blackbeard_visitor_id(cookies)
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_blackbeard_visitor_id(cookies)
|
34
|
+
id = Blackbeard.db.increment("visitor_id")
|
35
|
+
cookies[:bbd] = { :value => id, :expires => Time.now + 31536000 }
|
36
|
+
id
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<h2>Metrics</h2>
|
2
|
+
<% if @metrics.any? %>
|
3
|
+
<ul>
|
4
|
+
<% @metrics.each do |metric| %>
|
5
|
+
<li><a href="<%= url("metrics/#{metric.type}/#{metric.name}") %>"><%= metric.name.capitalize %> - <%= metric.type.capitalize %></a></li>
|
6
|
+
<% end %>
|
7
|
+
</ul>
|
8
|
+
<% else %>
|
9
|
+
No metrics recorded.
|
10
|
+
<% end %>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<h2><%= @metric.name.capitalize %> - <%= @metric.type.capitalize %></h2>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<th>Hour</th>
|
6
|
+
<th>Count</th>
|
7
|
+
</tr>
|
8
|
+
|
9
|
+
<% @metric.hours.each do |metric_hour| %>
|
10
|
+
<tr>
|
11
|
+
<td><%= metric_hour.hour %></td>
|
12
|
+
<td><%= metric_hour.result %></td>
|
13
|
+
</tr>
|
14
|
+
<% end %>
|
15
|
+
|
16
|
+
</table>
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'blackbeard'
|
3
|
+
require 'blackbeard/dashboard/helpers'
|
4
|
+
|
5
|
+
module Blackbeard
|
6
|
+
class Dashboard < Sinatra::Base
|
7
|
+
set :root, File.expand_path(File.dirname(__FILE__) + "/dashboard")
|
8
|
+
set :public_folder, Proc.new { "#{root}/public" }
|
9
|
+
set :views, Proc.new { "#{root}/views" }
|
10
|
+
set :raise_errors, true
|
11
|
+
set :show_exceptions, false
|
12
|
+
|
13
|
+
helpers Blackbeard::DashboardHelpers
|
14
|
+
|
15
|
+
get '/' do
|
16
|
+
redirect url('/metrics')
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/metrics' do
|
20
|
+
@metrics = Blackbeard::Metric.all
|
21
|
+
erb 'metrics/index'.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
get "/metrics/:type/:name" do
|
25
|
+
@metric = Blackbeard::Metric.new_from_type_name(params[:type], params[:name])
|
26
|
+
erb 'metrics/show'.to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "blackbeard/metric"
|
2
|
+
|
3
|
+
module Blackbeard
|
4
|
+
class Metric::Total < Metric
|
5
|
+
|
6
|
+
def add(uid, amount)
|
7
|
+
key = key_for_hour(tz.now)
|
8
|
+
db.set_add_member(hours_set_key, key)
|
9
|
+
db.increment_by_float(key, amount.to_f)
|
10
|
+
end
|
11
|
+
|
12
|
+
def result_for_hour_key(key)
|
13
|
+
db.get(key).to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "blackbeard/metric"
|
2
|
+
|
3
|
+
module Blackbeard
|
4
|
+
class Metric::Unique < Metric
|
5
|
+
|
6
|
+
def add(uid)
|
7
|
+
key = key_for_hour(tz.now)
|
8
|
+
db.set_add_member(hours_set_key, key)
|
9
|
+
db.set_add_member(key, uid)
|
10
|
+
end
|
11
|
+
|
12
|
+
def result_for_hour_key(key)
|
13
|
+
db.set_count(key)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "blackbeard/storable"
|
2
|
+
|
3
|
+
module Blackbeard
|
4
|
+
class Metric < Storable
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
@name = name.to_s.downcase
|
9
|
+
db.hash_key_set_if_not_exists(metrics_key, key, tz.now.to_date)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.new_from_type_name(type, name)
|
13
|
+
self.const_get(type.capitalize).new(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.new_from_key(key)
|
17
|
+
if key =~ /^metrics::(.+)::(.+)$/
|
18
|
+
new_from_type_name($1, $2)
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.all
|
25
|
+
all_keys.map{ |key| new_from_key(key) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.all_keys
|
29
|
+
db.hash_keys(metrics_key)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.count
|
33
|
+
db.hash_length(metrics_key)
|
34
|
+
end
|
35
|
+
|
36
|
+
def type
|
37
|
+
self.class.name.split("::").last.downcase
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.metrics_key
|
41
|
+
"metrics"
|
42
|
+
end
|
43
|
+
|
44
|
+
def key
|
45
|
+
"metrics::#{ type }::#{ name }"
|
46
|
+
end
|
47
|
+
|
48
|
+
def hours
|
49
|
+
hour_keys.each do |hour_key|
|
50
|
+
{
|
51
|
+
:hour => hour_key.split("::").last,
|
52
|
+
:result => result_for_hour_key(hour_key)
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def result_for_hour(time)
|
58
|
+
key = key_for_hour(time)
|
59
|
+
result_for_hour_key(key)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def hour_keys
|
65
|
+
db.hash_keys(hours_set_key)
|
66
|
+
end
|
67
|
+
|
68
|
+
def metrics_key
|
69
|
+
self.class.metrics_key
|
70
|
+
end
|
71
|
+
|
72
|
+
def hours_set_key
|
73
|
+
"#{key}::hours"
|
74
|
+
end
|
75
|
+
|
76
|
+
def key_for_hour(time)
|
77
|
+
"#{key}::#{ time.strftime("%Y%m%d%H") }"
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "blackbeard/context"
|
2
|
+
require "blackbeard/metric"
|
3
|
+
require "blackbeard/metric/unique"
|
4
|
+
require "blackbeard/metric/total"
|
5
|
+
|
6
|
+
module Blackbeard
|
7
|
+
class Pirate
|
8
|
+
attr_accessor :total_metrics, :unique_metrics
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@total_metrics = {}
|
12
|
+
@unique_metrics = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def total_metric(name)
|
16
|
+
@total_metrics[name] ||= Metric::Total.new(name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def unique_metric(name)
|
20
|
+
@unique_metrics[name] ||= Metric::Unique.new(name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def context(options = {})
|
24
|
+
Context.new(self, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'redis-namespace'
|
3
|
+
|
4
|
+
module Blackbeard
|
5
|
+
class RedisStore
|
6
|
+
attr_reader :redis
|
7
|
+
|
8
|
+
def initialize(r, namespace)
|
9
|
+
r ||= Redis.new
|
10
|
+
@redis = Redis::Namespace.new(namespace.to_sym, :redis => r)
|
11
|
+
end
|
12
|
+
|
13
|
+
def keys
|
14
|
+
redis.keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def hash_key_set_if_not_exists(hash_key, field, value)
|
18
|
+
redis.hsetnx(hash_key, field, value)
|
19
|
+
end
|
20
|
+
|
21
|
+
def hash_length(hash_key)
|
22
|
+
redis.hlen(hash_key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def hash_keys(hash_key)
|
26
|
+
redis.hkeys(hash_key)
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_add_member(set_key, member)
|
30
|
+
redis.sadd(set_key, member)
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_count(set_key)
|
34
|
+
redis.scard(set_key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def del(*keys)
|
38
|
+
redis.del(*keys)
|
39
|
+
end
|
40
|
+
|
41
|
+
def increment_by_float(key, increment)
|
42
|
+
redis.incrbyfloat(key, increment)
|
43
|
+
end
|
44
|
+
|
45
|
+
def increment(key)
|
46
|
+
redis.incr(key)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get(key)
|
50
|
+
redis.get(key)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/lib/blackbeard.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "blackbeard/configuration"
|
2
|
+
require "blackbeard/pirate"
|
3
|
+
|
4
|
+
module Blackbeard
|
5
|
+
extend self
|
6
|
+
attr_accessor :config
|
7
|
+
|
8
|
+
def tz
|
9
|
+
config.tz
|
10
|
+
end
|
11
|
+
|
12
|
+
def db
|
13
|
+
config.db
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.pirate
|
17
|
+
@config ||= Configuration.new
|
18
|
+
yield(config)
|
19
|
+
Blackbeard::Pirate.new
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
Blackbeard.pirate {}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'rack/test'
|
4
|
+
require 'blackbeard/dashboard'
|
5
|
+
|
6
|
+
describe Blackbeard::Dashboard do
|
7
|
+
include Rack::Test::Methods
|
8
|
+
|
9
|
+
let(:app) { Blackbeard::Dashboard }
|
10
|
+
|
11
|
+
describe "/" do
|
12
|
+
it "should redirect" do
|
13
|
+
get "/"
|
14
|
+
last_response.should be_redirect
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "/metrics" do
|
19
|
+
it "should list all the metrics" do
|
20
|
+
Blackbeard::Metric::Total.new("Jostling")
|
21
|
+
get "/metrics"
|
22
|
+
|
23
|
+
last_response.should be_ok
|
24
|
+
last_response.body.should include('Jostling')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "/metrics/:type/:name" do
|
29
|
+
it "should show a metric" do
|
30
|
+
metric = Blackbeard::Metric::Total.new("Jostling")
|
31
|
+
get "/metrics/#{metric.type}/#{metric.name}"
|
32
|
+
|
33
|
+
last_response.should be_ok
|
34
|
+
last_response.body.should include("Jostling")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/metric_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Blackbeard::Metric do
|
4
|
+
describe "self.all" do
|
5
|
+
before :each do
|
6
|
+
Blackbeard::Metric::Total.new("one-total")
|
7
|
+
Blackbeard::Metric::Total.new("two-total")
|
8
|
+
Blackbeard::Metric::Unique.new("one-unique")
|
9
|
+
end
|
10
|
+
it "should return a Metric Object for each Metric created" do
|
11
|
+
Blackbeard::Metric.all.should have(3).metrics
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should instantiate each metric with the correct class" do
|
15
|
+
Blackbeard::Metric.all.select{|m| m.name == "two-total"}.should have(1).metric
|
16
|
+
Blackbeard::Metric.all.select{|m| m.name == "two-total"}.first.should be_a(Blackbeard::Metric::Total)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/spec/pirate_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Blackbeard::Metric::Total do
|
4
|
+
|
5
|
+
let(:metric) { Blackbeard::Metric::Total.new("page views") }
|
6
|
+
let(:uid) { "unique identifier" }
|
7
|
+
let(:ouid) { "other unique identifier" }
|
8
|
+
|
9
|
+
describe "add" do
|
10
|
+
it "should create a new metric" do
|
11
|
+
expect{
|
12
|
+
metric.add(uid, 1)
|
13
|
+
}.to change{ Blackbeard::Metric.count }.by(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not create a new metric if it already exists" do
|
17
|
+
metric.add(uid, 1)
|
18
|
+
expect{
|
19
|
+
metric.add(uid, 1)
|
20
|
+
}.to_not change{ Blackbeard::Metric.count }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should increment the metric by the amount" do
|
24
|
+
expect{
|
25
|
+
metric.add(uid, 2)
|
26
|
+
}.to change{ metric.result_for_hour(Blackbeard.tz.now) }.by(2)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should increment an existing amount" do
|
30
|
+
metric.add(uid, 1)
|
31
|
+
expect{
|
32
|
+
metric.add(uid, 2)
|
33
|
+
}.to change{ metric.result_for_hour(Blackbeard.tz.now) }.from(1).to(3)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should handle negatives ok" do
|
37
|
+
metric.add(uid, 2)
|
38
|
+
expect{
|
39
|
+
metric.add(uid, -1)
|
40
|
+
}.to change{ metric.result_for_hour(Blackbeard.tz.now) }.from(2).to(1)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle floats" do
|
44
|
+
metric.add(uid, 2.5)
|
45
|
+
expect{
|
46
|
+
metric.add(uid, 1.25)
|
47
|
+
}.to change{ metric.result_for_hour(Blackbeard.tz.now) }.to(3.75)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
describe "result_for_hour" do
|
54
|
+
it "should return 0 if no metric has been recorded" do
|
55
|
+
metric.result_for_hour(Blackbeard.tz.now).should be_zero
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return sum if metric called more than once" do
|
59
|
+
metric.add(uid, 2)
|
60
|
+
metric.add(uid, 4)
|
61
|
+
metric.result_for_hour(Blackbeard.tz.now).should == 6
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Blackbeard::Metric::Unique do
|
4
|
+
|
5
|
+
let(:metric) { Blackbeard::Metric::Unique.new("join") }
|
6
|
+
let(:uid) { "unique identifier" }
|
7
|
+
let(:ouid) { "other unique identifier" }
|
8
|
+
|
9
|
+
describe "add" do
|
10
|
+
it "should create a new metric" do
|
11
|
+
expect{
|
12
|
+
metric.add(uid)
|
13
|
+
}.to change{ Blackbeard::Metric.count }.by(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not create a new metric if it already exists" do
|
17
|
+
metric.add(uid)
|
18
|
+
expect{
|
19
|
+
metric.add(uid)
|
20
|
+
}.to_not change{ Blackbeard::Metric.count }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should increment the metric for the uid" do
|
24
|
+
expect{
|
25
|
+
metric.add(uid)
|
26
|
+
}.to change{ metric.result_for_hour(Blackbeard.tz.now) }.by(1)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not increment the metric for duplicate uids" do
|
30
|
+
metric.add(uid)
|
31
|
+
expect{
|
32
|
+
metric.add(uid)
|
33
|
+
}.to_not change{ metric.result_for_hour(Blackbeard.tz.now) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "result_for_hour" do
|
38
|
+
it "should return 0 if no metric has been recorded" do
|
39
|
+
metric.result_for_hour(Blackbeard.tz.now).should be_zero
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return 1 if metric called once" do
|
43
|
+
metric.add(uid)
|
44
|
+
metric.result_for_hour(Blackbeard.tz.now).should == 1
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return 1 if metric called more than once" do
|
48
|
+
3.times{ metric.add(uid) }
|
49
|
+
metric.result_for_hour(Blackbeard.tz.now).should == 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return 2 if metric was called with 2 uniques" do
|
53
|
+
metric.add(uid)
|
54
|
+
metric.add(ouid)
|
55
|
+
metric.result_for_hour(Blackbeard.tz.now).should == 2
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blackbeard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Graff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sinatra-base
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.4'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: tzinfo
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: redis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 3.0.4
|
107
|
+
type: :runtime
|
108
|
+
prerelease: false
|
109
|
+
version_requirements: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ~>
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '3.0'
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 3.0.4
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: redis-namespace
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ~>
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '1.4'
|
124
|
+
- - '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 1.4.1
|
127
|
+
type: :runtime
|
128
|
+
prerelease: false
|
129
|
+
version_requirements: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '1.4'
|
134
|
+
- - '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 1.4.1
|
137
|
+
description: Blackbeard is a Redis backed metrics collection system with a Rack dashboard.
|
138
|
+
It dreams of being a replacement for rollout and split, but is early in it's development.
|
139
|
+
email:
|
140
|
+
- robert_graff@yahoo.com
|
141
|
+
executables: []
|
142
|
+
extensions: []
|
143
|
+
extra_rdoc_files: []
|
144
|
+
files:
|
145
|
+
- .gitignore
|
146
|
+
- Gemfile
|
147
|
+
- LICENSE.txt
|
148
|
+
- README.md
|
149
|
+
- Rakefile
|
150
|
+
- TODO.md
|
151
|
+
- blackbeard.gemspec
|
152
|
+
- lib/blackbeard.rb
|
153
|
+
- lib/blackbeard/configuration.rb
|
154
|
+
- lib/blackbeard/context.rb
|
155
|
+
- lib/blackbeard/dashboard.rb
|
156
|
+
- lib/blackbeard/dashboard/helpers.rb
|
157
|
+
- lib/blackbeard/dashboard/views/layout.erb
|
158
|
+
- lib/blackbeard/dashboard/views/metrics/index.erb
|
159
|
+
- lib/blackbeard/dashboard/views/metrics/show.erb
|
160
|
+
- lib/blackbeard/metric.rb
|
161
|
+
- lib/blackbeard/metric/total.rb
|
162
|
+
- lib/blackbeard/metric/unique.rb
|
163
|
+
- lib/blackbeard/pirate.rb
|
164
|
+
- lib/blackbeard/redis_store.rb
|
165
|
+
- lib/blackbeard/storable.rb
|
166
|
+
- lib/blackbeard/version.rb
|
167
|
+
- spec/blackbeard_spec.rb
|
168
|
+
- spec/dashboard_spec.rb
|
169
|
+
- spec/metric_spec.rb
|
170
|
+
- spec/pirate_spec.rb
|
171
|
+
- spec/spec_helper.rb
|
172
|
+
- spec/total_metric_spec.rb
|
173
|
+
- spec/unique_metric_spec.rb
|
174
|
+
homepage: https://github.com/goldstar/blackbeard
|
175
|
+
licenses:
|
176
|
+
- MIT
|
177
|
+
metadata: {}
|
178
|
+
post_install_message:
|
179
|
+
rdoc_options: []
|
180
|
+
require_paths:
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - '>='
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - '>='
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
requirements: []
|
193
|
+
rubyforge_project:
|
194
|
+
rubygems_version: 2.2.1
|
195
|
+
signing_key:
|
196
|
+
specification_version: 4
|
197
|
+
summary: Blackbeard is a Redis backed metrics collection system with a Rack dashboard
|
198
|
+
test_files:
|
199
|
+
- spec/blackbeard_spec.rb
|
200
|
+
- spec/dashboard_spec.rb
|
201
|
+
- spec/metric_spec.rb
|
202
|
+
- spec/pirate_spec.rb
|
203
|
+
- spec/spec_helper.rb
|
204
|
+
- spec/total_metric_spec.rb
|
205
|
+
- spec/unique_metric_spec.rb
|