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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f3d2dc51c06a85eb50d460ee02c27930bc2de5765e4745c73ddbd7cdc83d789
4
- data.tar.gz: e3e6036dc2db7b777081398d4ee9bcde2a6707095bd7c30215956f98da6c6f82
3
+ metadata.gz: cc87690d4e0c3d33349d21bba5008b3a303badd7e5e383e4b00145f057099d6c
4
+ data.tar.gz: 8f621fb6f7a4f30b7c515ba666b6a9e82bbdea7cc2f776be2848c280716f8da5
5
5
  SHA512:
6
- metadata.gz: ec1b7c2ef3957736a5832dee9c2a20f627e809c59e698401d44fdc1c356f6e5ba46d85b33e456446b036b7d83d70f97eb2f0a7bfea5fb8a48340893643fa3e40
7
- data.tar.gz: d772a35fca2f2595305125b65e51b1f825df8c51ae5e020b553d5aabaf0cdfb62d7aa5c78586bf50945ebf54fa2265ddc4c3e7d7b9cfaa9da60f17e3dabef2cd
6
+ metadata.gz: 394689968d6234bcd0bfe7c93345e3f8032159eba78106136fbe65bb9b72eb578ced91688ea47dfe2910a28e908137fa105dd8ba039a8534e0dee92e31bc3be8
7
+ data.tar.gz: dc0a38e689115d0c43014988b3a902335f983a24094c24a652aa89a35a80622aa891e05f6c5fddb25eaa763767f048d3f0b92a7fdcfbd9bda80ff1827cc1a0db
@@ -3,20 +3,33 @@
3
3
  @layer theme, utilities;
4
4
  @layer theme {
5
5
  :root, :host {
6
+ --color-amber-50: oklch(98.7% 0.022 95.277);
7
+ --color-amber-300: oklch(87.9% 0.169 91.605);
6
8
  --color-amber-600: oklch(66.6% 0.179 58.318);
9
+ --color-amber-900: oklch(41.4% 0.112 45.904);
7
10
  --color-sky-600: oklch(58.8% 0.158 241.966);
8
11
  --color-sky-700: oklch(50% 0.134 242.749);
9
12
  --color-sky-900: oklch(39.1% 0.09 240.876);
10
13
  --color-rose-600: oklch(58.6% 0.253 17.585);
14
+ --color-slate-50: oklch(98.4% 0.003 247.858);
11
15
  --color-slate-100: oklch(96.8% 0.007 247.896);
12
16
  --color-slate-200: oklch(92.9% 0.013 255.508);
17
+ --color-slate-300: oklch(86.9% 0.022 252.894);
18
+ --color-slate-400: oklch(70.4% 0.04 256.788);
13
19
  --color-slate-500: oklch(55.4% 0.046 257.417);
20
+ --color-slate-600: oklch(44.6% 0.043 257.281);
14
21
  --color-slate-700: oklch(37.2% 0.044 257.287);
22
+ --color-slate-800: oklch(27.9% 0.041 260.031);
15
23
  --color-slate-900: oklch(20.8% 0.042 265.755);
16
24
  --color-white: #fff;
17
25
  --spacing: 0.25rem;
26
+ --container-xl: 36rem;
27
+ --text-xs: 0.75rem;
28
+ --text-xs--line-height: calc(1 / 0.75);
18
29
  --text-sm: 0.875rem;
19
30
  --text-sm--line-height: calc(1.25 / 0.875);
31
+ --text-base: 1rem;
32
+ --text-base--line-height: calc(1.5 / 1);
20
33
  --text-lg: 1.125rem;
21
34
  --text-lg--line-height: calc(1.75 / 1.125);
22
35
  --text-xl: 1.25rem;
@@ -26,10 +39,25 @@
26
39
  --font-weight-medium: 500;
27
40
  --font-weight-semibold: 600;
28
41
  --tracking-tight: -0.025em;
42
+ --tracking-wide: 0.025em;
43
+ --radius-md: 0.375rem;
44
+ --radius-lg: 0.5rem;
45
+ --radius-xl: 0.75rem;
29
46
  --radius-2xl: 1rem;
30
47
  }
31
48
  }
32
49
  @layer utilities {
50
+ .sr-only {
51
+ position: absolute;
52
+ width: 1px;
53
+ height: 1px;
54
+ padding: 0;
55
+ margin: -1px;
56
+ overflow: hidden;
57
+ clip-path: inset(50%);
58
+ white-space: nowrap;
59
+ border-width: 0;
60
+ }
33
61
  .absolute {
34
62
  position: absolute;
35
63
  }
@@ -48,15 +76,36 @@
48
76
  .-z-10 {
49
77
  z-index: calc(10 * -1);
50
78
  }
79
+ .mx-auto {
80
+ margin-inline: auto;
81
+ }
82
+ .mt-0\.5 {
83
+ margin-top: calc(var(--spacing) * 0.5);
84
+ }
51
85
  .mt-1 {
52
86
  margin-top: var(--spacing);
53
87
  }
54
88
  .mt-3 {
55
89
  margin-top: calc(var(--spacing) * 3);
56
90
  }
91
+ .mt-4 {
92
+ margin-top: calc(var(--spacing) * 4);
93
+ }
57
94
  .mt-6 {
58
95
  margin-top: calc(var(--spacing) * 6);
59
96
  }
97
+ .mb-1\.5 {
98
+ margin-bottom: calc(var(--spacing) * 1.5);
99
+ }
100
+ .mb-4 {
101
+ margin-bottom: calc(var(--spacing) * 4);
102
+ }
103
+ .mb-6 {
104
+ margin-bottom: calc(var(--spacing) * 6);
105
+ }
106
+ .block {
107
+ display: block;
108
+ }
60
109
  .flex {
61
110
  display: flex;
62
111
  }
@@ -81,6 +130,9 @@
81
130
  .w-full {
82
131
  width: 100%;
83
132
  }
133
+ .max-w-xl {
134
+ max-width: var(--container-xl);
135
+ }
84
136
  .\!min-w-0 {
85
137
  min-width: 0px !important;
86
138
  }
@@ -90,18 +142,36 @@
90
142
  .shrink-0 {
91
143
  flex-shrink: 0;
92
144
  }
145
+ .cursor-pointer {
146
+ cursor: pointer;
147
+ }
93
148
  .grid-cols-1 {
94
149
  grid-template-columns: repeat(1, minmax(0, 1fr));
95
150
  }
151
+ .flex-col {
152
+ flex-direction: column;
153
+ }
96
154
  .items-center {
97
155
  align-items: center;
98
156
  }
157
+ .items-end {
158
+ align-items: flex-end;
159
+ }
160
+ .items-start {
161
+ align-items: flex-start;
162
+ }
163
+ .justify-between {
164
+ justify-content: space-between;
165
+ }
99
166
  .justify-center {
100
167
  justify-content: center;
101
168
  }
102
169
  .gap-2 {
103
170
  gap: calc(var(--spacing) * 2);
104
171
  }
172
+ .gap-3 {
173
+ gap: calc(var(--spacing) * 3);
174
+ }
105
175
  .gap-4 {
106
176
  gap: calc(var(--spacing) * 4);
107
177
  }
@@ -119,38 +189,88 @@
119
189
  .rounded-2xl {
120
190
  border-radius: var(--radius-2xl);
121
191
  }
192
+ .rounded-lg {
193
+ border-radius: var(--radius-lg);
194
+ }
195
+ .rounded-md {
196
+ border-radius: var(--radius-md);
197
+ }
198
+ .rounded-xl {
199
+ border-radius: var(--radius-xl);
200
+ }
122
201
  .border {
123
202
  border-style: var(--tw-border-style);
124
203
  border-width: 1px;
125
204
  }
205
+ .border-dashed {
206
+ --tw-border-style: dashed;
207
+ border-style: dashed;
208
+ }
209
+ .border-amber-300 {
210
+ border-color: var(--color-amber-300);
211
+ }
126
212
  .border-slate-200 {
127
213
  border-color: var(--color-slate-200);
128
214
  }
215
+ .border-slate-300 {
216
+ border-color: var(--color-slate-300);
217
+ }
218
+ .bg-amber-50 {
219
+ background-color: var(--color-amber-50);
220
+ }
221
+ .bg-slate-50 {
222
+ background-color: var(--color-slate-50);
223
+ }
129
224
  .bg-slate-100 {
130
225
  background-color: var(--color-slate-100);
131
226
  }
227
+ .bg-slate-900 {
228
+ background-color: var(--color-slate-900);
229
+ }
132
230
  .bg-white {
133
231
  background-color: var(--color-white);
134
232
  }
135
233
  .\!p-0 {
136
234
  padding: 0px !important;
137
235
  }
236
+ .p-4 {
237
+ padding: calc(var(--spacing) * 4);
238
+ }
138
239
  .p-5 {
139
240
  padding: calc(var(--spacing) * 5);
140
241
  }
141
242
  .px-3 {
142
243
  padding-inline: calc(var(--spacing) * 3);
143
244
  }
245
+ .px-4 {
246
+ padding-inline: calc(var(--spacing) * 4);
247
+ }
248
+ .px-6 {
249
+ padding-inline: calc(var(--spacing) * 6);
250
+ }
251
+ .py-2 {
252
+ padding-block: calc(var(--spacing) * 2);
253
+ }
144
254
  .py-2\.5 {
145
255
  padding-block: calc(var(--spacing) * 2.5);
146
256
  }
257
+ .py-12 {
258
+ padding-block: calc(var(--spacing) * 12);
259
+ }
147
260
  .pb-8 {
148
261
  padding-bottom: calc(var(--spacing) * 8);
149
262
  }
263
+ .text-center {
264
+ text-align: center;
265
+ }
150
266
  .text-3xl {
151
267
  font-size: var(--text-3xl);
152
268
  line-height: var(--tw-leading, var(--text-3xl--line-height));
153
269
  }
270
+ .text-base {
271
+ font-size: var(--text-base);
272
+ line-height: var(--tw-leading, var(--text-base--line-height));
273
+ }
154
274
  .text-lg {
155
275
  font-size: var(--text-lg);
156
276
  line-height: var(--tw-leading, var(--text-lg--line-height));
@@ -163,6 +283,10 @@
163
283
  font-size: var(--text-xl);
164
284
  line-height: var(--tw-leading, var(--text-xl--line-height));
165
285
  }
286
+ .text-xs {
287
+ font-size: var(--text-xs);
288
+ line-height: var(--tw-leading, var(--text-xs--line-height));
289
+ }
166
290
  .leading-none {
167
291
  --tw-leading: 1;
168
292
  line-height: 1;
@@ -179,12 +303,22 @@
179
303
  --tw-tracking: var(--tracking-tight);
180
304
  letter-spacing: var(--tracking-tight);
181
305
  }
306
+ .tracking-wide {
307
+ --tw-tracking: var(--tracking-wide);
308
+ letter-spacing: var(--tracking-wide);
309
+ }
182
310
  .break-words {
183
311
  overflow-wrap: break-word;
184
312
  }
313
+ .whitespace-pre-wrap {
314
+ white-space: pre-wrap;
315
+ }
185
316
  .text-amber-600 {
186
317
  color: var(--color-amber-600);
187
318
  }
319
+ .text-amber-900 {
320
+ color: var(--color-amber-900);
321
+ }
188
322
  .text-rose-600 {
189
323
  color: var(--color-rose-600);
190
324
  }
@@ -194,15 +328,30 @@
194
328
  .text-sky-700 {
195
329
  color: var(--color-sky-700);
196
330
  }
331
+ .text-slate-400 {
332
+ color: var(--color-slate-400);
333
+ }
197
334
  .text-slate-500 {
198
335
  color: var(--color-slate-500);
199
336
  }
337
+ .text-slate-600 {
338
+ color: var(--color-slate-600);
339
+ }
200
340
  .text-slate-700 {
201
341
  color: var(--color-slate-700);
202
342
  }
343
+ .text-slate-800 {
344
+ color: var(--color-slate-800);
345
+ }
203
346
  .text-slate-900 {
204
347
  color: var(--color-slate-900);
205
348
  }
349
+ .text-white {
350
+ color: var(--color-white);
351
+ }
352
+ .uppercase {
353
+ text-transform: uppercase;
354
+ }
206
355
  .tabular-nums {
207
356
  --tw-numeric-spacing: tabular-nums;
208
357
  font-variant-numeric: var(--tw-ordinal,) var(--tw-slashed-zero,) var(--tw-numeric-figure,) var(--tw-numeric-spacing,) var(--tw-numeric-fraction,);
@@ -212,14 +361,37 @@
212
361
  box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
213
362
  }
214
363
  @media (hover: hover) {
364
+ .hover\:bg-slate-700:hover {
365
+ background-color: var(--color-slate-700);
366
+ }
215
367
  .hover\:text-sky-900:hover {
216
368
  color: var(--color-sky-900);
217
369
  }
218
370
  }
219
371
  @media (width >= 40rem) {
372
+ .sm\:col-span-2 {
373
+ grid-column: span 2 / span 2;
374
+ }
220
375
  .sm\:grid-cols-2 {
221
376
  grid-template-columns: repeat(2, minmax(0, 1fr));
222
377
  }
378
+ .sm\:flex-row {
379
+ flex-direction: row;
380
+ }
381
+ .sm\:items-center {
382
+ align-items: center;
383
+ }
384
+ .sm\:justify-between {
385
+ justify-content: space-between;
386
+ }
387
+ .sm\:text-right {
388
+ text-align: right;
389
+ }
390
+ }
391
+ @media (width >= 48rem) {
392
+ .md\:grid-cols-4 {
393
+ grid-template-columns: repeat(4, minmax(0, 1fr));
394
+ }
223
395
  }
224
396
  @media (width >= 64rem) {
225
397
  .lg\:w-2\/5 {
@@ -228,10 +400,21 @@
228
400
  .lg\:grid-cols-3 {
229
401
  grid-template-columns: repeat(3, minmax(0, 1fr));
230
402
  }
403
+ .lg\:grid-cols-4 {
404
+ grid-template-columns: repeat(4, minmax(0, 1fr));
405
+ }
231
406
  .lg\:grid-cols-5 {
232
407
  grid-template-columns: repeat(5, minmax(0, 1fr));
233
408
  }
234
409
  }
410
+ @media (width >= 80rem) {
411
+ .xl\:grid-cols-2 {
412
+ grid-template-columns: repeat(2, minmax(0, 1fr));
413
+ }
414
+ .xl\:grid-cols-3 {
415
+ grid-template-columns: repeat(3, minmax(0, 1fr));
416
+ }
417
+ }
235
418
  }
236
419
  @property --tw-border-style {
237
420
  syntax: "*";
@@ -0,0 +1,16 @@
1
+ module LesliAudit
2
+ class AnalyticsController < ApplicationController
3
+
4
+ # GET /analytics
5
+ def index
6
+ dashboard = AnalyticsService.new(current_user, query).dashboard
7
+
8
+ @summary = dashboard.fetch(:summary)
9
+ @request_trend = dashboard.fetch(:request_trend)
10
+ @top_controllers = dashboard.fetch(:top_controllers)
11
+ @browsers = dashboard.fetch(:browsers)
12
+ @devices = dashboard.fetch(:devices)
13
+ @operating_systems = dashboard.fetch(:operating_systems)
14
+ end
15
+ end
16
+ end
@@ -4,7 +4,9 @@ module LesliAudit
4
4
 
5
5
  # GET /logs
6
6
  def index
7
- @logs = respond_with_pagination(LesliAudit::LogService.new(current_user, query).index)
7
+ @log_kind = log_service.kind
8
+ @available_log_kinds = log_service.available_kinds
9
+ @logs = respond_with_pagination(log_service.index)
8
10
  end
9
11
 
10
12
  # GET /logs/:id
@@ -14,7 +16,15 @@ module LesliAudit
14
16
  private
15
17
 
16
18
  def set_log
17
- @log = current_user.logs.find(params[:id])
19
+ @log_kind = log_service.kind
20
+ @log = log_service.find(params[:id])
21
+ end
22
+
23
+ def log_service
24
+ @log_service ||= LesliAudit::LogService.new(
25
+ current_user,
26
+ query.merge(kind: params[:kind])
27
+ )
18
28
  end
19
29
  end
20
30
  end
@@ -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
@@ -39,6 +39,7 @@ module LesliAudit
39
39
  def log_requests
40
40
  return unless Lesli.config.audit.dig(:enable_analytics)
41
41
  return unless current_user
42
+
42
43
  log_account_requests
43
44
  log_user_requests
44
45
  log_user_journal
@@ -128,14 +129,14 @@ module LesliAudit
128
129
 
129
130
  def log_user_journal
130
131
  return unless Lesli.config.audit.dig(:enable_journals)
131
- current_user.account.audit.user_journals.create({
132
+ current_user.account.audit.user_journals.create!(
133
+ event_uid: request.request_id,
132
134
  request_controller: controller_path,
133
135
  request_action: action_name,
134
136
  session_id: session[:user_session_id],
135
137
  user_id: current_user.id,
136
- date: Date2.new.date.to_s,
137
- processed_at: Time.current
138
- })
138
+ date: Date2.new.date.to_s
139
+ )
139
140
  end
140
141
 
141
142
  # Track specific account activity
@@ -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
@@ -31,5 +31,17 @@ module LesliAudit
31
31
  has_many :user_requests,
32
32
  inverse_of: :account,
33
33
  dependent: :delete_all
34
+
35
+ has_many :traffic_rollups,
36
+ inverse_of: :account,
37
+ dependent: :delete_all
38
+
39
+ has_many :traffic_breakdowns,
40
+ inverse_of: :account,
41
+ dependent: :delete_all
42
+
43
+ has_many :cloudflare_traffic_syncs,
44
+ inverse_of: :account,
45
+ dependent: :delete_all
34
46
  end
35
47
  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