# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/agent/protect/rule/input_classification/rates' module Contrast module Agent module Protect module Rule module InputClassification # This class will hold match information when input classification is being saved in LRU cache. class MatchRates < Rates # @return [Integer] attr_reader :input_matches # @return [Integer] attr_reader :input_mismatches # @return [Integer] attr_reader :request_matches # @return [String] attr_reader :score_level # @param rule_id [String] # @param input_type [Symbol, nil] # @param score_level [String] def initialize rule_id, input_type, score_level super(rule_id, input_type) @input_matches = 0 @request_matches = 0 @input_mismatches = 0 @score_level = score_level if Contrast::Agent::Reporting::ScoreLevel.to_a.include?(score_level) end # Increase the match count for the given rule_id and input. def increase_match_for_input @input_matches += 1 end def increase_mismatch_for_input @input_mismatches += 1 end # increase the missmatch count for the given rule_id and input. def increase_match_for_request @request_matches += 1 end def empty? super && @input_matches.zero? && @input_mismatches.zero? && @request_matches.zero? end # Returns Agent Telemetry reportable fields. # # @return [Hash] def to_fields { "#{ to_field_title }.input_matches" => input_matches, "#{ to_field_title }.input_mismatches" => input_mismatches, "#{ to_field_title }.request_matches" => request_matches } end end end end end end end