trifle-stats 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bdd4c1f777b76f6684d0ba5bbe2e3931585506325b4984192754570631d0c988
4
+ data.tar.gz: 11b7dafeaead2148f1659c663335b6a0d6e4f4daba8dd7b9dbf339783d757db6
5
+ SHA512:
6
+ metadata.gz: 8a84f59fe3277ce49ab4608a2fd47e7238488ae98e616d071c290900843b13ce33560a46d10a94d402cf33416b417f22f39c1bebfeb4340d6d5aae33753d7655
7
+ data.tar.gz: 6f4b533bebe8f4fb2cda97d40b7e3b7d503033be99441194dfe5266eec35462c3b4db1641742c67a035692e618c3b9411fcb29471ef2592a74e0bd705401de89
@@ -0,0 +1,37 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Rspec
35
+ run: bundle exec rspec
36
+ - name: Rubocop
37
+ run: bundle exec rubocop
@@ -0,0 +1,12 @@
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
12
+ /.byebug_history
@@ -0,0 +1,18 @@
1
+ image:
2
+ file: .gitpod/Dockerfile
3
+ tasks:
4
+ - init: bundle install
5
+ command: ./bin/console
6
+ - command: redis-server
7
+ github:
8
+ prebuilds:
9
+ # enable for the master/default branch (defaults to true)
10
+ master: true
11
+ # enable for all branches in this repo (defaults to false)
12
+ branches: false
13
+ # enable for pull requests coming from this repo (defaults to true)
14
+ pullRequests: true
15
+ # add a check to pull requests (defaults to true)
16
+ addCheck: true
17
+ # add a "Review in Gitpod" button as a comment to pull requests (defaults to false)
18
+ addComment: false
@@ -0,0 +1 @@
1
+ FROM trifle/gitpod:0.1.0
@@ -0,0 +1,77 @@
1
+ FROM ubuntu:20.04
2
+
3
+ ENV TZ=Europe/Bratislava
4
+ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
5
+
6
+ RUN useradd gitpod -u 33333
7
+ RUN mkdir -p /home/gitpod && chown gitpod:gitpod /home/gitpod
8
+
9
+ # install ubuntu packages
10
+ RUN apt-get update -q \
11
+ && apt-get install -y \
12
+ build-essential \
13
+ apt-transport-https \
14
+ bash \
15
+ xterm \
16
+ xvfb \
17
+ x11vnc \
18
+ libpq-dev \
19
+ git \
20
+ curl \
21
+ wget \
22
+ unzip \
23
+ dirmngr \
24
+ gpg \
25
+ gnupg2 \
26
+ locales \
27
+ autoconf \
28
+ libncurses5-dev \
29
+ libgl1-mesa-dev \
30
+ libglu1-mesa-dev \
31
+ libpng-dev \
32
+ unixodbc-dev \
33
+ libssl-dev \
34
+ libreadline-dev \
35
+ zlib1g-dev \
36
+ ffmpeg \
37
+ tmux \
38
+ runit-systemd \
39
+ htop \
40
+ vim \
41
+ && apt-get clean
42
+
43
+ #set the locale
44
+ RUN locale-gen en_US.UTF-8
45
+ ENV LANG en_US.UTF-8
46
+ ENV LANGUAGE en_US:en
47
+ ENV LC_ALL en_US.UTF-8
48
+
49
+ RUN curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
50
+ RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
51
+
52
+ RUN apt-get update -q \
53
+ && apt-get install -y \
54
+ postgresql postgresql-contrib \
55
+ # mongodb-org \
56
+ redis-server \
57
+ mariadb-server \
58
+ && apt-get clean
59
+
60
+ USER gitpod
61
+
62
+ #install asdf
63
+ ENV ASDF_ROOT /home/gitpod/.asdf
64
+ ENV PATH "${ASDF_ROOT}/bin:${ASDF_ROOT}/shims:$PATH"
65
+ RUN git clone https://github.com/asdf-vm/asdf.git ${ASDF_ROOT} --branch v0.8.0
66
+
67
+ RUN asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby.git
68
+
69
+ # install ruby
70
+ ENV RUBY_VERSION 3.0.0
71
+ RUN ASDF_RUBY_BUILD_VERSION=v20201225 asdf install ruby ${RUBY_VERSION} \
72
+ && asdf global ruby ${RUBY_VERSION}
73
+
74
+ # throw errors if Gemfile has been modified since Gemfile.lock
75
+ RUN gem install bundler
76
+
77
+ CMD ["bash"]
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,15 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+
5
+ AllCops:
6
+ TargetRubyVersion: '2.6'
7
+ Exclude:
8
+ - 'bin/**/*'
9
+ - 'Rakefile'
10
+ - 'spec/**/*'
11
+ - 'Gemfile'
12
+ - trifle-stats.gemspec
13
+
14
+ Style/Documentation:
15
+ Enabled: false
@@ -0,0 +1 @@
1
+ ruby-3.0.0
@@ -0,0 +1 @@
1
+ ruby 3.0.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in trifle-stats.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ trifle-stats (0.1.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
+ ruby
55
+ x86_64-darwin-20
56
+
57
+ DEPENDENCIES
58
+ bundler (~> 2.1)
59
+ byebug
60
+ dotenv
61
+ rake (~> 12.0)
62
+ rspec (~> 3.0)
63
+ rubocop (= 1.0.0)
64
+ trifle-stats!
65
+
66
+ BUNDLED WITH
67
+ 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.
@@ -0,0 +1,129 @@
1
+ # Trifle
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/trifle-stats.svg)](https://badge.fury.io/rb/trifle-stats)
4
+ ![Ruby](https://github.com/trifle-io/trifle-stats/workflows/Ruby/badge.svg?branch=main)
5
+ [![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/trifle-io/trifle-stats)
6
+
7
+ Simple analytics backed by Redis, Postgres, MongoDB, Google Analytics, Segment, or whatever. [^1]
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.
10
+
11
+ [^1] TBH only Redis for now 💔.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'trifle-stats'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install trifle-stats
28
+
29
+ ## Usage
30
+
31
+ You don't need to use it with Rails, but you still need to run `Trifle::Stats.configure`. If youre running it with Rails, create `config/initializers/trifle-stats.rb` and configure the gem.
32
+
33
+ ```ruby
34
+ Trifle::Stats.configure do |config|
35
+ config.driver = Trifle::Stats::Driver::Redis.new
36
+ config.track_ranges = [:hour, :day]
37
+ config.time_zone = 'Europe/Bratislava'
38
+ config.beginning_of_week = :monday
39
+ end
40
+ ```
41
+
42
+ ### Track values
43
+
44
+ Available ranges are `:minute`, `:hour`, `:day`, `:week`, `:month`, `:quarter`, `:year`.
45
+
46
+ Now track your first metrics
47
+ ```ruby
48
+ Trifle::Stats.track(key: 'event::logs', at: Time.now, values: {count: 1, duration: 2, lines: 241})
49
+ => [{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}}]
50
+ # or do it few more times
51
+ Trifle::Stats.track(key: 'event::logs', at: Time.now, values: {count: 1, duration: 1, lines: 56})
52
+ => [{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}}]
53
+ Trifle::Stats.track(key: 'event::logs', at: Time.now, values: {count: 1, duration: 5, lines: 361})
54
+ => [{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
+ ```
56
+
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
+ ### Get values
71
+
72
+ Retrieve your values for specific `range`.
73
+ ```ruby
74
+ Trifle::Stats.values(key: 'event::logs', from: Time.now, to: Time.now, range: :day)
75
+ => [{2021-01-25 00:00:00 +0100=>{"count"=>3, "duration"=>8, "lines"=>658}}]
76
+ ```
77
+
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
+ ## Contributing
128
+
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/trifle-io/trifle-stats.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trifle/stats"
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__)
@@ -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
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trifle/stats/driver/redis'
4
+ require 'trifle/stats/driver/process'
5
+ require 'trifle/stats/mixins/packer'
6
+ require 'trifle/stats/nocturnal'
7
+ require 'trifle/stats/configuration'
8
+ require 'trifle/stats/operations/timeseries/increment'
9
+ require 'trifle/stats/operations/timeseries/values'
10
+ require 'trifle/stats/version'
11
+
12
+ module Trifle
13
+ module Stats
14
+ class Error < StandardError; end
15
+ class DriverNotFound < Error; end
16
+
17
+ def self.default
18
+ @default ||= Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield(default)
23
+
24
+ default
25
+ end
26
+
27
+ def self.track(key:, at:, values:, config: nil)
28
+ Trifle::Stats::Operations::Timeseries::Increment.new(
29
+ key: key,
30
+ at: at,
31
+ values: values,
32
+ config: config
33
+ ).perform
34
+ end
35
+
36
+ def self.values(key:, from:, to:, range:, config: nil)
37
+ Trifle::Stats::Operations::Timeseries::Values.new(
38
+ key: key,
39
+ from: from,
40
+ to: to,
41
+ range: range,
42
+ config: config
43
+ ).perform
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzinfo'
4
+
5
+ module Trifle
6
+ module Stats
7
+ class Configuration
8
+ attr_writer :driver
9
+ attr_accessor :track_ranges, :separator, :time_zone,
10
+ :beginning_of_week
11
+
12
+ def initialize
13
+ @separator = '::'
14
+ @ranges = %i[minute hour day week month quarter year]
15
+ @beginning_of_week = :monday
16
+ @time_zone = 'GMT'
17
+ end
18
+
19
+ def tz
20
+ TZInfo::Timezone.get(@time_zone)
21
+ rescue TZInfo::InvalidTimezoneIdentifier => e
22
+ puts "Trifle: #{e} - #{time_zone}; Defaulting to GMT."
23
+
24
+ TZInfo::Timezone.get('GMT')
25
+ end
26
+
27
+ def ranges
28
+ return @ranges if blank?(track_ranges)
29
+
30
+ @ranges & track_ranges
31
+ end
32
+
33
+ def driver
34
+ raise DriverNotFound if @driver.nil?
35
+
36
+ @driver
37
+ end
38
+
39
+ private
40
+
41
+ def blank?(obj)
42
+ obj.respond_to?(:empty?) ? !!obj.empty? : !obj
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,59 @@
1
+ # Driver
2
+
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
+
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]
50
+
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}}]
55
+
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
+ ```
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../mixins/packer'
4
+
5
+ module Trifle
6
+ module Stats
7
+ module Driver
8
+ class Process
9
+ include Mixins::Packer
10
+ def initialize
11
+ @data = {}
12
+ end
13
+
14
+ def inc(key:, **values)
15
+ self.class.pack(hash: values).each do |k, c|
16
+ d = @data.fetch(key, {})
17
+ d[k] = d[k].to_i + c
18
+ @data[key] = d
19
+ end
20
+ end
21
+
22
+ def get(key:)
23
+ self.class.unpack(
24
+ hash: @data.fetch(key, {})
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require_relative '../mixins/packer'
5
+
6
+ module Trifle
7
+ module Stats
8
+ module Driver
9
+ class Redis
10
+ include Mixins::Packer
11
+ attr_accessor :prefix
12
+
13
+ def initialize(client = ::Redis.current, prefix: 'trfl')
14
+ @client = client
15
+ @prefix = prefix
16
+ end
17
+
18
+ def inc(key:, **values)
19
+ pkey = [@prefix, key].join('::')
20
+
21
+ self.class.pack(hash: values).each do |k, c|
22
+ @client.hincrby(pkey, k, c)
23
+ end
24
+ end
25
+
26
+ def get(key:)
27
+ pkey = [@prefix, key].join('::')
28
+
29
+ self.class.unpack(
30
+ hash: @client.hgetall(pkey)
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
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,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
5
+ class Nocturnal # rubocop:disable Metrics/ClassLength
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:, config: nil)
12
+ list = []
13
+ from = new(from, config: config).send(range)
14
+ to = new(to, config: config).send(range)
15
+ item = from.dup
16
+ while item <= to
17
+ list << item
18
+ item = Nocturnal.new(item, config: config).send("next_#{range}")
19
+ end
20
+ list
21
+ end
22
+
23
+ def initialize(at, config: nil)
24
+ @at = at
25
+ @config = config
26
+ end
27
+
28
+ def config
29
+ @config || Trifle::Stats.default
30
+ end
31
+
32
+ def change(**fractions)
33
+ Time.new(
34
+ fractions.fetch(:year, @at.year),
35
+ fractions.fetch(:month, @at.month),
36
+ fractions.fetch(:day, @at.day),
37
+ fractions.fetch(:hour, @at.hour),
38
+ fractions.fetch(:minute, @at.min),
39
+ 0, # second
40
+ config.tz.utc_offset
41
+ )
42
+ end
43
+
44
+ def minute
45
+ change
46
+ end
47
+
48
+ def next_minute
49
+ Nocturnal.new(
50
+ minute + 60,
51
+ config: config
52
+ ).minute
53
+ end
54
+
55
+ def hour
56
+ change(minute: 0)
57
+ end
58
+
59
+ def next_hour
60
+ Nocturnal.new(
61
+ hour + 60 * 60,
62
+ config: config
63
+ ).hour
64
+ end
65
+
66
+ def day
67
+ change(hour: 0, minute: 0)
68
+ end
69
+
70
+ def next_day
71
+ Nocturnal.new(
72
+ day + 60 * 60 * 24,
73
+ config: config
74
+ ).day
75
+ end
76
+
77
+ def week
78
+ today = day
79
+
80
+ (today.to_date - days_to_week_start).to_time
81
+ end
82
+
83
+ def next_week
84
+ Nocturnal.new(
85
+ week + 60 * 60 * 24 * 7,
86
+ config: config
87
+ ).week
88
+ end
89
+
90
+ def days_to_week_start
91
+ start_day_number = DAYS_INTO_WEEK.fetch(
92
+ config.beginning_of_week
93
+ )
94
+
95
+ (@at.wday - start_day_number) % 7
96
+ end
97
+
98
+ def month
99
+ change(day: 1, hour: 0, minute: 0)
100
+ end
101
+
102
+ def next_month
103
+ Nocturnal.new(
104
+ month + 60 * 60 * 24 * 31,
105
+ config: config
106
+ ).month
107
+ end
108
+
109
+ def quarter
110
+ first_quarter_month = @at.month - (2 + @at.month) % 3
111
+
112
+ change(
113
+ month: first_quarter_month,
114
+ day: 1,
115
+ hour: 0,
116
+ minute: 0
117
+ )
118
+ end
119
+
120
+ def next_quarter
121
+ Nocturnal.new(
122
+ quarter + 60 * 60 * 24 * 31 * 3,
123
+ config: config
124
+ ).quarter
125
+ end
126
+
127
+ def year
128
+ change(month: 1, day: 1, hour: 0, minute: 0)
129
+ end
130
+
131
+ def next_year
132
+ Nocturnal.new(
133
+ year + 60 * 60 * 24 * 31 * 12,
134
+ config: config
135
+ ).year
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
5
+ module Operations
6
+ module Timeseries
7
+ class Increment
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.inc(
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
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
5
+ module Operations
6
+ module Timeseries
7
+ class Values
8
+ attr_reader :key, :range
9
+
10
+ def initialize(**keywords)
11
+ @key = keywords.fetch(:key)
12
+ @from = keywords.fetch(:from)
13
+ @to = keywords.fetch(:to)
14
+ @range = keywords.fetch(:range)
15
+ @config = keywords[:config]
16
+ end
17
+
18
+ def config
19
+ @config || Trifle::Stats.default
20
+ end
21
+
22
+ def timeline
23
+ Nocturnal.timeline(from: @from, to: @to, range: range)
24
+ end
25
+
26
+ def perform
27
+ timeline.map do |at|
28
+ {
29
+ at => config.driver.get(
30
+ key: [key, range, at.to_i].join(config.separator)
31
+ )
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trifle
4
+ module Stats
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'lib/trifle/stats/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'trifle-stats'
5
+ spec.version = Trifle::Stats::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 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"
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = "https://github.com/trifle-io/trifle-stats"
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,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trifle-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jozef Vaclavik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-30 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 Stats allows you to submit counters andautomatically storing them
126
+ for each range.Supports multiple backend drivers.
127
+ email:
128
+ - jozef@hey.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".github/workflows/ruby.yml"
134
+ - ".gitignore"
135
+ - ".gitpod.yml"
136
+ - ".gitpod/Dockerfile"
137
+ - ".gitpod/base/Dockerfile"
138
+ - ".rspec"
139
+ - ".rubocop.yml"
140
+ - ".ruby-version"
141
+ - ".tool-versions"
142
+ - ".travis.yml"
143
+ - Gemfile
144
+ - Gemfile.lock
145
+ - LICENSE
146
+ - README.md
147
+ - Rakefile
148
+ - bin/console
149
+ - bin/setup
150
+ - lib/trifle/stats.rb
151
+ - lib/trifle/stats/configuration.rb
152
+ - lib/trifle/stats/driver/README.md
153
+ - lib/trifle/stats/driver/process.rb
154
+ - lib/trifle/stats/driver/redis.rb
155
+ - lib/trifle/stats/mixins/packer.rb
156
+ - lib/trifle/stats/nocturnal.rb
157
+ - lib/trifle/stats/operations/timeseries/increment.rb
158
+ - lib/trifle/stats/operations/timeseries/values.rb
159
+ - lib/trifle/stats/version.rb
160
+ - trifle-stats.gemspec
161
+ homepage: https://github.com/trifle-io/trifle-stats
162
+ licenses: []
163
+ metadata:
164
+ homepage_uri: https://github.com/trifle-io/trifle-stats
165
+ source_code_uri: https://github.com/trifle-io/trifle-stats
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '2.6'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubygems_version: 3.2.3
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Simple analytics for tracking events and status counts
185
+ test_files: []