Skip to content

Commit bd1ebf4

Browse files
author
Codeflash Bot
committed
temp logging
1 parent 5703889 commit bd1ebf4

File tree

1 file changed

+22
-62
lines changed

1 file changed

+22
-62
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -121,58 +121,32 @@
121121

122122

123123
def log_code_repair_to_db(
124-
code_repair_log_db: Path,
125-
optimization_id: str,
126-
trace_id: str | None = None,
127-
passed: str | None = None,
128-
faster: str | None = None,
124+
code_repair_log_db: Path, optimization_id: str, trace_id: str, passed: str, faster: str
129125
) -> None:
130-
"""Log code repair data to SQLite database.
131-
132-
Uses upsert pattern to allow incremental logging with different columns at different places.
133-
Only non-None values will be updated; existing values are preserved.
134-
"""
126+
"""Log code repair data to SQLite database."""
135127
try:
136-
conn = sqlite3.connect(code_repair_log_db)
137-
cursor = conn.cursor()
138-
139-
# Build dynamic upsert query based on provided columns
140-
columns = ["optimization_id"]
141-
values = [optimization_id]
142-
update_parts = ["updated_at = CURRENT_TIMESTAMP"]
143-
144-
if trace_id is not None:
145-
columns.append("trace_id")
146-
values.append(trace_id)
147-
update_parts.append("trace_id = excluded.trace_id")
148-
149-
if passed is not None:
150-
columns.append("passed")
151-
values.append(passed)
152-
update_parts.append("passed = excluded.passed")
153-
154-
if faster is not None:
155-
columns.append("faster")
156-
values.append(faster)
157-
update_parts.append("faster = excluded.faster")
158-
159-
placeholders = ", ".join(["?"] * len(values))
160-
columns_str = ", ".join(columns)
161-
update_str = ", ".join(update_parts)
162-
163-
cursor.execute(
164-
f"""
165-
INSERT INTO code_repair_logs_cf ({columns_str})
166-
VALUES ({placeholders})
167-
ON CONFLICT(optimization_id) DO UPDATE SET {update_str}
168-
""", # noqa: S608
169-
values,
170-
)
171-
conn.commit()
172-
conn.close()
128+
with sqlite3.connect(code_repair_log_db) as conn:
129+
cursor = conn.cursor()
130+
cursor.execute("""
131+
CREATE TABLE IF NOT EXISTS code_repair_logs_cf (
132+
optimization_id TEXT PRIMARY KEY,
133+
trace_id TEXT,
134+
passed TEXT,
135+
faster TEXT,
136+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
137+
)
138+
""")
139+
cursor.execute(
140+
"""
141+
INSERT INTO code_repair_logs_cf (optimization_id, trace_id, passed, faster)
142+
VALUES (?, ?, ?, ?)
143+
""",
144+
(optimization_id, trace_id, passed, faster),
145+
)
146+
conn.commit()
173147
except Exception as e:
174148
sentry_sdk.capture_exception(e)
175-
logger.exception(e)
149+
logger.exception("Error logging code repair to db")
176150

177151

178152
class CandidateProcessor:
@@ -447,20 +421,6 @@ def optimize_function(self) -> Result[BestOptimization, str]:
447421
initialization_result = self.can_be_optimized()
448422
if not is_successful(initialization_result):
449423
return Failure(initialization_result.failure())
450-
conn = sqlite3.connect(self.code_repair_log_db)
451-
cursor = conn.cursor()
452-
cursor.execute("""
453-
CREATE TABLE IF NOT EXISTS code_repair_logs_cf (
454-
optimization_id TEXT PRIMARY KEY,
455-
trace_id TEXT,
456-
passed TEXT,
457-
faster TEXT,
458-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
459-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
460-
)
461-
""")
462-
conn.commit()
463-
conn.close()
464424
should_run_experiment, code_context, original_helper_code = initialization_result.unwrap()
465425

466426
code_print(

0 commit comments

Comments
 (0)