import{isEmpty,humanSize,humanDuration}from"../base/helpers.mjs";import{View}from"./base.mjs";import{ElementBuilder}from"../base/builder.mjs";import{ColorScale,getTextColorForBackground}from"../graphics/colors.mjs";const E=new ElementBuilder({statusIcon:"enfugue-status-icon",statusVersion:"enfugue-status-version",statusUptime:"enfugue-status-uptime",statusGpuName:"enfugue-status-gpu-name",statusGpuTemp:"enfugue-status-gpu-temp",statusGpuLoad:"enfugue-status-gpu-load",statusGpuMemory:"enfugue-status-gpu-memory"}),statusColorScale=new ColorScale([[0,255,0],[255,255,0],[255,0,0]]);class StatusView extends View{static tagName="enfugue-status";constructor(t,s){super(t),this.status=s,this.statusClickCallbacks=[]}onStatusClicked(t){this.statusClickCallbacks.push(t)}async statusClicked(){if(!isEmpty(this.status)&&"error"!==this.label&&"idle"!==this.label)for(let t of this.statusClickCallbacks)await t()}get version(){return this.status.version}get uptime(){return humanDuration(this.status.uptime)}get label(){return this.status.status}get gpuLoad(){return Math.ceil(100*this.status.gpu.load)}get gpuMemoryTotal(){return this.status.gpu.memory.total}get gpuMemoryFree(){return this.status.gpu.memory.free}get gpuMemoryUsed(){return this.status.gpu.memory.used}get gpuMemoryUtil(){return Math.ceil(this.gpuMemoryUsed/this.gpuMemoryTotal*100)}get gpuMemoryUsage(){return`${humanSize(1e6*this.gpuMemoryUsed)} / ${humanSize(1e6*this.gpuMemoryTotal)}`}get gpuName(){return this.status.gpu.name}get gpuTemp(){return this.status.gpu.temp}updateStatus(t){"error"===t?this.icon.class("error").data("tooltip","Engine status is <strong>indeterminable</strong>"):(this.status=t,void 0!==this.node&&this.updateNodeStatus())}updateNodeStatus(){let t=this.node.find(E.getCustomTag("statusIcon")),s=this.node.find(E.getCustomTag("statusVersion")),e=this.node.find(E.getCustomTag("statusUptime")),u=this.node.find(E.getCustomTag("statusGpuName")),a=this.node.find(E.getCustomTag("statusGpuTemp")),o=this.node.find(E.getCustomTag("statusGpuLoad")),i=this.node.find(E.getCustomTag("statusGpuMemory"));this.setStatusNodeDetails(t,s,e,u,a,o,i)}setStatusNodeDetails(t,s,e,u,a,o,i){let r=statusColorScale.get((this.gpuTemp-50)/50).join(","),n=statusColorScale.get(this.gpuLoad/100).join(","),g=`rgba(${statusColorScale.get(this.gpuMemoryUtil/100).join(",")}, 0.6)`;t.class(this.label).data("tooltip",`Engine status is <strong>${this.label}</strong>`),s.show().content(this.version),e.show().content(this.uptime),u.show().content(this.gpuName),a.show().content(`${this.gpuTemp}`).css("color",`rgb(${r})`),o.show().content(`${this.gpuLoad}`).css("color",`rgb(${n})`),i.show().content(this.gpuMemoryUsage).css({"background-image":`linear-gradient(to right, ${g} 0%, ${g} ${this.gpuMemoryUtil}%, transparent calc(${this.gpuMemoryUtil}% + 1px))`})}async build(){let t=await super.build(),s=E.statusIcon().on("click",(()=>this.statusClicked())),e=E.statusVersion(),u=E.statusUptime(),a=E.statusGpuName(),o=E.statusGpuTemp(),i=E.statusGpuLoad(),r=E.statusGpuMemory();return isEmpty(this.status)?(s.addClass("unknown").data("tooltip","Engine status is <strong>unknown</strong>"),e.hide(),u.hide(),a.hide(),o.hide(),i.hide(),r.hide()):this.setStatusNodeDetails(s,e,u,a,o,i,r),t.content(s,e,u,a,o,i,r)}}export{StatusView};
