File: metrics.rb

package info (click to toggle)
ruby-unleash 3.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,098; makefile: 10; sh: 4
file content (33 lines) | stat: -rw-r--r-- 926 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Unleash
  class Metrics
    attr_accessor :features

    # NOTE: no mutexes for features

    def initialize
      self.features = {}
    end

    def to_s
      self.features.to_json
    end

    def increment(feature, choice)
      raise "InvalidArgument choice must be :yes or :no" unless [:yes, :no].include? choice

      self.features[feature] = { yes: 0, no: 0 } unless self.features.include? feature
      self.features[feature][choice] += 1
    end

    def increment_variant(feature, variant)
      self.features[feature] = { yes: 0, no: 0 } unless self.features.include? feature
      self.features[feature]['variant'] = {}     unless self.features[feature].include? 'variant'
      self.features[feature]['variant'][variant] = 0 unless self.features[feature]['variant'].include? variant
      self.features[feature]['variant'][variant] += 1
    end

    def reset
      self.features = {}
    end
  end
end