Class: Aidp::Database::Repositories::EvaluationRepository

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

Overview

Repository for evaluations table Replaces evaluations/*.json and index.json

Constant Summary collapse

VALID_RATINGS =
%w[good neutral bad].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) ⇒ EvaluationRepository

Returns a new instance of EvaluationRepository.



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

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

Instance Method Details

#any?Boolean

Check if any evaluations exist

Returns:

  • (Boolean)


180
181
182
183
184
185
186
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 180

def any?
  count = query_value(
    "SELECT COUNT(*) FROM evaluations WHERE project_dir = ?",
    [project_dir]
  ) || 0
  count.positive?
end

#clearHash

Clear all evaluations

Returns:

  • (Hash)

    Result with count



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 165

def clear
  count = query_value(
    "SELECT COUNT(*) FROM evaluations WHERE project_dir = ?",
    [project_dir]
  ) || 0

  execute("DELETE FROM evaluations WHERE project_dir = ?", [project_dir])

  Aidp.log_debug("evaluation_repository", "cleared", count: count)
  {success: true, count: count}
end

#delete(id) ⇒ Hash

Delete an evaluation

Parameters:

  • id (String)

    Evaluation ID

Returns:

  • (Hash)

    Result



153
154
155
156
157
158
159
160
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 153

def delete(id)
  execute(
    "DELETE FROM evaluations WHERE id = ? AND project_dir = ?",
    [id, project_dir]
  )
  Aidp.log_debug("evaluation_repository", "deleted", id: id)
  {success: true, id: id}
end

#list(limit: 50, rating: nil, target_type: nil) ⇒ Array<Hash>

List evaluations with optional filtering

Parameters:

  • limit (Integer) (defaults to: 50)

    Maximum records

  • rating (String, nil) (defaults to: nil)

    Filter by rating

  • target_type (String, nil) (defaults to: nil)

    Filter by target type

Returns:

  • (Array<Hash>)

    Evaluations



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 72

def list(limit: 50, rating: nil, target_type: nil)
  conditions = ["project_dir = ?"]
  params = [project_dir]

  if rating
    conditions << "rating = ?"
    params << rating
  end

  if target_type
    conditions << "target_type = ?"
    params << target_type
  end

  params << limit

  rows = query(
    <<~SQL,
      SELECT * FROM evaluations
      WHERE #{conditions.join(" AND ")}
      ORDER BY created_at DESC
      LIMIT ?
    SQL
    params
  )

  rows.map { |row| deserialize_evaluation(row) }
end

#load(id) ⇒ Hash?

Load an evaluation by ID

Parameters:

  • id (String)

    Evaluation ID

Returns:

  • (Hash, nil)

    Evaluation or nil



58
59
60
61
62
63
64
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 58

def load(id)
  row = query_one(
    "SELECT * FROM evaluations WHERE id = ? AND project_dir = ?",
    [id, project_dir]
  )
  deserialize_evaluation(row)
end

#statsHash

Get statistics

Returns:

  • (Hash)

    Statistics



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 104

def stats
  total = query_value(
    "SELECT COUNT(*) FROM evaluations WHERE project_dir = ?",
    [project_dir]
  ) || 0

  by_rating = {}
  %w[good neutral bad].each do |r|
    by_rating[r.to_sym] = query_value(
      "SELECT COUNT(*) FROM evaluations WHERE project_dir = ? AND rating = ?",
      [project_dir, r]
    ) || 0
  end

  by_type_rows = query(
    <<~SQL,
      SELECT target_type, COUNT(*) as count
      FROM evaluations
      WHERE project_dir = ?
      GROUP BY target_type
    SQL
    [project_dir]
  )
  by_target_type = by_type_rows.each_with_object({}) do |row, h|
    h[row["target_type"]] = row["count"]
  end

  first_row = query_one(
    "SELECT created_at FROM evaluations WHERE project_dir = ? ORDER BY created_at ASC LIMIT 1",
    [project_dir]
  )
  last_row = query_one(
    "SELECT created_at FROM evaluations WHERE project_dir = ? ORDER BY created_at DESC LIMIT 1",
    [project_dir]
  )

  {
    total: total,
    by_rating: by_rating,
    by_target_type: by_target_type,
    first_evaluation: first_row&.dig("created_at"),
    last_evaluation: last_row&.dig("created_at")
  }
end

#store(record) ⇒ Hash

Store a new evaluation

Parameters:

  • record (Hash)

    Evaluation data with keys:

    • id [String] Evaluation ID
    • rating [String] good/neutral/bad
    • target_type [String] Type of item being evaluated
    • target_id [String] ID of item being evaluated
    • feedback [String, nil] User feedback text
    • context [Hash, nil] Additional context

Returns:

  • (Hash)

    Result with :success and :id



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aidp/database/repositories/evaluation_repository.rb', line 27

def store(record)
  execute(
    insert_sql([
      :id, :project_dir, :rating, :target_type, :target_id,
      :comment, :context
    ]),
    [
      record[:id],
      project_dir,
      record[:rating],
      record[:target_type],
      record[:target_id],
      record[:feedback],
      serialize_json(record[:context] || {})
    ]
  )

  Aidp.log_debug("evaluation_repository", "stored",
    id: record[:id], rating: record[:rating])

  {success: true, id: record[:id]}
rescue => e
  Aidp.log_debug("evaluation_repository", "store_failed",
    id: record[:id], error: e.message)
  {success: false, error: e.message, id: record[:id]}
end