Class: AppConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/moesif_rack/app_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug) ⇒ AppConfig

Returns a new instance of AppConfig.



12
13
14
15
16
# File 'lib/moesif_rack/app_config.rb', line 12

def initialize(debug)
  @debug = debug
  @moesif_helpers = MoesifHelpers.new(debug)
  @regex_config_helper = RegexConfigHelper.new(debug)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/moesif_rack/app_config.rb', line 10

def config
  @config
end

#last_download_timeObject

Returns the value of attribute last_download_time.



10
11
12
# File 'lib/moesif_rack/app_config.rb', line 10

def last_download_time
  @last_download_time
end

#recent_etagObject

Returns the value of attribute recent_etag.



10
11
12
# File 'lib/moesif_rack/app_config.rb', line 10

def recent_etag
  @recent_etag
end

Instance Method Details

#calculate_weight(sample_rate) ⇒ Object



96
97
98
# File 'lib/moesif_rack/app_config.rb', line 96

def calculate_weight(sample_rate)
  sample_rate == 0 ? 1 : (100 / sample_rate).floor
end

#get_config(api_controller) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/moesif_rack/app_config.rb', line 32

def get_config(api_controller)
  # Get Application Config
  @moesif_helpers.log_debug('try to loading etag')
  config_json, _context = api_controller.get_app_config
  @config = config_json
  @recent_etag = _context.response.headers[:x_moesif_config_etag]
  @last_download_time = Time.now.utc
  @moesif_helpers.log_debug('new config downloaded')
  @moesif_helpers.log_debug(config_json.to_s)
rescue MoesifApi::APIException => e
  if e.response_code.between?(401, 403)
    @moesif_helpers.log_debug 'Unauthorized access getting application configuration. Please check your Appplication Id.'
  end
  @moesif_helpers.log_debug 'Error getting application configuration, with status code:'
  @moesif_helpers.log_debug e.response_code
rescue StandardError => e
  @moesif_helpers.log_debug e.to_s
end

#get_sampling_percentage(event_model, user_id, company_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/moesif_rack/app_config.rb', line 51

def get_sampling_percentage(event_model, user_id, company_id)
  # if we do not have config for some reason we return 100
  if !@config.nil?
  # Get sampling percentage
    @moesif_helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
    @moesif_helpers.log_debug(@config.to_s)

    # Get Regex Sampling rate
    regex_config = @config.fetch('regex_config', nil)

    if !regex_config.nil? and !event_model.nil?
      config_mapping = @regex_config_helper.prepare_config_mapping(event_model)
      regex_sample_rate = @regex_config_helper.fetch_sample_rate_on_regex_match(regex_config,
                                                                                config_mapping)
      return regex_sample_rate unless regex_sample_rate.nil?
    end

    # Get user sample rate object
    user_sample_rate = @config.fetch('user_sample_rate', nil)

    # Get company sample rate object
    company_sample_rate = @config.fetch('company_sample_rate', nil)

    # Get sample rate for the user if exist
    if !user_id.nil? && !user_sample_rate.nil? && user_sample_rate.key?(user_id)
      return user_sample_rate.fetch(user_id)
    end

    # Get sample rate for the company if exist
    if !company_id.nil? && !company_sample_rate.nil? && company_sample_rate.key?(company_id)
      return company_sample_rate.fetch(company_id)
    end

    # Return overall sample rate
    @config.fetch('sample_rate', 100)
  else
    @moesif_helpers.log_debug 'Assuming default behavior as response body is nil - '
    100
  end
rescue StandardError => e
  @moesif_helpers.log_debug 'Error while geting sampling percentage, assuming default behavior'
  @moesif_helpers.log_debug e.to_s
  100
end

#should_reload(etag_from_create_event) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moesif_rack/app_config.rb', line 18

def should_reload(etag_from_create_event)
  if @last_download_time.nil?
    return true
  elsif Time.now.utc > (@last_download_time + 300)
    return true
  elsif !etag_from_create_event.nil? && !@recent_etag.nil?
    @moesif_helpers.log_debug('comparing if etag from event and recent etag match ' + etag_from_create_event + ' ' + @recent_etag)

    return etag_from_create_event != @recent_etag
  end
  @moesif_helpers.log_debug('should skip reload config')
  return false;
end