Files
Alfredv2/alfred/app/static/js/alfred-logs-v2.js
T

46 lines
3.3 KiB
JavaScript

/* ALFRED_GLASS_LOGS_V2 — aucun HTML de journal injecte, seulement textContent. */
(() => {
'use strict';
const $=id=>document.getElementById(id);
let all=[];let authenticated=false;let pending=false;
const when=new Intl.DateTimeFormat('fr-FR',{dateStyle:'short',timeStyle:'medium',timeZone:'Europe/Paris'});
const el=(tag,cls,text)=>{const e=document.createElement(tag);if(cls)e.className=cls;if(text!=null)e.textContent=String(text);return e;};
async function identity(){try{const r=await fetch('/api/state',{cache:'no-store'});if(!r.ok)return;const d=await r.json();const n=String(d.assistant_name||d.title||'').trim();if(!n)return;$('lgName').textContent=n;$('lgLetter').textContent=Array.from(n)[0].toLocaleUpperCase('fr-FR');document.title='Journaux · '+n;}catch(_){}}
identity();
function render(){
const needle=$('lgSearch').value.trim().toLocaleLowerCase('fr-FR');
const component=$('lgComponent').value,level=$('lgLevel').value;
const filtered=all.filter(x=>(!component||x.component===component)&&(!level||x.level===level)&&(!needle||(x.message||'').toLocaleLowerCase('fr-FR').includes(needle)));
const root=$('lgEvents');root.replaceChildren();
$('lgCount').textContent=`${filtered.length} / ${all.length} événement(s)`;
if(!filtered.length){root.append(el('div','lg-empty',authenticated?'Aucun événement correspondant aux filtres.':'Connecte-toi via la page Configuration pour consulter les journaux.'));return;}
for(const x of filtered.slice().reverse()){
const line=el('div','lg-event');line.dataset.level=x.level;
let date='—';try{date=when.format(new Date(x.at));}catch(_){}
const time=el('time','',date);time.dateTime=x.at;
line.append(time,el('span','lg-component',x.component),el('span','lg-level',x.level),el('span','lg-message',x.message));
root.append(line);
}
}
async function refresh(){
if(pending)return;pending=true;$('lgStatus').textContent='Actualisation…';
try{const r=await fetch('/api/alfred/logs?limit='+encodeURIComponent($('lgLimit').value),{cache:'no-store'});const d=await r.json();
if(r.status===401){authenticated=false;all=[];$('lgStatus').textContent='Accès administrateur requis · Connexion depuis /config';render();return;}
if(!r.ok||!d.ok)throw Error(d.error||`HTTP ${r.status}`);
authenticated=true;all=Array.isArray(d.items)?d.items:[];
$('lgStatus').textContent='Dernière lecture : '+new Date().toLocaleTimeString('fr-FR',{timeZone:'Europe/Paris'});
render();
}catch(e){$('lgStatus').textContent='Journal indisponible : '+e.message;}finally{pending=false;}
}
for(const id of ['lgSearch','lgComponent','lgLevel'])$(id).addEventListener('input',render);
$('lgLimit').addEventListener('change',refresh);
$('lgRefresh').addEventListener('click',refresh);
$('lgExport').addEventListener('click',()=>{
if(!authenticated||!all.length)return;
const file=new Blob([JSON.stringify({source:'alfred.service',exported_at:new Date().toISOString(),events:all},null,2)],{type:'application/json'});
const url=URL.createObjectURL(file);const a=document.createElement('a');a.href=url;a.download='alfred-journaux-'+new Date().toISOString().slice(0,10)+'.json';a.click();URL.revokeObjectURL(url);
});
setInterval(()=>{if($('lgAuto').checked&&!document.hidden)refresh();},10000);
window.addEventListener('focus',refresh);refresh();
})();