Files
karung-counting-feedmill-se…/archive/update_batches.py
T

78 lines
2.7 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()
# Check current batches on 2026-08-20
cur.execute("SELECT id, batch_number, count, start_time, end_time FROM batches WHERE counting_date = '2026-08-20' ORDER BY batch_number ASC")
old_rows = cur.fetchall()
print("Before update:")
for r in old_rows:
print(r)
# Step A: Shift batch 2..10 to batch + 2
cur.execute("SELECT id, batch_number FROM batches WHERE counting_date = '2026-08-20' AND batch_number >= 2 ORDER BY batch_number DESC")
shift_rows = cur.fetchall()
for bid, bnum in shift_rows:
new_bnum = bnum + 2
cur.execute("UPDATE batches SET batch_number = ? WHERE id = ?", (new_bnum, bid))
# Step B: Update batch 1 (id 1525) to batch 1 with 219 sacks
cur.execute('''
UPDATE batches
SET count = 219,
start_time = '2026-08-20T09:56:44',
end_time = '2026-08-20T10:16:22'
WHERE id = 1525
''')
# Step C: Insert batch 2 (168 sacks) and batch 3 (174 sacks)
cur.execute('''
INSERT INTO batches (counting_date, batch_number, camera_name, object_label, count, start_time, end_time)
VALUES ('2026-08-20', 2, 'CC1', 'karung-pakan', 168, '2026-08-20T10:24:47', '2026-08-20T10:55:49')
''')
cur.execute('''
INSERT INTO batches (counting_date, batch_number, camera_name, object_label, count, start_time, end_time)
VALUES ('2026-08-20', 3, 'CC1', 'karung-pakan', 174, '2026-08-20T11:03:30', '2026-08-20T11:19:19')
''')
# 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
cur.execute("SELECT id, batch_number, count, start_time, end_time FROM batches WHERE counting_date = '2026-08-20' ORDER BY batch_number ASC")
new_rows = cur.fetchall()
print("\nAfter update:")
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()