trifle-ruby 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +14 -0
- data/.ruby-version +1 -0
- data/.tool-versions +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +66 -0
- data/LICENSE +21 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/trifle/ruby.rb +45 -0
- data/lib/trifle/ruby/client.rb +21 -0
- data/lib/trifle/ruby/configuration.rb +38 -0
- data/lib/trifle/ruby/driver/redis.rb +30 -0
- data/lib/trifle/ruby/mixins/packer.rb +53 -0
- data/lib/trifle/ruby/nocturnal.rb +126 -0
- data/lib/trifle/ruby/resource.rb +36 -0
- data/lib/trifle/ruby/version.rb +7 -0
- data/trifle-ruby.gemspec +36 -0
- metadata +179 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 47c197bc8154939d1331b265309acc9c9cd121f13bb27e9dbd73118dad8ba469
|
4
|
+
data.tar.gz: 1236e4ba3bb3eafda1eb765a1cc88718697a9133f18b70bf0ea308f46e1247ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 449db56a29665c5f6d555ec9cb295aed5042cc80d8dbc5441c7a2c95079999438bc2d0c4e7fe7acfce56832d3bdd4e588698c51db2891792b7ce523065e18577
|
7
|
+
data.tar.gz: 16acbd7793798bc895e88ae257bee2f59461d2531fdab09e5e0f6b5c98444deb0073099a85acf0745c97d88cfbe9977aaee547669a7a311420dee49320f10f88
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.0.0
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
trifle-ruby (3.0.0)
|
5
|
+
redis (>= 4.2)
|
6
|
+
tzinfo (~> 2.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.2)
|
12
|
+
byebug (11.1.3)
|
13
|
+
concurrent-ruby (1.1.8)
|
14
|
+
diff-lcs (1.4.4)
|
15
|
+
dotenv (2.7.6)
|
16
|
+
parallel (1.20.1)
|
17
|
+
parser (3.0.0.0)
|
18
|
+
ast (~> 2.4.1)
|
19
|
+
rainbow (3.0.0)
|
20
|
+
rake (12.3.3)
|
21
|
+
redis (4.2.5)
|
22
|
+
regexp_parser (2.0.3)
|
23
|
+
rexml (3.2.4)
|
24
|
+
rspec (3.10.0)
|
25
|
+
rspec-core (~> 3.10.0)
|
26
|
+
rspec-expectations (~> 3.10.0)
|
27
|
+
rspec-mocks (~> 3.10.0)
|
28
|
+
rspec-core (3.10.1)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-expectations (3.10.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.10.0)
|
33
|
+
rspec-mocks (3.10.1)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.10.0)
|
36
|
+
rspec-support (3.10.1)
|
37
|
+
rubocop (1.0.0)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 2.7.1.5)
|
40
|
+
rainbow (>= 2.2.2, < 4.0)
|
41
|
+
regexp_parser (>= 1.8)
|
42
|
+
rexml
|
43
|
+
rubocop-ast (>= 0.6.0)
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
46
|
+
rubocop-ast (1.4.1)
|
47
|
+
parser (>= 2.7.1.5)
|
48
|
+
ruby-progressbar (1.11.0)
|
49
|
+
tzinfo (2.0.4)
|
50
|
+
concurrent-ruby (~> 1.0)
|
51
|
+
unicode-display_width (1.7.0)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
x86_64-darwin-20
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
bundler (~> 2.1)
|
58
|
+
byebug
|
59
|
+
dotenv
|
60
|
+
rake (~> 12.0)
|
61
|
+
rspec (~> 3.0)
|
62
|
+
rubocop (= 1.0.0)
|
63
|
+
trifle-ruby!
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.2.3
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Jozef Vaclavik
|
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 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,
|
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Trifle
|
2
|
+
|
3
|
+
Trifle - simple analytics backed by Redis, Postgres, MongoDB, Google Analytics, Segment, or whatever. [^1]
|
4
|
+
|
5
|
+
Trifle is a _way too_ simple timeline analytics that helps you track custom metrics. Automatically increments counters for each enabled range. It supports timezones and different week beginning.
|
6
|
+
|
7
|
+
[^1] TBH only Redis for now 💔.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'trifle-ruby'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install trifle-ruby
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
You don't need to use it with Rails, but you still need to run `Trifle::Ruby.configure`. If youre running it with Rails, create `config/initializers/trifle-ruby.rb` and configure the gem.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Trifle::Ruby.configure do |config|
|
31
|
+
config.driver = Trifle::Ruby::Driver::Redis.new
|
32
|
+
config.track_ranges = [:hour, :day]
|
33
|
+
config.time_zone = 'Europe/Bratislava'
|
34
|
+
config.beginning_of_week = :monday
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Available ranges are `:minute`, `:hour`, `:day`, `:week`, `:month`, `:quarter`, `:year`.
|
39
|
+
|
40
|
+
Now track your first metrics
|
41
|
+
```ruby
|
42
|
+
Trifle::Ruby.track(key: 'event::logs', at: Time.zone.now, values: {count: 1, duration: 2, lines: 241})
|
43
|
+
=> [{2021-01-25 16:00:00 +0100=>{:count=>1, :duration=>2, :lines=>241}}, {2021-01-25 00:00:00 +0100=>{:count=>1, :duration=>2, :lines=>241}}]
|
44
|
+
# or do it few more times
|
45
|
+
Trifle::Ruby.track(key: 'event::logs', at: Time.zone.now, values: {count: 1, duration: 1, lines: 56})
|
46
|
+
=> [{2021-01-25 16:00:00 +0100=>{:count=>1, :duration=>1, :lines=>56}}, {2021-01-25 00:00:00 +0100=>{:count=>1, :duration=>1, :lines=>56}}]
|
47
|
+
Trifle::Ruby.track(key: 'event::logs', at: Time.zone.now, values: {count: 1, duration: 5, lines: 361})
|
48
|
+
=> [{2021-01-25 16:00:00 +0100=>{:count=>1, :duration=>5, :lines=>361}}, {2021-01-25 00:00:00 +0100=>{:count=>1, :duration=>5, :lines=>361}}]
|
49
|
+
```
|
50
|
+
|
51
|
+
You can then retrieve your values for specific `range`.
|
52
|
+
```ruby
|
53
|
+
Trifle::Ruby.values_for(key: 'event::logs', from: Time.now.beginning_of_day, to: Time.now.end_of_day, range: :day)
|
54
|
+
=> [{2021-01-25 00:00:00 +0100=>{"count"=>3, "duration"=>8, "lines"=>658}}]
|
55
|
+
```
|
56
|
+
|
57
|
+
You can also store nested counters like
|
58
|
+
```ruby
|
59
|
+
Trifle::Ruby.track(key: 'event::logs', at: Time.zone.now, values: {
|
60
|
+
count: 1,
|
61
|
+
duration: {
|
62
|
+
parsing: 21,
|
63
|
+
compression: 8,
|
64
|
+
upload: 1
|
65
|
+
},
|
66
|
+
lines: 25432754
|
67
|
+
})
|
68
|
+
```
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/trifle-io/trifle-ruby.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "trifle/ruby"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/trifle/ruby.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'trifle/ruby/driver/redis'
|
4
|
+
require 'trifle/ruby/mixins/packer'
|
5
|
+
require 'trifle/ruby/nocturnal'
|
6
|
+
require 'trifle/ruby/client'
|
7
|
+
require 'trifle/ruby/configuration'
|
8
|
+
require 'trifle/ruby/resource'
|
9
|
+
require 'trifle/ruby/version'
|
10
|
+
|
11
|
+
module Trifle
|
12
|
+
module Ruby
|
13
|
+
class Error < StandardError; end
|
14
|
+
|
15
|
+
def self.config
|
16
|
+
@config ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
yield(config)
|
21
|
+
|
22
|
+
config
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.client
|
26
|
+
@client ||= Client.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.track(key:, at:, values:)
|
30
|
+
config.ranges.map do |range|
|
31
|
+
Resource.new(
|
32
|
+
key: key,
|
33
|
+
range: range,
|
34
|
+
at: Nocturnal.new(at).send("beginning_of_#{range}")
|
35
|
+
).increment(**values)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.values_for(key:, from:, to:, range:)
|
40
|
+
Nocturnal.timeline(from: from, to: to, range: range).map do |at|
|
41
|
+
Resource.new(key: key, range: range, at: at).values
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trifle
|
4
|
+
module Ruby
|
5
|
+
class Client
|
6
|
+
def get(key:)
|
7
|
+
driver.get(key: key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def inc(key:, **values)
|
11
|
+
driver.inc(key: key, **values)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def driver
|
17
|
+
@driver ||= Trifle::Ruby.config.driver
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tzinfo'
|
4
|
+
|
5
|
+
module Trifle
|
6
|
+
module Ruby
|
7
|
+
class Configuration
|
8
|
+
attr_accessor :driver, :track_ranges, :separator, :time_zone,
|
9
|
+
:beginning_of_week
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@separator = '::'
|
13
|
+
@ranges = %i[minute hour day week month quarter year]
|
14
|
+
@beginning_of_week = :monday
|
15
|
+
end
|
16
|
+
|
17
|
+
def tz
|
18
|
+
TZInfo::Timezone.get(@time_zone)
|
19
|
+
rescue TZInfo::InvalidTimezoneIdentifier => e
|
20
|
+
puts "Trifle: #{e} - #{time_zone_name}; Defaulting to GMT."
|
21
|
+
|
22
|
+
TZInfo::Timezone.get('GMT')
|
23
|
+
end
|
24
|
+
|
25
|
+
def ranges
|
26
|
+
return @ranges if blank?(track_ranges)
|
27
|
+
|
28
|
+
@ranges & track_ranges
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def blank?(obj)
|
34
|
+
obj.respond_to?(:empty?) ? !!obj.empty? : !obj
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
module Trifle
|
6
|
+
module Ruby
|
7
|
+
module Driver
|
8
|
+
class Redis
|
9
|
+
attr_accessor :prefix
|
10
|
+
|
11
|
+
def initialize(client = ::Redis.current, prefix: 'bw')
|
12
|
+
@client = client
|
13
|
+
@prefix = prefix
|
14
|
+
end
|
15
|
+
|
16
|
+
def inc(key:, **values)
|
17
|
+
pkey = [@prefix, key].join('::')
|
18
|
+
values.each do |k, c|
|
19
|
+
@client.hincrby(pkey, k, c)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def get(key:)
|
24
|
+
pkey = [@prefix, key].join('::')
|
25
|
+
@client.hgetall(pkey)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trifle
|
4
|
+
module Ruby
|
5
|
+
module Mixins
|
6
|
+
module Packer
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def pack(hash:, prefix: nil)
|
13
|
+
hash.inject({}) do |o, (k, v)|
|
14
|
+
key = [prefix, k].compact.join('.')
|
15
|
+
if v.instance_of?(Hash)
|
16
|
+
o.update(
|
17
|
+
pack(hash: v, prefix: key)
|
18
|
+
)
|
19
|
+
else
|
20
|
+
o.update({ key => v })
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def unpack(hash:)
|
26
|
+
hash.inject({}) do |out, (key, v)|
|
27
|
+
deep_merge(
|
28
|
+
out,
|
29
|
+
key.split('.').reverse.inject(v.to_i) { |o, k| { k => o } }
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def deep_merge(this_hash, other_hash, &block)
|
35
|
+
deep_merge!(this_hash.dup, other_hash, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def deep_merge!(this_hash, other_hash, &block)
|
39
|
+
this_hash.merge!(other_hash) do |key, this_val, other_val|
|
40
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
41
|
+
deep_merge(this_val, other_val, &block)
|
42
|
+
elsif block_given?
|
43
|
+
block.call(key, this_val, other_val)
|
44
|
+
else
|
45
|
+
other_val
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trifle
|
4
|
+
module Ruby
|
5
|
+
class Nocturnal
|
6
|
+
DAYS_INTO_WEEK = {
|
7
|
+
sunday: 0, monday: 1, tuesday: 2, wednesday: 3,
|
8
|
+
thursday: 4, friday: 5, saturday: 6
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
def self.timeline(from:, to:, range:)
|
12
|
+
list = []
|
13
|
+
item = new(from).send("beginning_of_#{range}")
|
14
|
+
while item < to
|
15
|
+
list << item
|
16
|
+
item = Nocturnal.new(list.last).send("next_#{range}")
|
17
|
+
end
|
18
|
+
list
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(at)
|
22
|
+
@at = at
|
23
|
+
@tz = Trifle::Ruby.config.tz
|
24
|
+
end
|
25
|
+
|
26
|
+
def change(**fractions)
|
27
|
+
Time.new(
|
28
|
+
fractions.fetch(:year, @at.year),
|
29
|
+
fractions.fetch(:month, @at.month),
|
30
|
+
fractions.fetch(:day, @at.day),
|
31
|
+
fractions.fetch(:hour, @at.hour),
|
32
|
+
fractions.fetch(:minute, @at.min),
|
33
|
+
0, # second
|
34
|
+
@tz.utc_offset
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def beginning_of_minute
|
39
|
+
change
|
40
|
+
end
|
41
|
+
|
42
|
+
def next_minute
|
43
|
+
Nocturnal.new(
|
44
|
+
beginning_of_minute + 60
|
45
|
+
).beginning_of_minute
|
46
|
+
end
|
47
|
+
|
48
|
+
def beginning_of_hour
|
49
|
+
change(minute: 0)
|
50
|
+
end
|
51
|
+
|
52
|
+
def next_hour
|
53
|
+
Nocturnal.new(
|
54
|
+
beginning_of_hour + 60 * 60
|
55
|
+
).beginning_of_hour
|
56
|
+
end
|
57
|
+
|
58
|
+
def beginning_of_day
|
59
|
+
change(hour: 0, minute: 0)
|
60
|
+
end
|
61
|
+
|
62
|
+
def next_day
|
63
|
+
Nocturnal.new(
|
64
|
+
beginning_of_day + 60 * 60 * 24
|
65
|
+
).beginning_of_day
|
66
|
+
end
|
67
|
+
|
68
|
+
def beginning_of_week
|
69
|
+
today = beginning_of_day
|
70
|
+
|
71
|
+
(today.to_date - days_to_week_start).to_time
|
72
|
+
end
|
73
|
+
|
74
|
+
def next_week
|
75
|
+
Nocturnal.new(
|
76
|
+
beginning_of_week + 60 * 60 * 24 * 7
|
77
|
+
).beginning_of_week
|
78
|
+
end
|
79
|
+
|
80
|
+
def days_to_week_start
|
81
|
+
start_day_number = DAYS_INTO_WEEK.fetch(
|
82
|
+
Trifle::Ruby.config.beginning_of_week
|
83
|
+
)
|
84
|
+
|
85
|
+
(@at.wday - start_day_number) % 7
|
86
|
+
end
|
87
|
+
|
88
|
+
def beginning_of_month
|
89
|
+
change(day: 1, hour: 0, minute: 0)
|
90
|
+
end
|
91
|
+
|
92
|
+
def next_month
|
93
|
+
Nocturnal.new(
|
94
|
+
beginning_of_month + 60 * 60 * 24 * 31
|
95
|
+
).beginning_of_month
|
96
|
+
end
|
97
|
+
|
98
|
+
def beginning_of_quarter
|
99
|
+
first_quarter_month = @at.month - (2 + @at.month) % 3
|
100
|
+
|
101
|
+
change(
|
102
|
+
month: first_quarter_month,
|
103
|
+
day: 1,
|
104
|
+
hour: 0,
|
105
|
+
minute: 0
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
109
|
+
def next_quarter
|
110
|
+
Nocturnal.new(
|
111
|
+
beginning_of_quarter + 60 * 60 * 24 * 31 * 3
|
112
|
+
).beginning_of_quarter
|
113
|
+
end
|
114
|
+
|
115
|
+
def beginning_of_year
|
116
|
+
change(month: 1, day: 1, hour: 0, minute: 0)
|
117
|
+
end
|
118
|
+
|
119
|
+
def next_year
|
120
|
+
Nocturnal.new(
|
121
|
+
beginning_of_year + 60 * 60 * 24 * 31 * 12
|
122
|
+
).beginning_of_year
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trifle
|
4
|
+
module Ruby
|
5
|
+
class Resource
|
6
|
+
include Mixins::Packer
|
7
|
+
attr_accessor :key, :range, :at
|
8
|
+
|
9
|
+
def initialize(key:, range:, at:)
|
10
|
+
@key = key
|
11
|
+
@range = range
|
12
|
+
@at = at
|
13
|
+
end
|
14
|
+
|
15
|
+
def full_key
|
16
|
+
[key, range, at.to_i].join(Trifle::Ruby.config.separator)
|
17
|
+
end
|
18
|
+
|
19
|
+
def increment(**values)
|
20
|
+
packed = self.class.pack(hash: values)
|
21
|
+
Trifle::Ruby.client.inc(key: full_key, **packed)
|
22
|
+
{
|
23
|
+
at => values
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def values
|
28
|
+
{
|
29
|
+
at => self.class.unpack(
|
30
|
+
hash: Trifle::Ruby.client.get(key: full_key)
|
31
|
+
)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/trifle-ruby.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'lib/trifle/ruby/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'trifle-ruby'
|
5
|
+
spec.version = Trifle::Ruby::VERSION
|
6
|
+
spec.authors = ['Jozef Vaclavik']
|
7
|
+
spec.email = ['jozef@hey.com']
|
8
|
+
|
9
|
+
spec.summary = 'Simple analytics for tracking events and status counts'
|
10
|
+
spec.description = 'Trifle (ruby) allows you to submit counters and'\
|
11
|
+
'automatically storing them for each range.'\
|
12
|
+
'Supports multiple backend drivers.'
|
13
|
+
spec.homepage = "https://github.com/trifle-io/trifle-ruby"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = "https://github.com/trifle-io/trifle-ruby"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_development_dependency('bundler', '~> 2.1')
|
29
|
+
spec.add_development_dependency('byebug', '>= 0')
|
30
|
+
spec.add_development_dependency('dotenv')
|
31
|
+
spec.add_development_dependency('rake', '~> 13.0')
|
32
|
+
spec.add_development_dependency('rspec', '~> 3.2')
|
33
|
+
spec.add_development_dependency('rubocop', '1.0.0')
|
34
|
+
spec.add_runtime_dependency('redis', '>= 4.2')
|
35
|
+
spec.add_runtime_dependency('tzinfo', '~> 2.0')
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trifle-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jozef Vaclavik
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-25 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: '2.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: redis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: tzinfo
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '2.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '2.0'
|
125
|
+
description: Trifle (ruby) allows you to submit counters andautomatically storing
|
126
|
+
them for each range.Supports multiple backend drivers.
|
127
|
+
email:
|
128
|
+
- jozef@hey.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".ruby-version"
|
137
|
+
- ".tool-versions"
|
138
|
+
- ".travis.yml"
|
139
|
+
- Gemfile
|
140
|
+
- Gemfile.lock
|
141
|
+
- LICENSE
|
142
|
+
- README.md
|
143
|
+
- Rakefile
|
144
|
+
- bin/console
|
145
|
+
- bin/setup
|
146
|
+
- lib/trifle/ruby.rb
|
147
|
+
- lib/trifle/ruby/client.rb
|
148
|
+
- lib/trifle/ruby/configuration.rb
|
149
|
+
- lib/trifle/ruby/driver/redis.rb
|
150
|
+
- lib/trifle/ruby/mixins/packer.rb
|
151
|
+
- lib/trifle/ruby/nocturnal.rb
|
152
|
+
- lib/trifle/ruby/resource.rb
|
153
|
+
- lib/trifle/ruby/version.rb
|
154
|
+
- trifle-ruby.gemspec
|
155
|
+
homepage: https://github.com/trifle-io/trifle-ruby
|
156
|
+
licenses: []
|
157
|
+
metadata:
|
158
|
+
homepage_uri: https://github.com/trifle-io/trifle-ruby
|
159
|
+
source_code_uri: https://github.com/trifle-io/trifle-ruby
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '3.0'
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubygems_version: 3.2.3
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: Simple analytics for tracking events and status counts
|
179
|
+
test_files: []
|