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
@@ -27,17 +27,111 @@ 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
 
33
33
  module LesliAudit
34
34
  class LogService < Lesli::ApplicationLesliService
35
+ LOG_KINDS = %w[user account].freeze
36
+ DEFAULT_KIND = "user"
37
+ DEFAULT_PER_PAGE = 12
38
+ MAX_PER_PAGE = 100
39
+
40
+ SEARCH_FIELDS = %i[
41
+ operation
42
+ description
43
+ source
44
+ action
45
+ actor_uid
46
+ subject_type
47
+ ].freeze
35
48
 
36
49
  def index
37
- current_user.logs.all
38
- .order(created_at: :desc)
39
- .page(query[:pagination][:page])
40
- .per(query[:pagination][:perPage])
50
+ logs = apply_search(logs_scope)
51
+
52
+ logs
53
+ .order(sort_order, id: :desc)
54
+ .page(page)
55
+ .per(per_page)
56
+ end
57
+
58
+ def find(id)
59
+ logs_scope.find(id)
60
+ end
61
+
62
+ def kind
63
+ requested_kind = query[:kind].to_s
64
+ LOG_KINDS.include?(requested_kind) ? requested_kind : DEFAULT_KIND
65
+ end
66
+
67
+ def available_kinds
68
+ LOG_KINDS
69
+ end
70
+
71
+ private
72
+
73
+ def logs_scope
74
+ return account_logs if kind == "account"
75
+
76
+ user_logs
77
+ end
78
+
79
+ def account_logs
80
+ audit_account.account_logs
81
+ end
82
+
83
+ def user_logs
84
+ audit_account.user_logs
85
+ end
86
+
87
+ def audit_account
88
+ current_user.account.audit
89
+ end
90
+
91
+ def apply_search(logs)
92
+ term = query[:search].to_s.strip
93
+ return logs if term.blank?
94
+
95
+ # Build one case-insensitive condition from a fixed field allowlist;
96
+ # the search text remains a bound value instead of becoming SQL.
97
+ pattern = "%#{ActiveRecord::Base.sanitize_sql_like(term.downcase)}%"
98
+ conditions = SEARCH_FIELDS.map do |field|
99
+ lower(log_table[field]).matches(pattern)
100
+ end
101
+
102
+ logs.where(conditions.reduce(:or))
103
+ end
104
+
105
+ def lower(attribute)
106
+ Arel::Nodes::NamedFunction.new("LOWER", [ attribute ])
107
+ end
108
+
109
+ def sort_order
110
+ direction = query.dig(:order, :dir).to_s.casecmp?("asc") ? :asc : :desc
111
+ sort_column.public_send(direction)
112
+ end
113
+
114
+ def sort_column
115
+ field = query.dig(:order, :by).to_s
116
+ field = "created_at" unless %w[operation source actor_uid created_at].include?(field)
117
+
118
+ log_table[field]
119
+ end
120
+
121
+ def log_table
122
+ kind == "account" ? AccountLog.arel_table : UserLog.arel_table
123
+ end
124
+
125
+ def page
126
+ requested_page = query.dig(:pagination, :page).to_i
127
+ requested_page.positive? ? requested_page : 1
128
+ end
129
+
130
+ def per_page
131
+ requested_size = query.dig(:pagination, :perPage).to_i
132
+ return DEFAULT_PER_PAGE unless requested_size.positive?
133
+
134
+ [ requested_size, MAX_PER_PAGE ].min
41
135
  end
42
136
  end
43
137
  end
@@ -27,55 +27,85 @@ 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
 
33
33
  module LesliAudit
34
34
  class RequestService < Lesli::ApplicationLesliService
35
+ ACCOUNT_WIDE_ROLES = %w[owner admin].freeze
36
+ DEFAULT_PER_PAGE = 12
37
+ MAX_PER_PAGE = 100
38
+
39
+ SORT_COLUMNS = {
40
+ "user" => Lesli::User.arel_table[:email],
41
+ "controller" => UserRequest.arel_table[:request_controller],
42
+ "action" => UserRequest.arel_table[:request_action],
43
+ "count" => UserRequest.arel_table[:request_count],
44
+ "date" => UserRequest.arel_table[:date]
45
+ }.freeze
46
+
47
+ def index
48
+ requests = current_user.account.audit.user_requests
49
+ .joins(:user)
50
+ .select(
51
+ "lesli_audit_user_requests.*",
52
+ "lesli_users.email AS user_email"
53
+ )
54
+
55
+ requests = requests.where(user_id: current_user.id) unless account_wide?
56
+ requests = apply_search(requests)
57
+
58
+ requests
59
+ .order(sort_order, id: :desc)
60
+ .page(page)
61
+ .per(per_page)
62
+ end
63
+
64
+ private
65
+
66
+ # LesliShield authorizes access to the controller. Built-in owner and
67
+ # admin roles additionally receive the account-wide reporting scope.
68
+ def account_wide?
69
+ current_user.respond_to?(:has_roles?) &&
70
+ current_user.has_roles?(*ACCOUNT_WIDE_ROLES)
71
+ end
72
+
73
+ def apply_search(requests)
74
+ term = query[:search].to_s.strip
75
+ return requests if term.blank?
76
+
77
+ pattern = "%#{ActiveRecord::Base.sanitize_sql_like(term.downcase)}%"
78
+
79
+ requests.where(
80
+ <<~SQL.squish,
81
+ LOWER(lesli_users.email) LIKE :pattern
82
+ OR LOWER(lesli_audit_user_requests.request_controller) LIKE :pattern
83
+ OR LOWER(lesli_audit_user_requests.request_action) LIKE :pattern
84
+ SQL
85
+ pattern: pattern
86
+ )
87
+ end
88
+
89
+ def sort_column
90
+ SORT_COLUMNS.fetch(query.dig(:order, :by).to_s, SORT_COLUMNS.fetch("date"))
91
+ end
92
+
93
+ def sort_order
94
+ return sort_column.asc if query.dig(:order, :dir).to_s.casecmp?("asc")
95
+
96
+ sort_column.desc
97
+ end
98
+
99
+ def page
100
+ requested_page = query.dig(:pagination, :page).to_i
101
+ requested_page.positive? ? requested_page : 1
102
+ end
103
+
104
+ def per_page
105
+ requested_size = query.dig(:pagination, :perPage).to_i
106
+ return DEFAULT_PER_PAGE unless requested_size.positive?
35
107
 
36
- # @overwrite
37
- # @return {Hash} Paginated list of the records
38
- # @param {query} Has of the formated queries/filters that will be applied to filter data
39
- # @description
40
- # @example
41
- def index
42
-
43
-
44
- requests = current_user.requests
45
- .order(created_at: :desc)
46
- .page(query[:pagination][:page])
47
- .per(query[:pagination][:perPage])
48
-
49
- return requests
50
-
51
-
52
- search_string = nil # query[:search].downcase.gsub(" ","%") unless query[:search].blank?
53
- order_string = "user_requests.created_at"
54
-
55
- user_requests = current_user.account.users.joins(:requests)
56
- #user_requests = user_requests.where("users.email = ?", query[:filters][:user]) unless query[:filters][:user].blank?
57
-
58
- if !search_string.blank?
59
- # user_requests = user_requests.where([
60
- # "lower(users.email) like :search_string",
61
- # "lower(request_controller) like :search_string",
62
- # "lower(request_action) like :search_string"
63
- # ].join(" or "), {
64
- # search_string: "%#{search_string}%"
65
- # })
66
- end
67
-
68
- user_requests.select(
69
- :id,
70
- "users.id as user_id",
71
- "users.email",
72
- :request_controller,
73
- :request_action,
74
- :request_method,
75
- :user_session_id,
76
- Date2.new.date_time.db_timestamps("user_requests")
77
- ).page(query[:pagination][:page])
78
- .per(query[:pagination][:perPage])
108
+ [ requested_size, MAX_PER_PAGE ].min
79
109
  end
80
110
  end
81
111
  end
@@ -0,0 +1,146 @@
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
+ # Applies the retention policy configured by the host application.
35
+ #
36
+ # Cleanup uses delete_all deliberately: analytics has no destroy callbacks,
37
+ # while activity events are append-only during their retained lifetime. Each
38
+ # relation is deleted in bounded primary-key batches and can be retried safely.
39
+ class RetentionCleanup
40
+ DEFAULT_BATCH_SIZE = 1_000
41
+
42
+ ANALYTICS_MODELS = [
43
+ LesliAudit::AccountDevice,
44
+ LesliAudit::AccountRequest,
45
+ LesliAudit::UserJournal,
46
+ LesliAudit::UserRequest
47
+ ].freeze
48
+
49
+ TRAFFIC_MODELS = [
50
+ LesliAudit::TrafficRollup
51
+ ].freeze
52
+
53
+ ACTIVITY_MODELS = [
54
+ LesliAudit::AccountLog,
55
+ LesliAudit::UserLog
56
+ ].freeze
57
+
58
+ def self.call(batch_size: DEFAULT_BATCH_SIZE, today: Date.current, now: Time.current)
59
+ new(batch_size: batch_size, today: today, now: now).call
60
+ end
61
+
62
+ def initialize(batch_size:, today:, now:)
63
+ @batch_size = positive_integer!(batch_size, :batch_size)
64
+ @today = today.to_date
65
+ @now = now
66
+ end
67
+
68
+ def call
69
+ deleted = {}
70
+
71
+ cleanup_group(
72
+ deleted,
73
+ ANALYTICS_MODELS,
74
+ column: :date,
75
+ cutoff: date_cutoff(:analytics_retention_days)
76
+ )
77
+ cleanup_group(
78
+ deleted,
79
+ TRAFFIC_MODELS,
80
+ column: :date,
81
+ cutoff: date_cutoff(:traffic_retention_days)
82
+ )
83
+ deleted[LesliAudit::CloudflareTrafficSync.table_name] = delete_with_cutoff(
84
+ LesliAudit::CloudflareTrafficSync.all,
85
+ :period_end,
86
+ date_cutoff(:traffic_retention_days)
87
+ )
88
+ cleanup_group(
89
+ deleted,
90
+ ACTIVITY_MODELS,
91
+ column: :created_at,
92
+ cutoff: time_cutoff(:audit_retention_days)
93
+ )
94
+
95
+ deleted
96
+ end
97
+
98
+ private
99
+
100
+ attr_reader :batch_size, :now, :today
101
+
102
+ def cleanup_group(deleted, models, column:, cutoff:)
103
+ models.each do |model|
104
+ deleted[model.table_name] = delete_with_cutoff(model.all, column, cutoff)
105
+ end
106
+ end
107
+
108
+ def delete_with_cutoff(relation, column, cutoff)
109
+ return 0 unless cutoff
110
+
111
+ relation
112
+ .where(relation.klass.arel_table[column].lt(cutoff))
113
+ .in_batches(of: batch_size)
114
+ .delete_all
115
+ end
116
+
117
+ def date_cutoff(setting)
118
+ days = retention_days(setting)
119
+ today - days if days
120
+ end
121
+
122
+ def time_cutoff(setting)
123
+ days = retention_days(setting)
124
+ now - days.days if days
125
+ end
126
+
127
+ def retention_days(setting)
128
+ value = Lesli.config.audit&.dig(setting)
129
+ return if value.nil?
130
+
131
+ positive_integer!(value, setting)
132
+ end
133
+
134
+ def positive_integer!(value, setting)
135
+ valid_value = value.is_a?(Integer) || (value.is_a?(String) && value.match?(/\A[1-9]\d*\z/))
136
+ raise ArgumentError unless valid_value
137
+
138
+ integer = value.to_i
139
+ return integer if integer.positive?
140
+
141
+ raise ArgumentError, "#{setting} must be a positive integer or nil"
142
+ rescue ArgumentError, TypeError
143
+ raise ArgumentError, "#{setting} must be a positive integer or nil"
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,216 @@
1
+ module LesliAudit
2
+ class TrafficService < Lesli::ApplicationLesliService
3
+ LIMIT = 10
4
+ STALE_AFTER = 36.hours
5
+
6
+ def dashboard
7
+ {
8
+ summary: summary,
9
+ trend: trend,
10
+ paths: paths,
11
+ resources: resources,
12
+ countries: breakdown(:country),
13
+ devices: breakdown(:device),
14
+ referrers: breakdown(:referrer),
15
+ synchronization: synchronization
16
+ }
17
+ end
18
+
19
+ def summary
20
+ page_views, visits = rollups.pick(
21
+ Arel.sql("COALESCE(SUM(page_views), 0)"),
22
+ Arel.sql("COALESCE(SUM(visits), 0)")
23
+ )
24
+
25
+ {
26
+ page_views: page_views.to_i,
27
+ visits: visits.to_i,
28
+ resources: rollups.group(:resource_type, :resource_id).count.length,
29
+ tracked_paths: rollups.group(:provider, :hostname, :path).count.length
30
+ }
31
+ end
32
+
33
+ def trend
34
+ rollups
35
+ .group(:date)
36
+ .order(:date)
37
+ .pluck(
38
+ :date,
39
+ Arel.sql("SUM(page_views)"),
40
+ Arel.sql("SUM(visits)")
41
+ )
42
+ .map do |date, page_views, visits|
43
+ {
44
+ date:,
45
+ page_views: page_views.to_i,
46
+ visits: visits.to_i
47
+ }
48
+ end
49
+ end
50
+
51
+ def resources
52
+ rollups
53
+ .group(:resource_type, :resource_id)
54
+ .order(Arel.sql("SUM(page_views) DESC"))
55
+ .limit(LIMIT)
56
+ .pluck(
57
+ :resource_type,
58
+ :resource_id,
59
+ Arel.sql("SUM(page_views)"),
60
+ Arel.sql("SUM(visits)")
61
+ )
62
+ .map do |resource_type, resource_id, page_views, visits|
63
+ {
64
+ "resource" => "#{resource_type} ##{resource_id}",
65
+ "page_views" => page_views.to_i,
66
+ "visits" => visits.to_i
67
+ }
68
+ end
69
+ end
70
+
71
+ def paths
72
+ # Hostname and provider remain part of the group so equal paths
73
+ # from different sites or providers are never combined.
74
+ rollups
75
+ .group(:provider, :hostname, :path)
76
+ .order(Arel.sql("SUM(visits) DESC"))
77
+ .limit(LIMIT)
78
+ .pluck(
79
+ :provider,
80
+ :hostname,
81
+ :path,
82
+ Arel.sql("SUM(page_views)"),
83
+ Arel.sql("SUM(visits)")
84
+ )
85
+ .map do |provider, hostname, path, page_views, visits|
86
+ {
87
+ "provider" => provider.to_s.titleize,
88
+ "hostname" => hostname,
89
+ "path" => path,
90
+ "page_views" => page_views.to_i,
91
+ "visits" => visits.to_i
92
+ }
93
+ end
94
+ end
95
+
96
+ def breakdown(dimension)
97
+ dimension = dimension.to_s
98
+ return [] unless account && TrafficBreakdown::DIMENSIONS.include?(dimension)
99
+
100
+ relation = account.traffic_breakdowns
101
+ .where(traffic_rollup_id: rollups.select(:id), dimension:)
102
+
103
+ total_page_views, total_visits = relation.pick(
104
+ Arel.sql("COALESCE(SUM(page_views), 0)"),
105
+ Arel.sql("COALESCE(SUM(visits), 0)")
106
+ )
107
+ records = relation
108
+ .group(:value)
109
+ .order(Arel.sql("SUM(page_views) DESC"))
110
+ .limit(LIMIT)
111
+ .pluck(
112
+ :value,
113
+ Arel.sql("SUM(page_views)"),
114
+ Arel.sql("SUM(visits)")
115
+ )
116
+
117
+ rows = records.map do |value, page_views, visits|
118
+ page_views = page_views.to_i
119
+ {
120
+ value:,
121
+ page_views:,
122
+ visits: visits.to_i,
123
+ percentage: percentage(page_views, total_page_views)
124
+ }
125
+ end
126
+
127
+ append_other(rows, total_page_views.to_i, total_visits.to_i)
128
+ end
129
+
130
+ # Synchronization is intentionally based on every rollup in the account,
131
+ # not the selected report filters. Filters must not make fresh data look old.
132
+ def synchronization(current_time: Time.current)
133
+ status = synchronization_status
134
+ last_synced_at = status[:last_synced_at]
135
+
136
+ {
137
+ source: status[:source].to_s.titleize.presence || "Cloudflare",
138
+ last_synced_at:,
139
+ stale: last_synced_at.present? && last_synced_at < STALE_AFTER.ago(current_time)
140
+ }
141
+ end
142
+
143
+ private
144
+
145
+ def account
146
+ @account ||= current_user&.account&.audit
147
+ end
148
+
149
+ def account_rollups
150
+ account ? account.traffic_rollups : TrafficRollup.none
151
+ end
152
+
153
+ def synchronization_status
154
+ if account
155
+ status = CloudflareTrafficSync.latest_status_for(account)
156
+ return status if status[:last_synced_at]
157
+ end
158
+
159
+ provider, last_synced_at = account_rollups
160
+ .order(snapshot_at: :desc)
161
+ .pick(:provider, :snapshot_at)
162
+
163
+ { source: provider || CloudflareTrafficSync::SOURCE, last_synced_at: }
164
+ end
165
+
166
+ def rollups
167
+ @rollups ||= begin
168
+ relation = account_rollups
169
+ relation = apply_time_filter(relation)
170
+ relation = relation.where(resource_type: query[:resource_type].to_s.strip) if query[:resource_type].present?
171
+ relation = relation.where(resource_id: resource_id_filter) if resource_id_filter
172
+ relation
173
+ end
174
+ end
175
+
176
+ def resource_id_filter
177
+ return @resource_id_filter if defined?(@resource_id_filter)
178
+
179
+ @resource_id_filter = Integer(query[:resource_id], exception: false)
180
+ @resource_id_filter = nil unless @resource_id_filter&.positive?
181
+ @resource_id_filter
182
+ end
183
+
184
+ def apply_time_filter(relation)
185
+ start_date = case query[:bytime].presence || "month"
186
+ when "day" then Date.current
187
+ when "week" then 6.days.ago.to_date
188
+ when "month" then 29.days.ago.to_date
189
+ when "sixmonth" then 6.months.ago.to_date
190
+ when "year" then 1.year.ago.to_date
191
+ end
192
+
193
+ start_date ? relation.where(date: start_date..) : relation
194
+ end
195
+
196
+ def percentage(count, total)
197
+ return 0.0 if total.to_i.zero?
198
+
199
+ ((count.to_f / total) * 100).round(1)
200
+ end
201
+
202
+ def append_other(rows, total_page_views, total_visits)
203
+ other_page_views = total_page_views - rows.sum { |row| row[:page_views] }
204
+ other_visits = total_visits - rows.sum { |row| row[:visits] }
205
+ return rows unless other_page_views.positive? || other_visits.positive?
206
+
207
+ rows << {
208
+ value: "other",
209
+ page_views: other_page_views,
210
+ visits: other_visits,
211
+ percentage: percentage(other_page_views, total_page_views),
212
+ other: true
213
+ }
214
+ end
215
+ end
216
+ end