Fix critical bug: add Group 0 to guarantee alarmed branches polled every cycle, regardless of epoch

This commit is contained in:
cheveguerra
2026-07-26 18:54:24 +00:00
parent 7b77793ef3
commit af250bae77
+45 -43
View File
@@ -2149,69 +2149,71 @@ def run_update(accounts=None, exclude=None, full_refresh=False):
active_locs_with_obj.sort(key=get_download_epoch) active_locs_with_obj.sort(key=get_download_epoch)
# 3. Construir Grupo A (Secuencial - 27 sucursales) # 3. Construir Grupo 0 (Alarmas Activas - SIEMPRE incluidas, sin excepción)
group_a = active_locs_with_obj[:27] # Se extraen ANTES de los demás grupos para garantizar presencia en cada ciclo.
group_a_ids = {x[0] for x in group_a} group_0 = []
group_0_ids = set()
# 4. Construir Grupo B (Alta Frecuencia - Hasta 7 sucursales) non_alarm_locs = []
group_b_pool = []
for loc_id, location in active_locs_with_obj: for loc_id, location in active_locs_with_obj:
if loc_id in group_a_ids:
continue
loc_id_str = str(loc_id) loc_id_str = str(loc_id)
prev = last_loc_map.get(loc_id_str, {}) prev = last_loc_map.get(loc_id_str, {})
status_val = prev.get('status', 0)
if isinstance(status_val, ArmingState):
status_val = status_val.value
is_disarmed = (status_val == 10200 or str(status_val).upper() == "DISARMED")
# Alarma real activa en zonas o particiones
is_triggered = ( is_triggered = (
len(prev.get('triggered_partitions', [])) > 0 or len(prev.get('triggered_partitions', [])) > 0 or
any(z.get('status') == 'Alarma' for z in prev.get('active_zones', [])) any(z.get('status') == 'Alarma' for z in prev.get('active_zones', []))
) )
has_faults = len(prev.get('active_zones', [])) > 0 if is_triggered:
group_0.append((loc_id, location))
if is_disarmed or is_triggered or has_faults: group_0_ids.add(loc_id)
# Peso de severidad: log_msg(f" 🚨 Grupo 0 (Alarma): '{location.location_name}' forzado en este ciclo.")
# 0 = Alarma Activa (Prioridad Absoluta) else:
# 1 = Zonas con fallas/abiertas (Prioridad Media) non_alarm_locs.append((loc_id, location))
# 2 = Desarmado simple (Prioridad Baja)
severity_weight = 2 # 4. Construir Grupo A (Secuencial - 27 sucursales, solo NO alarmadas)
if is_triggered: group_a = non_alarm_locs[:27]
severity_weight = 0 group_a_ids = {x[0] for x in group_a}
elif has_faults:
severity_weight = 1 # 5. Construir Grupo B (Alta Frecuencia - Hasta 7 sucursales no alarmadas)
group_b_pool = []
group_b_pool.append((loc_id, location, severity_weight)) for loc_id, location in non_alarm_locs:
if loc_id in group_a_ids:
# Ordenar por severidad de fallo/alarma primero, y en caso de empate por antigüedad continue
def sort_group_b(item): loc_id_str = str(loc_id)
loc_tuple = item
loc_id_str = str(loc_tuple[0])
prev = last_loc_map.get(loc_id_str, {}) prev = last_loc_map.get(loc_id_str, {})
dl_epoch = prev.get('last_download_epoch', 0)
severity = loc_tuple[2] status_val = prev.get('status', 0)
return (severity, dl_epoch) if isinstance(status_val, ArmingState):
status_val = status_val.value
is_disarmed = (status_val == 10200 or str(status_val).upper() == "DISARMED")
has_faults = len(prev.get('active_zones', [])) > 0
if is_disarmed or has_faults:
severity_weight = 1 if has_faults else 2
group_b_pool.append((loc_id, location, severity_weight))
# Ordenar por severidad primero, luego por antigüedad
def sort_group_b(item):
loc_id_str = str(item[0])
prev = last_loc_map.get(loc_id_str, {})
return (item[2], prev.get('last_download_epoch', 0))
group_b_pool.sort(key=sort_group_b) group_b_pool.sort(key=sort_group_b)
group_b = [(x[0], x[1]) for x in group_b_pool[:7]] group_b = [(x[0], x[1]) for x in group_b_pool[:7]]
group_b_ids = {x[0] for x in group_b} group_b_ids = {x[0] for x in group_b}
# 5. Construir Grupo C (Triggers de SOAP pendientes) # 6. Construir Grupo C (Triggers de SOAP pendientes, excluye alarmadas, A y B)
group_c = [] group_c = []
for loc_id, location in active_locs_with_obj: for loc_id, location in active_locs_with_obj:
if loc_id in group_a_ids or loc_id in group_b_ids: if loc_id in group_0_ids or loc_id in group_a_ids or loc_id in group_b_ids:
continue continue
loc_id_str = str(loc_id) loc_id_str = str(loc_id)
prev = last_loc_map.get(loc_id_str, {}) prev = last_loc_map.get(loc_id_str, {})
if prev.get('needs_full_update', False): if prev.get('needs_full_update', False):
group_c.append((loc_id, location)) group_c.append((loc_id, location))
# Lote completo a procesar en este ciclo # Lote completo a procesar en este ciclo.
lote_completo = group_a + group_b + group_c # group_0 va PRIMERO: sucursales en alarma activa garantizadas en cada ciclo.
lote_completo = group_0 + group_a + group_b + group_c
lote_ids = {x[0] for x in lote_completo} lote_ids = {x[0] for x in lote_completo}
loc_list = [] loc_list = []