fnordmetric-client 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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sw?
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color -fd
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ilya Averyanov
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,29 @@
1
+ # Fnordmetric::Client
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fnordmetric-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fnordmetric-client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/fnordmetric-client/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ilya Averyanov"]
6
+ gem.email = ["av@fun-box.ru"]
7
+ gem.description = %q{Standalone client for fnordmetric}
8
+ gem.summary = %q{Standalone and a safer client for fnordmetric https://github.com/paulasmuth/fnordmetric}
9
+ gem.homepage = "https://github.com/savonarola/fnordmetric-client"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "fnordmetric-client"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = FnordmetricClient::VERSION
17
+
18
+ gem.add_runtime_dependency('json')
19
+
20
+ gem.add_development_dependency('rake')
21
+ gem.add_development_dependency('mock_redis')
22
+ gem.add_development_dependency('rspec')
23
+ gem.add_development_dependency('timecop')
24
+ end
@@ -0,0 +1,3 @@
1
+ class FnordmetricClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require "fnordmetric-client/version"
2
+ require 'securerandom'
3
+ require 'json/add/core'
4
+
5
+ class FnordmetricClient
6
+ DEFAULT_EVENT_TTL = 3600
7
+ DEFAULT_PREFIX = 'fnordmetric'
8
+ DEFAULT_OVERFLOW_LIMIT = 50000
9
+
10
+ def initialize(opts ={})
11
+ raise ArgumentError, "Bad options" unless opts.is_a?(Hash)
12
+
13
+ @redis = opts[:redis]
14
+ raise ArgumentError, "Bad :redis passed" unless %w{hincrby set lpush expire llen}.all?{|m| @redis.respond_to?(m) }
15
+
16
+ @prefix = opts[:prefix] || DEFAULT_PREFIX
17
+ @event_ttl = opts[:event_ttl] || DEFAULT_EVENT_TTL
18
+ @overflow_limit = opts[:overflow_limit] || DEFAULT_OVERFLOW_LIMIT
19
+ end
20
+
21
+ def event(type, args = {})
22
+ push_event({'_type' => type.to_s}.merge(args))
23
+ end
24
+
25
+ private
26
+
27
+ def key(*elements)
28
+ elements.map(&:to_s).unshift(@prefix).join('-')
29
+ end
30
+
31
+ def push_event(event_data)
32
+ return if @redis.llen(key(:queue)) >= @overflow_limit
33
+
34
+ event_id = get_next_uuid
35
+ @redis.hincrby(key(:testdata), "events_received", 1)
36
+ @redis.hincrby(key(:stats), "events_received", 1)
37
+ @redis.set(key(:event, event_id), event_data.to_json)
38
+ @redis.expire(key(:event, event_id), @event_ttl)
39
+ @redis.lpush(key(:queue), event_id)
40
+ event_id
41
+ end
42
+
43
+ def get_next_uuid
44
+ SecureRandom.hex(16)
45
+ end
46
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe FnordmetricClient do
4
+ let(:redis){ MockRedis.new }
5
+ let(:prefix){ 'pref' }
6
+ let(:event_ttl){ 10 }
7
+ let(:overflow_limit){ 5 }
8
+ let(:client){ described_class.new(:redis => redis, :prefix => prefix, :overflow_limit => overflow_limit, :event_ttl => event_ttl) }
9
+
10
+ before(:each) do
11
+ redis.flushall
12
+ end
13
+
14
+ describe "#new" do
15
+ it "should raise exception when receives a non-hashlike object" do
16
+ expect{ described_class.new(123) }.to raise_exception(ArgumentError)
17
+ end
18
+
19
+ it "should raise exception when redis is not passed" do
20
+ expect{ described_class.new({:redis => 123}) }.to raise_exception(ArgumentError)
21
+ expect{ described_class.new({}) }.to raise_exception(ArgumentError)
22
+ end
23
+ end
24
+
25
+ describe '#event' do
26
+
27
+ context "when redis is not overflown" do
28
+ it "should put event id into queue" do
29
+ client.event('foo', :some => 'payload')
30
+ redis.llen("#{prefix}-queue").to_i.should == 1
31
+ end
32
+
33
+ it "should put event data by the key pushed into queue" do
34
+ client.event('foo', :some => 'payload')
35
+ event_id = redis.lrange("#{prefix}-queue", 0, 1).first
36
+ event_data_json = redis.get "#{prefix}-event-#{event_id}"
37
+ event_data_json.should_not be_nil
38
+ event_data = JSON.parse(event_data_json)
39
+ event_data.should == {'_type' => 'foo', 'some' => 'payload'}
40
+ end
41
+
42
+ it "should increase stat counters" do
43
+ client.event('foo', :some => 'payload')
44
+ redis.hget("#{prefix}-stats", "events_received").to_i.should == 1
45
+ redis.hget("#{prefix}-testdata", "events_received").to_i.should == 1
46
+ client.event('foo', :some => 'payload')
47
+ redis.hget("#{prefix}-stats", "events_received").to_i.should == 2
48
+ redis.hget("#{prefix}-testdata", "events_received").to_i.should == 2
49
+ end
50
+
51
+ it "should generate different event ids" do
52
+ client.event('foo', :some => 'payload')
53
+ client.event('foo', :some => 'payload')
54
+ redis.llen("#{prefix}-queue").to_i.should == 2
55
+ end
56
+
57
+ it "should expire event data" do
58
+ client.event('foo', :some => 'payload')
59
+ event_id = redis.lrange("#{prefix}-queue", 0, 1).first
60
+ event_data_json = redis.get "#{prefix}-event-#{event_id}"
61
+ event_data_json.should_not be_nil
62
+ Timecop.freeze(Time.now + event_ttl + 1) do
63
+ event_data_json = redis.get "#{prefix}-event-#{event_id}"
64
+ event_data_json.should be_nil
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ context "when redis is overflown" do
71
+ it "should not send events to queue" do
72
+ overflow_limit.times{ redis.lpush "#{prefix}-queue", "xxx" }
73
+ client.event('foo', :some => 'payload')
74
+ redis.llen("#{prefix}-queue").to_i.should == overflow_limit
75
+ redis.keys('*').size.should == 1
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require(:default, :development, :test)
4
+ require File.join(File.dirname(__FILE__), '../lib/fnordmetric-client')
5
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fnordmetric-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ilya Averyanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70354132801040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70354132801040
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70354132800620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70354132800620
36
+ - !ruby/object:Gem::Dependency
37
+ name: mock_redis
38
+ requirement: &70354132800200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70354132800200
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70354132799780 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70354132799780
58
+ - !ruby/object:Gem::Dependency
59
+ name: timecop
60
+ requirement: &70354132799360 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70354132799360
69
+ description: Standalone client for fnordmetric
70
+ email:
71
+ - av@fun-box.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - fnordmetric-client.gemspec
83
+ - lib/fnordmetric-client.rb
84
+ - lib/fnordmetric-client/version.rb
85
+ - spec/fnordmetric-client_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/savonarola/fnordmetric-client
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.11
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Standalone and a safer client for fnordmetric https://github.com/paulasmuth/fnordmetric
111
+ test_files:
112
+ - spec/fnordmetric-client_spec.rb
113
+ - spec/spec_helper.rb