temps tours

This commit is contained in:
2025-12-12 10:49:13 -05:00
parent f85cd66795
commit f271cea42c
4 changed files with 72 additions and 67 deletions

View File

@ -2,6 +2,7 @@
from engine.logger import log_info, log_debug
from decimal import Decimal
from datetime import datetime, timezone
from engine.utils import format_temps_decimal
# -------------------------------------------------
# Règle de priorité : remplace, rejette, ou force tour
# -------------------------------------------------
@ -111,7 +112,7 @@ def check_interval_rule(cur, detail_table, tp_id, dossard, timestamp, interval_s
log_debug(f"[INTERVAL] tp_id={tp_id} dossard={dossard} interval_sec={interval_sec}")
last_timestamp = get_last_valid_lecture_time(cur, detail_table, tp_id, dossard, timestamp)
last_timestamp, last_tour = get_last_valid_lecture_time(cur, detail_table, tp_id, dossard, timestamp)
log_debug(f"[INTERVAL] last_valid={last_timestamp} current={timestamp}")
@ -137,27 +138,26 @@ def check_interval_rule(cur, detail_table, tp_id, dossard, timestamp, interval_s
def get_last_valid_lecture_time(cur, detail_table, tp_id, dossard, current_ts):
log_debug(f"[get_last_valid_lecture_time] tp={tp_id} dossard={dossard} current={current_ts}")
cur.execute(f"""
SELECT timestamp_read
SELECT timestamp_read, tour
FROM `{detail_table}`
WHERE tp_id = %s
AND cla_info_dossard = %s
AND status = 1
AND timestamp_read < %s
AND cla_info_dossard = %s
AND status = 1
AND timestamp_read < %s
ORDER BY timestamp_read DESC
LIMIT 1
""", (tp_id, dossard, current_ts))
log_debug(
f"SELECT timestamp_read FROM `{detail_table}` "
f"WHERE tp_id = {tp_id} AND cla_info_dossard = '{dossard}' "
f"AND status = 1 AND timestamp_read < {current_ts} "
f"ORDER BY timestamp_read DESC LIMIT 1;"
)
row = cur.fetchone()
log_debug(f"[get_last_valid_lecture_time] result={row}")
if not row:
return None
return Decimal(str(row["timestamp_read"]))
if not row:
return None, None
return (
Decimal(str(row["timestamp_read"])),
row["tour"]
)
# -------------------------------------------------
@ -184,25 +184,48 @@ def check_tour_rule(
):
last_timestamp, last_tour = get_last_valid_lecture_time(
cur, detail_table, tp_id, dossard, timestamp
)
if last_tour is None:
last_tour=0
if last_timestamp is None:
last_timestamp = Decimal("0")
tour=last_tour+1;
tempstour = timestamp - last_timestamp
tempstour_str = format_temps_decimal(tempstour)
return tour, tempstour,tempstour_str
def update_detail(
cur,
cnx,
detail_table,
detail_id,
tour,
tempstour,
tempstour_str
):
cur.execute(f"""
SELECT tour, timestamp_read
FROM `{detail_table}`
WHERE tp_id = %s
AND cla_info_dossard = %s
AND status = 1
ORDER BY detail_id DESC
LIMIT 1
""", (tp_id, dossard))
UPDATE `{detail_table}`
SET tour = %s,
tempstour = %s,
tempstour_str = %s,
reason_code = NULL,
reason_ref_id = NULL
WHERE detail_id = %s
""", (
tour,
tempstour,
tempstour_str,
detail_id
))
row = cur.fetchone()
if not row or row["tour"] is None:
return 1, None
last_tour = row["tour"]
prev_ts = Decimal(str(row["timestamp_read"]))
return last_tour + 1, prev_ts
cnx.commit()
def update_detail_status_zero(cur, cnx, detail_table, detail_id, reason_code, reason_ref_id):
@ -218,4 +241,5 @@ def update_detail_status_zero(cur, cnx, detail_table, detail_id, reason_code, re
WHERE detail_id = %s
""", (reason_code, reason_ref_id, detail_id))
cnx.commit()
cnx.commit()