Fix some blocked/allow laggards after migrating. Add DuckDB for outstanding analyitcs performance. Start adding an import for all bot networks
This commit is contained in:
126
app/controllers/bot_network_ranges_controller.rb
Normal file
126
app/controllers/bot_network_ranges_controller.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BotNetworkRangesController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
before_action :require_admin
|
||||
|
||||
def index
|
||||
@bot_sources = BotNetworkRangeImporter::BOT_SOURCES
|
||||
@recent_imports = DataImport.where(import_type: 'bot_network_ranges').order(created_at: :desc).limit(10)
|
||||
@bot_network_ranges = NetworkRange.where("source LIKE 'bot_import_%'").order(created_at: :desc).limit(50)
|
||||
end
|
||||
|
||||
def import
|
||||
source_key = params[:source]
|
||||
options = import_options
|
||||
|
||||
if source_key.present?
|
||||
# Perform import synchronously for immediate feedback
|
||||
begin
|
||||
result = BotNetworkRangeImporter.import_from_source(source_key, options)
|
||||
|
||||
# Create a data import record
|
||||
DataImport.create!(
|
||||
import_type: 'bot_network_ranges',
|
||||
source: source_key.to_s,
|
||||
status: 'completed',
|
||||
records_processed: result[:imported],
|
||||
notes: "Imported from #{result[:source]}: #{result[:note] || 'Success'}"
|
||||
)
|
||||
|
||||
flash[:notice] = "Successfully imported #{result[:imported]} ranges from #{result[:source]}"
|
||||
rescue => e
|
||||
flash[:alert] = "Failed to import from #{source_key}: #{e.message}"
|
||||
end
|
||||
else
|
||||
flash[:alert] = "Please select a source to import from"
|
||||
end
|
||||
|
||||
redirect_to bot_network_ranges_path
|
||||
end
|
||||
|
||||
def import_async
|
||||
source_key = params[:source]
|
||||
options = import_options
|
||||
|
||||
if source_key.present?
|
||||
# Create a data import record for tracking
|
||||
data_import = DataImport.create!(
|
||||
import_type: 'bot_network_ranges',
|
||||
source: source_key.to_s,
|
||||
status: 'pending',
|
||||
records_processed: 0,
|
||||
notes: "Import job queued for #{source_key}"
|
||||
)
|
||||
|
||||
# Queue the background job
|
||||
ImportBotNetworkRangesJob.perform_later(source_key, options.merge(data_import_id: data_import.id))
|
||||
|
||||
flash[:notice] = "Import job queued for #{source_key}. You'll be notified when it's complete."
|
||||
else
|
||||
flash[:alert] = "Please select a source to import from"
|
||||
end
|
||||
|
||||
redirect_to bot_network_ranges_path
|
||||
end
|
||||
|
||||
def import_all
|
||||
options = import_options
|
||||
|
||||
# Create a data import record for batch import
|
||||
data_import = DataImport.create!(
|
||||
import_type: 'bot_network_ranges',
|
||||
source: 'all_sources',
|
||||
status: 'pending',
|
||||
records_processed: 0,
|
||||
notes: "Batch import job queued for all available sources"
|
||||
)
|
||||
|
||||
# Queue the batch import job
|
||||
ImportAllBotNetworkRangesJob.perform_later(options.merge(data_import_id: data_import.id))
|
||||
|
||||
flash[:notice] = "Batch import job queued for all sources. This may take several minutes."
|
||||
redirect_to bot_network_ranges_path
|
||||
end
|
||||
|
||||
def show
|
||||
@network_ranges = NetworkRange.where("source LIKE 'bot_import_#{params[:source]}%'")
|
||||
.order(created_at: :desc)
|
||||
.page(params[:page])
|
||||
.per(50)
|
||||
|
||||
@source_name = BotNetworkRangeImporter::BOT_SOURCES[params[:source].to_sym]&.dig(:name) || params[:source]
|
||||
@import_stats = NetworkRange.where("source LIKE 'bot_import_#{params[:source]}%'")
|
||||
.group(:source)
|
||||
.count
|
||||
end
|
||||
|
||||
def destroy
|
||||
source = params[:source]
|
||||
deleted_count = NetworkRange.where("source LIKE 'bot_import_#{source}%'").delete_all
|
||||
|
||||
flash[:notice] = "Deleted #{deleted_count} network ranges from #{source}"
|
||||
redirect_to bot_network_ranges_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def require_admin
|
||||
redirect_to root_path, alert: 'Admin access required' unless current_user&.admin?
|
||||
end
|
||||
|
||||
def import_options
|
||||
options = {}
|
||||
|
||||
# AWS-specific options
|
||||
if params[:aws_services].present?
|
||||
options[:aws_services] = params[:aws_services].split(',').map(&:strip)
|
||||
end
|
||||
|
||||
# Batch size control
|
||||
options[:batch_size] = params[:batch_size].to_i if params[:batch_size].present?
|
||||
options[:batch_size] = 1000 if options[:batch_size].zero?
|
||||
|
||||
options
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user