Class: TheMechanic2::BenchmarksController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- TheMechanic2::BenchmarksController
- Defined in:
- lib/the_mechanic_2/benchmarks_controller.rb
Overview
Main controller for benchmark operations Handles UI rendering and API endpoints
Instance Method Summary collapse
-
#export ⇒ Object
POST /ask_the_mechanic_2/export Exports results in specified format.
-
#index ⇒ Object
GET /ask_the_mechanic Renders the main benchmarking UI.
-
#run ⇒ Object
POST /ask_the_mechanic_2/run Executes benchmark comparison.
-
#validate ⇒ Object
POST /ask_the_mechanic_2/validate Validates code without executing.
Instance Method Details
#export ⇒ Object
POST /ask_the_mechanic_2/export Exports results in specified format
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/the_mechanic_2/benchmarks_controller.rb', line 97 def export results_data = params[:results] format = params[:format] || 'json' unless results_data render json: { error: 'Results data is required' }, status: :unprocessable_entity return end begin result = BenchmarkResult.new(results_data.permit!.to_h.symbolize_keys) case format when 'json' render json: result.to_json, content_type: 'application/json' when 'markdown' render plain: result.to_markdown, content_type: 'text/markdown' else render json: { error: 'Invalid format. Use json or markdown' }, status: :unprocessable_entity end rescue StandardError => e render json: { error: 'Export failed', message: e. }, status: :internal_server_error end end |
#index ⇒ Object
GET /ask_the_mechanic Renders the main benchmarking UI
11 12 13 |
# File 'lib/the_mechanic_2/benchmarks_controller.rb', line 11 def index render template: 'the_mechanic_2/benchmarks/index' end |
#run ⇒ Object
POST /ask_the_mechanic_2/run Executes benchmark comparison
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/the_mechanic_2/benchmarks_controller.rb', line 54 def run # Validate request request = BenchmarkRequest.new( shared_setup: params[:shared_setup], code_a: params[:code_a], code_b: params[:code_b], timeout: params[:timeout]&.to_i ) unless request.valid? render json: { error: 'Invalid request', errors: request.all_errors }, status: :unprocessable_entity return end # Validate code security validation_result = validate_code_security(request) unless validation_result[:valid] render json: { error: 'Security validation failed', errors: validation_result[:errors] }, status: :unprocessable_entity return end # Execute benchmark begin service = BenchmarkService.new results = service.run( shared_setup: request.shared_setup, code_a: request.code_a, code_b: request.code_b, timeout: request.timeout ) render json: results rescue RailsRunnerService::BenchmarkTimeout => e render json: { error: 'Benchmark timeout', message: e. }, status: :request_timeout rescue RailsRunnerService::BenchmarkError => e render json: { error: 'Benchmark execution failed', message: e. }, status: :internal_server_error rescue StandardError => e render json: { error: 'Unexpected error', message: e. }, status: :internal_server_error end end |
#validate ⇒ Object
POST /ask_the_mechanic_2/validate Validates code without executing
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/the_mechanic_2/benchmarks_controller.rb', line 17 def validate code_a = params[:code_a] code_b = params[:code_b] errors = [] # Validate code_a if code_a.present? validation_a = SecurityService.validate(code_a) errors.concat(validation_a[:errors].map { |e| "Code A: #{e}" }) unless validation_a[:valid] else errors << 'Code A: Code is required' end # Validate code_b if code_b.present? validation_b = SecurityService.validate(code_b) errors.concat(validation_b[:errors].map { |e| "Code B: #{e}" }) unless validation_b[:valid] else errors << 'Code B: Code is required' end # Validate shared_setup if provided if params[:shared_setup].present? validation_setup = SecurityService.validate(params[:shared_setup]) errors.concat(validation_setup[:errors].map { |e| "Shared Setup: #{e}" }) unless validation_setup[:valid] end if errors.empty? render json: { valid: true, message: 'All code is valid' } else render json: { valid: false, errors: errors }, status: :unprocessable_entity end end |