78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
import sqlite3
|
|
import shutil
|
|
from datetime import datetime
|
|
|
|
db_path = '/opt/jetson-counter/jetson_counter.db'
|
|
|
|
# 1. Backup database
|
|
backup_path = f'/opt/jetson-counter/jetson_counter.db.bak_{datetime.now().strftime("%Y%m%d_%H%M%S")}'
|
|
shutil.copy2(db_path, backup_path)
|
|
print(f"Backup created at: {backup_path}")
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
cur = conn.cursor()
|
|
|
|
# Batches to merge: 28 to 35 on 2026-08-20
|
|
# IDs: 1552 (b28: 6), 1553 (b29: 14), 1554 (b30: 19), 1555 (b31: 27),
|
|
# 1556 (b32: 44), 1557 (b33: 44), 1558 (b34: 42), 1559 (b35: 411)
|
|
# Total count = 6 + 14 + 19 + 27 + 44 + 44 + 42 + 411 = 607
|
|
# Start time = 2026-08-20T21:09:35.192219 (from batch 28)
|
|
# End time = 2026-08-20T22:00:29.667740 (from batch 35)
|
|
|
|
# Step A: Update Batch #28 (id 1552) to contain merged total
|
|
cur.execute('''
|
|
UPDATE batches
|
|
SET count = 607,
|
|
start_time = '2026-08-20T21:09:35.192219',
|
|
end_time = '2026-08-20T22:00:29.667740'
|
|
WHERE id = 1552
|
|
''')
|
|
|
|
# Step B: Delete merged batches 29..35 (ids 1553..1559)
|
|
cur.execute('''
|
|
DELETE FROM batches
|
|
WHERE id IN (1553, 1554, 1555, 1556, 1557, 1558, 1559)
|
|
''')
|
|
|
|
# Step C: Shift Batch #36 (id 1560) and any subsequent batches down by 7
|
|
# (Batch 36 becomes Batch 29)
|
|
cur.execute('''
|
|
SELECT id, batch_number FROM batches
|
|
WHERE counting_date = '2026-08-20' AND batch_number > 35
|
|
ORDER BY batch_number ASC
|
|
''')
|
|
shift_rows = cur.fetchall()
|
|
for bid, bnum in shift_rows:
|
|
new_bnum = bnum - 7
|
|
cur.execute("UPDATE batches SET batch_number = ? WHERE id = ?", (new_bnum, bid))
|
|
|
|
# Step D: Recalculate daily_summaries for 2026-08-20
|
|
cur.execute('''
|
|
SELECT SUM(count), COUNT(id)
|
|
FROM batches
|
|
WHERE counting_date = '2026-08-20' AND camera_name = 'CC1' AND object_label = 'karung-pakan'
|
|
''')
|
|
sum_row = cur.fetchone()
|
|
tot_count = sum_row[0] if sum_row[0] is not None else 0
|
|
tot_batches = sum_row[1] if sum_row[1] is not None else 0
|
|
|
|
cur.execute('''
|
|
INSERT OR REPLACE INTO daily_summaries
|
|
(counting_date, camera_name, object_label, total_count, total_batches, updated_at)
|
|
VALUES ('2026-08-20', 'CC1', 'karung-pakan', ?, ?, CURRENT_TIMESTAMP)
|
|
''', (tot_count, tot_batches))
|
|
|
|
conn.commit()
|
|
|
|
# Verify new data around batch 27..30
|
|
cur.execute("SELECT id, batch_number, count, start_time, end_time FROM batches WHERE counting_date = '2026-08-20' AND batch_number >= 25 ORDER BY batch_number ASC")
|
|
new_rows = cur.fetchall()
|
|
print("\nAfter merge:")
|
|
for r in new_rows:
|
|
print(r)
|
|
|
|
cur.execute("SELECT * FROM daily_summaries WHERE counting_date = '2026-08-20'")
|
|
print("\nDaily Summary:", cur.fetchall())
|
|
|
|
conn.close()
|