show completion in frontend
This commit is contained in:
+106
-22
@@ -471,11 +471,69 @@ def create_schedule_version(
|
||||
return next_version
|
||||
|
||||
|
||||
def get_schedule_week(start_date: str, db_path: str | Path = DB_PATH) -> list[ScheduleRow]:
|
||||
def get_schedule_versions(db_path: str | Path = DB_PATH) -> list[dict[str, Any]]:
|
||||
with get_connection(db_path) as conn:
|
||||
version_row = conn.execute("SELECT MAX(schedule_version) AS latest FROM schedules").fetchone()
|
||||
latest = version_row["latest"]
|
||||
if latest is None:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT
|
||||
schedule_version,
|
||||
MIN(created_at) AS created_at,
|
||||
COUNT(*) AS entry_count
|
||||
FROM schedules
|
||||
GROUP BY schedule_version
|
||||
ORDER BY schedule_version DESC
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
return [
|
||||
{
|
||||
"schedule_version": int(row["schedule_version"]),
|
||||
"created_at": row["created_at"],
|
||||
"entry_count": int(row["entry_count"]),
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
|
||||
|
||||
def resolve_schedule_version(
|
||||
version: int | None = None,
|
||||
db_path: str | Path = DB_PATH,
|
||||
) -> int | None:
|
||||
with get_connection(db_path) as conn:
|
||||
if version is not None:
|
||||
row = conn.execute(
|
||||
"SELECT 1 FROM schedules WHERE schedule_version = ? LIMIT 1",
|
||||
(version,),
|
||||
).fetchone()
|
||||
return int(version) if row else None
|
||||
|
||||
latest_row = conn.execute("SELECT MAX(schedule_version) AS latest FROM schedules").fetchone()
|
||||
latest = latest_row["latest"]
|
||||
return int(latest) if latest is not None else None
|
||||
|
||||
|
||||
def get_schedule_week(
|
||||
start_date: str,
|
||||
version: int | None = None,
|
||||
db_path: str | Path = DB_PATH,
|
||||
) -> list[ScheduleRow]:
|
||||
with get_connection(db_path) as conn:
|
||||
if version is not None:
|
||||
version_row = conn.execute(
|
||||
"SELECT 1 AS exists_row FROM schedules WHERE schedule_version = ? LIMIT 1",
|
||||
(version,),
|
||||
).fetchone()
|
||||
if version_row is None:
|
||||
return []
|
||||
selected_version = int(version)
|
||||
else:
|
||||
latest_row = conn.execute("SELECT MAX(schedule_version) AS latest FROM schedules").fetchone()
|
||||
latest = latest_row["latest"]
|
||||
if latest is None:
|
||||
return []
|
||||
selected_version = int(latest)
|
||||
|
||||
if selected_version is None:
|
||||
return []
|
||||
|
||||
rows = conn.execute(
|
||||
@@ -514,7 +572,7 @@ def get_schedule_week(start_date: str, db_path: str | Path = DB_PATH) -> list[Sc
|
||||
ORDER BY s.scheduled_date, s.shift_index, s.sequence_in_shift
|
||||
""",
|
||||
(
|
||||
latest,
|
||||
selected_version,
|
||||
start_date,
|
||||
start_date,
|
||||
start_date,
|
||||
@@ -544,11 +602,27 @@ def get_schedule_week(start_date: str, db_path: str | Path = DB_PATH) -> list[Sc
|
||||
return result
|
||||
|
||||
|
||||
def get_schedule_rows_for_latest_version(db_path: str | Path = DB_PATH) -> list[ScheduleRow]:
|
||||
def get_schedule_rows(
|
||||
version: int | None = None,
|
||||
db_path: str | Path = DB_PATH,
|
||||
) -> list[ScheduleRow]:
|
||||
with get_connection(db_path) as conn:
|
||||
version_row = conn.execute("SELECT MAX(schedule_version) AS latest FROM schedules").fetchone()
|
||||
latest = version_row["latest"]
|
||||
if latest is None:
|
||||
if version is not None:
|
||||
version_row = conn.execute(
|
||||
"SELECT 1 AS exists_row FROM schedules WHERE schedule_version = ? LIMIT 1",
|
||||
(version,),
|
||||
).fetchone()
|
||||
if version_row is None:
|
||||
return []
|
||||
selected_version = int(version)
|
||||
else:
|
||||
latest_row = conn.execute("SELECT MAX(schedule_version) AS latest FROM schedules").fetchone()
|
||||
latest = latest_row["latest"]
|
||||
if latest is None:
|
||||
return []
|
||||
selected_version = int(latest)
|
||||
|
||||
if selected_version is None:
|
||||
return []
|
||||
|
||||
rows = conn.execute(
|
||||
@@ -570,7 +644,7 @@ def get_schedule_rows_for_latest_version(db_path: str | Path = DB_PATH) -> list[
|
||||
WHERE s.schedule_version = ?
|
||||
ORDER BY s.scheduled_date, s.shift_index, s.sequence_in_shift
|
||||
""",
|
||||
(latest,),
|
||||
(selected_version,),
|
||||
).fetchall()
|
||||
|
||||
result: list[ScheduleRow] = []
|
||||
@@ -593,21 +667,31 @@ def get_schedule_rows_for_latest_version(db_path: str | Path = DB_PATH) -> list[
|
||||
|
||||
return result
|
||||
|
||||
def mark_tests_completed(test_ids_with_device: list[tuple[str, str]], db_path: str | Path = DB_PATH) -> None:
|
||||
def mark_tests_completed(test_ids_with_device: list[tuple[str, str]], db_path: str | Path = DB_PATH) -> int:
|
||||
if not test_ids_with_device:
|
||||
return
|
||||
return 0
|
||||
|
||||
with get_connection(db_path) as conn:
|
||||
conn.executemany(
|
||||
"""
|
||||
UPDATE tests
|
||||
SET status = 'completed', updated_at = CURRENT_TIMESTAMP
|
||||
WHERE test_id = ?
|
||||
AND device = ?
|
||||
AND status != 'completed'
|
||||
""",
|
||||
test_ids_with_device,
|
||||
)
|
||||
# executemany() doesn't properly return rowcount, so we track manually
|
||||
total_updated = 0
|
||||
for test_id, device in test_ids_with_device:
|
||||
cursor = conn.execute(
|
||||
"""
|
||||
UPDATE tests
|
||||
SET status = 'completed', updated_at = CURRENT_TIMESTAMP
|
||||
WHERE test_id = ?
|
||||
AND device = ?
|
||||
AND status != 'completed'
|
||||
""",
|
||||
(test_id, device),
|
||||
)
|
||||
if cursor.rowcount > 0:
|
||||
total_updated += cursor.rowcount
|
||||
print(f"[db] marked {test_id} on {device} as completed")
|
||||
else:
|
||||
print(f"[db] {test_id} on {device}: no update (not found or already completed)")
|
||||
|
||||
return total_updated
|
||||
|
||||
|
||||
def mark_overdue_as_rerun(db_path: str | Path = DB_PATH) -> int:
|
||||
|
||||
Reference in New Issue
Block a user