Class: Leva::OptimizationRun
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Leva::OptimizationRun
- Defined in:
- app/models/leva/optimization_run.rb
Overview
Tracks the progress and status of prompt optimization runs.
Constant Summary collapse
- STEPS =
Defined optimization steps for display
{ "validating" => { label: "Validating dataset", icon: "check" }, "splitting_data" => { label: "Splitting data", icon: "scissors" }, "generating_signature" => { label: "Generating signature", icon: "code" }, "bootstrapping" => { label: "Bootstrapping examples", icon: "zap" }, "evaluating" => { label: "Evaluating results", icon: "bar-chart" }, "building_result" => { label: "Building prompt", icon: "package" }, "complete" => { label: "Complete", icon: "check-circle" } }.freeze
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Returns a hash for JSON API response.
-
#complete!(created_prompt) ⇒ void
Marks the run as completed with the created prompt.
-
#current_step_label ⇒ String
Returns the human-readable label for the current step.
-
#elapsed_time ⇒ ActiveSupport::Duration?
Returns elapsed time since the run started.
-
#elapsed_time_formatted ⇒ String
Formats elapsed time for display.
-
#fail!(error) ⇒ void
Marks the run as failed.
-
#start! ⇒ void
Marks the run as started.
-
#update_progress(step:, progress:, examples_processed: nil, total: nil) ⇒ void
Updates the progress of the optimization run.
Instance Method Details
#as_json(options = {}) ⇒ Hash
Returns a hash for JSON API response.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/leva/optimization_run.rb', line 121 def as_json( = {}) { id: id, status: status, current_step: current_step, current_step_label: current_step_label, progress: progress, examples_processed: examples_processed, total_examples: total_examples, elapsed_time: elapsed_time_formatted, error_message: , prompt_id: prompt_id, prompt_name: prompt_name } end |
#complete!(created_prompt) ⇒ void
This method returns an undefined value.
Marks the run as completed with the created prompt.
70 71 72 73 74 75 76 77 |
# File 'app/models/leva/optimization_run.rb', line 70 def complete!(created_prompt) update!( status: :completed, prompt: created_prompt, current_step: "complete", progress: 100 ) end |
#current_step_label ⇒ String
Returns the human-readable label for the current step.
91 92 93 |
# File 'app/models/leva/optimization_run.rb', line 91 def current_step_label STEPS.dig(current_step, :label) || current_step&.humanize || "Initializing" end |
#elapsed_time ⇒ ActiveSupport::Duration?
Returns elapsed time since the run started.
98 99 100 101 102 |
# File 'app/models/leva/optimization_run.rb', line 98 def elapsed_time return nil unless running? || completed? || failed? (completed? || failed? ? updated_at : Time.current) - created_at end |
#elapsed_time_formatted ⇒ String
Formats elapsed time for display.
107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/leva/optimization_run.rb', line 107 def elapsed_time_formatted seconds = elapsed_time&.to_i || 0 if seconds < 60 "#{seconds}s" elsif seconds < 3600 "#{seconds / 60}m #{seconds % 60}s" else "#{seconds / 3600}h #{(seconds % 3600) / 60}m" end end |
#fail!(error) ⇒ void
This method returns an undefined value.
Marks the run as failed.
83 84 85 86 |
# File 'app/models/leva/optimization_run.rb', line 83 def fail!(error) = error.is_a?(Exception) ? "#{error.class}: #{error.message}" : error.to_s update!(status: :failed, error_message: ) end |
#start! ⇒ void
This method returns an undefined value.
Marks the run as started.
48 49 50 |
# File 'app/models/leva/optimization_run.rb', line 48 def start! update!(status: :running, current_step: "validating", progress: 0) end |
#update_progress(step:, progress:, examples_processed: nil, total: nil) ⇒ void
This method returns an undefined value.
Updates the progress of the optimization run.
59 60 61 62 63 64 |
# File 'app/models/leva/optimization_run.rb', line 59 def update_progress(step:, progress:, examples_processed: nil, total: nil) attrs = { current_step: step, progress: progress } attrs[:examples_processed] = examples_processed if examples_processed attrs[:total_examples] = total if total update!(attrs) end |