ristretta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e2dee7894e25b7b85adc61ae2f669b6c459bcfc82507b25806416f182053f24
4
+ data.tar.gz: 63a9267063194183b52ec09b7f2100eb5ad05c58ec918cb44ceec6b8bf06de20
5
+ SHA512:
6
+ metadata.gz: 6d7bf30b573e099f82af6a010fd61bdb915ac83c86e79e8fae3463b74f975193fd17c59e9d97345b99ca9f505a3ee6987198cc80b53e7db3210c66bc601e7259
7
+ data.tar.gz: a847a5732bc5863be930440eb533818cfe0cd8e04742fffe229607e7f732cec012bc54e9426c76d83e39aa6a69734121d560b2e380ea37de8cc21af1e70b95e1
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.4.5
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-27
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ristretta.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ristretta (0.1.0)
5
+ redis
6
+ redis-namespace
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.4.4)
13
+ parallel (1.20.1)
14
+ parser (3.0.1.1)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.0.0)
17
+ rake (13.0.3)
18
+ redis (4.2.5)
19
+ redis-namespace (1.8.1)
20
+ redis (>= 3.0.4)
21
+ regexp_parser (2.1.1)
22
+ rexml (3.2.5)
23
+ rspec (3.10.0)
24
+ rspec-core (~> 3.10.0)
25
+ rspec-expectations (~> 3.10.0)
26
+ rspec-mocks (~> 3.10.0)
27
+ rspec-core (3.10.1)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-expectations (3.10.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-mocks (3.10.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-support (3.10.2)
36
+ rubocop (1.12.1)
37
+ parallel (~> 1.10)
38
+ parser (>= 3.0.0.0)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ regexp_parser (>= 1.8, < 3.0)
41
+ rexml
42
+ rubocop-ast (>= 1.2.0, < 2.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 3.0)
45
+ rubocop-ast (1.4.1)
46
+ parser (>= 2.7.1.5)
47
+ ruby-progressbar (1.11.0)
48
+ unicode-display_width (2.0.0)
49
+
50
+ PLATFORMS
51
+ x86_64-darwin-19
52
+
53
+ DEPENDENCIES
54
+ rake (~> 13.0)
55
+ ristretta!
56
+ rspec (~> 3.0)
57
+ rubocop (~> 1.7)
58
+
59
+ BUNDLED WITH
60
+ 2.2.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Lord Meidar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Ristretta
2
+
3
+ Ristretta is a library that allows easily collecting event data in Redis sorted sets. It uses a timestamp as the event score to ensure the oridnality.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ristretta'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ristretta
20
+
21
+ ## Usage
22
+
23
+ ### Configuration
24
+
25
+ Ristretta accepts a configuration block that allows the following options:
26
+
27
+ ```ruby
28
+ Ristretta.config do |config|
29
+ config.redis_client = redis # a pre-configured Redis client
30
+ config.namespace = 'ristretta' # namespace for all your redis keys
31
+ config.version = 1 # easily expire all previous event collectors
32
+ config.subject_id_method = :id # This is the method that will be used to extract an unique id for the passed event_subject instance
33
+ end
34
+
35
+ ```
36
+
37
+ ### Recording Events
38
+
39
+ ```ruby
40
+ Ristretta::Event.create(event_subject, event_type, event_attrs, timestamp)
41
+ ```
42
+
43
+ | Parameter | Description | Required | Default |
44
+ | --------- | ----------- | -------- | ------- |
45
+ | event_subject | the object that the event will be associated with | V | X |
46
+ | event_type | the event name (string) | V | X |
47
+ | event_attrs | hash with the event details | x | `{}` |
48
+ | timestamp | the event timestamp | x | `Time.now.to_i` |
49
+
50
+
51
+ ### Fetching Events
52
+
53
+ ```ruby
54
+ Ristretta::Event.find(options = {})
55
+ ```
56
+
57
+ `options` includes the following keys:
58
+
59
+ | key | Description | Required | Default |
60
+ | --------- | ----------- | -------- | ------- |
61
+ | event_subject | the object that the event will be associated with | V | X |
62
+ | event_type | the event name (string) | V | X |
63
+ | since | timestamp of the olders event you want | x | beginning of time |
64
+ | until | timestamp of the newest event you want | x | `Time.now.to_i` |
65
+
66
+ the returned value is a series of time ordered events (new to old) that will respond to `event_attrs` and `timestamp`
67
+
68
+ ##
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eladmeidar/ristretta.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ristretta"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/ristretta.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'redis-namespace'
5
+ require 'json'
6
+ require_relative "ristretta/version"
7
+ require_relative "ristretta/exceptions"
8
+ require_relative "ristretta/configuration"
9
+ require_relative "ristretta/event"
10
+
11
+ module Ristretta
12
+
13
+ def self.config(&block)
14
+ yield(configuration)
15
+ end
16
+
17
+ def self.client
18
+ configuration.client
19
+ end
20
+
21
+ def self.event_key(options = {})
22
+ key = "events:v#{configuration.version.to_s}:#{options[:event_subject].send(configuration.subject_id_method)}:#{options[:event_type]}"
23
+ save_key_name(key)
24
+ key
25
+ end
26
+
27
+ def self.clear_all_events!
28
+ client.smembers("events:keys").each do |key|
29
+ client.del key
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def self.save_key_name(key)
36
+ client.sadd "events:keys", key
37
+ end
38
+
39
+ def self.configuration
40
+ @configuration || Ristretta::Configuration.new
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ module Ristretta
2
+ class Configuration
3
+ attr_accessor :redis_client,
4
+ :namespace,
5
+ :subject_id_method,
6
+ :version
7
+
8
+ def initialize
9
+ @version = "1"
10
+ @namespace = "ristretta"
11
+ @subject_id_method = :id
12
+ end
13
+
14
+ def client
15
+ @client ||= Redis::Namespace.new(namespace.to_sym, redis: redis_client)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module Ristretta
2
+ class Event
3
+ attr_reader :event_attrs, :timestamp
4
+
5
+ def initialize(event_attrs, timestamp)
6
+ @event_attrs = JSON.parse(event_attrs)
7
+ @timestamp = timestamp.to_i
8
+ end
9
+
10
+ def Event.create(event_subject, event_type, event_attrs, timestamp = Time.now.to_i)
11
+ Ristretta.client.zadd(Ristretta.event_key({
12
+ event_type: event_type,
13
+ event_subject: event_subject
14
+ }), timestamp, (event_attrs.merge(timestamp: timestamp.to_i)).to_json, nx: true)
15
+ end
16
+
17
+ def Event.find(options = {})
18
+ raise(Exceptions::SubjectNotSpecified, "event_subject must be specified") if options[:event_subject].nil?
19
+ raise(Exceptions::TypeNotSpecified, "event_type must be specified") if options[:event_type].nil?
20
+
21
+ start_timestamp = options[:since].to_i
22
+ end_timestamp = options[:until] || Time.now.to_i
23
+
24
+ Ristretta.client.zrangebyscore(Ristretta.event_key(options), start_timestamp, end_timestamp, with_scores: true).collect do |event_data|
25
+ self.new(event_data.first, event_data.last.to_i)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Ristretta
2
+ module Exceptions
3
+ class TypeNotSpecified < ArgumentError; end
4
+ class SubjectNotSpecified < ArgumentError; end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ristretta
4
+ VERSION = "0.1.0"
5
+ end
data/ristretta.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ristretta/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ristretta"
7
+ spec.version = Ristretta::VERSION
8
+ spec.authors = ["Lord Meidar"]
9
+ spec.email = ["elad@shinobidevs.com"]
10
+
11
+ spec.summary = "RISTRETTA is a Redis based library to track time series events"
12
+ spec.description = "RISTRETTA is a Redis based library to track time series events"
13
+ spec.homepage = "https://github.com/eladmeidar/ristretta"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/eladmeidar/ristretta"
21
+ spec.metadata["changelog_uri"] = "https://github.com/eladmeidar/ristretta"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "redis"
34
+ spec.add_dependency "redis-namespace"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ristretta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lord Meidar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: redis-namespace
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: RISTRETTA is a Redis based library to track time series events
42
+ email:
43
+ - elad@shinobidevs.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - lib/ristretta.rb
61
+ - lib/ristretta/configuration.rb
62
+ - lib/ristretta/event.rb
63
+ - lib/ristretta/exceptions.rb
64
+ - lib/ristretta/version.rb
65
+ - ristretta.gemspec
66
+ homepage: https://github.com/eladmeidar/ristretta
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ allowed_push_host: https://rubygems.org
71
+ homepage_uri: https://github.com/eladmeidar/ristretta
72
+ source_code_uri: https://github.com/eladmeidar/ristretta
73
+ changelog_uri: https://github.com/eladmeidar/ristretta
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.4.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.0.8
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: RISTRETTA is a Redis based library to track time series events
93
+ test_files: []