(function(){
var csrf='';
var pollTimer=null;
var statusEl=null;
var outputEl=null;
function init(){
var m=document.querySelector('meta[name="csrf-token"]');
csrf=m?m.content:'';
statusEl=document.getElementById('nc-engine-status');
outputEl=document.getElementById('nc-engine-output');
pollStatus();
pollTimer=setInterval(pollStatus,10000);
}
function pollStatus(){
fetch('api/system/engine_status.php',{credentials:'same-origin'})
.then(function(r){return r.json();})
.then(function(d){
if(!statusEl)return;
var running=d.running||d.ok;
statusEl.textContent=running?'Running':'Offline';
statusEl.className='nc-engine-ind '+(running?'up':'down');
})
.catch(function(){
if(statusEl){statusEl.textContent='Offline';statusEl.className='nc-engine-ind down';}
});
}
function log(text,cls){
if(!outputEl)return;
var ts=new Date().toLocaleTimeString();
var line=document.createElement('div');
var d=document.createElement('div');
d.textContent=text;
var safe=d.innerHTML;
line.innerHTML='<span class="nc-ts">['+ts+']</span> '+(cls?'<span class="'+cls+'">':'')+safe+(cls?'</span>':'');
outputEl.appendChild(line);
outputEl.scrollTop=outputEl.scrollHeight;
}
function postCmd(action,service,extra){
var fd=new FormData();
fd.set('_csrf',csrf);
fd.set('action',action);
fd.set('node','669427');
if(service)fd.set('service',service);
if(extra){Object.keys(extra).forEach(function(k){fd.set(k,extra[k]);});}
log(action+(service?' -> '+service:'')+' ...','');
fetch('api/fleet/node_cmd.php',{method:'POST',body:fd,credentials:'same-origin'})
.then(function(r){return r.json();})
.then(function(data){
if(data.needs_pin){
log('Fleet PIN required for: '+action,'nc-err');
promptPin(action,service);
return;
}
if(data.ok){
log(data.output||'OK','nc-ok');
if(data.elapsed)log('-- completed in '+data.elapsed+(data.route?' | route: '+data.route:''),'nc-ts');
}else{
log(data.output||data.error||'Command failed','nc-err');
}
})
.catch(function(e){log('Request failed: '+e.message,'nc-err');});
}
function promptPin(action,service){
var pin=prompt('Enter fleet PIN for: '+action);
if(!pin)return;
postCmd(action,service,{fleet_pin:pin});
}
function cmdHealth(){
log('Checking engine health...','');
fetch('api/system/engine_status.php',{credentials:'same-origin'})
.then(function(r){return r.json();})
.then(function(d){
var parts=[];
Object.keys(d).forEach(function(k){parts.push(k+': '+d[k]);});
log(parts.join(' | ')||'OK','nc-ok');
})
.catch(function(e){log('Health check failed: '+e.message,'nc-err');});
}
function cmdLogs(){
postCmd('logs_tail','amazon-ssm-agent');
}
function cmdConfig(){
postCmd('service_status',null);
}
function cmdRestart(){
if(!confirm('Restart core service on this node?'))return;
postCmd('service_restart','amazon-ssm-agent');
}
document.addEventListener('click',function(e){
var el=e.target.closest('[data-nc="engine-cmd"]');
if(!el)return;
var cmd=el.dataset.cmd;
switch(cmd){
case 'engine_health':cmdHealth();break;
case 'engine_logs':cmdLogs();break;
case 'engine_config':cmdConfig();break;
case 'engine_restart':cmdRestart();break;
}
});
if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}
else{init();}
})();
