From 1a3f6ffb6f562cee68cde1a3b90ecd649b71518d Mon Sep 17 00:00:00 2001 From: dsutanto Date: Wed, 29 Jul 2026 14:36:29 +0700 Subject: [PATCH] Live Counter get total count per batch 3 --- recounting_dashboard.py | 31 ++++++++++++++++++++++++------- templates/recounting.html | 17 ++++++++++++++--- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/recounting_dashboard.py b/recounting_dashboard.py index a5ad8a1..df91b6c 100644 --- a/recounting_dashboard.py +++ b/recounting_dashboard.py @@ -35,7 +35,7 @@ FLASK_DEBUG = os.getenv("FLASK_DEBUG", "false").lower() == "true" _http_session = requests.Session() _http_session.timeout = 3 -_FILENAME_RE = re.compile(r"batch_(\d+)_(\d{8})_\d{6}\.mp4") +_FILENAME_RE = re.compile(r"batch[_-](\d+)[_-](\d{8})[_-]\d{6}\.mp4", re.IGNORECASE) def _parse_filename(filename): @@ -128,12 +128,16 @@ def api_batch_result(): if batch_num is None: return jsonify({"success": False, "error": "Cannot parse batch number from filename"}), 200 - data = _api_get(LIVE_API_URL, f"/api/day-detail/{date_str}") - if not data: - return jsonify({"success": False, "error": "Live API unreachable"}), 200 + def _match_in_batches(batches): + for b in batches: + if b.get("batch_number") == batch_num: + return b + return None - for b in data.get("batches", []): - if b.get("batch_number") == batch_num: + data = _api_get(LIVE_API_URL, f"/api/day-detail/{date_str}") + if data: + b = _match_in_batches(data.get("batches", [])) + if b: return jsonify({ "success": True, "batch_number": batch_num, @@ -143,7 +147,20 @@ def api_batch_result(): "end_time": b.get("end_time"), }) - return jsonify({"success": False, "error": f"Batch #{batch_num} not found on {date_str}"}), 200 + recent = _api_get(LIVE_API_URL, "/api/recent-batches?limit=100") + if recent: + for entry in recent if isinstance(recent, list) else recent.get("batches", []): + if isinstance(entry, dict) and entry.get("batch_number") == batch_num: + return jsonify({ + "success": True, + "batch_number": batch_num, + "date": entry.get("date", date_str), + "count": entry["count"], + "start_time": entry.get("start_time"), + "end_time": entry.get("end_time"), + }) + + return jsonify({"success": False, "error": f"Batch #{batch_num} not found"}), 200 @app.route("/api/stop-recount", methods=["POST"]) diff --git a/templates/recounting.html b/templates/recounting.html index a1bf1ba..6b433db 100644 --- a/templates/recounting.html +++ b/templates/recounting.html @@ -490,15 +490,24 @@ async function loadBatchResult(filePath) { batchResultLoaded = false; document.getElementById('live-title').innerHTML = '◉ Live Counter'; document.getElementById('live-label').textContent = 'Objects Counted'; - document.getElementById('live-status').textContent = 'waiting...'; + document.getElementById('live-count').textContent = '--'; + document.getElementById('live-batch').textContent = '--'; + document.getElementById('live-time').textContent = '--'; + document.getElementById('live-status').textContent = 'no file selected'; document.getElementById('live-status').className = 'counter-status disconnected'; - loadLiveProgress(); return; } try { const res = await fetch('/api/batch-result?file=' + encodeURIComponent(filePath)); const data = await res.json(); if (!data.success) { + document.getElementById('live-title').innerHTML = '◉ Live Counter'; + document.getElementById('live-count').textContent = '--'; + document.getElementById('live-label').textContent = 'Objects Counted'; + document.getElementById('live-batch').textContent = '--'; + document.getElementById('live-time').textContent = data.error || 'not found'; + document.getElementById('live-status').textContent = 'not found'; + document.getElementById('live-status').className = 'counter-status disconnected'; batchResultLoaded = false; return; } @@ -508,7 +517,9 @@ async function loadBatchResult(filePath) { document.getElementById('live-label').textContent = 'Batch #' + data.batch_number; document.getElementById('live-batch').textContent = data.batch_number; document.getElementById('live-time').textContent = data.date; - document.getElementById('live-status').textContent = 'recorded \u00b7 ' + (data.start_time ? new Date(data.start_time).toLocaleTimeString('en-US', {hour12: false, hour:'2-digit', minute:'2-digit'}) : '--') + ' \u2013 ' + (data.end_time ? new Date(data.end_time).toLocaleTimeString('en-US', {hour12: false, hour:'2-digit', minute:'2-digit'}) : '--'); + var startStr = data.start_time ? new Date(data.start_time).toLocaleTimeString('en-US', {hour12: false, hour:'2-digit', minute:'2-digit'}) : '--'; + var endStr = data.end_time ? new Date(data.end_time).toLocaleTimeString('en-US', {hour12: false, hour:'2-digit', minute:'2-digit'}) : '--'; + document.getElementById('live-status').textContent = 'recorded \u00b7 ' + startStr + ' \u2013 ' + endStr; document.getElementById('live-status').className = 'counter-status connected'; } catch (err) { console.error('Failed to load batch result:', err);