@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, run, reset, and migrate commands
3 lines (2 loc) • 3.48 kB
JavaScript
import{a as m}from"../packem_shared/admin-url-DfVehV3e.mjs";import{i as $}from"../packem_shared/command-Bjv4VmYA.mjs";import{s as S}from"../packem_shared/resolve-target-vxaXHmvg.mjs";const M="__lunora_admin__:getFunctionStats",y=10,v=/\/$/u,u=(o,e)=>({calls:o.calls,conflicts:o.conflicts??0,errors:o.errors,lastErrorMessage:o.lastErrorMessage,maxDurationMs:o.maxDurationMs,meanDurationMs:o.calls===0?0:o.totalDurationMs/o.calls,path:o.path,rate:e}),O=(o,e)=>{const t=o.filter(r=>(r.conflicts??0)>0).map(r=>u(r,r.calls===0?0:(r.conflicts??0)/r.calls)).toSorted((r,s)=>s.rate-r.rate||s.conflicts-r.conflicts).slice(0,e),i=o.filter(r=>r.errors>0).map(r=>u(r,r.calls===0?0:r.errors/r.calls)).toSorted((r,s)=>s.rate-r.rate||s.errors-r.errors).slice(0,e),n=o.map(r=>u(r,0)).toSorted((r,s)=>s.maxDurationMs-r.maxDurationMs).slice(0,e);return{errorHotspots:i,latencyOutliers:n,totalFunctions:o.length,writeContention:t}},p=o=>`${(o*100).toFixed(1)}%`,f=o=>o<1e3?`${Math.round(o).toString()}ms`:`${(o/1e3).toFixed(2)}s`,d=(o,e,t,i)=>[o,...e.length===0?[` ${t}`]:e.map(n=>` ${i(n)}`)],N=o=>{const e=t=>t.lastErrorMessage?` — ${t.lastErrorMessage}`:"";return[`Insights over ${o.totalFunctions.toString()} function${o.totalFunctions===1?"":"s"}`,"",...d("Write-conflict hot-spots (OCC contention — candidates for sharding):",o.writeContention,"none — no write conflicts observed",t=>`${t.path} ${t.conflicts.toString()}/${t.calls.toString()} calls (${p(t.rate)})`),"",...d("Error hot-spots:",o.errorHotspots,"none — no errors observed",t=>`${t.path} ${t.errors.toString()}/${t.calls.toString()} calls (${p(t.rate)})${e(t)}`),"",...d("Latency outliers (slowest single call):",o.latencyOutliers,"none — no functions have run",t=>`${t.path} max ${f(t.maxDurationMs)}, mean ${f(t.meanDurationMs)} over ${t.calls.toString()} calls`)].join(`
`)},w=o=>o===void 0||!Number.isFinite(o)||o<=0?y:Math.floor(o),x=async o=>{if(o.prod&&o.url===void 0)return o.logger.error("--prod requires an explicit --url (refusing to report from the implicit localhost worker)"),{code:1};const e=o.token??process.env.LUNORA_ADMIN_TOKEN;if(!e)return o.logger.error("admin token required — pass --token or set LUNORA_ADMIN_TOKEN"),{code:1};const t=m(o.url,o.logger);if(t===void 0)return{code:1};const i=`${t.replace(v,"")}/_lunora/rpc`,n=globalThis.fetch;if(typeof n!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");const r={args:{},functionPath:M};o.shard!==void 0&&(r.shardKey=o.shard),o.logger.info(`POST ${i} -> insights`);const s=await n(i,{body:JSON.stringify(r),headers:{authorization:`Bearer ${e}`,"content-type":"application/json"},method:"POST"}),a=await s.text();if(!s.ok)return o.logger.error(`insights failed: HTTP ${String(s.status)}: ${a}`),{code:1};let l;try{l=JSON.parse(a)}catch{return o.logger.error(`insights failed: worker returned non-JSON: ${a}`),{code:1}}const h=l.result??l,{functions:g}=h;if(!Array.isArray(g))return o.logger.error("insights failed: response carried no `functions` array"),{code:1};const c=O(g,w(o.limit));return o.logger.info(o.json?JSON.stringify(c,void 0,2):N(c)),{code:0,report:c}},k=$(({cwd:o,logger:e,options:t})=>{const i=t.limit===void 0?void 0:Number.parseInt(t.limit,10);return x({json:t.json,limit:i,logger:e,prod:t.prod,shard:t.shard,token:t.token,url:S({cwd:o,prod:t.prod===!0,url:t.url})})});export{O as buildInsightsReport,k as execute,N as formatInsightsReport,x as runInsightsCommand};