implentation tour pas fini

This commit is contained in:
2025-12-12 09:52:22 -05:00
parent 5873ab4900
commit f85cd66795
7 changed files with 268 additions and 111 deletions

View File

@ -1,5 +1,7 @@
# regles.py
from engine.logger import log_info, log_debug
from decimal import Decimal
from datetime import datetime, timezone
# -------------------------------------------------
# Règle de priorité : remplace, rejette, ou force tour
# -------------------------------------------------
@ -94,4 +96,126 @@ def resolve_priority(
# Même priorité → rejet
return 0, "EQUAL_PRIORITY", det_id, None
return 0, "EQUAL_PRIORITY", det_id, None
# -------------------------------------------------
# Regle interval
# ------------------------------------------------
def check_interval_rule(cur, detail_table, tp_id, dossard, timestamp, interval_sec, det_id):
"""
Retourne :
status (1 ou 0)
reason_code
reason_ref_id
"""
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)
log_debug(f"[INTERVAL] last_valid={last_timestamp} current={timestamp}")
if last_timestamp and interval_sec > 0:
delta = (timestamp - last_timestamp)
log_debug(f"[INTERVAL] delta={delta:.3f}s")
if delta < interval_sec:
reason = f"SKIP_INTERVAL {delta:.1f}s < {interval_sec}s"
log_debug(
f"[INTERVAL] REJECT dossard={dossard} tp_id={tp_id} "
f"reason='{reason}' det_id={det_id}"
)
return (0, reason, det_id)
log_debug(f"[INTERVAL] OK dossard={dossard} tp_id={tp_id}")
return (1, None, None)
# -------------------------------------------------
# Dernière lecture valide (timestamp_read < T)
# -------------------------------------------------
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
FROM `{detail_table}`
WHERE tp_id = %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"]))
# -------------------------------------------------
# Conversion d'un timestamp Decimal en datetime UTC
# -------------------------------------------------
def ts_to_utc(ts):
ts = Decimal(str(ts))
seconds = int(ts)
micro = int((ts - Decimal(seconds)) * Decimal(1_000_000))
return datetime.fromtimestamp(seconds, tz=timezone.utc).replace(microsecond=micro)
# -------------------------------------------------
# Recherche du dernier tour pour un coureur à un TP
# -------------------------------------------------
#def compute_tour(cur, detail_table, tp_id, dossard):
def check_tour_rule(
cur,
detail_table,
tp_id,
dossard,
timestamp,
det_id
):
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))
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
def update_detail_status_zero(cur, cnx, detail_table, detail_id, reason_code, reason_ref_id):
"""
Met à jour un enregistrement Detail existant pour le passer en status=0,
avec reason_code et reason_ref_id.
"""
cur.execute(f"""
UPDATE `{detail_table}`
SET status = 0,
reason_code = %s,
reason_ref_id = %s
WHERE detail_id = %s
""", (reason_code, reason_ref_id, detail_id))
cnx.commit()