From 5d3e35a4ac03ec4bcea2dc883d818983d7c341ba Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Mon, 17 Nov 2025 16:17:59 +1100 Subject: [PATCH] Add a setting for maximum age of events --- app/models/setting.rb | 5 ++++ app/views/sessions/new.html.erb | 2 +- app/views/settings/index.html.erb | 34 ++++++++++++++++++++++--- config/recurring.yml | 12 ++++----- test/jobs/process_waf_event_job_test.rb | 6 ----- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index 35b95b9..ea3266f 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -15,4 +15,9 @@ class Setting < ApplicationRecord def self.ipapi_key get('ipapi_key', ENV['IPAPI_KEY']) end + + # Convenience method for event retention days (default: 90 days) + def self.event_retention_days + get('event_retention_days', '90').to_i + end end diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 9a7b55d..1325b4e 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -36,7 +36,7 @@ <% end %> - <%= form_with url: session_url, class: "contents" do |form| %> + <%= form_with url: session_url, class: "contents", data: { turbo: false } do |form| %>
<%= form.email_field :email_address, required: true, autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email_address], class: "block shadow-sm rounded-md border border-gray-400 focus:outline-blue-600 px-3 py-2 mt-2 w-full" %>
diff --git a/app/views/settings/index.html.erb b/app/views/settings/index.html.erb index 9601fec..8c15ccf 100644 --- a/app/views/settings/index.html.erb +++ b/app/views/settings/index.html.erb @@ -50,11 +50,37 @@ - -
+ +
-

Additional Settings

-

More configuration options will be added here as needed.

+

Data Retention

+ +
+ <%= form_with url: settings_path, method: :patch, class: "space-y-4" do |f| %> + <%= hidden_field_tag :key, 'event_retention_days' %> + +
+ +
+ <%= number_field_tag :value, + @settings['event_retention_days']&.value || 90, + class: "flex-1 min-w-0 block w-full px-3 py-2 rounded-md border-gray-300 focus:ring-blue-500 focus:border-blue-500 sm:text-sm", + placeholder: "90", + min: 0 %> + <%= f.submit "Update", class: "ml-3 inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" %> +
+

+ Events older than this many days will be automatically deleted by the cleanup job (runs hourly). + Set to 0 to disable automatic cleanup. Default: 90 days. +

+

+ Current setting: <%= Setting.event_retention_days %> days +

+
+ <% end %> +
diff --git a/config/recurring.yml b/config/recurring.yml index ac05346..ca8d105 100644 --- a/config/recurring.yml +++ b/config/recurring.yml @@ -12,12 +12,6 @@ # No recurring tasks configured yet # (previously had clear_solid_queue_finished_jobs, but now preserve_finished_jobs: false in queue.yml) -# Backfill network intelligence for recent events (catches events before network data imported) -backfill_recent_network_intelligence: - class: BackfillRecentNetworkIntelligenceJob - queue: default - schedule: every 5 minutes - # Clean up failed jobs older than 1 day cleanup_failed_jobs: command: "SolidQueue::FailedExecution.where('created_at < ?', 1.day.ago).delete_all" @@ -29,3 +23,9 @@ expired_rules_cleanup: class: ExpiredRulesCleanupJob queue: default schedule: every hour + +# Clean up old events based on retention setting +cleanup_old_events: + class: CleanupOldEventsJob + queue: background + schedule: every hour diff --git a/test/jobs/process_waf_event_job_test.rb b/test/jobs/process_waf_event_job_test.rb index d52bbd3..48694d5 100644 --- a/test/jobs/process_waf_event_job_test.rb +++ b/test/jobs/process_waf_event_job_test.rb @@ -354,10 +354,4 @@ class ProcessWafEventJobTest < ActiveJob::TestCase assert_equal 100, Event.count assert processing_time < 5.seconds, "Processing 100 events should take less than 5 seconds" end - - # Integration with Other Jobs - test "coordinates with BackfillRecentNetworkIntelligenceJob" do - # This would be tested based on how the job enqueues other jobs - # Implementation depends on your specific job coordination logic - end end \ No newline at end of file