Add a setting for maximum age of events

This commit is contained in:
Dan Milne
2025-11-17 16:17:59 +11:00
parent 830810305b
commit 5d3e35a4ac
5 changed files with 42 additions and 17 deletions

View File

@@ -15,4 +15,9 @@ class Setting < ApplicationRecord
def self.ipapi_key def self.ipapi_key
get('ipapi_key', ENV['IPAPI_KEY']) get('ipapi_key', ENV['IPAPI_KEY'])
end end
# Convenience method for event retention days (default: 90 days)
def self.event_retention_days
get('event_retention_days', '90').to_i
end
end end

View File

@@ -36,7 +36,7 @@
</div> </div>
<% end %> <% end %>
<%= form_with url: session_url, class: "contents" do |form| %> <%= form_with url: session_url, class: "contents", data: { turbo: false } do |form| %>
<div class="my-5"> <div class="my-5">
<%= 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" %> <%= 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" %>
</div> </div>

View File

@@ -50,11 +50,37 @@
</div> </div>
</div> </div>
<!-- Future Settings Section --> <!-- Data Retention Settings -->
<div class="mt-6 bg-gray-50 shadow sm:rounded-lg"> <div class="mt-6 bg-white shadow sm:rounded-lg">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">
<h3 class="text-lg font-medium leading-6 text-gray-900 mb-2">Additional Settings</h3> <h3 class="text-lg font-medium leading-6 text-gray-900 mb-4">Data Retention</h3>
<p class="text-sm text-gray-500">More configuration options will be added here as needed.</p>
<div class="mb-6">
<%= form_with url: settings_path, method: :patch, class: "space-y-4" do |f| %>
<%= hidden_field_tag :key, 'event_retention_days' %>
<div>
<label for="event_retention_days" class="block text-sm font-medium text-gray-700">
Event Retention Period (days)
</label>
<div class="mt-1 flex rounded-md shadow-sm">
<%= 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" %>
</div>
<p class="mt-2 text-sm text-gray-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.
</p>
<p class="mt-1 text-xs text-gray-400">
Current setting: <strong><%= Setting.event_retention_days %> days</strong>
</p>
</div>
<% end %>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -12,12 +12,6 @@
# No recurring tasks configured yet # No recurring tasks configured yet
# (previously had clear_solid_queue_finished_jobs, but now preserve_finished_jobs: false in queue.yml) # (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 # Clean up failed jobs older than 1 day
cleanup_failed_jobs: cleanup_failed_jobs:
command: "SolidQueue::FailedExecution.where('created_at < ?', 1.day.ago).delete_all" command: "SolidQueue::FailedExecution.where('created_at < ?', 1.day.ago).delete_all"
@@ -29,3 +23,9 @@ expired_rules_cleanup:
class: ExpiredRulesCleanupJob class: ExpiredRulesCleanupJob
queue: default queue: default
schedule: every hour schedule: every hour
# Clean up old events based on retention setting
cleanup_old_events:
class: CleanupOldEventsJob
queue: background
schedule: every hour

View File

@@ -354,10 +354,4 @@ class ProcessWafEventJobTest < ActiveJob::TestCase
assert_equal 100, Event.count assert_equal 100, Event.count
assert processing_time < 5.seconds, "Processing 100 events should take less than 5 seconds" assert processing_time < 5.seconds, "Processing 100 events should take less than 5 seconds"
end 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 end