diff --git a/tests/kilometrage/run.mjs b/tests/kilometrage/run.mjs index b824463..c163cca 100644 --- a/tests/kilometrage/run.mjs +++ b/tests/kilometrage/run.mjs @@ -157,6 +157,15 @@ if (process.argv.includes('--vues')) { const v = (await c.query("SELECT * FROM capacite_estimee WHERE branche >= '2020-03-01' AND branche < '2020-03-02'")).rows[0]; proche('sans compteur : sessions du branchement 7,7 / 0,95', v.capacite_kwh, 7.7 / 0.95); eq('niveau lu, pas estime', v.niveau_estime, false); + // Niveau de fin pris d'un releve photo_autonomie apres le debranchement : estime aussi. + await ev('2020-05-01T10:00Z', 'on'); await ev('2020-05-01T14:00Z', 'off'); + await c.query(`INSERT INTO session_charge (debut, fin, terminee, vehicule, vehicule_sur, energie_kwh, energie_station_kwh) + VALUES ('2020-05-01T10:05Z','2020-05-01T13:00Z',true,'TESTV',true,3.4,3.5)`); + await c.query(`INSERT INTO niveau_saisi (saisi_a, niveau_pct, ref_kwh, source) VALUES ('2020-05-01T10:01Z', 14, NULL, 'whatsapp')`); + await c.query(`INSERT INTO releve_km (vehicule, ts, km, niveau_pct, source) VALUES ('TESTV','2020-05-01T14:06Z',9100,55,'photo_autonomie:23/42')`); + const fin = (await c.query("SELECT * FROM capacite_estimee WHERE branche >= '2020-05-01' AND branche < '2020-05-02'")).rows[0]; + proche('fin par releve : 3,5 / (55-14) %', fin.capacite_kwh, 3.5 / 0.41); + eq('fin par releve estime -> niveau_estime', fin.niveau_estime, true); // 12/10 : niveau deduit de l'autonomie (photo VW) -> la saisie sans source est marquee. const { marquerNiveauxEstimes } = await import('../../tools/kilometrage.mjs'); await ev('2020-04-01T10:00Z', 'on'); await ev('2020-04-01T14:00Z', 'off'); diff --git a/tools/kilometrage.mjs b/tools/kilometrage.mjs index 91089d7..dd0e770 100644 --- a/tools/kilometrage.mjs +++ b/tools/kilometrage.mjs @@ -200,6 +200,10 @@ WITH tr AS ( WHERE r.vehicule = v.vehicule AND r.niveau_pct IS NOT NULL AND r.ts >= v.debranche AND r.ts <= v.debranche + interval '1 hour' ORDER BY r.ts LIMIT 1) AS niveau_releve_apres, + (SELECT r.source FROM releve_km r + WHERE r.vehicule = v.vehicule AND r.niveau_pct IS NOT NULL + AND r.ts >= v.debranche AND r.ts <= v.debranche + interval '1 hour' + ORDER BY r.ts LIMIT 1) AS niveau_releve_source, (SELECT nf.niveau_pct FROM niveau_fin nf WHERE nf.saisi_a >= v.debranche - interval '15 minutes' AND nf.saisi_a <= v.debranche + interval '1 hour' ORDER BY nf.saisi_a LIMIT 1) AS niveau_fin_saisi, @@ -213,7 +217,9 @@ WITH tr AS ( SELECT branche, debranche, coalesce(vehicule, '?') AS vehicule, niveau_pct, niveau_fin, kwh_livres, plein, -- Un des deux niveaux est deduit de l'autonomie affichee (pas une lecture). - (coalesce(source, '') ILIKE '%autonomie%' OR coalesce(niveau_fin_source, '') ILIKE '%autonomie%') AS niveau_estime, + (coalesce(source, '') ILIKE '%autonomie%' + OR (niveau_fin_saisi IS NOT NULL AND coalesce(niveau_fin_source, '') ILIKE '%autonomie%') + OR (niveau_fin_saisi IS NULL AND NOT plein AND coalesce(niveau_releve_source, '') ILIKE '%autonomie%')) AS niveau_estime, CASE WHEN vehicule IS NOT NULL AND niveau_fin - niveau_pct >= 10 AND kwh_livres > 0 THEN kwh_livres / ((niveau_fin - niveau_pct) / 100.0) END AS capacite_kwh FROM f`;