lesli_audit 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +6 -1
  3. data/app/assets/stylesheets/lesli_audit/application.css +549 -35
  4. data/app/controllers/lesli_audit/accounts_controller.rb +7 -3
  5. data/app/controllers/lesli_audit/analytics_controller.rb +16 -0
  6. data/app/controllers/lesli_audit/logs_controller.rb +12 -7
  7. data/app/controllers/lesli_audit/traffic_controller.rb +25 -0
  8. data/app/controllers/lesli_audit/users_controller.rb +7 -4
  9. data/app/interfaces/lesli_audit/logger_interface.rb +56 -58
  10. data/app/jobs/lesli_audit/cloudflare_traffic_sync_job.rb +78 -0
  11. data/app/jobs/lesli_audit/retention_cleanup_job.rb +9 -0
  12. data/app/models/concerns/lesli_audit/append_only.rb +12 -0
  13. data/app/models/lesli_audit/account.rb +38 -10
  14. data/app/models/lesli_audit/account_device.rb +3 -1
  15. data/app/models/lesli_audit/account_log.rb +12 -7
  16. data/app/models/lesli_audit/account_request.rb +3 -1
  17. data/app/models/lesli_audit/cloudflare_traffic_sync.rb +99 -0
  18. data/app/models/lesli_audit/traffic_breakdown.rb +38 -0
  19. data/app/models/lesli_audit/traffic_rollup.rb +33 -0
  20. data/app/models/lesli_audit/user_journal.rb +14 -3
  21. data/app/models/lesli_audit/user_log.rb +12 -3
  22. data/app/models/lesli_audit/user_request.rb +7 -3
  23. data/app/services/lesli_audit/activity_audit.rb +134 -0
  24. data/app/services/lesli_audit/analytics_service.rb +237 -0
  25. data/app/services/lesli_audit/cloudflare/configuration.rb +92 -0
  26. data/app/services/lesli_audit/cloudflare/graphql_client.rb +309 -0
  27. data/app/services/lesli_audit/cloudflare/snapshot_converter.rb +101 -0
  28. data/app/services/lesli_audit/cloudflare/sync_service.rb +79 -0
  29. data/app/services/lesli_audit/cloudflare_traffic_snapshot.rb +182 -0
  30. data/app/services/lesli_audit/log_service.rb +99 -5
  31. data/app/services/lesli_audit/request_service.rb +74 -44
  32. data/app/services/lesli_audit/retention_cleanup.rb +146 -0
  33. data/app/services/lesli_audit/traffic_service.rb +216 -0
  34. data/app/services/lesli_audit/traffic_snapshot_importer.rb +120 -0
  35. data/app/services/lesli_audit/user_agent_parser.rb +51 -0
  36. data/app/services/lesli_audit/user_service.rb +119 -150
  37. data/app/views/lesli_audit/{visitors → analytics}/index.html.erb +40 -30
  38. data/app/views/lesli_audit/logs/index.html.erb +88 -19
  39. data/app/views/lesli_audit/logs/show.html.erb +41 -7
  40. data/app/views/lesli_audit/partials/_navigation.html.erb +5 -4
  41. data/app/views/lesli_audit/requests/index.html.erb +80 -19
  42. data/app/views/lesli_audit/traffic/_breakdown.html.erb +46 -0
  43. data/app/views/lesli_audit/traffic/index.html.erb +177 -0
  44. data/app/views/lesli_audit/users/index.html.erb +97 -43
  45. data/config/locales/translations.en.yml +137 -1
  46. data/config/locales/translations.es.yml +137 -1
  47. data/config/locales/translations.fr.yml +132 -1
  48. data/config/locales/translations.it.yml +132 -1
  49. data/config/locales/translations.pt.yml +132 -1
  50. data/config/routes.rb +6 -22
  51. data/db/migrate/v1.0/0501000110_create_lesli_audit_accounts.rb +14 -1
  52. data/db/migrate/v1.0/0501110110_create_lesli_audit_account_logs.rb +11 -6
  53. data/db/migrate/v1.0/0501110210_create_lesli_audit_account_devices.rb +10 -7
  54. data/db/migrate/v1.0/0501110310_create_lesli_audit_account_requests.rb +10 -7
  55. data/db/migrate/v1.0/0501120110_create_lesli_audit_user_logs.rb +12 -7
  56. data/db/migrate/v1.0/0501120210_create_lesli_audit_user_journals.rb +16 -8
  57. data/db/migrate/v1.0/0501120310_create_lesli_audit_user_requests.rb +13 -10
  58. data/db/migrate/v1.0/0501130110_create_lesli_audit_traffic_rollups.rb +55 -0
  59. data/db/migrate/v1.0/0501130210_create_lesli_audit_traffic_breakdowns.rb +41 -0
  60. data/db/migrate/v1.0/0501130310_create_lesli_audit_cloudflare_traffic_syncs.rb +42 -0
  61. data/db/seed/devices.rb +3 -2
  62. data/db/seed/requests.rb +3 -3
  63. data/db/seed/traffic.rb +128 -0
  64. data/db/seed/users.rb +68 -27
  65. data/db/seeds.rb +3 -2
  66. data/lib/lesli_audit/engine.rb +1 -12
  67. data/lib/lesli_audit/version.rb +2 -2
  68. data/lib/tasks/lesli_audit_cloudflare_traffic.rake +16 -0
  69. data/lib/tasks/lesli_audit_tasks.rake +12 -4
  70. data/readme.md +28 -5
  71. metadata +36 -9
  72. data/app/assets/stylesheets/lesli_audit/application.tailwind.css +0 -2
  73. data/app/controllers/lesli_audit/visitors_controller.rb +0 -14
  74. data/app/models/concerns/lesli_audit/user_associations.rb +0 -11
  75. data/app/services/lesli_audit/visitor_service.rb +0 -239
@@ -0,0 +1,25 @@
1
+ module LesliAudit
2
+ class TrafficController < ApplicationController
3
+ def index
4
+ dashboard = TrafficService.new(current_user, traffic_query).dashboard
5
+
6
+ @summary = dashboard.fetch(:summary)
7
+ @trend = dashboard.fetch(:trend)
8
+ @paths = dashboard.fetch(:paths)
9
+ @resources = dashboard.fetch(:resources)
10
+ @countries = dashboard.fetch(:countries)
11
+ @devices = dashboard.fetch(:devices)
12
+ @referrers = dashboard.fetch(:referrers)
13
+ @synchronization = dashboard.fetch(:synchronization)
14
+ end
15
+
16
+ private
17
+
18
+ def traffic_query
19
+ params
20
+ .permit(:bytime, :resource_type, :resource_id)
21
+ .to_h
22
+ .symbolize_keys
23
+ end
24
+ end
25
+ end
@@ -27,15 +27,18 @@ Building a better future, one line of code at a time.
27
27
  @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
28
 
29
29
  // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
- // ·
30
+ // ·
31
31
  =end
32
32
  module LesliAudit
33
33
  class UsersController < ApplicationController
34
-
35
34
  # GET /users
36
35
  def index
37
- @users = UserService.new(current_user, query).users
38
- @working_hours = UserService.new(current_user, query).working_hours
36
+ dashboard = UserService.new(current_user, query).dashboard
37
+
38
+ @summary = dashboard.fetch(:summary)
39
+ @attention = dashboard.fetch(:attention)
40
+ @registrations = dashboard.fetch(:registrations)
41
+ @working_hours = dashboard.fetch(:working_hours)
39
42
  end
40
43
  end
41
44
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Lesli
4
4
 
5
- Copyright (c) 2023, Lesli Technologies, S. A.
5
+ Copyright (c) 2026, Lesli Technologies, S. A.
6
6
 
7
7
  This program is free software: you can redistribute it and/or modify
8
8
  it under the terms of the GNU General Public License as published by
@@ -19,7 +19,7 @@ along with this program. If not, see http://www.gnu.org/licenses/.
19
19
 
20
20
  Lesli · Ruby on Rails SaaS Development Framework.
21
21
 
22
- Made with ♥ by https://www.lesli.tech
22
+ Made with ♥ by LesliTech
23
23
  Building a better future, one line of code at a time.
24
24
 
25
25
  @contact hello@lesli.tech
@@ -29,57 +29,79 @@ Building a better future, one line of code at a time.
29
29
  // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
30
  // ·
31
31
  =end
32
- require 'device_detector'
32
+
33
33
  module LesliAudit
34
34
  module LoggerInterface
35
- def get_user_agent(as_string=true)
36
-
37
- # parse user agent
38
- user_agent = DeviceDetector.new(request.env["HTTP_USER_AGENT"])
39
-
40
- #user_agent_version = user_agent.version.to_a.first(2).join(".")
41
-
42
- # return user agent as object
43
- {
44
- platform: user_agent.os_name,
45
- browser: user_agent.name,
46
- device: user_agent.device_type
47
- }
35
+ def get_user_agent
36
+ LesliAudit::UserAgentParser.parse(request.user_agent)
48
37
  end
49
38
 
50
39
  def log_requests
40
+ return unless Lesli.config.audit.dig(:enable_analytics)
41
+ return unless current_user
42
+
51
43
  log_account_requests
52
44
  log_user_requests
53
- log_devices
54
- end
45
+ log_user_journal
46
+ end
55
47
 
56
- def log_account_requests
48
+ def log_devices
57
49
  return unless Lesli.config.audit.dig(:enable_analytics)
58
50
  return unless current_user
59
51
 
52
+ user_agent = get_user_agent()
53
+
54
+ # Try to save a unique record for this request configuration
55
+ current_user.account.audit.account_devices.upsert(
56
+ {
57
+ date: Date2.new.date.to_s,
58
+ :agent_platform => user_agent&.dig(:platform) || "unknown",
59
+ :agent_browser => user_agent&.dig(:browser) || "unknown",
60
+ :agent_device => user_agent&.dig(:device) || "unknown",
61
+ :agent_count => 1
62
+ },
63
+
64
+ # group of columns to consider a request as unique
65
+ unique_by: :lesli_audit_devices_index,
66
+
67
+ # if request id is not unique
68
+ # - increase the counter for this configuration
69
+ # - update the datetime of the last request
70
+ on_duplicate: Arel.sql(
71
+ "agent_count = lesli_audit_account_devices.agent_count + 1, " \
72
+ "updated_at = CURRENT_TIMESTAMP"
73
+ )
74
+ )
75
+ end
76
+
77
+ private
78
+
79
+ def log_account_requests
80
+
60
81
  # Try to save a unique record for this request configuration
61
82
  current_user.account.audit.account_requests.upsert(
62
83
  {
63
84
  request_controller: controller_path,
64
85
  request_action: action_name,
65
86
  request_count: 1,
66
- created_at: Date2.new.date.to_s
87
+ date: Date2.new.date.to_s
67
88
  },
68
-
89
+
69
90
  # group of columns to consider a request as unique
70
- unique_by: %i[request_controller request_action created_at account_id],
91
+ unique_by: :lesli_audit_account_requests_index,
71
92
 
72
93
  # if request id is not unique, increase the counter for this configuration
73
- on_duplicate: Arel.sql("request_count = lesli_audit_account_requests.request_count + 1")
94
+ on_duplicate: Arel.sql(
95
+ "request_count = lesli_audit_account_requests.request_count + 1, " \
96
+ "updated_at = CURRENT_TIMESTAMP"
97
+ )
74
98
  )
75
99
  end
76
100
 
77
101
  # Track all user activity
78
102
  # this is disabled by default in the settings file
79
103
  def log_user_requests
80
- return unless current_user
81
104
  return unless session[:user_session_id]
82
- return unless Lesli.config.audit.dig(:enable_analytics)
83
105
 
84
106
  # Try to save a unique record for this request configuration
85
107
  current_user.account.audit.user_requests.upsert(
@@ -91,53 +113,29 @@ module LesliAudit
91
113
  date: Date2.new.date.to_s,
92
114
  request_count: 1
93
115
  },
94
-
116
+
95
117
  # group of columns to consider a request as unique
96
- unique_by: %i[request_controller request_action date user_id session_id],
118
+ unique_by: :lesli_audit_user_requests_index,
97
119
 
98
120
  # if request id is not unique
99
121
  # - increase the counter for this configuration
100
122
  # - update the datetime of the last request
101
123
  on_duplicate: Arel.sql(
102
- "request_count = lesli_audit_user_requests.request_count + 1,updated_at = #{LesliDate::Compatibility.db_now}"
124
+ "request_count = lesli_audit_user_requests.request_count + 1, " \
125
+ "updated_at = CURRENT_TIMESTAMP"
103
126
  )
104
- )
127
+ )
128
+ end
105
129
 
130
+ def log_user_journal
106
131
  return unless Lesli.config.audit.dig(:enable_journals)
107
- current_user.account.audit.user_journals.create({
132
+ current_user.account.audit.user_journals.create!(
133
+ event_uid: request.request_id,
108
134
  request_controller: controller_path,
109
135
  request_action: action_name,
110
136
  session_id: session[:user_session_id],
111
137
  user_id: current_user.id,
112
138
  date: Date2.new.date.to_s
113
- })
114
- end
115
-
116
- def log_devices
117
- return unless Lesli.config.audit.dig(:enable_analytics)
118
- return unless current_user
119
-
120
- user_agent = get_user_agent(false)
121
-
122
- # Try to save a unique record for this request configuration
123
- current_user.account.audit.account_devices.upsert(
124
- {
125
- :created_at => Date2.new.date.to_s,
126
- :agent_platform => user_agent&.dig(:platform) || "unknown",
127
- :agent_browser => user_agent&.dig(:browser) || "unknown",
128
- :agent_device => user_agent&.dig(:device) || "unknown",
129
- :agent_count => 1
130
- },
131
-
132
- # group of columns to consider a request as unique
133
- unique_by: %i[agent_platform agent_browser agent_device created_at account_id],
134
-
135
- # if request id is not unique
136
- # - increase the counter for this configuration
137
- # - update the datetime of the last request
138
- on_duplicate: Arel.sql(
139
- 'agent_count = lesli_audit_account_devices.agent_count + 1'
140
- )
141
139
  )
142
140
  end
143
141
 
@@ -0,0 +1,78 @@
1
+ module LesliAudit
2
+ class CloudflareTrafficSyncJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def self.enqueue_recent(configuration: Cloudflare::Configuration.from_application)
6
+ return unless configuration.enabled?
7
+
8
+ configuration.validate!
9
+ if configuration.account_ids.empty?
10
+ raise Cloudflare::Configuration::Invalid,
11
+ "At least one account ID is required for scheduled synchronization"
12
+ end
13
+
14
+ configuration.account_ids.each { |account_id| perform_later(account_id) }
15
+ end
16
+
17
+ def self.enqueue_backfill(account_id, days: nil,
18
+ configuration: Cloudflare::Configuration.from_application)
19
+ configuration.validate!
20
+ days = Integer(days || configuration.backfill_days)
21
+ raise ArgumentError, "days must be positive" unless days.positive?
22
+
23
+ end_on = Date.yesterday
24
+ start_on = end_on - (days - 1).days
25
+ perform_later(account_id, start_on:, end_on:)
26
+ end
27
+
28
+ def perform(account_id, start_on: nil, end_on: nil)
29
+ configuration = traffic_configuration
30
+ return unless configuration.enabled?
31
+
32
+ configuration.validate!
33
+ core_account = Lesli::Account.find(account_id)
34
+ audit_account = LesliAudit::Account.find_by!(account_id: core_account.id)
35
+ start_on, end_on = period(configuration, start_on:, end_on:)
36
+ sync = find_sync(audit_account, configuration, start_on, end_on)
37
+ execution_id = sync.acquire!
38
+ return unless execution_id
39
+
40
+ result = sync_service(configuration).call(
41
+ account: core_account,
42
+ start_on:,
43
+ end_on:
44
+ )
45
+ sync.complete!(execution_id:, snapshots_count: result.snapshots_count)
46
+ rescue => error
47
+ sync&.fail!(execution_id:, error:) if execution_id
48
+ raise
49
+ end
50
+
51
+ private
52
+
53
+ def traffic_configuration
54
+ Cloudflare::Configuration.from_application
55
+ end
56
+
57
+ def sync_service(configuration)
58
+ Cloudflare::SyncService.new(configuration:)
59
+ end
60
+
61
+ def period(configuration, start_on:, end_on:)
62
+ end_on = end_on&.to_date || Date.yesterday
63
+ start_on = start_on&.to_date || end_on - (configuration.reimport_days - 1).days
64
+ raise ArgumentError, "end_on must be on or after start_on" if end_on < start_on
65
+
66
+ [ start_on, end_on ]
67
+ end
68
+
69
+ def find_sync(account, configuration, start_on, end_on)
70
+ CloudflareTrafficSync.create_or_find_by!(
71
+ account:,
72
+ zone_id: configuration.zone_id,
73
+ period_start: start_on,
74
+ period_end: end_on
75
+ )
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,9 @@
1
+ module LesliAudit
2
+ # Scheduling belongs to the host application. This job only provides a
3
+ # backend-agnostic Active Job entry point for its scheduler.
4
+ class RetentionCleanupJob < ApplicationJob
5
+ def perform(batch_size: LesliAudit::RetentionCleanup::DEFAULT_BATCH_SIZE)
6
+ LesliAudit::RetentionCleanup.call(batch_size: batch_size)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module LesliAudit
2
+ module AppendOnly
3
+ extend ActiveSupport::Concern
4
+
5
+ # New records remain writable until their first INSERT. Once persisted,
6
+ # Active Record blocks ordinary updates and destroys. Retention cleanup
7
+ # and tenant hard-purge foreign keys intentionally operate below models.
8
+ def readonly?
9
+ persisted? || super
10
+ end
11
+ end
12
+ end
@@ -1,19 +1,47 @@
1
1
  module LesliAudit
2
2
  class Account < ApplicationRecord
3
- belongs_to :account, class_name: "Lesli::Account"
3
+ belongs_to :account,
4
+ class_name: "Lesli::Account",
5
+ inverse_of: :audit
6
+
7
+ validates :account_id, uniqueness: true
8
+
4
9
  has_many :dashboards
5
10
 
6
- has_many :account_logs
7
- has_many :account_devices
8
- has_many :account_requests
11
+ has_many :account_logs,
12
+ inverse_of: :account,
13
+ dependent: :delete_all
14
+
15
+ has_many :account_devices,
16
+ inverse_of: :account,
17
+ dependent: :delete_all
18
+
19
+ has_many :account_requests,
20
+ inverse_of: :account,
21
+ dependent: :delete_all
22
+
23
+ has_many :user_logs,
24
+ inverse_of: :account,
25
+ dependent: :delete_all
26
+
27
+ has_many :user_journals,
28
+ inverse_of: :account,
29
+ dependent: :delete_all
30
+
31
+ has_many :user_requests,
32
+ inverse_of: :account,
33
+ dependent: :delete_all
9
34
 
10
- has_many :user_logs
11
- has_many :user_journals
12
- has_many :user_requests
35
+ has_many :traffic_rollups,
36
+ inverse_of: :account,
37
+ dependent: :delete_all
13
38
 
14
- after_create :initialize_account
39
+ has_many :traffic_breakdowns,
40
+ inverse_of: :account,
41
+ dependent: :delete_all
15
42
 
16
- def initialize_account
17
- end
43
+ has_many :cloudflare_traffic_syncs,
44
+ inverse_of: :account,
45
+ dependent: :delete_all
18
46
  end
19
47
  end
@@ -1,5 +1,7 @@
1
1
  module LesliAudit
2
2
  class AccountDevice < ApplicationRecord
3
- belongs_to :account, class_name: 'Lesli::Account'
3
+ belongs_to :account,
4
+ class_name: "LesliAudit::Account",
5
+ inverse_of: :account_devices
4
6
  end
5
7
  end
@@ -1,11 +1,16 @@
1
1
  module LesliAudit
2
2
  class AccountLog < ApplicationRecord
3
- belongs_to :account, optional: true
4
- belongs_to :user, class_name: "Lesli::User", optional: true
5
-
6
- enum :operation, {
7
- :account_creation => 'account_creation',
8
- :account_initialization => 'account_initialization'
9
- }
3
+ include LesliAudit::AppendOnly
4
+
5
+ belongs_to :account,
6
+ class_name: "LesliAudit::Account",
7
+ inverse_of: :account_logs
8
+
9
+ belongs_to :user,
10
+ class_name: "Lesli::User",
11
+ inverse_of: :account_logs,
12
+ optional: true
13
+
14
+ validates :operation, presence: true
10
15
  end
11
16
  end
@@ -1,5 +1,7 @@
1
1
  module LesliAudit
2
2
  class AccountRequest < ApplicationRecord
3
- belongs_to :account
3
+ belongs_to :account,
4
+ class_name: "LesliAudit::Account",
5
+ inverse_of: :account_requests
4
6
  end
5
7
  end
@@ -0,0 +1,99 @@
1
+ require "securerandom"
2
+
3
+ module LesliAudit
4
+ class CloudflareTrafficSync < ApplicationRecord
5
+ SOURCE = "Cloudflare"
6
+ RUNNING_TIMEOUT = 2.hours
7
+ MAX_ERROR_LENGTH = 2_000
8
+
9
+ belongs_to :account,
10
+ class_name: "LesliAudit::Account",
11
+ inverse_of: :cloudflare_traffic_syncs
12
+
13
+ enum :status, {
14
+ pending: "pending",
15
+ running: "running",
16
+ succeeded: "succeeded",
17
+ failed: "failed"
18
+ }
19
+
20
+ validates :zone_id, :period_start, :period_end, presence: true
21
+ validates :snapshots_count,
22
+ numericality: { only_integer: true, greater_than_or_equal_to: 0 }
23
+ validate :period_ends_after_it_starts
24
+
25
+ def self.latest_status_for(account)
26
+ {
27
+ source: SOURCE,
28
+ last_synced_at: where(account:).maximum(:last_synced_at)
29
+ }
30
+ end
31
+
32
+ # The row lock lets only one worker own this exact account, zone, and period.
33
+ # The execution ID prevents an old worker from finishing a newer retry.
34
+ def acquire!(at: Time.current)
35
+ new_execution_id = SecureRandom.uuid
36
+
37
+ with_lock do
38
+ return if active_execution?(at)
39
+
40
+ update!(
41
+ status: :running,
42
+ execution_id: new_execution_id,
43
+ started_at: at,
44
+ finished_at: nil,
45
+ snapshots_count: 0,
46
+ error_message: nil
47
+ )
48
+ end
49
+
50
+ new_execution_id
51
+ end
52
+
53
+ def complete!(execution_id:, snapshots_count:, at: Time.current)
54
+ finish_execution(execution_id) do
55
+ update!(
56
+ status: :succeeded,
57
+ execution_id: nil,
58
+ snapshots_count:,
59
+ finished_at: at,
60
+ last_synced_at: at,
61
+ error_message: nil
62
+ )
63
+ end
64
+ end
65
+
66
+ def fail!(execution_id:, error:, at: Time.current)
67
+ finish_execution(execution_id) do
68
+ update!(
69
+ status: :failed,
70
+ execution_id: nil,
71
+ finished_at: at,
72
+ error_message: error.message.to_s.first(MAX_ERROR_LENGTH)
73
+ )
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def period_ends_after_it_starts
80
+ return if period_start.blank? || period_end.blank? || period_start <= period_end
81
+
82
+ errors.add(:period_end, "must be on or after period_start")
83
+ end
84
+
85
+ def finish_execution(expected_execution_id)
86
+ with_lock do
87
+ return false unless running? && execution_id == expected_execution_id
88
+
89
+ yield
90
+ end
91
+
92
+ true
93
+ end
94
+
95
+ def active_execution?(at)
96
+ running? && started_at.present? && started_at >= RUNNING_TIMEOUT.ago(at)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,38 @@
1
+ module LesliAudit
2
+ class TrafficBreakdown < ApplicationRecord
3
+ DIMENSIONS = %w[country device browser operating_system referrer].freeze
4
+
5
+ belongs_to :account,
6
+ class_name: "LesliAudit::Account",
7
+ inverse_of: :traffic_breakdowns
8
+
9
+ belongs_to :traffic_rollup,
10
+ class_name: "LesliAudit::TrafficRollup",
11
+ inverse_of: :traffic_breakdowns
12
+
13
+ before_validation :normalize_dimension
14
+
15
+ validates :dimension, inclusion: { in: DIMENSIONS }
16
+ validates :value, presence: true
17
+ validates :value,
18
+ uniqueness: { scope: %i[account_id traffic_rollup_id dimension] }
19
+ validates :page_views, :visits,
20
+ numericality: { only_integer: true, greater_than_or_equal_to: 0 }
21
+
22
+ validate :rollup_belongs_to_account
23
+
24
+ private
25
+
26
+ def normalize_dimension
27
+ self.dimension = dimension.to_s.strip.downcase.presence
28
+ self.value = value.to_s.strip.presence
29
+ end
30
+
31
+ def rollup_belongs_to_account
32
+ return if account_id.blank? || traffic_rollup.blank?
33
+ return if traffic_rollup.account_id == account_id
34
+
35
+ errors.add(:traffic_rollup, "must belong to the same account")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ module LesliAudit
2
+ class TrafficRollup < ApplicationRecord
3
+ belongs_to :account,
4
+ class_name: "LesliAudit::Account",
5
+ inverse_of: :traffic_rollups
6
+
7
+ has_many :traffic_breakdowns,
8
+ inverse_of: :traffic_rollup,
9
+ dependent: :delete_all
10
+
11
+ before_validation :normalize_identifiers
12
+
13
+ validates :provider,
14
+ presence: true,
15
+ format: { with: /\A[a-z][a-z0-9_-]*\z/ }
16
+ validates :hostname, :path, :resource_type, :date, :snapshot_at, presence: true
17
+ validates :resource_id,
18
+ numericality: { only_integer: true, greater_than: 0 }
19
+ validates :path,
20
+ uniqueness: { scope: %i[account_id provider hostname date] }
21
+ validates :page_views, :visits,
22
+ numericality: { only_integer: true, greater_than_or_equal_to: 0 }
23
+
24
+ private
25
+
26
+ def normalize_identifiers
27
+ self.provider = provider.to_s.strip.downcase.presence
28
+ self.hostname = hostname.to_s.strip.downcase.presence
29
+ self.path = path.to_s.strip.presence
30
+ self.resource_type = resource_type.to_s.strip.presence
31
+ end
32
+ end
33
+ end
@@ -1,7 +1,18 @@
1
1
  module LesliAudit
2
2
  class UserJournal < ApplicationRecord
3
- belongs_to :account
4
- belongs_to :user, class_name: "Lesli::User"
5
- belongs_to :session, class_name: "LesliShield::User::Session"
3
+ belongs_to :account,
4
+ class_name: "LesliAudit::Account",
5
+ inverse_of: :user_journals
6
+
7
+ belongs_to :user,
8
+ class_name: "Lesli::User",
9
+ inverse_of: :journals,
10
+ optional: true
11
+
12
+ validates :event_uid, presence: true
13
+ validates :request_controller, :request_action, presence: true, if: :retain?
14
+
15
+ scope :retained, -> { where(retain: true) }
16
+ scope :transient, -> { where(retain: false) }
6
17
  end
7
18
  end
@@ -1,7 +1,16 @@
1
1
  module LesliAudit
2
2
  class UserLog < ApplicationRecord
3
- belongs_to :account, optional: true
4
- belongs_to :user, class_name: "Lesli::User"
5
- belongs_to :session, class_name: "LesliShield::User::Session", optional: true
3
+ include LesliAudit::AppendOnly
4
+
5
+ belongs_to :account,
6
+ class_name: "LesliAudit::Account",
7
+ inverse_of: :user_logs
8
+
9
+ belongs_to :user,
10
+ class_name: "Lesli::User",
11
+ inverse_of: :logs,
12
+ optional: true
13
+
14
+ validates :operation, presence: true
6
15
  end
7
16
  end
@@ -1,7 +1,11 @@
1
1
  module LesliAudit
2
2
  class UserRequest < ApplicationRecord
3
- belongs_to :account
4
- belongs_to :user, class_name: "Lesli::User"
5
- belongs_to :session, class_name: "LesliShield::User::Session"
3
+ belongs_to :account,
4
+ class_name: "LesliAudit::Account",
5
+ inverse_of: :user_requests
6
+
7
+ belongs_to :user,
8
+ class_name: "Lesli::User",
9
+ inverse_of: :requests
6
10
  end
7
11
  end