import{u as t}from"./experiment-C9fiIMV8.js";import{_ as e,u as s,i as a,h as r,k as i,a as c,e as n,d as l,f as h,o as u,M as o}from"./index-mtICtCH6.js";import{C as p,u as _,a as d,m}from"./chart-C_A2TaVz.js";import"./SLLoading-Ba4myrl9.js";const g={key:0,class:"chart-page"},M={key:0,class:"font-semibold pt-5 text-center"},f=e({__name:"ChartPage",setup(e){const f=s(),v=t(),b=o(),q=f.colors[0],w=(()=>(t,e)=>f.colors[e])();let S=new Map,I=[];const R=a([]),x=a("initing");(async function(){const{data:t}=await r.get(`/experiment/${v.id}/chart`);E(t)})().then((()=>{D.start(),x.value="success"}));const E=t=>{I=t.charts||[],R.value=t.namespaces||[];const e=I.map((t=>(t._cid=A(t.id),D.addSource(t.source),t.id)));let s=!1;const a=R.value;R.value.forEach((t=>{s=!1,t.charts.forEach((t=>{"object"==typeof t&&(t=t.id);for(let a=0;a<e.length;a++){const r=e[a];if(r===t){s=!0,S.set(r,a);break}}if(!s)throw new Error("Charts and groups cannot correspond.")}))})),a.forEach((t=>{t.charts=t.charts.map((t=>"object"==typeof t?I[S.get(t.id)]:I[S.get(t)]))})),R.value=a};class k{constructor(){this._map=new Map}canRequest(t){return!this._map.has(t)||"success"===this._map.get(t)}setPending(t){this._map.set(t,"pending")}setSuccess(t){this._map.set(t,"success")}setError(t){this._map.set(t,"error")}get(t){return this._map.get(t)}}class y{constructor(){this._map=new Map,this._intervalMap=new Map,this._callbackMap=new Map}setInterval(t,e,s=void 0){if(void 0===e)return;if(!s&&!this._callbackMap.get(t))return;let a=1e3;if(e<100)a=1e3;else if(e<500)a=1e3;else if(e<1e3)a=2e3;else{const t=Math.floor((e-1e3)/500);a=Math.min(8e3,2e3+1e3*t)}this._intervalMap.get(t)!==a&&(this.clear(t),s=s||this._callbackMap.get(t),this._map.set(t,setInterval(s,a)),this._callbackMap.set(t,s),this._intervalMap.set(t,a))}clear(t){const e=this._map.get(t);e&&clearInterval(e),this._map.delete(t),this._intervalMap.delete(t),this._callbackMap.delete(t)}clearAll(){this._map.forEach((t=>{clearInterval(t)})),this._map.clear(),this._intervalMap.clear()}}const D=new class{constructor(){this._sources=new Set,this._sourceMap=new Map,this._distributeMap=new Map,this._experiment_id=v.id,this.timer=null,this._timerMap=new y,this._intervalMap=new Map,this._request=new k,this._started=!1}setSourceData(t,e,s){e&&this._sourceMap.set(t,e);const a=this._distributeMap.get(t);if(!a)return this.setRequestStatus(t,s);Promise.all(Array.from(a.values()).map((a=>a(t,e,s)))).then((()=>{var a;this.setRequestStatus(t,s),this._timerMap.setInterval(t,null==(a=null==e?void 0:e.list)?void 0:a.length)}))}addSource(t){(t=t||[]).map((t=>{if(!this._started)return this._sources.add(t);this._sources.has(t)||(this._sources.add(t),this._getTagRequestInterval(t))}))}start(){const t=[];this._sources.forEach((e=>{t.push(this._getSoureceData(e))})),Promise.all(t).then((()=>{v.isRunning&&(this._sources.forEach((t=>{this._getTagRequestInterval(t)})),this._started=!0)}))}_getTagRequestInterval(t){var e;this._timerMap.setInterval(t,(null==(e=this._sourceMap.get(t))?void 0:e.list.length)||0,(()=>{v.charts&&E(v.charts),Number(this._experiment_id)===Number(b.params.experimentId)?(this._getSoureceData(t),v.isRunning||this._timerMap.clearAll()):this._timerMap.clearAll()}))}_getSoureceData(t){if(this._request.canRequest(t))return this._request.setPending(t),r.get(`/experiment/${this._experiment_id}/tag/${t}`).then((e=>{this.setSourceData(t,e.data,null)})).catch((e=>{this.setSourceData(t,null,e)}))}setRequestStatus(t,e){e?this._request.setError(t):this._request.setSuccess(t)}$on(t,e,s){(t=t||[]).map((t=>{const a=this._distributeMap.get(t);return a?a.set(e,s):this._distributeMap.set(t,new Map([[e,s]])),this._request.canRequest(t)?this._getSoureceData(t):"error"===this._request.get(t)?s(t,null,!0):void 0}))}$off(t,e){(null==this?void 0:this._distributeMap)&&(t=t||[]).map((t=>{const s=this._distributeMap.get(t);s&&s.delete(e)}))}delete(){this._timerMap.clearAll()}},$=(t,e,s)=>D.$on(t,e,s),j=(t,e)=>D.$off(t,e);i((()=>{D.delete()}));const A=t=>t;return(t,e)=>"success"===x.value?(u(),c("div",g,[n(p,{groups:R.value,"update-chart-status":l(_),"update-namespace-status":l(d),media:l(m),"default-color":l(q),"get-color":l(w),subscribe:$,unsubscribe:j},null,8,["groups","update-chart-status","update-namespace-status","media","default-color","get-color"]),0===R.value.length?(u(),c("p",M,"Empty Chart")):h("",!0)])):h("",!0)}},[["__scopeId","data-v-dc50ebdf"]]);export{f as default};
