81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { getMarches } = require('../services/baserow');
|
|
|
|
const DELAI_CRITIQUE = 45;
|
|
const DELAI_ATTENTION = 90;
|
|
|
|
function parseNum(v) {
|
|
const n = parseFloat(String(v || '').replace(',', '.'));
|
|
return isNaN(n) ? 0 : n;
|
|
}
|
|
|
|
function getDelaiRestant(r) {
|
|
if (r.delai_restant != null) return parseInt(r.delai_restant, 10);
|
|
const fin = r.date_fin || r.datefin;
|
|
if (!fin) return null;
|
|
const d = new Date(fin);
|
|
if (isNaN(d.getTime())) return null;
|
|
return Math.ceil((d - new Date()) / 86400000);
|
|
}
|
|
|
|
function isCloture(r) {
|
|
const obs = String(r.observation || '').toLowerCase();
|
|
return obs.includes('clôtur') || obs.includes('clotur') || !!r.date_cloture;
|
|
}
|
|
|
|
// GET /api/stats
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const rows = await getMarches();
|
|
|
|
const actifs = rows.filter(r => !isCloture(r));
|
|
|
|
// Nb marchés par statut
|
|
const parStatut = {};
|
|
for (const r of rows) {
|
|
const s = String(r.statut || 'Inconnu');
|
|
parStatut[s] = (parStatut[s] || 0) + 1;
|
|
}
|
|
|
|
// Taux d'avancement physique moyen (marchés actifs)
|
|
const tauxList = actifs.map(r => parseNum(r.taux_phy)).filter(v => v > 0);
|
|
const tauxMoyen = tauxList.length
|
|
? Math.round(tauxList.reduce((a, b) => a + b, 0) / tauxList.length * 10) / 10
|
|
: 0;
|
|
|
|
// Alertes délais
|
|
const alertes = actifs
|
|
.map(r => ({ ...r, _delai: getDelaiRestant(r) }))
|
|
.filter(r => r._delai !== null && r._delai <= DELAI_ATTENTION)
|
|
.map(r => ({
|
|
id: r.id,
|
|
ref: r.ref || r.reference || '',
|
|
entrepreneur: r.entrepreneur || '',
|
|
projet: r.projet || '',
|
|
region: r.region || '',
|
|
avt_fin: parseNum(r.avt_fin ?? r.avtfin),
|
|
delai_restant: r._delai,
|
|
niveau: r._delai <= DELAI_CRITIQUE ? 'critique' : 'attention',
|
|
}));
|
|
|
|
res.json({
|
|
total: rows.length,
|
|
actifs: actifs.length,
|
|
clotures: rows.length - actifs.length,
|
|
par_statut: parStatut,
|
|
taux_avancement_moyen: tauxMoyen,
|
|
alertes_delais: {
|
|
count: alertes.length,
|
|
critique: alertes.filter(a => a.niveau === 'critique').length,
|
|
attention: alertes.filter(a => a.niveau === 'attention').length,
|
|
items: alertes,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|