diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..c90579e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: Ruby + +on: + push: + branches: + - main + + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + ruby: + - '3.2.2' + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the default task + run: bundle exec rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e1d557c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,34 @@ +AllCops: + TargetRubyVersion: 3.2.2 + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Metrics/AbcSize: + Enabled: false + +Metrics/BlockLength: + Enabled: false + +Metrics/CyclomaticComplexity: + Enabled: false + +Style/FrozenStringLiteralComment: + Enabled: false + +Metrics/ModuleLength: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Layout/LineLength: + Max: 120 + +Metrics/MethodLength: + Max: 20 diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..be94e6f --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.2.2 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2ef9d54 --- /dev/null +++ b/Gemfile @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# Specify your gem's dependencies in statisfy.gemspec +gemspec + +gem "rake", "~> 13.0" + +gem "rubocop", "~> 1.21" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..0aca73b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,42 @@ +PATH + remote: . + specs: + statisfy (0.0.1) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + json (2.6.3) + parallel (1.22.1) + parser (3.2.2.0) + ast (~> 2.4.1) + rainbow (3.1.1) + rake (13.0.6) + regexp_parser (2.8.2) + rexml (3.2.6) + rubocop (1.49.0) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.28.0) + parser (>= 3.2.1.0) + ruby-progressbar (1.13.0) + unicode-display_width (2.4.2) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + rake (~> 13.0) + rubocop (~> 1.21) + statisfy! + +BUNDLED WITH + 2.4.10 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..af36d12 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Michaël Villeneuve + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f1c944 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Statisfy + +TODO: Delete this and the text below, and describe your gem + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/statisfy`. To experiment with that code, run `bin/console` for an interactive prompt. + +## Installation + +TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +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). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/statisfy. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..1924143 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: :rubocop diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..7666e7d --- /dev/null +++ b/bin/console @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "statisfy" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/statisfy.rb b/lib/statisfy.rb new file mode 100644 index 0000000..5670e35 --- /dev/null +++ b/lib/statisfy.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require_relative "statisfy/configuration" +require_relative "statisfy/counter" + +module Statisfy + class Error < StandardError; end + + class << self + def configuration + @configuration ||= Configuration.new + end + + def configure + yield(configuration) + end + end +end diff --git a/lib/statisfy/configuration.rb b/lib/statisfy/configuration.rb new file mode 100644 index 0000000..43b24b0 --- /dev/null +++ b/lib/statisfy/configuration.rb @@ -0,0 +1,9 @@ +module Statisfy + class Configuration + attr_accessor :redis_client, :append_to_counters, :default_async_method, :counters_path + + def initialize + @default_async_method = :perform_async + end + end +end diff --git a/lib/statisfy/counter.rb b/lib/statisfy/counter.rb new file mode 100644 index 0000000..e8d664c --- /dev/null +++ b/lib/statisfy/counter.rb @@ -0,0 +1,242 @@ +require_relative "subscriber" + +module Statisfy + module Counter + def self.included(klass) + klass.extend(ClassMethods) + klass.class_eval do + include Subscriber + attr_accessor :params, :subject + end + end + + module ClassMethods + # + # This is a DSL method that helps you define a counter + # It will create a method that will be called when the event is triggered + # It will also create a method that will be called when you want to get the value of the counter + # + # @param every: the event(s) that will trigger the counter + # @param type: by default it increments, but you can also use :average + # @param if: a block that returns a condition that must be met for the counter to be incremented (optional) + # @param if_async: same as if option but runs async to avoid slowing down inserts and updates (optional) + # @param uniq_by: a block to get the identifier of the element to be counted (optional) + # @param scopes: a block to get the list of scopes for which the counter must be incremented (optional) + # + def count(args = {}) + raise ArgumentError, "You must provide at least one event" if args[:every].blank? + + catch_events(*args[:every], if: args[:if] || -> { true }) + apply_default_counter_options(args) + const_set(:COUNTER_TYPE, args[:type] || :increment) + class_eval(&Statisfy.configuration.append_to_counters) if Statisfy.configuration.append_to_counters.present? + end + + # + # This method serves as a syntactic sugar + # The below methods could be written directly in the class definition + # but the `count` DSL defines them automatically based on the options provided + # + def apply_default_counter_options(args) + define_method(:identifier, args[:uniq_by] || -> { params["id"] }) + define_method(:scopes, args[:scopes]) if args[:scopes].present? + define_method(:if_async, args[:if_async] || -> { true }) + define_method(:decrement?, args[:decrement_if] || -> { false }) + define_method(:value, args[:value] || -> {}) + define_method(:should_run?, args[:if] || -> { true }) + end + + # + # This is the method that is called when you want to get the value of a counter. + # + # By default it returns the number of elements in the set. + # You can override it if the counter requires more complex logic + # see RateOfAutonomousUsers for example + # + # @param scope: the scope of the counter (an Organisation or a Department) + # @param month: the month for which you want the value of the counter (optional) + # + def value(scope:, month: nil) + if const_get(:COUNTER_TYPE) == :average + average_for(scope:, month:) + else + number_of_elements_in(scope:, month:) + end + end + + def number_of_elements_in(scope:, month: nil, group: nil) + redis_client.scard(key_for(group:, scope:, month:)) + end + + # + # Returns the list of elements in the set (in case you use .append and not .increment) + # + def elements_in(scope:, month: nil, group: nil) + redis_client.lrange(key_for(group:, scope:, month:), 0, -1) + end + + # + # Returns the average of the elements in the set + # Example: + # append(value: 1) + # append(value: 2) + # average_for(scope: Organisation.first) # => 1.5 + # + def average_for(scope:, month: nil) + stored_values = elements_in(scope:, month:) + return 0 if stored_values.empty? + + stored_values.map(&:to_i).reduce(:+) / stored_values.length.to_f + end + + # + # This is the name of the Redis key that will be used to store the counter + # + def key_for(scope:, month: nil, group: nil) + { + counter: name.demodulize.underscore, + group:, + month:, + scope_type: scope.class.name, + scope_id: scope.id + }.to_json + end + + def redis_client + Statisfy.configuration.redis_client + end + + # + # Returns a hash of values grouped by month: + # { + # "01/2024" => 33.3, + # "02/2024" => 36.6, + # "03/2024" => 38.2, + # } + # + # @param scope: the scope of the counter (an Organisation or a Department) + # @param start_at: the date from which you want to start counting (optional) + # @param stop_at: the date at which you want to stop counting (optional) + # + def values_grouped_by_month(scope:, start_at: nil, stop_at: nil) + x_months = 24 + + if start_at.present? || scope&.created_at.present? + start_at ||= scope.created_at + x_months = ((Time.zone.today.year * 12) + Time.zone.today.month) - ((start_at.year * 12) + start_at.month) + end + + relevant_months = (0..x_months).map do |i| + (x_months - i).months.ago.beginning_of_month + end + + relevant_months + .filter { |month| stop_at.blank? || month < stop_at } + .to_h do |month| + [month.strftime("%m/%Y"), value(scope:, month: month.strftime("%Y-%m")).round(2)] + end + end + # rubocop:enable Metrics/AbcSize + + # + # This allows to run a counter increment manually + # It is useful when you want to backfill counters + # + def initialize_with(resource, options = {}) + counter = new + counter.params = resource + + return unless options[:skip_validation] || counter.should_run? + + counter.perform(resource) + end + + # + # Returns the list of all the keys of this counter for a given scope (optional) + # and a given month (optional) + # + def all_keys(scope: nil, month: nil, group: nil) + redis_client.keys("*\"counter\":\"#{name.demodulize.underscore}\"*").filter do |json| + key = JSON.parse(json) + + scope_matches = scope.nil? || (key["scope_type"] == scope.class.name && key["scope_id"] == scope.id) + month_matches = month.nil? || key["month"] == month + group_matches = group.nil? || key["group"] == group + + scope_matches && month_matches && group_matches + end + end + + # + # This allows to reset all the counters for a given scope (optional) + # and a given month (optional) + # + def reset(scope: nil, month: nil, group: nil) + all_keys(scope:, month:, group:).each do |key| + redis_client.del(key) + end + + true + end + end + + protected + + def scopes_with_global + (scopes + [Department.new]).flatten.compact + end + + def month_to_set + params["created_at"].to_date.strftime("%Y-%m") + end + + def scopes + [subject.department, subject.organisation] + end + + def process_event + return unless if_async + + if value.present? + append(value:) + else + decrement? ? decrement : increment + end + end + + # + # This allows to iterate over all the counters that need to be updated + # (in general the Department(s) and Organisation(s) for both the current month and the global counter) + # + def all_counters_of(group:) + [month_to_set, nil].each do |month| + scopes_with_global.each do |scope| + yield self.class.key_for(group:, scope:, month:), identifier + end + end + end + + def increment(group: nil) + all_counters_of(group:) do |key, id| + self.class.redis_client.sadd?(key, id) + end + end + + def decrement(group: nil) + all_counters_of(group:) do |key, id| + self.class.redis_client.srem?(key, id) + end + end + + # + # To be used to store a list of values instead of a basic counter + # + def append(value:, group: nil) + all_counters_of(group:) do |key| + self.class.redis_client.rpush(key, value) + end + end + end +end + +# rubocop:enable Metrics/ModuleLength diff --git a/lib/statisfy/subscriber.rb b/lib/statisfy/subscriber.rb new file mode 100644 index 0000000..b60bf97 --- /dev/null +++ b/lib/statisfy/subscriber.rb @@ -0,0 +1,74 @@ +module Statisfy + module Subscriber + def self.included(klass) + klass.extend(ClassMethods) + end + + module ClassMethods + def catch_events(*event_names, **options) + define_method(:should_run?, &options[:if] || -> { true }) + [*event_names].flatten.map do |event_name| + model_and_event_from_event_name(event_name).tap do |model, event| + append_callback_to_model(model, event) + define_subject_method(model) + end + end + end + + def append_callback_to_model(model, event) + listener = self + model.class_eval do + after_commit on: event do + counter = listener.new + counter.subject = self + + next unless counter.should_run? + + if listener.respond_to?(Statisfy.configuration.default_async_method) + listener.send(Statisfy.configuration.default_async_method, attributes) + else + counter.perform(attributes) + end + end + end + end + + def model_and_event_from_event_name(event_name) + model_with_event = event_name.to_s.split("_") + event = { + "created": :create, + "updated": :update, + "destroyed": :destroy + }[model_with_event.pop.to_sym] + + model_name = model_with_event.join("_").camelize + + [Object.const_get(model_name), event] + rescue NameError + raise Statisfy::Error, "The model #{model_name} does not exist" + end + + def define_subject_method(model) + instance_name = model.name.underscore + return if method_defined?(instance_name) + + define_method(instance_name) do + model = instance_name.camelize.constantize + @subject ||= model.find_by(id: params["id"]) + end + alias_method :subject, instance_name + end + end + + # + # This is the method that will be called when an event is triggered + # It will be executed in the background by Sidekiq + # + # @resource_or_hash [Hash] The attributes of the model that triggered the event + the previous_changes + # + def perform(resource_or_hash) + @params = resource_or_hash + process_event + end + end +end diff --git a/statisfy.gemspec b/statisfy.gemspec new file mode 100644 index 0000000..0872278 --- /dev/null +++ b/statisfy.gemspec @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +Gem::Specification.new do |s| + s.name = "statisfy" + s.version = "0.0.1" + s.date = "2023-12-07" + s.summary = "A performant and flexible counter solution" + s.description = "A performant and flexible counter solution that allows to make statistics on your models" + s.authors = ["Michaël Villeneuve"] + s.homepage = "https://github.com/Michaelvilleneuve/statisfy" + s.email = "contact@michaelvilleneuve.fr" + s.files = Dir["lib/**/*"] + s.license = "MIT" +end