1import{r as u,a as x,j as e,dd as W,$,a0 as D,a1 as S,a2 as A,a3 as m,B as L,R as Z,bS as ee,C as n,e as l,b as d,d as c,V as ae,T as U,q as f,c_ as T,s as se,f as te,o as h,X as P,t as re,U as F,a7 as o,b9 as ie}from"./index-CuSEeJFb.js";import{T as ne,a as le,b as g,c as y}from"./tabs-CcPOXtvT.js";import{P as de}from"./progress-B2eMqtVV.js";import{T as ce}from"./trending-down-C0n_6ywh.js";import{R as me,T as oe,a3 as xe,a2 as V}from"./generateCategoricalChart-lXkql-gd.js";import{B as he}from"./BarChart-CPEOGCBJ.js";import{C as ge,X as ye,Y as pe}from"./YAxis-edJjQlh1.js";import"./throttle-Bgu_Mp4s.js";import"./debounce-BH2V1_7f.js";import"./tiny-invariant-BaFNuDhB.js";import"./string-CFFdVFMV.js";function Te(){const[t,q]=u.useState("30d"),[r,K]=u.useState("all"),[N,v]=u.useState(!1),{data:b=[],isLoading:k,refetch:I}=x({queryKey:["/api/admin/analytics/kpis",t,r],queryFn:async()=>(await o(`/api/admin/analytics/kpis?timeRange=${t}&selectedCompany=${r}`)).json(),retry:!1}),{data:i,isLoading:O,refetch:E}=x({queryKey:["/api/admin/analytics/system-health",t],queryFn:async()=>(await o(`/api/admin/analytics/system-health?timeRange=${t}`)).json(),retry:!1}),{data:w=[],isLoading:B,refetch:H}=x({queryKey:["/api/admin/analytics/platform-roi",t,r],queryFn:async()=>(await o(`/api/admin/analytics/platform-roi?timeRange=${t}&selectedCompany=${r}`)).json(),retry:!1}),{data:C=[],isLoading:_,refetch:M}=x({queryKey:["/api/admin/analytics/anomalies",r],queryFn:async()=>(await o(`/api/admin/analytics/anomalies?selectedCompany=${r}`)).json(),retry:!1}),{data:je,isLoading:ue,refetch:X}=x({queryKey:["/api/admin/analytics/campaigns",t,r],queryFn:async()=>(await o(`/api/admin/analytics/campaigns?timeRange=${t}&selectedCompany=${r}`)).json(),retry:!1}),{data:fe,isLoading:Ne,refetch:z}=x({queryKey:["/api/admin/analytics/user-engagement",t,r],queryFn:async()=>(await o(`/api/admin/analytics/user-engagement?timeRange=${t}&selectedCompany=${r}`)).json(),retry:!1}),Y=async()=>{v(!0);try{await o("/api/admin/analytics/refresh",{method:"POST"}),await Promise.all([I(),E(),H(),M(),X(),z()])}catch(a){console.error("Error refreshing analytics:",a)}finally{v(!1)}},j=a=>{const s=typeof a=="string"?parseFloat(a):a;return new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:0,maximumFractionDigits:0}).format(s)},R=a=>`${(typeof a=="string"?parseFloat(a):a).toFixed(1)}%`,G=a=>{switch(a){case"financial":return e.jsx(ie,{className:"h-5 w-5"});case"user_engagement":return e.jsx(F,{className:"h-5 w-5"});case"system_health":return e.jsx(f,{className:"h-5 w-5"});default:return e.jsx(U,{className:"h-5 w-5"})}},Q=(a,s)=>a==="improving"?"text-green-600 bg-green-50":a==="declining"?"text-red-600 bg-red-50":s==="up"?"text-blue-600 bg-blue-50":s==="down"?"text-orange-600 bg-orange-50":"text-gray-600 bg-gray-50",J=a=>{switch(a){case"critical":return"bg-red-100 text-red-800 border-red-200";case"high":return"bg-orange-100 text-orange-800 border-orange-200";case"medium":return"bg-yellow-100 text-yellow-800 border-yellow-200";case"low":return"bg-blue-100 text-blue-800 border-blue-200";default:return"bg-gray-100 text-gray-800 border-gray-200"}},p=(a,s)=>s==="uptime"?a>=99?"text-green-600":a>=95?"text-yellow-600":"text-red-600":s==="error"?a<=1?"text-green-600":a<=5?"text-yellow-600":"text-red-600":s==="usage"?a<=70?"text-green-600":a<=85?"text-yellow-600":"text-red-600":"text-gray-600";return e.jsx(W,{children:e.jsxs("div",{className:"space-y-6","data-testid":"admin-analytics-dashboard",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900 dark:text-gray-100","data-testid":"dashboard-title",children:"ð Advanced Analytics Dashboard"}),e.jsx("p",{className:"text-gray-600 dark:text-gray-400",children:"Comprehensive insights into campaign performance, user engagement, and system health"})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs($,{value:t,onValueChange:q,"data-testid":"select-timerange",children:[e.jsx(D,{className:"w-32",children:e.jsx(S,{})}),e.jsxs(A,{children:[e.jsx(m,{value:"7d",children:"Last 7 days"}),e.jsx(m,{value:"30d",children:"Last 30 days"}),e.jsx(m,{value:"90d",children:"Last 90 days"}),e.jsx(m,{value:"1y",children:"Last year"})]})]}),e.jsxs($,{value:r,onValueChange:K,"data-testid":"select-company",children:[e.jsx(D,{className:"w-40",children:e.jsx(S,{placeholder:"All Companies"})}),e.jsxs(A,{children:[e.jsx(m,{value:"all",children:"All Companies"}),e.jsx(m,{value:"1",children:"Company A"}),e.jsx(m,{value:"2",children:"Company B"})]})]}),e.jsxs(L,{onClick:Y,disabled:N,variant:"outline",size:"sm","data-testid":"button-refresh",children:[e.jsx(Z,{className:`h-4 w-4 mr-2 ${N?"animate-spin":""}`}),"Refresh"]}),e.jsxs(L,{variant:"outline",size:"sm","data-testid":"button-export",children:[e.jsx(ee,{className:"h-4 w-4 mr-2"}),"Export"]})]})]}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6",children:k?Array.from({length:4}).map((a,s)=>e.jsx(n,{className:"relative overflow-hidden",children:e.jsx(l,{className:"p-6",children:e.jsxs("div",{className:"animate-pulse space-y-3",children:[e.jsx("div",{className:"h-4 bg-gray-200 dark:bg-gray-700 rounded w-24"}),e.jsx("div",{className:"h-8 bg-gray-200 dark:bg-gray-700 rounded w-32"}),e.jsx("div",{className:"h-3 bg-gray-200 dark:bg-gray-700 rounded w-20"})]})})},s)):b.length>0?b.map(a=>e.jsxs(n,{className:"relative overflow-hidden","data-testid":`kpi-card-${a.kpiName}`,children:[e.jsxs(d,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(c,{className:"text-sm font-medium text-gray-600 dark:text-gray-400",children:a.displayName}),e.jsx("div",{className:`p-2 rounded-md ${Q(a.trend,a.changeDirection)}`,children:G(a.kpiCategory)})]}),e.jsxs(l,{children:[e.jsx("div",{className:"text-2xl font-bold text-gray-900 dark:text-gray-100","data-testid":`kpi-value-${a.kpiName}`,children:a.format==="currency"?j(a.currentValue):a.format==="percentage"?R(a.currentValue):a.currentValue.toLocaleStr
1ing()}),a.previousValue&&e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[a.changeDirection==="up"?e.jsx(ae,{className:"h-4 w-4 text-green-600"}):a.changeDirection==="down"?e.jsx(ce,{className:"h-4 w-4 text-red-600"}):null,e.jsxs("span",{className:`text-sm ${a.trend==="improving"?"text-green-600":a.trend==="declining"?"text-red-600":"text-gray-600"}`,"data-testid":`kpi-change-${a.kpiName}`,children:[a.changePercent>0?"+":"",a.changePercent.toFixed(1),"% vs last period"]})]}),a.targetValue&&e.jsxs("div",{className:"mt-2",children:[e.jsxs("div",{className:"flex justify-between text-sm text-gray-600 dark:text-gray-400 mb-1",children:[e.jsx("span",{children:"Target"}),e.jsx("span",{children:a.format==="currency"?j(a.targetValue):a.format==="percentage"?R(a.targetValue):a.targetValue})]}),e.jsx(de,{value:Math.min(100,parseFloat(a.currentValue)/parseFloat(a.targetValue)*100),className:"h-2"})]})]})]},a.id)):e.jsx("div",{className:"col-span-full",children:e.jsx(n,{children:e.jsxs(l,{className:"p-6 text-center",children:[e.jsx(U,{className:"h-12 w-12 mx-auto mb-4 text-gray-400"}),e.jsx("p",{className:"text-gray-500",children:"No KPI data available"}),e.jsx("p",{className:"text-sm text-gray-400 mt-1",children:"Click Refresh to load analytics data"})]})})})}),e.jsxs(ne,{defaultValue:"overview",className:"w-full",children:[e.jsxs(le,{className:"grid w-full grid-cols-6",children:[e.jsx(g,{value:"overview","data-testid":"tab-overview",children:"Overview"}),e.jsx(g,{value:"campaigns","data-testid":"tab-campaigns",children:"Campaigns"}),e.jsx(g,{value:"platforms","data-testid":"tab-platforms",children:"Platform ROI"}),e.jsx(g,{value:"users","data-testid":"tab-users",children:"User Engagement"}),e.jsx(g,{value:"system","data-testid":"tab-system",children:"System Health"}),e.jsx(g,{value:"anomalies","data-testid":"tab-anomalies",children:"Anomalies"})]}),e.jsxs(y,{value:"overview",className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs(n,{"data-testid":"card-system-health",children:[e.jsx(d,{children:e.jsxs(c,{className:"flex items-center gap-2",children:[e.jsx(f,{className:"h-5 w-5"}),"System Health Overview"]})}),e.jsx(l,{children:e.jsx("div",{className:"space-y-4",children:O?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"animate-pulse bg-gray-200 dark:bg-gray-700 h-4 rounded"}),e.jsx("div",{className:"animate-pulse bg-gray-200 dark:bg-gray-700 h-4 rounded"}),e.jsx("div",{className:"animate-pulse bg-gray-200 dark:bg-gray-700 h-4 rounded"})]}):i?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex justify-between items-center","data-testid":"metric-uptime",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:"Uptime"}),e.jsxs("span",{className:`font-medium ${p(i.uptime,"uptime")}`,"data-testid":"value-uptime",children:[i.uptime,"%"]})]}),e.jsxs("div",{className:"flex justify-between items-center","data-testid":"metric-error-rate",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:"Error Rate"}),e.jsxs("span",{className:`font-medium ${p(i.errorRate,"error")}`,"data-testid":"value-error-rate",children:[i.errorRate,"%"]})]}),e.jsxs("div",{className:"flex justify-between items-center","data-testid":"metric-cpu-usage",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:"CPU Usage"}),e.jsxs("span",{className:`font-medium ${p(i.cpuUsage,"usage")}`,"data-testid":"value-cpu-usage",children:[i.cpuUsage,"%"]})]}),e.jsxs("div",{className:"flex justify-between items-center","data-testid":"metric-memory-usage",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:"Memory Usage"}),e.jsxs("span",{className:`font-medium ${p(i.memoryUsage,"usage")}`,"data-testid":"value-memory-usage",children:[i.memoryUsage,"%"]})]}),e.jsxs("div",{className:"flex justify-between items-center","data-testid":"metric-ai-uptime",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:"AI Service Uptime"}),e.jsxs("span",{className:`font-medium ${p(i.aiServiceUptime,"uptime")}`,"data-testid":"value-ai-uptime",children:[i.aiServiceUptime,"%"]})]})]}):e.jsx("div",{className:"text-sm text-gray-500",children:"No system health data available"})})})]}),e.jsxs(n,{"data-testid":"card-recent-anomalies",children:[e.jsx(d,{children:e.jsxs(c,{className:"flex items-center gap-2",children:[e.jsx(T,{className:"h-5 w-5"}),"Recent Anomalies"]})}),e.jsx(l,{children:e.jsx("div",{className:"space-y-3",children:_?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"animate-pulse bg-gray-200 dark:bg-gray-700 h-16 rounded"}),e.jsx("div",{className:"animate-pulse bg-gray-200 dark:bg-gray-700 h-16 rounded"})]}):C.length>0?C.slice(0,3).map(a=>e.jsxs("div",{className:"flex items-start gap-3 p-3 rounded-lg border","data-testid":`anomaly-item-${a.id}`,children:[e.jsx(se,{variant:"outline",className:J(a.severity),"data-testid":`badge-severity-${a.severity}`,children:a.severity}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-gray-100 truncate","data-testid":`anomaly-description-${a.id}`,children:a.description}),e.jsxs("p",{className:"text-xs text-gray-500 dark:text-gray-400","data-testid":`anomaly-details-${a.id}`,children:[new Date(a.detectedAt).toLocaleDateString()," ⢠",a.deviationPercent,"% deviation"]})]})]},a.id)):e.jsxs("div",{className:"text-center py-4",children:[e.jsx(te,{className:"h-8 w-8 mx-auto mb-2 text-green-500"}),e.jsx("p",{className:"text-sm text-gray-500",children:"No anomalies detected"})]})})})]})]}),e.jsxs(n,{"data-testid":"card-platform-roi-chart",children:[e.jsxs(d,{children:[e.jsx(c,{children:"Platform ROI Comparison"}),e.jsx(h,{children:"Return on investment across marketing platforms"})]}),e.jsx(l,{children:B?e.jsx("div",{className:"h-80 flex items-center justify-center",children:e.jsx("div",{className:"animate-pulse text-gray-400",children:"Loading platform data..."})}):w.length>0?e.jsx("div",{className:"h-80",children:e.jsx(me,{width:"100%",height:"100%",children:e.jsxs(he,{data:w,children:[e.jsx(ge,{strokeDasharray:"3 3"}),e.jsx(ye,{dataKey:"platform"}),e.jsx(pe,{}),e.jsx(oe,{formatter:(a,s)=>[s==="roi"?`${a}%`:s==="roas"?`${a}x`:j(a),s]}),e.jsx(xe,{}),e.jsx(V,{dataKey:"roi",fill:"#3b82f6",name:"ROI %"}),e.jsx(V,{dataKey:"roas",fill:"#10b981",name:"ROAS"})]})})}):e.jsx("div",{className:"h-80 flex items-center justify-center text-center",children:e.jsxs("div",{children:[e.jsx(P,{className:"h-12 w-12 mx-auto mb-4 text-gray-400"}),e.jsx("p",{className:"text-gray-500",children:"No platform ROI data available"}),e.jsx("p",{className:"text-sm text-gray-400 mt-1",children:"Data will appear once campaigns are tracked"})]})})})]})]}),e.jsx(y,{value:"campaigns",className:"space-y-6",children:e.jsxs(n,{"data-testid":"card-campaign-performance",children:[e.jsxs(d,{children:[e.jsx(c,{children:"Campaign Performance Metrics"}),e.jsx(h,{children:"Detailed analysis of campaign effectiveness"})]}),e.jsx(l,{children:e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(P,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Campaign analytics will be displayed here"}),e.jsx("p",{className:"text-sm",children:"Data integration in progress..."})]})})]})}),e.jsx(y,{value:"platforms",className:"space-y-6",children:e.jsxs(n,{"data-testid":"card-platform-details",children:[e.jsxs(d,{children:[e.jsx(c,{children:"Platform ROI Details"}),e.jsx(h,{children:"Detailed breakdown of performance by platform"})]}),e.jsx(l,{children:e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(re,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Platform ROI details will be displayed here"}),e.jsx("p",{className:"text-sm",children:"Data integration in progress..."})]})})]})}),e.jsx(y,{value:"users",className:"space-y-6",children:e.jsxs(n,{"data-testid":"card-user-engagement",children:[e.jsxs(d,{children:[e.jsx(c,{children:"User Engagement Analytics"}),e.jsx(h,{children:"User behavior and engagement metrics"})]}),e.jsx(l,{children:e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(F,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"User engagement analytics will be displayed here"}),e.jsx("p",{className:"text-sm",children:"Data integration in progress..."})]})})]})}),e.jsx(y,{value:"system",className:"space-y-6",children:e.jsxs(n,{"data-testid":"card-system-health-detailed",children:[e.jsxs(d,{children:[e.jsx(c,{children:"System Health Details"}),e.jsx(h,{children:"Comprehensive system monitoring and health metrics"})]}),e.jsx(l,{children:e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(f,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Detailed system health metrics will be displayed here"}),e.jsx("p",{className:"text-sm",children:"Data integration in progress..."})]})})]})}),e.jsx(y,{value:"anomalies",className:"space-y-6",children:e.jsxs(n,{"data-testid":"card-anomalies-detailed",children:[e.jsxs(d,{children:[e.jsx(c,{children:"Anomaly Detection & Analysis"}),e.jsx(h,{children:"Advanced anomaly detection with detailed analysis"})]}),e.jsx(l,{children:e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(T,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}
1),e.jsx("p",{children:"Detailed anomaly analysis will be displayed here"}),e.jsx("p",{className:"text-sm",children:"Data integration in progress..."})]})})]})})]})]})})}export{Te as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.