snappy_stats 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/lib/snappy_stats/config.rb +33 -0
- data/lib/snappy_stats/version.rb +3 -0
- data/lib/snappy_stats.rb +103 -0
- data/snappy_stats.gemspec +26 -0
- metadata +136 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Tim Williams
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SnappyStats
|
2
|
+
module Config
|
3
|
+
extend self
|
4
|
+
|
5
|
+
attr_accessor :namespace
|
6
|
+
attr_accessor :raise_connection_errors
|
7
|
+
|
8
|
+
def init!
|
9
|
+
# all keys are prefixed with this namespace
|
10
|
+
self.namespace = 'stats'
|
11
|
+
# rescue Redis connection errors
|
12
|
+
self.raise_connection_errors = false
|
13
|
+
end
|
14
|
+
|
15
|
+
# Set the Redis connection to use
|
16
|
+
#
|
17
|
+
# arg - A Redis connection or a Hash of Redis connection options
|
18
|
+
#
|
19
|
+
# Returns the Redis client
|
20
|
+
def redis=(arg)
|
21
|
+
if arg.is_a? Redis
|
22
|
+
@redis = arg
|
23
|
+
else
|
24
|
+
@redis = Redis.new(arg)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the Redis connection
|
29
|
+
def redis
|
30
|
+
@redis ||= Redis.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/snappy_stats.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require "snappy_stats/version"
|
2
|
+
|
3
|
+
require 'redis'
|
4
|
+
require 'active_support/time'
|
5
|
+
require 'snappy_stats/config'
|
6
|
+
|
7
|
+
module SnappyStats
|
8
|
+
|
9
|
+
GRANULARITIES = {
|
10
|
+
# Available for 24 hours
|
11
|
+
minute: {
|
12
|
+
size: 1440,
|
13
|
+
ttl: 172800,
|
14
|
+
factor: 60
|
15
|
+
},
|
16
|
+
hour: {
|
17
|
+
# Available for 7 days
|
18
|
+
size: 168,
|
19
|
+
ttl: 1209600,
|
20
|
+
factor: 3600
|
21
|
+
},
|
22
|
+
day: {
|
23
|
+
# Available for 24 months
|
24
|
+
size: 365,
|
25
|
+
ttl: 63113880,
|
26
|
+
factor: 86400
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
def self.hash_key
|
31
|
+
"#{SnappyStats.config.namespace}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.configure
|
35
|
+
yield(config)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.config
|
39
|
+
Config
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.connection
|
43
|
+
@connection ||= config.redis
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.getSecondsTimestamp
|
47
|
+
Time.now.getutc.to_i
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.getRoundedTimestamp( ts , precision )
|
51
|
+
ts = ts || self.getSecondsTimestamp
|
52
|
+
precision = precision || 1
|
53
|
+
( ts / precision ).floor * precision
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.getFactoredTimestamp( ts_seconds, factor )
|
57
|
+
ts_seconds = ts_seconds || self.getSecondsTimestamp
|
58
|
+
( ts_seconds / factor ).floor * factor
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.recordHitNow(key)
|
62
|
+
self.recordHit(Time.now.utc.to_i, key)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.recordHit( time, key )
|
66
|
+
GRANULARITIES.keys.each do | gran |
|
67
|
+
granularity = GRANULARITIES[gran]
|
68
|
+
size = granularity[:size]
|
69
|
+
factor = granularity[:factor]
|
70
|
+
ttl = granularity[:ttl]
|
71
|
+
tsround = SnappyStats.getRoundedTimestamp(time, size * factor)
|
72
|
+
redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}"
|
73
|
+
ts = getFactoredTimestamp time, factor
|
74
|
+
val = SnappyStats.connection.hincrby redis_key, ts, 1
|
75
|
+
puts val
|
76
|
+
puts "#{redis_key}, #{ts},#{SnappyStats.connection.hgetall(redis_key)}"
|
77
|
+
SnappyStats.connection.expireat redis_key, tsround + ttl
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.get(gran, from, to, key)
|
82
|
+
granularity = GRANULARITIES[gran]
|
83
|
+
size = granularity[:size]
|
84
|
+
factor = granularity[:factor]
|
85
|
+
|
86
|
+
from = self.getFactoredTimestamp( from, factor )
|
87
|
+
to = self.getFactoredTimestamp( to, factor )
|
88
|
+
|
89
|
+
ts = from
|
90
|
+
i = 0
|
91
|
+
results = {}
|
92
|
+
while ts <= to
|
93
|
+
tsround = getRoundedTimestamp( ts, size * factor )
|
94
|
+
redis_key = "#{hash_key}:#{key}:#{gran}:#{tsround}"
|
95
|
+
results[ts] = SnappyStats.connection.hget( redis_key, ts )
|
96
|
+
i = i+1
|
97
|
+
ts = ts + GRANULARITIES[gran][:factor]
|
98
|
+
end
|
99
|
+
results
|
100
|
+
end
|
101
|
+
|
102
|
+
config.init!
|
103
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'snappy_stats/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "snappy_stats"
|
8
|
+
spec.version = SnappyStats::VERSION
|
9
|
+
spec.authors = ["Tim Williams"]
|
10
|
+
spec.email = ["tim@teachmatic.com"]
|
11
|
+
spec.description = %q{Metrics storage and retrieval library for time series data.}
|
12
|
+
spec.summary = %q{Redis backed simple time series statistics gem}
|
13
|
+
spec.homepage = ""
|
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", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "fakeredis"
|
25
|
+
spec.add_development_dependency "timecop"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snappy_stats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
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'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakeredis
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: timecop
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Metrics storage and retrieval library for time series data.
|
95
|
+
email:
|
96
|
+
- tim@teachmatic.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/snappy_stats.rb
|
108
|
+
- lib/snappy_stats/config.rb
|
109
|
+
- lib/snappy_stats/version.rb
|
110
|
+
- snappy_stats.gemspec
|
111
|
+
homepage: ''
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 1.8.23
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: Redis backed simple time series statistics gem
|
136
|
+
test_files: []
|