Class: Tasker::TaskTransition

Inherits:
ApplicationRecord show all
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

Instance Method Summary collapse

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

Parameters:

  • start_time (Time)

    The start of the time range

  • end_time (Time)

    The end of the time range

Returns:

  • (ActiveRecord::Relation)

    Transitions within the 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

Parameters:

  • state (String, Symbol)

    The state to find the most recent transition to

Returns:

  • (TaskTransition, nil)

    The most recent transition to the given 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

.statisticsHash

Get transition statistics for analytics

Returns:

  • (Hash)

    Statistics about transitions



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

Parameters:

  • key (String, Symbol)

    The metadata key to search for

  • value (Object)

    The value to match

Returns:

  • (ActiveRecord::Relation)

    Transitions with matching metadata



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

Returns:

  • (Boolean)

    True if transitioning to cancelled state



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

Returns:

  • (Boolean)

    True if transitioning to a completion state



122
123
124
# File 'app/models/tasker/task_transition.rb', line 122

def completion_transition?
  %w[complete resolved_manually].include?(to_state)
end

#descriptionString

Get human-readable description of the transition

Returns:

  • (String)

    Description of what this transition represents



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_previousFloat?

Get the duration since the previous transition

Returns:

  • (Float, nil)

    Duration in seconds since 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

Returns:

  • (Boolean)

    True if transitioning to an error state



115
116
117
# File 'app/models/tasker/task_transition.rb', line 115

def error_transition?
  to_state == 'error'
end

#formatted_metadataHash

Get formatted metadata for display

Returns:

  • (Hash)

    Formatted metadata with additional computed fields



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

Parameters:

  • key (String, Symbol)

    The metadata key

  • default (Object) (defaults to: nil)

    Default value if key not found

Returns:

  • (Object)

    The metadata value or 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

Parameters:

  • key (String, Symbol)

    The metadata key to check for

Returns:

  • (Boolean)

    True if the metadata contains the key



173
174
175
# File 'app/models/tasker/task_transition.rb', line 173

def has_metadata?(key)
  .key?(key.to_s)
end

#set_metadata(key, value) ⇒ Object

Set metadata value

Parameters:

  • key (String, Symbol)

    The metadata key

  • value (Object)

    The value to set

Returns:

  • (Object)

    The set 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