Skip to content

Commit

Permalink
#185448076 : Governance rules support (#45)
Browse files Browse the repository at this point in the history
Governance rules support.
  • Loading branch information
xinghengwang authored Jul 10, 2023
1 parent 4ab439f commit d3a9249
Show file tree
Hide file tree
Showing 17 changed files with 2,849 additions and 693 deletions.
11 changes: 11 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Layout/IndentationWidth:
Width: 2

Metrics/MethodLength:
Max: 50

Metrics/AbcSize:
Max: 50

Style/Documentation:
Enabled: false
26 changes: 13 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
PATH
remote: .
specs:
moesif_rack (1.4.17)
moesif_api (~> 1.2.14)
moesif_rack (1.5.1)
moesif_api (~> 1.2.17)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
http-accept (1.7.0)
http-cookie (1.0.4)
http-cookie (1.0.5)
domain_name (~> 0.5)
json (2.6.1)
json (2.6.3)
json_mapper (0.2.1)
json (>= 1.4.3)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
moesif_api (1.2.14)
mime-types-data (3.2023.0218.1)
moesif_api (1.2.17)
json_mapper (~> 0.2, >= 0.2.1)
moesif_unirest (~> 1.1.6)
moesif_unirest (1.1.6)
addressable (~> 2.8, >= 2.8.0)
json (~> 2.6, >= 2.6.1)
rest-client (~> 2.1, >= 2.1.0)
netrc (0.11.0)
power_assert (2.0.1)
public_suffix (4.0.6)
power_assert (2.0.3)
public_suffix (5.0.1)
rake (13.0.6)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
test-unit (3.5.3)
test-unit (3.6.1)
power_assert
unf (0.1.4)
unf_ext
unf_ext (0.0.8)
unf_ext (0.0.8.2)

PLATFORMS
arm64-darwin-21
Expand All @@ -54,4 +54,4 @@ DEPENDENCIES
test-unit (~> 3.5, >= 3.5.0)

BUNDLED WITH
2.3.6
2.3.26
207 changes: 87 additions & 120 deletions lib/moesif_rack/app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,134 +3,101 @@
require 'time'
require 'zlib'
require 'stringio'
require_relative './moesif_helpers.rb'
require_relative './regex_config_helper.rb'
require_relative './moesif_helpers'
require_relative './regex_config_helper'

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

def initialize debug
@debug = debug
@moesif_helpers = MoesifHelpers.new(debug)
@regex_config_helper = RegexConfigHelper.new(debug)
def get_config(api_controller)
# Get Application Config
config_api_response = api_controller.get_app_config
@moesif_helpers.log_debug('new config downloaded')
@moesif_helpers.log_debug(config_api_response.to_s)
config_api_response
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

def get_config(api_controller)
# Get Application Config
begin
config_api_response = api_controller.get_app_config()
@moesif_helpers.log_debug("new config downloaded")
@moesif_helpers.log_debug(config_api_response.to_s)
return config_api_response
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 => e
@moesif_helpers.log_debug e.to_s
end
rescue
end
def parse_configuration(config_api_response)
# Parse configuration object and return Etag, sample rate and last updated time

def parse_configuration(config_api_response)
# Parse configuration object and return Etag, sample rate and last updated time
begin
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
response_body = decompress_gzip_body(config_api_response)
@moesif_helpers.log_debug(response_body.to_s)

# Check if response body is not nil
if !response_body.nil? then
# Return Etag, sample rate and last updated time
return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc
else
@moesif_helpers.log_debug 'Response body is nil, assuming default behavior'
# Response body is nil, so assuming default behavior
return nil, nil, Time.now.utc
end
rescue => exception
@moesif_helpers.log_debug 'Error while parsing the configuration object, assuming default behavior'
@moesif_helpers.log_debug exception.to_s
# Assuming default behavior
return nil, nil, Time.now.utc
end
end
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
response_body = @moesif_helpers.decompress_gzip_body(config_api_response)
@moesif_helpers.log_debug(response_body.to_json)

def get_sampling_percentage(event_model, config_api_response, user_id, company_id)
# Get sampling percentage
begin
# Check if response body is not nil
if !config_api_response.nil? then
@moesif_helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
@moesif_helpers.log_debug(config_api_response.to_s)

# Get Regex Sampling rate
regex_config = config_api_response.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)
if !regex_sample_rate.nil?
return regex_sample_rate
end
end

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

# Get company sample rate object
company_sample_rate = config_api_response.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 sample rate
return config_api_response.fetch('sample_rate', 100)
else
@moesif_helpers.log_debug 'Assuming default behavior as response body is nil - '
return 100
end
rescue => exception
@moesif_helpers.log_debug 'Error while geting sampling percentage, assuming default behavior'
@moesif_helpers.log_debug exception.to_s
return 100
end
end
# Check if response body is not nil
return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc unless response_body.nil?

def decompress_gzip_body(config_api_response)
# Decompress gzip response body
begin
# Check if the content-encoding header exist and is of type zip
if config_api_response.headers.key?(:content_encoding) && config_api_response.headers[:content_encoding].eql?( 'gzip' ) then

# Create a GZipReader object to read data
gzip_reader = Zlib::GzipReader.new(StringIO.new(config_api_response.raw_body.to_s))

# Read the body
uncompressed_string = gzip_reader.read

# Return the parsed body
return JSON.parse( uncompressed_string )
else
@moesif_helpers.log_debug 'Content Encoding is of type other than gzip, returning nil'
return nil
end
rescue => exception
@moesif_helpers.log_debug 'Error while decompressing the response body'
@moesif_helpers.log_debug exception.to_s
return nil
end
end
# Return Etag, sample rate and last updated time

@moesif_helpers.log_debug 'Response body is nil, assuming default behavior'
# Response body is nil, so assuming default behavior
[nil, nil, Time.now.utc]
rescue StandardError => e
@moesif_helpers.log_debug 'Error while parsing the configuration object, assuming default behavior'
@moesif_helpers.log_debug e.to_s
# Assuming default behavior
[nil, nil, Time.now.utc]
end

def get_sampling_percentage(event_model, config_api_response, user_id, company_id)
# Get sampling percentage

# Check if response body is not nil
if !config_api_response.nil?
@moesif_helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
@moesif_helpers.log_debug(config_api_response.to_s)

def calculate_weight(sample_rate)
return sample_rate == 0 ? 1 : (100 / sample_rate).floor
# Get Regex Sampling rate
regex_config = config_api_response.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_api_response.fetch('user_sample_rate', nil)

# Get company sample rate object
company_sample_rate = config_api_response.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 sample rate
config_api_response.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

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

0 comments on commit d3a9249

Please sign in to comment.