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

@ -10,11 +10,7 @@ from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from decimal import Decimal
from engine.utils import format_temps_decimal
from engine.engine_components.regles import resolve_priority
from engine.engine_components.regles import check_interval_rule
from engine.engine_components.regles import get_last_valid_lecture_time
from engine.engine_components.regles import ts_to_utc
from engine.engine_components.regles import update_detail_status_zero
from engine.engine_components import regles
LOCAL_TZ = ZoneInfo("America/Toronto")
@ -97,7 +93,7 @@ def process_course(cur, cnx, con_id: int):
max_id = max(max_id, rcourse_id)
dt_utc = ts_to_utc(timestamp)
dt_utc = regles.ts_to_utc(timestamp)
dt_local = dt_utc.astimezone(LOCAL_TZ)
log_debug(f"[READ] id={rcourse_id} utc={dt_utc} loc={chr_loc}")
@ -195,7 +191,7 @@ def process_course(cur, cnx, con_id: int):
interval_sec = 0
# Appel de la règle dintervalle
status_interval, rc_interval, rr_interval = check_interval_rule(
status_interval, rc_interval, rr_interval = regles.check_interval_rule(
cur,
detail_table,
tp_id,
@ -208,7 +204,7 @@ def process_course(cur, cnx, con_id: int):
# Si la règle dintervalle rejette
if status_interval == 0:
update_detail_status_zero(
regles.update_detail_status_zero(
cur,
cnx,
detail_table,
@ -226,7 +222,7 @@ def process_course(cur, cnx, con_id: int):
# -------------------------------------------------
# Appel de la règle tour
status_tour = check_tour_rule(
tour, tempstour,tempstour_str = regles.check_tour_rule(
cur,
detail_table,
tp_id,
@ -235,37 +231,22 @@ def process_course(cur, cnx, con_id: int):
det_id
)
# Si la règle dtour rejette
if status_interval == 0:
update_detail_status_zero(
cur,
cnx,
detail_table,
detail_id, # <-- l'ID que tu as obtenu juste après insert_detail_record
rc_interval, # reason_code retourné par la règle
rr_interval # reason_ref_id retourné par la règle
)
# Et on arrête le traitement de cette lecture
continue
regles.update_detail(
cur,
cnx,
detail_table,
detail_id,
tour,
tempstour,
tempstour_str
)
#tour, prev_ts = compute_tour(cur, detail_table, tp_id, dossard)
#if forced_tour is not None:
# tour = forced_tour
#if prev_ts is None:
# tempstour = Decimal("0")
#else:
# tempstour = timestamp - prev_ts
#tempstour_str = format_temps_decimal(tempstour)

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()