Class: Tasker::TaskTransition
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Tasker::TaskTransition
- Defined in:
- app/models/tasker/task_transition.rb
Overview
TaskTransition represents state transitions for Task entities using Statesman
This model stores the audit trail of all task state changes, providing a complete history of task lifecycle events with metadata and timestamps.
Class Method Summary collapse
-
.in_time_range(start_time, end_time) ⇒ ActiveRecord::Relation
Find all transitions that occurred within a time range.
-
.most_recent_to_state(state) ⇒ TaskTransition?
Find the most recent transition to a specific state.
-
.statistics ⇒ Hash
Get transition statistics for analytics.
-
.with_metadata_value(key, value) ⇒ ActiveRecord::Relation
Find transitions with specific metadata values.
Instance Method Summary collapse
-
#cancellation_transition? ⇒ Boolean
Check if this transition represents cancellation.
-
#completion_transition? ⇒ Boolean
Check if this transition represents completion.
-
#description ⇒ String
Get human-readable description of the transition.
-
#duration_since_previous ⇒ Float?
Get the duration since the previous transition.
-
#error_transition? ⇒ Boolean
Check if this transition represents an error state.
-
#formatted_metadata ⇒ Hash
Get formatted metadata for display.
-
#get_metadata(key, default = nil) ⇒ Object
Get metadata value with default.
-
#has_metadata?(key) ⇒ Boolean
Check if transition has specific metadata.
-
#set_metadata(key, value) ⇒ Object
Set metadata value.
Methods inherited from ApplicationRecord
configure_database_connections, database_configuration_exists?
Class Method Details
.in_time_range(start_time, end_time) ⇒ ActiveRecord::Relation
Find all transitions that occurred within a time range
53 54 55 |
# File 'app/models/tasker/task_transition.rb', line 53 def in_time_range(start_time, end_time) where(created_at: start_time..end_time) end |
.most_recent_to_state(state) ⇒ TaskTransition?
Find the most recent transition to a specific state
44 45 46 |
# File 'app/models/tasker/task_transition.rb', line 44 def most_recent_to_state(state) to_state(state.to_s).order(sort_key: :desc).first end |
.statistics ⇒ Hash
Get transition statistics for analytics
69 70 71 72 73 74 75 76 |
# File 'app/models/tasker/task_transition.rb', line 69 def statistics { total_transitions: count, states: group(:to_state).count, recent_activity: where(created_at: 24.hours.ago..Time.current).count, average_time_between_transitions: average_time_between_transitions } end |
.with_metadata_value(key, value) ⇒ ActiveRecord::Relation
Find transitions with specific metadata values
62 63 64 |
# File 'app/models/tasker/task_transition.rb', line 62 def (key, value) where('metadata->:key = :value', key: key.to_s, value: value.to_json.delete('"')) end |
Instance Method Details
#cancellation_transition? ⇒ Boolean
Check if this transition represents cancellation
129 130 131 |
# File 'app/models/tasker/task_transition.rb', line 129 def cancellation_transition? to_state == 'cancelled' end |
#completion_transition? ⇒ Boolean
Check if this transition represents completion
122 123 124 |
# File 'app/models/tasker/task_transition.rb', line 122 def completion_transition? %w[complete resolved_manually].include?(to_state) end |
#description ⇒ String
Get human-readable description of the transition
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/tasker/task_transition.rb', line 136 def description case to_state when 'pending' 'Task initialized and ready for processing' when 'in_progress' 'Task execution started' when 'complete' 'Task completed successfully' when 'error' 'Task encountered an error' when 'cancelled' 'Task was cancelled' when 'resolved_manually' 'Task was manually resolved' else "Task transitioned to #{to_state}" end end |
#duration_since_previous ⇒ Float?
Get the duration since the previous transition
101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/tasker/task_transition.rb', line 101 def duration_since_previous previous_transition = self.class.where(task_id: task_id) .where(sort_key: ...sort_key) .order(sort_key: :desc) .first return nil unless previous_transition created_at - previous_transition.created_at end |
#error_transition? ⇒ Boolean
Check if this transition represents an error state
115 116 117 |
# File 'app/models/tasker/task_transition.rb', line 115 def error_transition? to_state == 'error' end |
#formatted_metadata ⇒ Hash
Get formatted metadata for display
158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/tasker/task_transition.rb', line 158 def = .dup # Add computed fields ['duration_since_previous'] = duration_since_previous ['transition_description'] = description ['transition_timestamp'] = created_at.iso8601 end |
#get_metadata(key, default = nil) ⇒ Object
Get metadata value with default
182 183 184 |
# File 'app/models/tasker/task_transition.rb', line 182 def (key, default = nil) .fetch(key.to_s, default) end |
#has_metadata?(key) ⇒ Boolean
Check if transition has specific metadata
173 174 175 |
# File 'app/models/tasker/task_transition.rb', line 173 def (key) .key?(key.to_s) end |
#set_metadata(key, value) ⇒ Object
Set metadata value
191 192 193 194 |
# File 'app/models/tasker/task_transition.rb', line 191 def (key, value) self. = .merge(key.to_s => value) value end |