Sha256: f218ca465d6f9c8a87e4efd6af8a3a74805ccdb0d3a4a145fd9cc149ebd0bce3

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Components
    module Sampling
      module Constants
        DEFAULT_SAMPLING_ENABLED            = false
        DEFAULT_SAMPLING_BASELINE           = 5
        DEFAULT_SAMPLING_REQUEST_FREQUENCY  = 5
        DEFAULT_SAMPLING_RESPONSE_FREQUENCY = 25
        DEFAULT_SAMPLING_WINDOW_MS          = 180_000
      end

      module ClassMethods #:nodoc:
        include Contrast::Components::ComponentBase
        include Constants
        include Contrast::Components::Interface

        access_component :config, :settings

        def sampling_enabled?
          sampling_control[:enabled]
        end

        def sampling_control
          @_sampling_control ||= begin
            cas = CONFIG.root.assess&.sampling
            settings = SETTINGS&.assess_state&.[](:sampling_settings)
            {
                enabled: true?([cas&.enable, settings&.enabled, DEFAULT_SAMPLING_ENABLED].reject(&:nil?)[0]),
                baseline: [cas&.baseline, settings&.baseline, DEFAULT_SAMPLING_BASELINE].map(&:to_i).find(&:positive?),
                request_frequency: [cas&.request_frequency, settings&.request_frequency, DEFAULT_SAMPLING_REQUEST_FREQUENCY].map(&:to_i).find(&:positive?),
                response_frequency: [cas&.response_frequency, settings&.response_frequency, DEFAULT_SAMPLING_RESPONSE_FREQUENCY].map(&:to_i).find(&:positive?),
                window: [cas&.window_ms, settings&.window_ms, DEFAULT_SAMPLING_WINDOW_MS].map(&:to_i).find(&:positive?)
            }
          end
        end

        # Used to reset sampling when settings from TeamServer change
        def reset_sampling_control
          @_sampling_control = nil
        end
      end

      module InstanceMethods #:nodoc:
        include Contrast::Components::ComponentBase
        include Constants
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
contrast-agent-4.3.2 lib/contrast/components/sampling.rb
contrast-agent-4.3.1 lib/contrast/components/sampling.rb
contrast-agent-4.3.0 lib/contrast/components/sampling.rb
contrast-agent-4.2.0 lib/contrast/components/sampling.rb
contrast-agent-4.1.0 lib/contrast/components/sampling.rb
contrast-agent-4.0.0 lib/contrast/components/sampling.rb
contrast-agent-3.16.0 lib/contrast/components/sampling.rb
contrast-agent-3.15.0 lib/contrast/components/sampling.rb
contrast-agent-3.14.0 lib/contrast/components/sampling.rb
contrast-agent-3.13.2 lib/contrast/components/sampling.rb
contrast-agent-3.13.1 lib/contrast/components/sampling.rb
contrast-agent-3.13.0 lib/contrast/components/sampling.rb
contrast-agent-3.12.2 lib/contrast/components/sampling.rb
contrast-agent-3.12.1 lib/contrast/components/sampling.rb
contrast-agent-3.12.0 lib/contrast/components/sampling.rb
contrast-agent-3.11.0 lib/contrast/components/sampling.rb