Parse batch number and date from upload filename for live API lookup
This commit is contained in:
1 parent
14ebfb7779
commit
f01601d1d7
3 files changed
+54
-19
No files matched your search
@@ -46,7 +46,7 @@ RECOUNT_CMD = os.getenv("RECOUNT_CMD", "bytetrack-counter config.env --source {p
|
||||
_http_session = requests.Session()
|
||||
_http_session.timeout = 3
|
||||
|
||||
_FILENAME_RE = re.compile(r"batch[_-]?(\d+)", re.IGNORECASE)
|
||||
_FILENAME_RE = re.compile(r"batch[_-]?(\d+)[_-](\d{8})[_-]\d{6}", re.IGNORECASE)
|
||||
|
||||
_recount_proc = None
|
||||
_recount_lock = threading.Lock()
|
||||
@@ -56,7 +56,8 @@ _current_file = None
|
||||
def _parse_filename(filename):
|
||||
m = _FILENAME_RE.search(filename)
|
||||
if m:
|
||||
return int(m.group(1)), None
|
||||
raw = m.group(2)
|
||||
return int(m.group(1)), f"{raw[:4]}-{raw[4:6]}-{raw[6:8]}"
|
||||
return None, None
|
||||
|
||||
|
||||
@@ -254,9 +255,13 @@ def api_proxy_stream():
|
||||
def api_batch_result():
|
||||
fpath = request.args.get("file", "")
|
||||
filename = Path(fpath).name
|
||||
batch_num, _ = _parse_filename(filename)
|
||||
batch_num, date_str = _parse_filename(filename)
|
||||
if batch_num is None:
|
||||
return jsonify({"success": False, "error": "Cannot parse batch number from filename"}), 200
|
||||
if not date_str:
|
||||
date_str = _folder_date(fpath, Path(UPLOAD_DIR))
|
||||
if not date_str:
|
||||
return jsonify({"success": False, "error": "Cannot determine date from filename"}), 200
|
||||
|
||||
def _match_in_batches(batches):
|
||||
for b in batches:
|
||||
@@ -264,20 +269,18 @@ def api_batch_result():
|
||||
return b
|
||||
return None
|
||||
|
||||
date_str = _folder_date(fpath, Path(UPLOAD_DIR))
|
||||
if date_str:
|
||||
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,
|
||||
"date": date_str,
|
||||
"count": b["count"],
|
||||
"start_time": b.get("start_time"),
|
||||
"end_time": b.get("end_time"),
|
||||
})
|
||||
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,
|
||||
"date": date_str,
|
||||
"count": b["count"],
|
||||
"start_time": b.get("start_time"),
|
||||
"end_time": b.get("end_time"),
|
||||
})
|
||||
|
||||
recent = _api_get(LIVE_API_URL, "/api/recent-batches?limit=100")
|
||||
if recent:
|
||||
|
||||
Reference in new issue
Block a user