"use strict";(()=>{var e={};e.id=5148,e.ids=[5148],e.modules={7849:e=>{e.exports=require("next/dist/client/components/action-async-storage.external")},2934:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},5403:e=>{e.exports=require("next/dist/client/components/request-async-storage.external")},4580:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},4749:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external")},5869:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},399:e=>{e.exports=require("next/dist/compiled/next-server/app-page.runtime.prod.js")},7147:e=>{e.exports=require("fs")},1017:e=>{e.exports=require("path")},1142:(e,t,s)=>{s.r(t),s.d(t,{GlobalError:()=>i.a,__next_app__:()=>m,originalPathname:()=>x,pages:()=>o,routeModule:()=>p,tree:()=>c});var a=s(482),r=s(9108),n=s(2563),i=s.n(n),d=s(8300),l={};for(let e in d)0>["default","tree","pages","GlobalError","originalPathname","__next_app__","routeModule"].indexOf(e)&&(l[e]=()=>d[e]);s.d(t,l);let c=["",{children:["(dashboard)",{children:["admin",{children:["usage",{children:["__PAGE__",{},{page:[()=>Promise.resolve().then(s.bind(s,6672)),"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\(dashboard)\\admin\\usage\\page.tsx"]}]},{}]},{}]},{layout:[()=>Promise.resolve().then(s.bind(s,1824)),"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\(dashboard)\\layout.tsx"]}]},{layout:[()=>Promise.resolve().then(s.bind(s,1342)),"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\layout.tsx"],"not-found":[()=>Promise.resolve().then(s.bind(s,8206)),"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\not-found.tsx"]}],o=["C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\(dashboard)\\admin\\usage\\page.tsx"],x="/(dashboard)/admin/usage/page",m={require:s,loadChunk:()=>Promise.resolve()},p=new a.AppPageRouteModule({definition:{kind:r.x.APP_PAGE,page:"/(dashboard)/admin/usage/page",pathname:"/admin/usage",bundlePath:"",filename:"",appPaths:[]},userland:{loaderTree:c}})},6672:(e,t,s)=>{s.r(t),s.d(t,{default:()=>d});var a=s(5036),r=s(2509),n=s(9829),i=s(867);async function d(){let e=await (0,r.e)(),{data:{user:t}}=await e.auth.getUser();t||(0,i.redirect)("/login"),await (0,n.j)(t.id)||(0,i.redirect)("/dashboard");let s=(0,r.createAdminClient)(),d=new Date;d.setHours(0,0,0,0),d.toISOString();let l=new Date(d);l.setDate(l.getDate()-30);let c=l.toISOString(),[o,x,m,p]=await Promise.all([s.from("licenses").select("*",{count:"exact",head:!0}).eq("status","active"),s.from("sites").select("*",{count:"exact",head:!0}).eq("status","active"),s.from("usage_events").select("total_tokens",{count:"exact"}).gte("created_at",c),s.from("usage_events").select(`
        site_id,
        total_tokens,
        type,
        sites (
          id,
          license_id,
          licenses (
            id,
            license_key,
            customer_email,
            tenant_id,
            tenants (
              id,
              name
            )
          )
        )
      `).gte("created_at",c)]),h=new Map;p.data&&p.data.forEach(e=>{let t=Array.isArray(e.sites)?e.sites[0]:e.sites,s=Array.isArray(t?.licenses)?t.licenses[0]:t?.licenses;if(!s||!s.id)return;let a=s.id,r=Array.isArray(s.tenants)?s.tenants[0]:s.tenants,n=h.get(a);n?(n.total_tokens+=e.total_tokens||0,"chat"===e.type?n.chat_requests+=1:"embedding"===e.type&&(n.embedding_requests+=1)):h.set(a,{license_id:a,license_key:s.license_key||"N/A",customer_email:s.customer_email||null,tenant_name:r?.name||"N/A",total_tokens:e.total_tokens||0,chat_requests:"chat"===e.type?1:0,embedding_requests:"embedding"===e.type?1:0})});let g=Array.from(h.values()).sort((e,t)=>t.total_tokens-e.total_tokens),u=g.reduce((e,t)=>e+t.total_tokens,0),y=g.reduce((e,t)=>e+t.chat_requests,0),j=g.reduce((e,t)=>e+t.embedding_requests,0);return(0,a.jsxs)("div",{className:"space-y-6",children:[a.jsx("div",{className:"flex items-center justify-between",children:(0,a.jsxs)("div",{children:[a.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:"Usage Analytics"}),a.jsx("p",{className:"text-gray-600 mt-1",children:"Monitor token usage and API requests across all licenses"})]})}),(0,a.jsxs)("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-6",children:[(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Active Licenses"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:o.count||0})]}),(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Active Sites"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:x.count||0})]}),(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Total Tokens (30d)"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:u.toLocaleString()})]}),(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Chat Requests (30d)"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:y.toLocaleString()})]})]}),(0,a.jsxs)("div",{className:"card-modern overflow-hidden",children:[(0,a.jsxs)("div",{className:"p-6 border-b border-gray-200",children:[a.jsx("h2",{className:"text-lg font-semibold text-gray-900",children:"Usage by License (Last 30 Days)"}),a.jsx("p",{className:"text-sm text-gray-600 mt-1",children:"Token usage and API requests per license, sorted by usage"})]}),a.jsx("div",{className:"overflow-x-auto",children:(0,a.jsxs)("table",{className:"min-w-full divide-y divide-gray-200",children:[a.jsx("thead",{className:"bg-gray-50",children:(0,a.jsxs)("tr",{children:[a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"License Key"}),a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Customer Email"}),a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Tenant"}),a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Total Tokens"}),a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Chat Requests"}),a.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Embedding Requests"})]})}),a.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:g.length>0?g.map(e=>(0,a.jsxs)("tr",{className:"hover:bg-gray-50",children:[a.jsx("td",{className:"px-6 py-4",children:a.jsx("code",{className:"text-sm font-mono text-gray-900 break-all",children:e.license_key})}),a.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:a.jsx("div",{className:"text-sm text-gray-900",children:e.customer_email?a.jsx("a",{href:`mailto:${e.customer_email}`,className:"text-indigo-600 hover:text-indigo-800 hover:underline",children:e.customer_email}):a.jsx("span",{className:"text-gray-400 italic",children:"No email"})})}),a.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:a.jsx("div",{className:"text-sm text-gray-900",children:e.tenant_name})}),a.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:a.jsx("div",{className:"text-sm font-semibold text-gray-900",children:e.total_tokens.toLocaleString()})}),a.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",children:e.chat_requests.toLocaleString()}),a.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",children:e.embedding_requests.toLocaleString()})]},e.license_id)):a.jsx("tr",{children:(0,a.jsxs)("td",{colSpan:6,className:"px-6 py-12 text-center text-gray-500",children:[a.jsx("p",{children:"No usage data found for the last 30 days."}),a.jsx("p",{className:"text-sm mt-2",children:"Usage data is recorded when chat messages or embeddings are processed."})]})})})]})})]}),g.length>0&&(0,a.jsxs)("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Total Embedding Requests"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:j.toLocaleString()})]}),(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Avg Tokens per Request"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:y>0?Math.round(u/y).toLocaleString():"0"})]}),(0,a.jsxs)("div",{className:"card-modern p-6",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-500 mb-2",children:"Licenses with Usage"}),a.jsx("p",{className:"text-3xl font-bold text-gray-900",children:g.length})]})]})]})}},9829:(e,t,s)=>{s.d(t,{j:()=>r});var a=s(2509);async function r(e){try{let t=(0,a.createAdminClient)(),{data:s,error:r}=await t.from("platform_users").select("role").eq("user_id",e).eq("role","super_admin").single();if(r||!s)return!1;return"super_admin"===s.role}catch(e){return console.error("Error checking super admin status:",e),!1}}}};var t=require("../../../../webpack-runtime.js");t.C(e);var s=e=>t(t.s=e),a=t.X(0,[1638,9989,665,1476,6040,867,9599],()=>s(1142));module.exports=a})();