Update
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
const $ = id => document.getElementById(id);
|
||||
const isDSM = location.pathname === '/dsm';
|
||||
$('home').hidden = isDSM; $('dsm').hidden = !isDSM;
|
||||
$(isDSM ? 'dsm-nav' : 'home-nav').classList.add('active');
|
||||
$('breadcrumb').textContent = isDSM ? 'DSM' : 'Übersicht';
|
||||
let token = '', demo = false, busy = false, restarting = false, canRestart = false;
|
||||
const history = {down: [], up: []};
|
||||
const number = n => n.toLocaleString('de-DE', {maximumFractionDigits: 1});
|
||||
const bytes = n => {if(n == null) return '—'; const unit = n >= 2**40 ? [2**40,'TiB'] : n >= 2**30 ? [2**30,'GiB'] : n >= 2**20 ? [2**20,'MiB'] : [1024,'KiB']; return `${number(n/unit[0])} ${unit[1]}`;};
|
||||
const escape = s => String(s).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||||
function fixture(){return {cpu:18.4,ram:{total:8*2**30,free:5.2*2**30},interfaces:[{name:'eth0',status:'up',down:24.8*2**20,up:6.3*2**20},{name:'eth1',status:'down',down:0,up:0}],pools:[{name:'vg1000',total:32*2**40,free:4*2**40}],volumes:[{name:'/volume1',total:28*2**40,free:12.6*2**40}],raids:[{name:'md2',level:'raid6',status:'OK',action:'idle'}],disks:Array.from({length:8},(_,i)=>({name:`sata${i+1}`,model:'Beispiellaufwerk · 8 TB',status:'OK',temperature:32+i%4})),dns:'Demo · Läuft',timestamp:Date.now()/1000,storage_age:0};}
|
||||
function chart(key, color){const c=$(key+'-chart'),ctx=c.getContext('2d'),arr=history[key];ctx.clearRect(0,0,c.width,c.height); if(arr.length<2)return;const max=Math.max(...arr,1);ctx.beginPath();arr.forEach((v,i)=>{const x=i*300/59,y=44-v/max*38;i?ctx.lineTo(x,y):ctx.moveTo(x,y);});ctx.strokeStyle=color;ctx.lineWidth=2;ctx.stroke();}
|
||||
function render(d){
|
||||
$('cpu').textContent=d.cpu==null?'—':number(d.cpu)+' %';$('cpu-bar').value=d.cpu||0;
|
||||
const ram=d.ram.total&&d.ram.free!=null?100*(1-d.ram.free/d.ram.total):null;
|
||||
$('ram').textContent=ram==null?'—':number(ram)+' %';$('ram-bar').value=ram||0;
|
||||
$('ram-detail').textContent=ram==null?'Nicht verfügbar':`${bytes(d.ram.total-d.ram.free)} von ${bytes(d.ram.total)}`;
|
||||
for(const [key,id,color] of [['down','download','#65dfb3'],['up','upload','#80b6ff']]){const measured=d.interfaces.filter(n=>n[key]!=null);const sum=measured.length?measured.reduce((a,n)=>a+n[key],0):null;$(id).textContent=sum==null?'—':bytes(sum)+'/s';if(sum!=null){history[key].push(sum);if(history[key].length>60)history[key].shift();}chart(key,color);}
|
||||
$('network').innerHTML=d.interfaces.map(n=>`<div class="network-row"><div>${escape(n.name)}<small class="${n.status==='up'?'ok':'warn'}">${escape(n.status==='up'?'Verbunden':n.status==='down'?'Getrennt':n.status)}</small></div><div>↓ ${bytes(n.down)}/s<small>↑ ${bytes(n.up)}/s</small></div></div>`).join('')||'<p class="quiet">Keine physischen Schnittstellen verfügbar.</p>';
|
||||
$('storage').innerHTML=[...d.pools.map(p=>({...p,label:'Pool / LVM'})),...d.volumes.map(p=>({...p,label:'Volume'}))].map(p=>`<div class="storage-row"><div class="row"><span>${escape(p.name)}</span><small>${p.label}</small></div><progress max="100" value="${p.total?100*(1-p.free/p.total):0}"></progress><div class="row"><small>${bytes(p.total-p.free)} von ${bytes(p.total)}</small><small>${bytes(p.free)} frei</small></div></div>`).join('')+(d.pools.length?'':'<p class="quiet">Pool-Kapazität nicht verfügbar.</p>')+d.raids.map(r=>`<p class="quiet">${escape(r.name)} · ${escape(r.level)} · <span class="${r.status==='OK'?'ok':'warn'}">${escape(r.status)}</span> · ${escape(r.action)}</p>`).join('');
|
||||
$('disks').innerHTML=d.disks.map(x=>`<tr><td>${escape(x.name)}</td><td>${escape(x.model)}</td><td class="${x.status==='OK'?'ok':'warn'}">${escape(x.status)}</td><td>${x.temperature==null?'—':escape(x.temperature)+' °C'}</td></tr>`).join('')||'<tr><td colspan="4">Keine Laufwerksdaten verfügbar.</td></tr>';
|
||||
$('dns-status').textContent=d.dns;$('updated').textContent=new Date(d.timestamp*1000).toLocaleTimeString('de-DE');
|
||||
canRestart=!!d.can_restart; $('restart').disabled=demo||restarting||!canRestart;
|
||||
}
|
||||
async function refresh(){if(busy)return;busy=true;try{const response=await fetch('/api/status',{headers:{Authorization:'Bearer '+token},signal:AbortSignal.timeout(90000)});if(response.status===401){$('login').hidden=false;throw Error('Verbinde dein NAS mit dem Zugriffsschlüssel.');}if(!response.ok)throw Error('Messwerte konnten nicht geladen werden.');let data=await response.json();demo=!!data.demo;if(demo)data=fixture();$('connection').textContent=demo?'DEMO · Beispieldaten':'LIVE · Verbunden';$('notice').textContent=demo?'VORSCHAU · Alle Werte sind Beispieldaten. Es besteht keine Verbindung zu deinem NAS. DNS-Neustart ist deaktiviert.':'';$('login').hidden=true;render(data);}catch(e){$('connection').textContent='Nicht verbunden';$('notice').textContent=e.message+' Angezeigte Werte sind gegebenenfalls veraltet.';$('restart').disabled=true;}finally{busy=false;}}
|
||||
$('login-form').addEventListener('submit',e=>{e.preventDefault();token=$('token').value;$('token').value='';refresh();});
|
||||
$('restart').onclick=()=>$('confirm').showModal();$('cancel').onclick=()=>$('confirm').close();
|
||||
$('confirm-restart').onclick=async()=>{$('confirm').close();restarting=true;$('restart').disabled=true;$('dns-status').textContent='Neustart läuft …';try{const r=await fetch('/api/dns/restart',{method:'POST',headers:{Authorization:'Bearer '+token},signal:AbortSignal.timeout(30000)});const data=await r.json();$('action-result').textContent=data.message||data.error;}catch(e){$('action-result').textContent='Keine Bestätigung erhalten. DNS-Status in DSM prüfen, bevor du erneut startest.';}finally{restarting=false;$('restart').disabled=demo||!canRestart;}};
|
||||
refresh();setInterval(refresh,5000);
|
||||
Reference in New Issue
Block a user