Class: Rack::AI::Features::AnomalyDetection
- Inherits:
-
Object
- Object
- Rack::AI::Features::AnomalyDetection
- Defined in:
- lib/rack/ai/features/anomaly_detection.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#initialize(provider, config) ⇒ AnomalyDetection
constructor
A new instance of AnomalyDetection.
- #process_request(env) ⇒ Object
- #process_response? ⇒ Boolean
Constructor Details
#initialize(provider, config) ⇒ AnomalyDetection
Returns a new instance of AnomalyDetection.
9 10 11 12 13 14 15 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 9 def initialize(provider, config) @name = :anomaly_detection @provider = provider @config = config @baseline_metrics = {} @request_patterns = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 7 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 7 def name @name end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
7 8 9 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 7 def provider @provider end |
Instance Method Details
#enabled? ⇒ Boolean
17 18 19 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 17 def enabled? @config.feature_enabled?(:anomaly_detection) end |
#process_request(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 25 def process_request(env) current_time = Time.now client_ip = get_client_ip(env) # Extract request metrics metrics = extract_request_metrics(env) # Update baseline and detect anomalies anomalies = detect_anomalies(client_ip, metrics, current_time) # Calculate risk score risk_score = calculate_risk_score(anomalies, metrics) # Determine action based on risk score action = determine_action(risk_score) { action: action, feature: :anomaly_detection, timestamp: current_time.iso8601, risk_score: risk_score, anomalies: anomalies, metrics: metrics, baseline_deviation: calculate_baseline_deviation(metrics) } end |
#process_response? ⇒ Boolean
21 22 23 |
# File 'lib/rack/ai/features/anomaly_detection.rb', line 21 def process_response? false end |