Class: SecurityService

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/ruby_cms/templates/services/security_service.rb

Constant Summary collapse

CACHE_DURATION =
10.minutes
SECURITY_EVENT_TYPES =
SecurityTracker::EVENT_TYPES

Instance Method Summary collapse

Constructor Details

#initialize(start_date, end_date, period = nil) ⇒ SecurityService

Returns a new instance of SecurityService.



7
8
9
10
11
12
# File 'lib/generators/ruby_cms/templates/services/security_service.rb', line 7

def initialize(start_date, end_date, period = nil)
  @start_date = start_date.beginning_of_day
  @end_date   = end_date.end_of_day
  @range      = @start_date..@end_date
  @period     = period || determine_period_from_date_range
end

Instance Method Details

#dashboard_statsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/ruby_cms/templates/services/security_service.rb', line 14

def dashboard_stats
  Rails.cache.fetch(cache_key("dashboard_stats"), expires_in: CACHE_DURATION) do
    {
      total_events: security_events.count,
      high_risk_events: high_risk_events.count,
      failed_logins: .count,
      successful_logins: .count,
      suspicious_activities: suspicious_activity_events.count,
      top_threat_ips: top_threat_ips,
      events_by_type: events_by_type,
      recent_events: recent_events
    }
  end
end

#event_type_stats(event_type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/ruby_cms/templates/services/security_service.rb', line 29

def event_type_stats(event_type)
  events = security_events.where(name: event_type)

  {
    events: events.order(time: :desc).limit(100),
    stats: {
      total: events.count,
      unique_ips: events.distinct.count(:ip_address),
      avg_per_day: average_per_day(events.count)
    }
  }
end

#ip_stats(ip_address) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/ruby_cms/templates/services/security_service.rb', line 42

def ip_stats(ip_address)
  events = security_events.where(ip_address:)

  {
    events: events.order(time: :desc).limit(100),
    stats: {
      total: events.count,
      unique_event_types: events.distinct.count(:name),
      first_seen: events.minimum(:time),
      last_seen: events.maximum(:time)
    }
  }
end