Class: Contrast::Agent::Protect::Rule::InputClassification::Statistics
- Includes:
- Utils, Components::Logger::InstanceMethods
- Defined in:
- lib/contrast/agent/protect/rule/input_classification/statistics.rb
Overview
This class will hold match information for each rule when input classification is being saved in LRU cache.
Constant Summary collapse
- CAPACITY =
Protect rules will always be fixed number, on other hand the number of inputs will grow, we need to limit the number of inputs to be cached.
30
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#fetch(rule_id, input_type) ⇒ Object
Get the statistics for the protect rule.
-
#initialize ⇒ Statistics
constructor
A new instance of Statistics.
-
#match!(rule_id, cached, request) ⇒ Object
This method will handle the statistics for the input match.
-
#mismatch!(rule_id, input_type) ⇒ Object
This method will handle the statistics for the input mismatch.
-
#push(rule_id, cached) ⇒ Object
Creates new statisctics for protect rule.
-
#to_events ⇒ Array<Contrast::Agent::Telemetry::InputAnalysisCacheEvent>
The events to be sent.
Methods included from Components::Logger::InstanceMethods
Methods included from Utils
Constructor Details
#initialize ⇒ Statistics
Returns a new instance of Statistics.
26 27 28 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 26 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 20 def data @data end |
Instance Method Details
#fetch(rule_id, input_type) ⇒ Object
Get the statistics for the protect rule.
91 92 93 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 91 def fetch rule_id, input_type safe_extract(@data[rule_id]&.select { |e| e.input_type == input_type }) end |
#match!(rule_id, cached, request) ⇒ Object
This method will handle the statistics for the input match
35 36 37 38 39 40 41 42 43 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 35 def match! rule_id, cached, request return unless Contrast::Agent::Telemetry::Base.enabled? push(rule_id, cached) fetch(rule_id, cached.result.input_type)&.increase_match_for_input return unless cached.request_id == request.__id__ fetch(rule_id, cached.result.input_type)&.increase_match_for_request end |
#mismatch!(rule_id, input_type) ⇒ Object
This method will handle the statistics for the input mismatch. Skip if this is the called with empty cache since it’s not fair.
50 51 52 53 54 55 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 50 def mismatch! rule_id, input_type return unless Contrast::Agent::Telemetry::Base.enabled? return if Contrast::Agent::Protect::InputAnalyzer.lru_cache.empty? fetch(rule_id, input_type)&.increase_mismatch_for_input end |
#push(rule_id, cached) ⇒ Object
Creates new statisctics for protect rule.
78 79 80 81 82 83 84 85 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 78 def push rule_id, cached new_entry = Contrast::Agent::Protect::Rule::InputClassification::MatchRates. new(rule_id, cached.result.input_type, cached.result.score_level) @data[rule_id] = [] if Contrast::Utils::DuckUtils.empty_duck?(@data[rule_id]) @data[rule_id].shift if @data[rule_id].length >= CAPACITY @data[rule_id] << new_entry unless saved?(rule_id, cached) end |
#to_events ⇒ Array<Contrast::Agent::Telemetry::InputAnalysisCacheEvent>
Returns the events to be sent.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/contrast/agent/protect/rule/input_classification/statistics.rb', line 58 def to_events events = [] data.each do |_rule_id, match_rates| match_rates.each do |match_rate| event = Contrast::Agent::Telemetry::InputAnalysisCacheEvent.new(match_rate.rule_id, match_rate) next if event.empty? events << event end end events rescue StandardError => e logger.error("[IA_LRU_Cache] Error while creating events: #{ e }", stacktrace: e.backtrace) [] end |