Class: Contrast::Agent::Protect::Rule::InputClassification::Statistics

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Methods included from Utils

#safe_extract

Constructor Details

#initializeStatistics

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

#dataObject (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.

Parameters:

  • rule_id (String)

    the Protect rule name.

  • input_type (Symbol)

    Type of the input



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

Parameters:



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.

Parameters:

  • rule_id (String)

    the Protect rule name.

  • input_type (Symbol)

    Type of the input



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.

Parameters:



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_eventsArray<Contrast::Agent::Telemetry::InputAnalysisCacheEvent>

Returns the events to be sent.

Returns:



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