lesli_audit 1.1.2 → 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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lesli_audit/application.css +183 -0
  3. data/app/controllers/lesli_audit/analytics_controller.rb +16 -0
  4. data/app/controllers/lesli_audit/logs_controller.rb +12 -2
  5. data/app/controllers/lesli_audit/traffic_controller.rb +25 -0
  6. data/app/controllers/lesli_audit/users_controller.rb +7 -4
  7. data/app/interfaces/lesli_audit/logger_interface.rb +5 -4
  8. data/app/jobs/lesli_audit/cloudflare_traffic_sync_job.rb +78 -0
  9. data/app/models/lesli_audit/account.rb +12 -0
  10. data/app/models/lesli_audit/cloudflare_traffic_sync.rb +99 -0
  11. data/app/models/lesli_audit/traffic_breakdown.rb +38 -0
  12. data/app/models/lesli_audit/traffic_rollup.rb +33 -0
  13. data/app/models/lesli_audit/user_journal.rb +0 -2
  14. data/app/services/lesli_audit/activity_audit.rb +8 -4
  15. data/app/services/lesli_audit/analytics_service.rb +237 -0
  16. data/app/services/lesli_audit/cloudflare/configuration.rb +92 -0
  17. data/app/services/lesli_audit/cloudflare/graphql_client.rb +309 -0
  18. data/app/services/lesli_audit/cloudflare/snapshot_converter.rb +101 -0
  19. data/app/services/lesli_audit/cloudflare/sync_service.rb +79 -0
  20. data/app/services/lesli_audit/cloudflare_traffic_snapshot.rb +182 -0
  21. data/app/services/lesli_audit/log_service.rb +99 -5
  22. data/app/services/lesli_audit/request_service.rb +73 -10
  23. data/app/services/lesli_audit/retention_cleanup.rb +16 -16
  24. data/app/services/lesli_audit/traffic_service.rb +216 -0
  25. data/app/services/lesli_audit/traffic_snapshot_importer.rb +120 -0
  26. data/app/services/lesli_audit/user_service.rb +119 -152
  27. data/app/views/lesli_audit/{visitors → analytics}/index.html.erb +40 -30
  28. data/app/views/lesli_audit/logs/index.html.erb +88 -19
  29. data/app/views/lesli_audit/logs/show.html.erb +36 -8
  30. data/app/views/lesli_audit/partials/_navigation.html.erb +5 -4
  31. data/app/views/lesli_audit/requests/index.html.erb +80 -19
  32. data/app/views/lesli_audit/traffic/_breakdown.html.erb +46 -0
  33. data/app/views/lesli_audit/traffic/index.html.erb +177 -0
  34. data/app/views/lesli_audit/users/index.html.erb +97 -40
  35. data/config/locales/translations.en.yml +137 -1
  36. data/config/locales/translations.es.yml +137 -1
  37. data/config/locales/translations.fr.yml +132 -1
  38. data/config/locales/translations.it.yml +132 -1
  39. data/config/locales/translations.pt.yml +132 -1
  40. data/config/routes.rb +6 -23
  41. data/db/migrate/v1.0/0501110110_create_lesli_audit_account_logs.rb +2 -1
  42. data/db/migrate/v1.0/0501120110_create_lesli_audit_user_logs.rb +2 -1
  43. data/db/migrate/v1.0/0501120210_create_lesli_audit_user_journals.rb +0 -3
  44. data/db/migrate/v1.0/0501130110_create_lesli_audit_traffic_rollups.rb +55 -0
  45. data/db/migrate/v1.0/0501130210_create_lesli_audit_traffic_breakdowns.rb +41 -0
  46. data/db/migrate/v1.0/0501130310_create_lesli_audit_cloudflare_traffic_syncs.rb +42 -0
  47. data/db/seed/devices.rb +2 -1
  48. data/db/seed/traffic.rb +128 -0
  49. data/db/seed/users.rb +68 -27
  50. data/db/seeds.rb +3 -2
  51. data/lib/lesli_audit/version.rb +2 -2
  52. data/lib/tasks/lesli_audit_cloudflare_traffic.rake +16 -0
  53. data/readme.md +10 -0
  54. metadata +24 -5
  55. data/app/controllers/lesli_audit/visitors_controller.rb +0 -14
  56. data/app/services/lesli_audit/visitor_service.rb +0 -212
@@ -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
@@ -12,8 +12,6 @@ module LesliAudit
12
12
  validates :event_uid, presence: true
13
13
  validates :request_controller, :request_action, presence: true, if: :retain?
14
14
 
15
- scope :pending, -> { where(processed_at: nil) }
16
- scope :processed, -> { where.not(processed_at: nil) }
17
15
  scope :retained, -> { where(retain: true) }
18
16
  scope :transient, -> { where(retain: false) }
19
17
  end
@@ -62,7 +62,8 @@ module LesliAudit
62
62
  operation: nil,
63
63
  description: nil,
64
64
  session_id: nil,
65
- subject: nil
65
+ subject: nil,
66
+ details: {}
66
67
  )
67
68
  @audit_account = resolve_audit_account(account)
68
69
  @actor = actor
@@ -73,8 +74,10 @@ module LesliAudit
73
74
  @description = description
74
75
  @session_id = session_id.presence
75
76
  @subject = subject
77
+ @details = details
76
78
 
77
79
  raise ArgumentError, "operation is required" if @operation.blank?
80
+ raise ArgumentError, "details must be a Hash" unless @details.is_a?(Hash)
78
81
  end
79
82
 
80
83
  def record_account!
@@ -100,8 +103,8 @@ module LesliAudit
100
103
  raise MissingAccount, "LesliAudit account is required"
101
104
  end
102
105
 
103
- # Keep event metadata deliberately small. Request parameters and other
104
- # unfiltered payloads are not accepted by this API.
106
+ # Callers build details explicitly from domain data. Never pass request
107
+ # parameters, credentials, or unfiltered model attributes as details.
105
108
  def event_attributes
106
109
  {
107
110
  engine: @engine,
@@ -112,7 +115,8 @@ module LesliAudit
112
115
  session_id: @session_id,
113
116
  actor_uid: actor_uid,
114
117
  subject_type: subject&.class&.name,
115
- subject_id: subject&.id
118
+ subject_id: subject&.id,
119
+ details: @details
116
120
  }
117
121
  end
118
122
 
@@ -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