Class: Aidp::Database::Repositories::ProgressRepository

Inherits:
Aidp::Database::Repository show all
Defined in:
lib/aidp/database/repositories/progress_repository.rb

Overview

Repository for progress table Replaces progress/execute.yml and progress/analyze.yml

Constant Summary collapse

VALID_MODES =
%w[execute analyze].freeze

Instance Attribute Summary

Attributes inherited from Aidp::Database::Repository

#project_dir, #table_name

Instance Method Summary collapse

Constructor Details

#initialize(project_dir: Dir.pwd) ⇒ ProgressRepository

Returns a new instance of ProgressRepository.



13
14
15
# File 'lib/aidp/database/repositories/progress_repository.rb', line 13

def initialize(project_dir: Dir.pwd)
  super(project_dir: project_dir, table_name: "progress")
end

Instance Method Details

#completed_steps(mode) ⇒ Array<String>

Get completed steps

Parameters:

  • mode (String, Symbol)

    Mode

Returns:

  • (Array<String>)

    Completed step names



37
38
39
40
# File 'lib/aidp/database/repositories/progress_repository.rb', line 37

def completed_steps(mode)
  progress = get(mode)
  progress[:steps_completed] || []
end

#current_step(mode) ⇒ String?

Get current step

Parameters:

  • mode (String, Symbol)

    Mode

Returns:

  • (String, nil)

    Current step name



46
47
48
49
# File 'lib/aidp/database/repositories/progress_repository.rb', line 46

def current_step(mode)
  progress = get(mode)
  progress[:current_step]
end

#get(mode) ⇒ Hash

Get progress for a mode

Parameters:

  • mode (String, Symbol)

    Mode (execute or analyze)

Returns:

  • (Hash)

    Progress data



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aidp/database/repositories/progress_repository.rb', line 21

def get(mode)
  mode_str = mode.to_s
  row = query_one(
    "SELECT * FROM progress WHERE project_dir = ? AND mode = ?",
    [project_dir, mode_str]
  )

  return empty_progress(mode_str) unless row

  deserialize_progress(row)
end

#mark_step_completed(mode, step_name) ⇒ Object

Mark step as completed

Parameters:

  • mode (String, Symbol)

    Mode

  • step_name (String)

    Step name



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aidp/database/repositories/progress_repository.rb', line 64

def mark_step_completed(mode, step_name)
  mode_str = mode.to_s
  progress = get(mode)

  steps = progress[:steps_completed] || []
  steps << step_name unless steps.include?(step_name)

  now = current_timestamp
  started_at = progress[:started_at] || now

  upsert_progress(
    mode: mode_str,
    current_step: nil,
    steps_completed: steps,
    started_at: started_at,
    updated_at: now
  )

  Aidp.log_debug("progress_repository", "step_completed",
    mode: mode_str, step: step_name)
end

#mark_step_in_progress(mode, step_name) ⇒ Object

Mark step as in progress

Parameters:

  • mode (String, Symbol)

    Mode

  • step_name (String)

    Step name



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aidp/database/repositories/progress_repository.rb', line 90

def mark_step_in_progress(mode, step_name)
  mode_str = mode.to_s
  progress = get(mode)

  now = current_timestamp
  started_at = progress[:started_at] || now

  upsert_progress(
    mode: mode_str,
    current_step: step_name,
    steps_completed: progress[:steps_completed] || [],
    started_at: started_at,
    updated_at: now
  )

  Aidp.log_debug("progress_repository", "step_in_progress",
    mode: mode_str, step: step_name)
end

#reset(mode) ⇒ Object

Reset progress for a mode

Parameters:

  • mode (String, Symbol)

    Mode



112
113
114
115
116
117
118
119
120
# File 'lib/aidp/database/repositories/progress_repository.rb', line 112

def reset(mode)
  mode_str = mode.to_s
  execute(
    "DELETE FROM progress WHERE project_dir = ? AND mode = ?",
    [project_dir, mode_str]
  )

  Aidp.log_debug("progress_repository", "reset", mode: mode_str)
end

#started_at(mode) ⇒ Time?

Get started_at timestamp

Parameters:

  • mode (String, Symbol)

    Mode

Returns:

  • (Time, nil)

    Started at time



126
127
128
129
130
131
132
133
# File 'lib/aidp/database/repositories/progress_repository.rb', line 126

def started_at(mode)
  progress = get(mode)
  return nil unless progress[:started_at]

  Time.parse(progress[:started_at])
rescue ArgumentError
  nil
end

#step_completed?(mode, step_name) ⇒ Boolean

Check if step is completed

Parameters:

  • mode (String, Symbol)

    Mode

  • step_name (String)

    Step name

Returns:

  • (Boolean)


56
57
58
# File 'lib/aidp/database/repositories/progress_repository.rb', line 56

def step_completed?(mode, step_name)
  completed_steps(mode).include?(step_name)
end

#upsert_progress(mode:, current_step:, steps_completed:, started_at:, updated_at:) ⇒ Object

Upsert progress data (for migrations and direct updates)

Parameters:

  • mode (String)

    Mode (execute or analyze)

  • current_step (String, nil)

    Current step name

  • steps_completed (Array<String>)

    Completed step names

  • started_at (String)

    Started at timestamp

  • updated_at (String)

    Updated at timestamp



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/aidp/database/repositories/progress_repository.rb', line 142

def upsert_progress(mode:, current_step:, steps_completed:, started_at:, updated_at:)
  existing = query_one(
    "SELECT id FROM progress WHERE project_dir = ? AND mode = ?",
    [project_dir, mode]
  )

  if existing
    execute(
      <<~SQL,
        UPDATE progress SET
          current_step = ?,
          steps_completed = ?,
          started_at = ?,
          updated_at = ?
        WHERE project_dir = ? AND mode = ?
      SQL
      [
        current_step,
        serialize_json(steps_completed),
        started_at,
        updated_at,
        project_dir,
        mode
      ]
    )
  else
    execute(
      insert_sql([
        :project_dir, :mode, :current_step, :steps_completed,
        :started_at, :updated_at
      ]),
      [
        project_dir,
        mode,
        current_step,
        serialize_json(steps_completed),
        started_at,
        updated_at
      ]
    )
  end
end