Files
redux-scraper/app/controllers/telegram_bot_logs_controller.rb
2025-08-05 05:05:21 +00:00

109 lines
2.9 KiB
Ruby

# typed: false
class TelegramBotLogsController < ApplicationController
before_action :set_telegram_bot_log, only: %i[show]
after_action :verify_authorized
def index
authorize TelegramBotLog
# Start with policy scope
@telegram_bot_logs = policy_scope(TelegramBotLog)
# Apply filters
@telegram_bot_logs = apply_filters(@telegram_bot_logs)
# Order by most recent first
@telegram_bot_logs = @telegram_bot_logs.recent
# Paginate with Kaminari
@limit = (params[:limit] || 50).to_i.clamp(1, 500)
@telegram_bot_logs = @telegram_bot_logs.page(params[:page]).per(@limit)
# Load associations for display
@telegram_bot_logs = @telegram_bot_logs.includes(:processed_image)
# Set up filter options for the view
@status_options = TelegramBotLog.statuses.keys
@filter_params =
params.slice(
:telegram_user_id,
:status,
:start_date,
:end_date,
:min_results,
:max_results,
:slow_requests,
)
end
def show
authorize @telegram_bot_log
# The processed_image association will be loaded automatically when accessed
end
private
def set_telegram_bot_log
@telegram_bot_log =
TelegramBotLog.includes(:processed_image).find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to telegram_bot_logs_path, alert: "Telegram bot log not found."
end
def apply_filters(scope)
# Filter by telegram user ID
if params[:telegram_user_id].present?
scope = scope.for_user(params[:telegram_user_id].to_i)
end
# Filter by status
if params[:status].present? && TelegramBotLog.statuses.key?(params[:status])
scope = scope.where(status: params[:status])
end
# Filter by date range
if params[:start_date].present?
begin
start_date = Date.parse(params[:start_date])
scope =
scope.where("request_timestamp >= ?", start_date.beginning_of_day)
rescue Date::Error
# Ignore invalid date
end
end
if params[:end_date].present?
begin
end_date = Date.parse(params[:end_date])
scope = scope.where("request_timestamp <= ?", end_date.end_of_day)
rescue Date::Error
# Ignore invalid date
end
end
# Filter by search results count
if params[:min_results].present?
scope =
scope.where("search_results_count >= ?", params[:min_results].to_i)
end
if params[:max_results].present?
scope =
scope.where("search_results_count <= ?", params[:max_results].to_i)
end
# Filter by performance metrics
if params[:slow_requests].present? && params[:slow_requests] == "true"
# Consider requests with total processing time > 1 second as slow
scope =
scope.where(
"(fingerprint_computation_time + search_computation_time) > ? OR fingerprint_computation_time IS NULL OR search_computation_time IS NULL",
1.0,
)
end
scope
end
end