trifle-stats 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bdd4c1f777b76f6684d0ba5bbe2e3931585506325b4984192754570631d0c988
4
- data.tar.gz: 11b7dafeaead2148f1659c663335b6a0d6e4f4daba8dd7b9dbf339783d757db6
3
+ metadata.gz: 61ed8d636a39a6b304b1877d29f6c70b6653b59c0a0a4e18bf9099cf0c903c7d
4
+ data.tar.gz: 99488122d8ac47e900dd3764cc61f3012a00a47433dd8b6a857185220fc72335
5
5
  SHA512:
6
- metadata.gz: 8a84f59fe3277ce49ab4608a2fd47e7238488ae98e616d071c290900843b13ce33560a46d10a94d402cf33416b417f22f39c1bebfeb4340d6d5aae33753d7655
7
- data.tar.gz: 6f4b533bebe8f4fb2cda97d40b7e3b7d503033be99441194dfe5266eec35462c3b4db1641742c67a035692e618c3b9411fcb29471ef2592a74e0bd705401de89
6
+ metadata.gz: 7e4885fb88963d054b811c68a717e7e77dc9ddad55c11a499f98d599531f2fda6c12dd14e8cb680c367a7260976a6a411f7008845913323813bcaa9d1854e1bb
7
+ data.tar.gz: ca1153f0d6ea56daf21d1fd507e0603e82bcfda0670a2093e6148a199928350d31ca7a980067d90add1e8c810fb719024602596764e8b025a490b7f9e3970780
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trifle-stats (0.1.0)
4
+ trifle-stats (0.2.0)
5
5
  redis (>= 4.2)
6
6
  tzinfo (~> 2.0)
7
7
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Trifle
1
+ # Trifle::Stats
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/trifle-stats.svg)](https://badge.fury.io/rb/trifle-stats)
4
4
  ![Ruby](https://github.com/trifle-io/trifle-stats/workflows/Ruby/badge.svg?branch=main)
@@ -6,9 +6,13 @@
6
6
 
7
7
  Simple analytics backed by Redis, Postgres, MongoDB, Google Analytics, Segment, or whatever. [^1]
8
8
 
9
- 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.
9
+ `Trifle::Stats` 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.
10
10
 
11
- [^1] TBH only Redis for now 💔.
11
+ [^1]: TBH only Redis for now 💔.
12
+
13
+ ## Documentation
14
+
15
+ You can find guides and documentation at https://trifle.io/docs/stats
12
16
 
13
17
  ## Installation
14
18
 
@@ -54,19 +58,6 @@ Trifle::Stats.track(key: 'event::logs', at: Time.now, values: {count: 1, duratio
54
58
  => [{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}}]
55
59
  ```
56
60
 
57
- You can also store nested counters like
58
- ```ruby
59
- Trifle::Stats.track(key: 'event::logs', at: Time.now, values: {
60
- count: 1,
61
- duration: {
62
- parsing: 21,
63
- compression: 8,
64
- upload: 1
65
- },
66
- lines: 25432754
67
- })
68
- ```
69
-
70
61
  ### Get values
71
62
 
72
63
  Retrieve your values for specific `range`.
@@ -75,55 +66,6 @@ Trifle::Stats.values(key: 'event::logs', from: Time.now, to: Time.now, range: :d
75
66
  => [{2021-01-25 00:00:00 +0100=>{"count"=>3, "duration"=>8, "lines"=>658}}]
76
67
  ```
77
68
 
78
- ### Configuration
79
-
80
- Configuration allows you to specify:
81
- - `driver` - backend driver used to persist and retrieve data.
82
- - `track_ranges` - list of timeline ranges you would like to track. Value must be list of symbols, defaults to `[:minute, :hour, :day, :week, :month, :quarter, :year]`.
83
- - `separator` - keys can get serialized in backend, separator is used to join these values. Value must be string, defaults to `::`.
84
- - `time_zone` - TZInfo zone to properly generate range for timeline values. Value must be valid TZ string identifier, otherwise it defaults and fallbacks to `'GMT'`.
85
- - `beginning_of_week` - first day of week. Value must be string, defaults to `:monday`.
86
-
87
- Gem expecs global configuration to be present. You can do this by creating initializer, or calling it on the beginning of your ruby script.
88
-
89
- Custom configuration can be passed as a keyword argument to `Resource` objects and all module methods (`track`, `values`). This way you can pass different driver or ranges for different type of data youre storing - ie set different ranges or set expiration date on your data.
90
-
91
- ```ruby
92
- configuration = Trifle::Stats::Configuration.new
93
- configuration.driver = Trifle::Stats::Driver::Redis.new
94
- configuration.track_ranges = [:day]
95
- configuration.time_zone = 'GMT'
96
- configuration.separator = '#'
97
-
98
- # or use different driver
99
- mongo_configuration = Trifle::Stats::Configuration.new
100
- mongo_configuration.driver = Trifle::Stats::Driver::MongoDB.new
101
- mongo_configuration.time_zone = 'Asia/Dubai'
102
- ```
103
-
104
- You can then pass it into module methods.
105
- ```ruby
106
- Trifle::Stats.track(key: 'event#checkout', at: Time.now, values: {count: 1}, config: configuration)
107
-
108
- Trifle::Stats.track(key: 'event#checkout', at: Time.now, values: {count: 1}, config: mongo_configuration)
109
- ```
110
-
111
- ### Driver
112
-
113
- Driver is a wrapper around existing client libraries that talk to DB or API. It is used to store and retrieve values. You can read more in [Driver Readme](https://github.com/trifle-io/trifle-stats/tree/main/lib/trifle/ruby/driver).
114
-
115
- ## Development
116
-
117
- 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.
118
-
119
- 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).
120
-
121
- ## Gitpod
122
-
123
- This repository comes Gitpod ready. If you wanna try and get your hands dirty with Trifle, click [here](https://gitpod.io/#https://github.com/trifle-io/trifle-stats) and watch magic happening.
124
-
125
- It launches from custom base image that includes Redis, MongoDB, Postgres & MariaDB. This should give you enough playground to launch `./bin/console` and start messing around. You can see the Gitpod image in the [hub](https://hub.docker.com/r/trifle/gitpod).
126
-
127
69
  ## Contributing
128
70
 
129
71
  Bug reports and pull requests are welcome on GitHub at https://github.com/trifle-io/trifle-stats.
data/lib/trifle/stats.rb CHANGED
@@ -6,6 +6,7 @@ require 'trifle/stats/mixins/packer'
6
6
  require 'trifle/stats/nocturnal'
7
7
  require 'trifle/stats/configuration'
8
8
  require 'trifle/stats/operations/timeseries/increment'
9
+ require 'trifle/stats/operations/timeseries/set'
9
10
  require 'trifle/stats/operations/timeseries/values'
10
11
  require 'trifle/stats/version'
11
12
 
@@ -33,6 +34,15 @@ module Trifle
33
34
  ).perform
34
35
  end
35
36
 
37
+ def self.assert(key:, at:, values:, config: nil)
38
+ Trifle::Stats::Operations::Timeseries::Set.new(
39
+ key: key,
40
+ at: at,
41
+ values: values,
42
+ config: config
43
+ ).perform
44
+ end
45
+
36
46
  def self.values(key:, from:, to:, range:, config: nil)
37
47
  Trifle::Stats::Operations::Timeseries::Values.new(
38
48
  key: key,
@@ -1,59 +1,11 @@
1
1
  # Driver
2
2
 
3
3
  Driver is a wrapper class that persists and retrieves values from backend. It needs to implement:
4
- - `inc(key:, **values)` method to store values
5
- - `get(key:)` method to retrieve values
6
-
7
- ## Packer Mixin
8
-
9
- Some databases cannot store nested hashes/values. Or they cannot perform increment on nested values that does not exist. For this reason you can use Packer mixin that helps you convert values to dot notation.
10
-
11
- ```ruby
12
- class Sample
13
- include Trifle::Stats::Mixins::Packer
14
- end
15
-
16
- values = { a: 1, b: { c: 22, d: 33 } }
17
- => {:a=>1, :b=>{:c=>22, :d=>33}}
18
-
19
- packed = Sample.pack(hash: values)
20
- => {"a"=>1, "b.c"=>22, "b.d"=>33}
21
-
22
- Sample.unpack(hash: packed)
23
- => {"a"=>1, "b"=>{"c"=>22, "d"=>33}}
24
- ```
25
4
 
26
- ## Dummy driver
27
-
28
- Sample of using custom driver that does, well, nothing useful.
29
-
30
- ```ruby
31
- irb(main):001:1* class Dummy
32
- irb(main):002:2* def inc(key:, **values)
33
- irb(main):003:2* puts "Dumping #{key} => #{values}"
34
- irb(main):004:1* end
35
- irb(main):005:2* def get(key:)
36
- irb(main):006:2* puts "Random for #{key}"
37
- irb(main):007:2* { count: rand(1000) }
38
- irb(main):008:1* end
39
- irb(main):009:0> end
40
- => :get
41
-
42
- irb(main):010:0> c = Trifle::Stats::Configuration.new
43
- => #<Trifle::Stats::Configuration:0x00007fe179aed848 @separator="::", @ranges=[:minute, :hour, :day, :week, :month, :quarter, :year], @beginning_of_week=:monday, @time_zone="GMT">
44
-
45
- irb(main):011:0> c.driver = Dummy.new
46
- => #<Dummy:0x00007fe176302ac8>
47
-
48
- irb(main):012:0> c.track_ranges = [:minute, :hour]
49
- => [:minute, :hour]
5
+ - `inc(key:, **values)` method add values
6
+ - `set(key:, **values)` method set values
7
+ - `get(key:)` method to retrieve values
50
8
 
51
- irb(main):013:0> Trifle::Stats.track(key: 'sample', at: Time.now, values: {count: 1}, config: c)
52
- Dumping sample::minute::1611696240 => {:count=>1}
53
- Dumping sample::hour::1611694800 => {:count=>1}
54
- => [{2021-01-26 21:24:00 +0000=>{:count=>1}}, {2021-01-26 21:00:00 +0000=>{:count=>1}}]
9
+ ## Documentation
55
10
 
56
- irb(main):014:0> Trifle::Stats.values(key: 'sample', from: Time.now, to: Time.now, range: :hour, config: c)
57
- Random for sample::hour::1611694800
58
- => [{2021-01-26 21:00:00 +0000=>{:count=>405}}]
59
- ```
11
+ For more details about Drivers, please refer to https://trifle.io/docs/stats/drivers
@@ -23,6 +23,12 @@ module Trifle
23
23
  end
24
24
  end
25
25
 
26
+ def set(key:, **values)
27
+ pkey = [@prefix, key].join('::')
28
+
29
+ @client.hmset(pkey, *self.class.pack(hash: values))
30
+ end
31
+
26
32
  def get(key:)
27
33
  pkey = [@prefix, key].join('::')
28
34
 
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
5
+ module Operations
6
+ module Timeseries
7
+ class Set
8
+ attr_reader :key, :values
9
+
10
+ def initialize(**keywords)
11
+ @key = keywords.fetch(:key)
12
+ @at = keywords.fetch(:at)
13
+ @values = keywords.fetch(:values)
14
+ @config = keywords[:config]
15
+ end
16
+
17
+ def config
18
+ @config || Trifle::Stats.default
19
+ end
20
+
21
+ def perform
22
+ config.ranges.map do |range|
23
+ at = Nocturnal.new(@at, config: config).send(range)
24
+ config.driver.set(
25
+ key: [key, range, at.to_i].join(config.separator),
26
+ **values
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Trifle
4
4
  module Stats
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
data/trifle-stats.gemspec CHANGED
@@ -6,15 +6,17 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ['Jozef Vaclavik']
7
7
  spec.email = ['jozef@hey.com']
8
8
 
9
- spec.summary = 'Simple analytics for tracking events and status counts'
10
- spec.description = 'Trifle Stats 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-stats"
9
+ spec.summary = 'Simple analytics backed by Redis, Postgres, MongoDB, '\
10
+ 'Google Analytics, Segment, or whatever.'
11
+ spec.description = 'Trifle::Stats is a way too simple timeline analytics '\
12
+ 'that helps you track custom metrics. Automatically '\
13
+ 'increments counters for each enabled range. '\
14
+ 'It supports timezones and different week beginning.'
15
+ spec.homepage = "https://trifle.io"
14
16
  spec.required_ruby_version = Gem::Requirement.new('>= 2.6')
15
17
 
16
18
  spec.metadata['homepage_uri'] = spec.homepage
17
- spec.metadata['source_code_uri'] = "https://github.com/trifle-io/trifle-stats"
19
+ spec.metadata['source_code_uri'] = 'https://github.com/trifle-io/trifle-stats'
18
20
 
19
21
  # Specify which files should be added to the gem when it is released.
20
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trifle-stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jozef Vaclavik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-30 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,8 +122,9 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '2.0'
125
- description: Trifle Stats allows you to submit counters andautomatically storing them
126
- for each range.Supports multiple backend drivers.
125
+ description: Trifle::Stats is a way too simple timeline analytics that helps you track
126
+ custom metrics. Automatically increments counters for each enabled range. It supports
127
+ timezones and different week beginning.
127
128
  email:
128
129
  - jozef@hey.com
129
130
  executables: []
@@ -139,7 +140,6 @@ files:
139
140
  - ".rubocop.yml"
140
141
  - ".ruby-version"
141
142
  - ".tool-versions"
142
- - ".travis.yml"
143
143
  - Gemfile
144
144
  - Gemfile.lock
145
145
  - LICENSE
@@ -155,13 +155,14 @@ files:
155
155
  - lib/trifle/stats/mixins/packer.rb
156
156
  - lib/trifle/stats/nocturnal.rb
157
157
  - lib/trifle/stats/operations/timeseries/increment.rb
158
+ - lib/trifle/stats/operations/timeseries/set.rb
158
159
  - lib/trifle/stats/operations/timeseries/values.rb
159
160
  - lib/trifle/stats/version.rb
160
161
  - trifle-stats.gemspec
161
- homepage: https://github.com/trifle-io/trifle-stats
162
+ homepage: https://trifle.io
162
163
  licenses: []
163
164
  metadata:
164
- homepage_uri: https://github.com/trifle-io/trifle-stats
165
+ homepage_uri: https://trifle.io
165
166
  source_code_uri: https://github.com/trifle-io/trifle-stats
166
167
  post_install_message:
167
168
  rdoc_options: []
@@ -181,5 +182,6 @@ requirements: []
181
182
  rubygems_version: 3.2.3
182
183
  signing_key:
183
184
  specification_version: 4
184
- summary: Simple analytics for tracking events and status counts
185
+ summary: Simple analytics backed by Redis, Postgres, MongoDB, Google Analytics, Segment,
186
+ or whatever.
185
187
  test_files: []
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- before_install: gem install bundler -v 2.1.4