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,134 @@
1
+ =begin
2
+
3
+ Lesli
4
+
5
+ Copyright (c) 2026, Lesli Technologies, S. A.
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see http://www.gnu.org/licenses/.
19
+
20
+ Lesli · Ruby on Rails SaaS Development Framework.
21
+
22
+ Made with ♥ by LesliTech
23
+ Building a better future, one line of code at a time.
24
+
25
+ @contact hello@lesli.tech
26
+ @website https://www.lesli.tech
27
+ @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
+
29
+ // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
+ // ·
31
+ =end
32
+
33
+ module LesliAudit
34
+ # Writes durable business and security events. Unlike request analytics,
35
+ # this entry point never rescues persistence errors: an auditable operation
36
+ # must fail when its event cannot be stored.
37
+ class ActivityAudit
38
+ class MissingAccount < StandardError; end
39
+
40
+ def self.record_account!(account:, actor: nil, **attributes)
41
+ return unless enabled?
42
+
43
+ new(account: account, actor: actor, **attributes).record_account!
44
+ end
45
+
46
+ def self.record_user!(user:, **attributes)
47
+ return unless enabled?
48
+
49
+ new(account: user&.account, actor: user, **attributes).record_user!
50
+ end
51
+
52
+ def self.enabled?
53
+ Lesli.config.audit&.dig(:enable_logs) == true
54
+ end
55
+
56
+ def initialize(
57
+ account:,
58
+ actor: nil,
59
+ engine: nil,
60
+ source: nil,
61
+ action: nil,
62
+ operation: nil,
63
+ description: nil,
64
+ session_id: nil,
65
+ subject: nil,
66
+ details: {}
67
+ )
68
+ @audit_account = resolve_audit_account(account)
69
+ @actor = actor
70
+ @engine = identifier(engine)
71
+ @source = identifier(source)
72
+ @action = identifier(action)
73
+ @operation = identifier(operation.presence || action.presence)
74
+ @description = description
75
+ @session_id = session_id.presence
76
+ @subject = subject
77
+ @details = details
78
+
79
+ raise ArgumentError, "operation is required" if @operation.blank?
80
+ raise ArgumentError, "details must be a Hash" unless @details.is_a?(Hash)
81
+ end
82
+
83
+ def record_account!
84
+ audit_account.account_logs.create!(event_attributes.merge(user: actor))
85
+ end
86
+
87
+ def record_user!
88
+ raise ArgumentError, "user is required" unless actor
89
+
90
+ audit_account.user_logs.create!(event_attributes.merge(user: actor))
91
+ end
92
+
93
+ private
94
+
95
+ attr_reader :actor, :audit_account, :subject
96
+
97
+ def resolve_audit_account(account)
98
+ resolved = account if account.is_a?(LesliAudit::Account)
99
+ resolved ||= account&.audit if account&.respond_to?(:audit)
100
+
101
+ return resolved if resolved
102
+
103
+ raise MissingAccount, "LesliAudit account is required"
104
+ end
105
+
106
+ # Callers build details explicitly from domain data. Never pass request
107
+ # parameters, credentials, or unfiltered model attributes as details.
108
+ def event_attributes
109
+ {
110
+ engine: @engine,
111
+ source: @source,
112
+ action: @action,
113
+ operation: @operation,
114
+ description: @description,
115
+ session_id: @session_id,
116
+ actor_uid: actor_uid,
117
+ subject_type: subject&.class&.name,
118
+ subject_id: subject&.id,
119
+ details: @details
120
+ }
121
+ end
122
+
123
+ def actor_uid
124
+ actor.uid if actor&.respond_to?(:uid)
125
+ end
126
+
127
+ def identifier(value)
128
+ return if value.nil?
129
+ return value.name if value.respond_to?(:name) && value.name.present?
130
+
131
+ value.to_s
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,237 @@
1
+ =begin
2
+
3
+ Lesli
4
+
5
+ Copyright (c) 2023, Lesli Technologies, S. A.
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see http://www.gnu.org/licenses/.
19
+
20
+ Lesli · Ruby on Rails SaaS development platform.
21
+
22
+ Made with ♥ by https://www.lesli.tech
23
+ Building a better future, one line of code at a time.
24
+
25
+ @contact hello@lesli.tech
26
+ @website https://www.lesli.tech
27
+ @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
+
29
+ // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
+ // ·
31
+ =end
32
+
33
+ module LesliAudit
34
+ class AnalyticsService < Lesli::ApplicationLesliService
35
+ CACHE_TTL = 1.hour
36
+ CACHE_VERSION = 1
37
+ LIMIT = 15
38
+
39
+ PLATFORM_ICONS = {
40
+ /chrome os|cros/i => "chrome-fill",
41
+ /ios|ipad|iphone|mac/i => "apple-fill",
42
+ /windows/i => "windows-fill",
43
+ /ubuntu/i => "ubuntu-fill",
44
+ /debian|fedora|linux/i => "computer-fill",
45
+ /android/i => "android-fill"
46
+ }.freeze
47
+
48
+ BROWSER_ICONS = {
49
+ /chrome|chromium/i => "chrome-fill",
50
+ /firefox/i => "firefox-fill",
51
+ /safari/i => "safari-fill",
52
+ /edge/i => "edge-fill",
53
+ /opera|opr/i => "opera-fill",
54
+ /internet explorer|msie/i => "ie-fill",
55
+ /brave/i => "brave-fill"
56
+ }.freeze
57
+
58
+ DEVICE_ICONS = {
59
+ "desktop" => "mac-line",
60
+ "tablet" => "tablet-line",
61
+ "smartphone" => "smartphone-line",
62
+ "unknown" => "file-unknow-line"
63
+ }.freeze
64
+
65
+ PERIODS = %w[day week month sixmonth year].freeze
66
+
67
+ # Cache the complete dashboard as one unit. This keeps the policy in one
68
+ # place and avoids six independent cache entries with different ages.
69
+ def dashboard
70
+ return build_dashboard unless cache_enabled?
71
+
72
+ cache_store.fetch(cache_key, expires_in: CACHE_TTL) do
73
+ build_dashboard
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def summary
80
+ controllers, total_requests = filtered_requests.pick(
81
+ Arel.sql("COUNT(DISTINCT request_controller)"),
82
+ Arel.sql("SUM(request_count)")
83
+ )
84
+
85
+ {
86
+ controllers: controllers.to_i,
87
+ requests: total_requests.to_i
88
+ }
89
+ end
90
+
91
+ def request_trend
92
+ date_group = daily_date_group
93
+
94
+ filtered_requests
95
+ .group(Arel.sql(date_group))
96
+ .order(Arel.sql("#{date_group} DESC"))
97
+ .limit(10)
98
+ .pluck(
99
+ Arel.sql(date_group),
100
+ Arel.sql("SUM(request_count)")
101
+ )
102
+ .map do |date, request_count|
103
+ {
104
+ "xaxiskey" => date,
105
+ "yaxiskey" => request_count.to_i
106
+ }
107
+ end
108
+ end
109
+
110
+ def top_controllers
111
+ filtered_requests
112
+ .group(:request_controller)
113
+ .order(Arel.sql("SUM(request_count) DESC"))
114
+ .limit(LIMIT)
115
+ .pluck(
116
+ :request_controller,
117
+ Arel.sql("SUM(request_count)")
118
+ )
119
+ .map do |controller, request_count|
120
+ {
121
+ "request_controller" => controller,
122
+ "requests" => request_count.to_i
123
+ }
124
+ end
125
+ end
126
+
127
+ def operating_systems
128
+ distribution(:agent_platform, :operating_system) do |operating_system|
129
+ icon_for(operating_system, PLATFORM_ICONS, "computer-fill")
130
+ end
131
+ end
132
+
133
+ def devices
134
+ distribution(:agent_device, :device) do |device|
135
+ DEVICE_ICONS.fetch(device.to_s, DEVICE_ICONS.fetch("unknown"))
136
+ end
137
+ end
138
+
139
+ def browsers
140
+ distribution(:agent_browser, :browser) do |browser|
141
+ icon_for(browser, BROWSER_ICONS, "global-fill")
142
+ end
143
+ end
144
+
145
+ def build_dashboard
146
+ {
147
+ summary: summary,
148
+ request_trend: request_trend,
149
+ top_controllers: top_controllers,
150
+ browsers: browsers,
151
+ devices: devices,
152
+ operating_systems: operating_systems
153
+ }
154
+ end
155
+
156
+ def cache_enabled?
157
+ Rails.env.production?
158
+ end
159
+
160
+ def cache_store
161
+ Rails.cache
162
+ end
163
+
164
+ def cache_key
165
+ # The date prevents a cached "day" period from crossing midnight,
166
+ # even when the one-hour TTL has not expired yet.
167
+ [
168
+ "lesli_audit",
169
+ "analytics",
170
+ "v#{CACHE_VERSION}",
171
+ account.id,
172
+ selected_period,
173
+ Date.current.iso8601
174
+ ].join("/")
175
+ end
176
+
177
+ def account
178
+ @account ||= current_user.account.audit
179
+ end
180
+
181
+ def filtered_requests
182
+ apply_time_filter(account.account_requests)
183
+ end
184
+
185
+ def filtered_devices
186
+ apply_time_filter(account.account_devices)
187
+ end
188
+
189
+ def daily_date_group
190
+ return "strftime('%Y-%m-%d', date)" if ActiveRecord::Base.connection.adapter_name == "SQLite"
191
+
192
+ "DATE_TRUNC('day', date)"
193
+ end
194
+
195
+ def distribution(column, value_key)
196
+ counts = filtered_devices.group(column).sum(:agent_count)
197
+ total = counts.values.sum
198
+
199
+ counts.map do |value, count|
200
+ {
201
+ value_key => value,
202
+ count: count,
203
+ icon: yield(value),
204
+ percentage: percentage(count, total)
205
+ }
206
+ end
207
+ end
208
+
209
+ def percentage(count, total)
210
+ return 0 if total.zero?
211
+
212
+ ((count.to_f / total) * 100).round
213
+ end
214
+
215
+ def icon_for(value, icons, default)
216
+ match = icons.find { |pattern, _icon| value.to_s.match?(pattern) }
217
+ match ? match.last : default
218
+ end
219
+
220
+ def apply_time_filter(relation)
221
+ start_date = case selected_period
222
+ when "day" then Date.current
223
+ when "week" then 6.days.ago.to_date
224
+ when "month" then 29.days.ago.to_date
225
+ when "sixmonth" then 6.months.ago.to_date
226
+ when "year" then 1.year.ago.to_date
227
+ end
228
+
229
+ start_date ? relation.where(date: start_date..) : relation
230
+ end
231
+
232
+ def selected_period
233
+ period = query[:bytime].to_s
234
+ PERIODS.include?(period) ? period : "all"
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,92 @@
1
+ module LesliAudit::Cloudflare
2
+ class Configuration
3
+ class Invalid < StandardError; end
4
+
5
+ DEFAULTS = {
6
+ enabled: false,
7
+ hostnames: [],
8
+ account_ids: [],
9
+ page_size: 1_000,
10
+ max_pages: 100,
11
+ backfill_days: 30,
12
+ reimport_days: 3,
13
+ resource_resolver: ->(**) { nil }
14
+ }.freeze
15
+
16
+ attr_reader :account_ids, :backfill_days, :hostnames, :max_pages,
17
+ :page_size, :reimport_days, :resource_resolver, :zone_id
18
+
19
+ def self.from_application
20
+ settings = DEFAULTS.merge(Lesli.config.audit.fetch(:cloudflare, {}))
21
+
22
+ new(
23
+ enabled: settings[:enabled],
24
+ token: credentials_token.presence || ENV["CLOUDFLARE_ANALYTICS_TOKEN"],
25
+ zone_id: settings[:zone_id],
26
+ hostnames: settings[:hostnames],
27
+ account_ids: settings[:account_ids],
28
+ page_size: settings[:page_size],
29
+ max_pages: settings[:max_pages],
30
+ backfill_days: settings[:backfill_days],
31
+ reimport_days: settings[:reimport_days],
32
+ resource_resolver: settings[:resource_resolver]
33
+ )
34
+ end
35
+
36
+ def self.credentials_token
37
+ Rails.application.credentials.dig(:cloudflare, :analytics, :token)
38
+ end
39
+ private_class_method :credentials_token
40
+
41
+ def initialize(enabled:, token:, zone_id:, hostnames:, account_ids:,
42
+ page_size:, max_pages:, backfill_days:, reimport_days:, resource_resolver:)
43
+ @enabled = ActiveModel::Type::Boolean.new.cast(enabled)
44
+ @token = token.to_s.strip.presence
45
+ @zone_id = zone_id.to_s.strip.presence
46
+ @hostnames = Array(hostnames)
47
+ .filter_map { |value| value.to_s.strip.downcase.presence }
48
+ .uniq
49
+ @account_ids = normalize_account_ids(account_ids)
50
+ @page_size = positive_integer(page_size, :page_size)
51
+ @max_pages = positive_integer(max_pages, :max_pages)
52
+ @backfill_days = positive_integer(backfill_days, :backfill_days)
53
+ @reimport_days = positive_integer(reimport_days, :reimport_days)
54
+ @resource_resolver = resource_resolver
55
+ end
56
+
57
+ def enabled?
58
+ @enabled
59
+ end
60
+
61
+ def token
62
+ @token
63
+ end
64
+
65
+ def validate!
66
+ raise Invalid, "Cloudflare analytics token is missing" if token.blank?
67
+ raise Invalid, "Cloudflare zone ID is missing" if zone_id.blank?
68
+ raise Invalid, "At least one Cloudflare hostname is required" if hostnames.empty?
69
+ raise Invalid, "resource_resolver must be callable" unless resource_resolver.respond_to?(:call)
70
+
71
+ self
72
+ end
73
+
74
+ private
75
+
76
+ def normalize_account_ids(values)
77
+ Array(values).filter_map do |value|
78
+ id = Integer(value, exception: false)
79
+ id if id&.positive?
80
+ end.uniq
81
+ end
82
+
83
+ def positive_integer(value, name)
84
+ number = Integer(value)
85
+ raise ArgumentError unless number.positive?
86
+
87
+ number
88
+ rescue ArgumentError, TypeError
89
+ raise Invalid, "#{name} must be a positive integer"
90
+ end
91
+ end
92
+ end