99 lines
2.9 KiB
Ruby
99 lines
2.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ProjectsController < ApplicationController
|
|
before_action :set_project, only: [:show, :edit, :update, :events, :analytics]
|
|
|
|
def index
|
|
@projects = Project.order(created_at: :desc)
|
|
end
|
|
|
|
def show
|
|
@recent_events = @project.recent_events(limit: 10)
|
|
@event_count = @project.event_count(24.hours.ago)
|
|
@blocked_count = @project.blocked_count(24.hours.ago)
|
|
@waf_status = @project.waf_status
|
|
end
|
|
|
|
def new
|
|
@project = Project.new
|
|
end
|
|
|
|
def create
|
|
@project = Project.new(project_params)
|
|
|
|
if @project.save
|
|
redirect_to @project, notice: "Project was successfully created. Use this DSN for your baffle-agent: #{@project.dsn}"
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
if @project.update(project_params)
|
|
redirect_to @project, notice: "Project was successfully updated."
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def events
|
|
@events = @project.events.recent.includes(:project)
|
|
|
|
# Apply filters
|
|
@events = @events.by_ip(params[:ip]) if params[:ip].present?
|
|
@events = @events.by_waf_action(params[:action]) if params[:action].present?
|
|
@events = @events.where(country_code: params[:country]) if params[:country].present?
|
|
|
|
# Debug info
|
|
Rails.logger.debug "Events count before pagination: #{@events.count}"
|
|
Rails.logger.debug "Project: #{@project&.name} (ID: #{@project&.id})"
|
|
|
|
# Paginate
|
|
@pagy, @events = pagy(@events, items: 50)
|
|
|
|
Rails.logger.debug "Events count after pagination: #{@events.count}"
|
|
Rails.logger.debug "Pagy info: #{@pagy.count} total, #{@pagy.pages} pages"
|
|
end
|
|
|
|
def analytics
|
|
@time_range = params[:time_range]&.to_i || 24 # hours
|
|
|
|
# Basic analytics
|
|
@total_events = @project.event_count(@time_range.hours.ago)
|
|
@blocked_events = @project.blocked_count(@time_range.hours.ago)
|
|
@allowed_events = @project.allowed_count(@time_range.hours.ago)
|
|
|
|
# Top blocked IPs
|
|
@top_blocked_ips = @project.top_blocked_ips(limit: 10, time_range: @time_range.hours.ago)
|
|
|
|
# Country distribution
|
|
@country_stats = @project.events
|
|
.where(timestamp: @time_range.hours.ago..Time.current)
|
|
.where.not(country_code: nil)
|
|
.group(:country_code)
|
|
.select('country_code, COUNT(*) as count')
|
|
.order('count DESC')
|
|
.limit(10)
|
|
|
|
# Action distribution
|
|
@action_stats = @project.events
|
|
.where(timestamp: @time_range.hours.ago..Time.current)
|
|
.group(:waf_action)
|
|
.select('waf_action as action, COUNT(*) as count')
|
|
.order('count DESC')
|
|
end
|
|
|
|
private
|
|
|
|
def set_project
|
|
@project = Project.find_by(slug: params[:id]) || Project.find_by(id: params[:id])
|
|
redirect_to projects_path, alert: "Project not found" unless @project
|
|
end
|
|
|
|
def project_params
|
|
params.require(:project).permit(:name, :enabled, settings: {})
|
|
end
|
|
end |