p3x-redis-ui-server
Version:
🏍️ P3X Redis UI server — Socket.IO backend for the dual Angular + React frontend with AI queries, 54 languages, and auto data decompression
2 lines (1 loc) • 2.19 kB
JavaScript
export default async e=>{const{socket:t,payload:o}=e;try{const s=t.p3xrs.ioredis;if(!s)return void t.emit(e.responseEvent,{status:"error",error:"Not connected to Redis"});const n=o.maxScanKeys||5e3,r=o.topN||20,a=500,i=await s.dbsize(),m=await new Promise((e,t)=>{const o=[],r=s.scanStream({count:500});r.on("data",e=>{o.length<n&&o.push(...e)}),r.on("end",()=>e(o.slice(0,n))),r.on("error",t)}),u={},d={},c={},p=[];let l=0,y=0,_=0;for(let e=0;e<m.length;e+=a){const t=m.slice(e,e+a),o=s.pipeline();for(const e of t)o.type(e),o.call("MEMORY","USAGE",e),o.ttl(e);const n=await o.exec();for(let e=0;e<t.length;e++){const o=t[e],s=n[3*e][0],r=n[3*e][1]||"unknown",a=n[3*e+1][0],i=n[3*e+1][1],m=n[3*e+2][0],f=n[3*e+2][1],h=s?"unknown":r,v=a||"number"!=typeof i?0:i;u[h]=(u[h]||0)+1,d[h]=(d[h]||0)+v;const k=o.indexOf(":"),b=k>0?o.substring(0,k+1):"(no prefix)";c[b]||(c[b]={keyCount:0,totalBytes:0}),c[b].keyCount++,c[b].totalBytes+=v,m||"number"!=typeof f?y++:f>=0?(l++,_+=f):y++,v>0&&p.push({key:o,bytes:v,type:h})}}p.sort((e,t)=>t.bytes-e.bytes);const f=p.slice(0,r),h=Object.entries(c).map(([e,t])=>({prefix:e,...t})).sort((e,t)=>t.totalBytes-e.totalBytes).slice(0,50),v=await s.info("server"),k={},b={redis_version:"version",redis_mode:"mode",uptime_in_seconds:"uptime"};for(const e of v.split("\r\n")){const[t,o]=e.split(":");t&&b[t]&&(k[b[t]]="uptime_in_seconds"===t?parseInt(o)||0:o||"unknown")}k.mode||(k.mode="standalone");const w=await s.info("memory"),g={},x={used_memory:"used",used_memory_human:"usedHuman",used_memory_rss:"rss",used_memory_rss_human:"rssHuman",used_memory_peak:"peak",used_memory_peak_human:"peakHuman",used_memory_lua:"lua",used_memory_overhead:"overhead",used_memory_dataset:"dataset",mem_fragmentation_ratio:"fragRatio",mem_allocator:"allocator"};for(const e of w.split("\r\n")){const[t,o]=e.split(":");t&&x[t]&&(g[x[t]]=isNaN(o)?o:Number(o))}t.emit(e.responseEvent,{status:"ok",data:{totalScanned:m.length,dbSize:i,typeDistribution:u,typeMemory:d,prefixMemory:h,topKeys:f,expirationOverview:{withTTL:l,persistent:y,avgTTL:l>0?Math.round(_/l):0},memoryInfo:g,serverInfo:k}})}catch(o){console.error(o),t.emit(e.responseEvent,{status:"error",error:o.message})}};