import{sendToApi as e}from"./lib/send-to-api.js";import{defineSmartComponent as t}from"./lib/smart/define-smart-component.js";import"./cc-smart-container.js";import"./cc-tile-metrics.js";import{getGrafanaOrganisation as a}from"@clevercloud/client/esm/api/v4/saas.js";import{getOrganisationApplicationMetrics as n}from"@clevercloud/client/esm/api/v4/resources.js";function i(e,t){return(e?.find((e=>e.name===t))?.data??[]).map((({timestamp:e,value:t})=>({timestamp:e/1e3,value:parseFloat(t)})))}t({selector:"cc-tile-metrics",params:{apiConfig:{type:Object},ownerId:{type:String},appId:{type:String},grafanaLink:{type:Object,optional:!0}},onContextUpdate({context:t,updateComponent:r,signal:o}){const{apiConfig:p,ownerId:s,appId:c,grafanaLink:m}=t;r("metricsState",{type:"loading"}),function({apiConfig:t,ownerId:a,appId:r,signal:o}){return n({ownerId:a,resourceId:r,interval:"P1D",span:"PT1H",only:["cpu","mem"]}).then(e({apiConfig:t,signal:o})).then((e=>({cpuMetrics:i(e,"cpu"),memMetrics:i(e,"mem")})))}({apiConfig:p,ownerId:s,appId:c,signal:o}).then((({cpuMetrics:e,memMetrics:t})=>{0===e.length||0===t.length||24===e.filter((e=>0===e.value)).length?r("metricsState",{type:"empty"}):r("metricsState",{type:"loaded",metricsData:{cpuMetrics:e,memMetrics:t}})})).catch((e=>{console.error(e),r("metricsState",{type:"error"})})),null==m?r("grafanaLinkState",{type:"hidden"}):(r("grafanaLinkState",{type:"loading"}),function({apiConfig:t,ownerId:n,appId:i,grafanaBaseLink:r,signal:o}){return a({id:n}).then(e({apiConfig:t,signal:o})).then((e=>{const t=new URL("/d/runtime/application-runtime",r);return t.searchParams.set("orgId",e.id),t.searchParams.set("var-SELECT_APP",i),t.toString()}))}({apiConfig:p,ownerId:s,appId:c,grafanaBaseLink:m.base,signal:o}).then((e=>{r("grafanaLinkState",{type:"loaded",link:e})})).catch((()=>{r("grafanaLinkState",{type:"loaded",link:m.console})})))}});
//# sourceMappingURL=cc-tile-metrics.smart.js.map
