Conseil simule aligne sur le moteur reel et validation par regimes
Le conseiller du backtest raisonne desormais comme le moteur de decision : besoin d abord, anticipation seulement si le gain depasse le seuil du vehicule, et plancher d appoint qui interdit de repasser a la pompe pour quelques litres. La campagne precedente le montrait : sans ce plancher, la strategie multipliait les passages quotidiens et le verdict la refusait a juste titre. Ajout de la validation par regimes : chaque periode atypique est evaluee avec un apprentissage arrete la veille de son debut, plus l experience ou la crise actuelle reste entierement hors entrainement.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""Ordonnanceur : cadences propres à chaque source, fenêtres, supervision, résilience."""
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import collector
|
||||
@@ -7,80 +8,91 @@ import storage
|
||||
|
||||
|
||||
def test_registry_covers_every_source_with_its_own_cadence():
|
||||
expected = {'local_prices', 'market_daily', 'ecb_fx', 'eu_bulletin', 'dgec_monthly',
|
||||
'portwatch', 'news_geo', 'promotions'}
|
||||
expected = {
|
||||
"local_prices",
|
||||
"market_daily",
|
||||
"ecb_fx",
|
||||
"eu_bulletin",
|
||||
"dgec_monthly",
|
||||
"portwatch",
|
||||
"news_geo",
|
||||
"promotions",
|
||||
}
|
||||
assert expected <= set(scheduler.JOBS)
|
||||
for name, job in scheduler.JOBS.items():
|
||||
assert job['source'] and job['frequency'], name
|
||||
assert job['cadence_seconds'] > 0, name
|
||||
assert callable(job['window']), name
|
||||
assert hasattr(collector, job['func']), name
|
||||
assert scheduler.JOBS['local_prices']['cadence_seconds'] == 3600, 'prix locaux : toutes les heures'
|
||||
assert scheduler.JOBS['news_geo']['cadence_seconds'] == 7200, 'actualités : toutes les 2 heures'
|
||||
assert job["source"] and job["frequency"], name
|
||||
assert job["cadence_seconds"] > 0, name
|
||||
assert callable(job["window"]), name
|
||||
assert hasattr(collector, job["func"]), name
|
||||
assert scheduler.JOBS["local_prices"]["cadence_seconds"] == 3600, "prix locaux : toutes les heures"
|
||||
assert scheduler.JOBS["news_geo"]["cadence_seconds"] == 7200, "actualités : toutes les 2 heures"
|
||||
|
||||
|
||||
def test_job_is_due_only_within_its_window(store, monkeypatch):
|
||||
monkeypatch.setattr(scheduler, 'JOBS', dict(
|
||||
scheduler.JOBS, ecb_fx=dict(scheduler.JOBS['ecb_fx'])))
|
||||
monkeypatch.setattr(scheduler, "JOBS", dict(scheduler.JOBS, ecb_fx=dict(scheduler.JOBS["ecb_fx"])))
|
||||
saturday = datetime(2026, 9, 12, 17, 0, tzinfo=timezone.utc) # samedi 19 h à Paris
|
||||
monday_morning = datetime(2026, 9, 14, 8, 0, tzinfo=timezone.utc) # lundi 10 h
|
||||
monday_evening = datetime(2026, 9, 14, 14, 30, tzinfo=timezone.utc) # lundi 16 h 30
|
||||
assert scheduler.is_due('ecb_fx', now=monday_evening) is True
|
||||
assert scheduler.is_due('ecb_fx', now=saturday) is False
|
||||
assert scheduler.is_due('ecb_fx', now=monday_morning) is False
|
||||
assert scheduler.is_due('local_prices', now=saturday) is True
|
||||
assert scheduler.is_due("ecb_fx", now=monday_evening) is True
|
||||
assert scheduler.is_due("ecb_fx", now=saturday) is False
|
||||
assert scheduler.is_due("ecb_fx", now=monday_morning) is False
|
||||
assert scheduler.is_due("local_prices", now=saturday) is True
|
||||
|
||||
|
||||
def test_cadence_and_retry_after_failure(store):
|
||||
now = datetime(2026, 9, 14, 12, 0, tzinfo=timezone.utc)
|
||||
storage.job_record('news_geo', 'src', 'freq', 7200, 'ok', now=now)
|
||||
assert scheduler.is_due('news_geo', now=now + timedelta(minutes=30)) is False
|
||||
assert scheduler.is_due('news_geo', now=now + timedelta(hours=2)) is True
|
||||
storage.job_record('news_geo', 'src', 'freq', 7200, 'error', now=now)
|
||||
assert scheduler.is_due('news_geo', now=now + timedelta(minutes=31)) is True, 'réessai après échec'
|
||||
storage.job_record("news_geo", "src", "freq", 7200, "ok", now=now)
|
||||
assert scheduler.is_due("news_geo", now=now + timedelta(minutes=30)) is False
|
||||
assert scheduler.is_due("news_geo", now=now + timedelta(hours=2)) is True
|
||||
storage.job_record("news_geo", "src", "freq", 7200, "error", now=now)
|
||||
assert scheduler.is_due("news_geo", now=now + timedelta(minutes=31)) is True, "réessai après échec"
|
||||
|
||||
|
||||
def test_next_run_respects_window(store):
|
||||
sunday = datetime(2026, 9, 13, 10, 0, tzinfo=timezone.utc) # dimanche
|
||||
upcoming = scheduler.next_run('eu_bulletin', now=sunday)
|
||||
assert datetime.fromisoformat(upcoming).astimezone(storage.PARIS).weekday() == 3, 'jeudi'
|
||||
upcoming = scheduler.next_run("eu_bulletin", now=sunday)
|
||||
assert datetime.fromisoformat(upcoming).astimezone(storage.PARIS).weekday() == 3, "jeudi"
|
||||
|
||||
|
||||
def test_run_records_supervision_and_survives_failures(store, monkeypatch):
|
||||
calls = []
|
||||
|
||||
def good(force=False):
|
||||
calls.append('local_prices')
|
||||
return dict(status='ok', stored=3, last_observation_at='2026-09-12T18:00:00',
|
||||
detail='3 changements')
|
||||
calls.append("local_prices")
|
||||
return dict(status="ok", stored=3, last_observation_at="2026-09-12T18:00:00", detail="3 changements")
|
||||
|
||||
def broken(force=False):
|
||||
calls.append('portwatch')
|
||||
raise OSError('source hors ligne')
|
||||
calls.append("portwatch")
|
||||
raise OSError("source hors ligne")
|
||||
|
||||
monkeypatch.setattr(collector, 'job_local_prices', good)
|
||||
monkeypatch.setattr(collector, 'job_portwatch', broken)
|
||||
monkeypatch.setattr(scheduler, 'JOBS', {
|
||||
name: dict(scheduler.JOBS[name], window=lambda now=None: True)
|
||||
for name in ('local_prices', 'portwatch')})
|
||||
outcome = scheduler.run(names=['local_prices', 'portwatch'], force=True)
|
||||
assert outcome['status'] == 'ok'
|
||||
assert outcome['results']['local_prices']['status'] == 'ok'
|
||||
assert outcome['results']['portwatch']['status'] == 'error'
|
||||
assert 'job_local_prices' not in str(outcome)
|
||||
state = storage.job_get('local_prices')
|
||||
assert state['status'] == 'ok' and state['stored'] == 3
|
||||
assert state['last_observation_at'] == '2026-09-12T18:00:00'
|
||||
assert state['last_success_at'] and state['next_run_at']
|
||||
failed = storage.job_get('portwatch')
|
||||
assert failed['status'] == 'error' and failed['fail_count'] == 1
|
||||
assert 'OSError' in failed['detail']
|
||||
monkeypatch.setattr(collector, "job_local_prices", good)
|
||||
monkeypatch.setattr(collector, "job_portwatch", broken)
|
||||
monkeypatch.setattr(
|
||||
scheduler,
|
||||
"JOBS",
|
||||
{
|
||||
name: dict(scheduler.JOBS[name], window=lambda now=None: True)
|
||||
for name in ("local_prices", "portwatch")
|
||||
},
|
||||
)
|
||||
outcome = scheduler.run(names=["local_prices", "portwatch"], force=True)
|
||||
assert outcome["status"] == "ok"
|
||||
assert outcome["results"]["local_prices"]["status"] == "ok"
|
||||
assert outcome["results"]["portwatch"]["status"] == "error"
|
||||
assert "job_local_prices" not in str(outcome)
|
||||
state = storage.job_get("local_prices")
|
||||
assert state["status"] == "ok" and state["stored"] == 3
|
||||
assert state["last_observation_at"] == "2026-09-12T18:00:00"
|
||||
assert state["last_success_at"] and state["next_run_at"]
|
||||
failed = storage.job_get("portwatch")
|
||||
assert failed["status"] == "error" and failed["fail_count"] == 1
|
||||
assert "OSError" in failed["detail"]
|
||||
|
||||
|
||||
def test_inventory_exposes_source_frequency_state_and_next_run(store):
|
||||
rows = {row['name']: row for row in scheduler.inventory()}
|
||||
assert rows['local_prices']['source'].startswith('prix-carburants.gouv.fr')
|
||||
assert rows['eu_bulletin']['frequency'].startswith('le jeudi')
|
||||
assert rows['promotions']['status'] == 'never'
|
||||
assert rows['dgec_monthly']['next_run_at']
|
||||
assert 'n’historise un événement' in rows['local_prices']['note']
|
||||
rows = {row["name"]: row for row in scheduler.inventory()}
|
||||
assert rows["local_prices"]["source"].startswith("prix-carburants.gouv.fr")
|
||||
assert rows["eu_bulletin"]["frequency"].startswith("le jeudi")
|
||||
assert rows["promotions"]["status"] == "never"
|
||||
assert rows["dgec_monthly"]["next_run_at"]
|
||||
assert "n’historise un événement" in rows["local_prices"]["note"]
|
||||
|
||||
Reference in New Issue
Block a user