import{_ as c,o as r,h as i,d as n,w as a,F as h,i as u,b as d,f as m,t as p,l as f,bl as g,q as y,X as v,L as S,S as L,T as C,B as _}from"./index-b7f60880.js";import{e as b}from"./eventsource-b9a9b3da.js";const F=e=>(L("data-v-4c845fdd"),e=e(),C(),e),E={key:0,class:"filter-controls mb-2"},w=F(()=>_("div",{id:"term",style:{"background-color":"#1e1e1e",padding:"16px","border-radius":"8px","overflow-y":"auto",height:"100%"}},null,-1)),I={name:"ConsoleDisplayer",data(){return{autoScroll:!0,logColorAnsiMap:{"\x1B[1;34m":"color: #39C5BB; font-weight: bold;","\x1B[1;36m":"color: #00FFFF; font-weight: bold;","\x1B[1;33m":"color: #FFFF00; font-weight: bold;","\x1B[31m":"color: #FF0000;","\x1B[1;31m":"color: #FF0000; font-weight: bold;","\x1B[0m":"color: inherit; font-weight: normal;","\x1B[32m":"color: #00FF00;",default:"color: #FFFFFF;"},logLevels:["DEBUG","INFO","WARNING","ERROR","CRITICAL"],selectedLevels:[0,1,2,3,4],levelColors:{DEBUG:"grey",INFO:"blue-lighten-3",WARNING:"amber",ERROR:"red",CRITICAL:"purple"},localLogCache:[],eventSource:null,retryTimer:null,retryAttempts:0,maxRetryAttempts:10,baseRetryDelay:1e3,lastEventId:null}},computed:{commonStore(){return v()}},props:{historyNum:{type:String,default:"-1"},showLevelBtns:{type:Boolean,default:!0}},watch:{selectedLevels:{handler(){this.refreshDisplay()},deep:!0}},async mounted(){await this.fetchLogHistory(),this.connectSSE()},beforeUnmount(){this.eventSource&&(this.eventSource.close(),this.eventSource=null),this.retryTimer&&(clearTimeout(this.retryTimer),this.retryTimer=null),this.retryAttempts=0},methods:{connectSSE(){this.eventSource&&(this.eventSource.close(),this.eventSource=null),console.log(`正在连接日志流... (尝试次数: ${this.retryAttempts})`);const e=localStorage.getItem("token");this.eventSource=new b.EventSourcePolyfill("/api/live-log",{headers:{Authorization:e?`Bearer ${e}`:""},heartbeatTimeout:3e5,withCredentials:!0}),this.eventSource.onopen=()=>{console.log("日志流连接成功！"),this.retryAttempts=0,this.lastEventId||this.fetchLogHistory()},this.eventSource.onmessage=t=>{try{t.lastEventId&&(this.lastEventId=t.lastEventId);const o=JSON.parse(t.data);this.processNewLogs([o])}catch(o){console.error("解析日志失败:",o)}},this.eventSource.onerror=t=>{if(t.status===401?console.error("鉴权失败 (401)，可能是 Token 过期了。"):console.warn("日志流连接错误:",t),this.eventSource&&(this.eventSource.close(),this.eventSource=null),this.retryAttempts>=this.maxRetryAttempts){console.error("❌ 已达到最大重试次数，停止重连。请刷新页面重试。");return}const o=Math.min(this.baseRetryDelay*Math.pow(2,this.retryAttempts),3e4);console.log(`⏳ ${o}ms 后尝试第 ${this.retryAttempts+1} 次重连...`),this.retryTimer&&(clearTimeout(this.retryTimer),this.retryTimer=null),this.retryTimer=setTimeout(async()=>{this.retryAttempts++,this.lastEventId||await this.fetchLogHistory(),this.connectSSE()},o)}},processNewLogs(e){if(!e||e.length===0)return;let t=!1;if(e.forEach(o=>{this.localLogCache.some(l=>l.time===o.time&&l.data===o.data&&l.level===o.level)||(this.localLogCache.push(o),t=!0,this.isLevelSelected(o.level)&&this.printLog(o.data))}),t){this.localLogCache.sort((s,l)=>s.time-l.time);const o=this.commonStore.log_cache_max_len||200;this.localLogCache.length>o&&this.localLogCache.splice(0,this.localLogCache.length-o)}},async fetchLogHistory(){try{const e=await S.get("/api/log-history");e.data.data.logs&&e.data.data.logs.length>0&&this.processNewLogs(e.data.data.logs)}catch(e){console.error("Failed to fetch log history:",e)}},getLevelColor(e){return this.levelColors[e]||"grey"},isLevelSelected(e){for(let t=0;t<this.selectedLevels.length;++t)if(this.logLevels[this.selectedLevels[t]]===e)return!0;return!1},refreshDisplay(){const e=document.getElementById("term");e&&(e.innerHTML="",this.localLogCache&&this.localLogCache.length>0&&this.localLogCache.forEach(t=>{this.isLevelSelected(t.level)&&this.printLog(t.data)}))},toggleAutoScroll(){this.autoScroll=!this.autoScroll},printLog(e){let t=document.getElementById("term");if(!t)return;let o=document.createElement("pre"),s=this.logColorAnsiMap.default;for(let l in this.logColorAnsiMap)if(e.startsWith(l)){s=this.logColorAnsiMap[l],e=e.replace(l,"").replace("\x1B[0m","");break}o.style=s,o.classList.add("console-log-line","fade-in"),o.innerText=`${e}`,t.appendChild(o),this.autoScroll&&(t.scrollTop=t.scrollHeight)}}},A=Object.assign(I,{setup(e){return(t,o)=>(r(),i("div",null,[e.showLevelBtns?(r(),i("div",E,[n(g,{modelValue:t.selectedLevels,"onUpdate:modelValue":o[0]||(o[0]=s=>t.selectedLevels=s),column:"",multiple:""},{default:a(()=>[(r(!0),i(h,null,u(t.logLevels,s=>(r(),d(f,{key:s,color:t.getLevelColor(s),filter:"",variant:"flat",size:"small","text-color":s==="DEBUG"||s==="INFO"?"black":"white",class:"font-weight-medium"},{default:a(()=>[m(p(s),1)]),_:2},1032,["color","text-color"]))),128))]),_:1},8,["modelValue"])])):y("",!0),w]))}}),N=c(A,[["__scopeId","data-v-4c845fdd"]]);export{N as C};
