1import{j as e}from"./ui-CSqyuXLl.js";import{u as H}from"./query-BeNKcRPl.js";import{A as R,B as g,a as ee,H as L,J as T,i as se,j as ae,d as h,e as p,f,b as te,g as N,h as m,P as A,K as $,S as u,T as le,C as ne,k as ce,s as M}from"./index-Dxk2ymNy.js";import{c as re,R as ie}from"./useAdminRealtime-B1CvSCGK.js";import{R as D,A as de,a as K,X as O,Y as E,T as S,f as oe,P as xe,g as he,h as q,B as me,e as ue}from"./charts-C4DDVxJp.js";import"./vendor-DW1aPxuE.js";const y=["hsl(var(--destructive))","hsl(var(--chart-2))","hsl(var(--chart-3))","hsl(var(--chart-4))","hsl(var(--primary))"];function ye(){var j;re();const{data:r,isLoading:d}=H({queryKey:["admin-intervention-logs"],queryFn:async()=>{const{data:t}=await M.from("ai_observability_logs").select("*").eq("intervention_triggered",!0).order("created_at",{ascending:!1}).limit(500);return t||[]}}),{data:n,isLoading:x}=H({queryKey:["admin-interventions-table"],queryFn:async()=>{const{data:t}=await M.from("ai_interventions").select("*").order("created_at",{ascending:!1}).limit(500);return t||[]}}),c=d||x,s=n?je(n):null;return e.jsxs("div",{className:"p-3 sm:p-4 md:p-6 space-y-4 sm:space-y-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 rounded-lg bg-chart-2/10",children:e.jsx(R,{className:"h-5 w-5 sm:h-6 sm:w-6 text-chart-2"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl sm:text-2xl font-bold",children:"Intervention Analytics"}),e.jsx("p",{className:"text-muted-foreground text-xs sm:text-sm",children:"Crisis support & effectiveness tracking"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(g,{variant:"outline",className:"flex items-center gap-1",children:[e.jsx(ie,{className:"h-3 w-3 text-green-500 animate-pulse"}),e.jsx("span",{className:"text-xs",children:"Live"})]}),e.jsxs(g,{variant:"outline",className:"flex items-center gap-1",children:[e.jsx(ee,{className:"h-3 w-3"}),e.jsx("span",{className:"text-xs",children:"Anonymized"})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-2 sm:gap-4",children:[e.jsx(v,{title:"Total Interventions",value:(s==null?void 0:s.totalInterventions)||0,icon:R,loading:c}),e.jsx(v,{title:"Acknowledged",value:`${(s==null?void 0:s.acknowledgementRate)||0}%`,icon:L,loading:c,color:"text-green-500"}),e.jsx(v,{title:"Helpful Rate",value:`${(s==null?void 0:s.helpfulRate)||0}%`,icon:T,loading:c,color:"text-primary"}),e.jsx(v,{title:"Avg Response",value:`${(s==null?void 0:s.avgTimeToAck)||0}m`,icon:se,loading:c}),e.jsx(v,{title:"Avg Risk Score",value:((j=s==null?void 0:s.avgRiskScore)==null?void 0:j.toFixed(1))||"0",icon:ae,loading:c,color:"text-orange-500"})]}),e.jsxs(h,{className:"border-primary/20",children:[e.jsxs(p,{children:[e.jsxs(f,{className:"flex items-center gap-2",children:[e.jsx(te,{className:"h-5 w-5 text-primary"}),"Intervention Effectiveness"]}),e.jsx(N,{children:"User feedback on intervention helpfulness - critical for AI improvement"})]}),e.jsx(m,{children:e.jsxs("div",{className:"grid md:grid-cols-3 gap-6",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium text-sm",children:"Feedback Distribution"}),e.jsx("div",{className:"flex items-center gap-4",children:e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex justify-between mb-1",children:[e.jsxs("span",{className:"text-sm text-green-500 flex items-center gap-1",children:[e.jsx(T,{className:"h-3 w-3"})," Helpful"]}),e.jsx("span",{className:"text-sm font-medium",children:(s==null?void 0:s.helpfulCount)||0})]}),e.jsx(A,{value:(s==null?void 0:s.helpfulRate)||0,className:"h-2 bg-muted"})]})}),e.jsx("div",{className:"flex items-center gap-4",children:e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex justify-between mb-1",children:[e.jsxs("span",{className:"text-sm text-red-500 flex items-center gap-1",children:[e.jsx($,{className:"h-3 w-3"})," Not Helpful"]}),e.jsx("span",{className:"text-sm font-medium",children:(s==null?void 0:s.notHelpfulCount)||0})]}),e.jsx(A,{value:(s==null?void 0:s.notHelpfulRate)||0,className:"h-2 bg-muted"})]})}),e.jsx("div",{className:"flex items-center gap-4",children:e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex justify-between mb-1",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:"No Feedback"}),e.jsx("span",{className:"text-sm font-medium",children:(s==null?void 0:s.noFeedbackCount)||0})]}),e.jsx(A,{value:(s==null?void 0:s.noFeedbackRate)||0,className:"h-2 bg-muted"})]})})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium text-sm",children:"Acknowledgement Funnel"}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(F,{label:"Triggered",count:(s==null?void 0:s.totalInterventions)||0,percentage:100}),e.jsx(F,{label:"Acknowledged",count:(s==null?void 0:s.acknowledgedCount)||0,percentage:(s==null?void 0:s.acknowledgementRate)||0}),e.jsx(F,{label:"Gave Feedback",count:((s==null?void 0:s.helpfulCount)||0)+((s==null?void 0:s.notHelpfulCount)||0),percentage:(s==null?void 0:s.feedbackRate)||0})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium text-sm",children:"Risk Score Distribution"}),c?e.jsx(u,{className:"h-[120px] w-full"}):e.jsx("div",{className:"space-y-2",children:((s==null?void 0:s.riskDistribution)||[]).map((t,i)=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-xs w-16",children:t.label}),e.jsx("div",{className:"flex-1 h-4 bg-muted rounded overflow-hidden",children:e.jsx("div",{className:"h-full bg-gradient-to-r from-green-500 via-yellow-500 to-red-500",style:{width:`${t.percentage}%`}})}),e.jsx("span",{className:"text-xs w-8 text-right",children:t.count})]},i))})]})]})})]}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-6",children:[e.jsxs(h,{children:[e.jsxs(p,{children:[e.jsxs(f,{className:"flex items-center gap-2",children:[e.jsx(le,{className:"h-5 w-5"}),"Interventions Over Time"]}),e.jsx(N,{children:"Daily intervention count"})]}),e.jsx(m,{children:c?e.jsx(u,{className:"h-[200px] w-full"}):e.jsx(D,{width:"100%",height:200,children:e.jsxs(de,{data:(s==null?void 0:s.interventionsOverTime)||[],children:[e.jsx(K,{strokeDasharray:"3 3",className:"stroke-muted"}),e.jsx(O,{dataKey:"date",className:"text-xs"}),e.jsx(E,{className:"text-xs"}),e.jsx(S,{contentStyle:{backgroundColor:"hsl(var(--card))",border:"1px solid hsl(var(--border))"}}),e.jsx(oe,{type:"monotone",dataKey:"count",stroke:"hsl(var(--chart-2))",fill:"hsl(var(--chart-2)/0.2)"})]})})})]}),e.jsxs(h,{children:[e.jsxs(p,{children:[e.jsxs(f,{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"h-5 w-5"}),"Intervention Types"]}),e.jsx(N,{children:"Breakdown by trigger type"})]}),e.jsx(m,{children:c?e.jsx(u,{className:"h-[200px] w-full"}):e.jsx(D,{width:"100%",height:200,children:e.jsxs(xe,{children:[e.jsx(he,{data:(s==null?void 0:s.typeBreakdown)||[],cx:"50%",cy:"50%",innerRadius:40,outerRadius:80,dataKey:"value",nameKey:"name",label:({name:t,percent:i})=>`${t} ${(i*100).toFixed(0)}%`,children:((s==null?void 0:s.typeBreakdown)||[]).map((t,i)=>e.jsx(q,{fill:y[i%y.length]},`cell-${i}`))}),e.jsx(S,{})]})})})]})]}),e.jsxs(h,{children:[e.jsxs(p,{children:[e.jsx(f,{children:"Actions Taken After Intervention"}),e.jsx(N,{children:"What users did after receiving an intervention"})]}),e.jsx(m,{children:c?e.jsx(u,{className:"h-[200px] w-full"}):e.jsx(D,{width:"100%",height:200,children:e.jsxs(me,{data:(s==null?void 0:s.actionsBreakdown)||[],children:[e.jsx(K,{strokeDasharray:"3 3",className:"stroke-muted"}),e.jsx(O,{dataKey:"action",className:"text-xs"}),e.jsx(E,{className:"text-xs"}),e.jsx(S,{contentStyle:{backgroundColor:"hsl(var(--card))",border:"1px solid hsl(var(--border))"}}),e.jsx(ue,{dataKey:"count",fill:"hsl(var(--primary))",radius:[4,4,0,0],children:((s==null?void 0:s.actionsBreakdown)||[]).map((t,i)=>e.jsx(q,{fill:y[i%y.length]},`cell-${i}`))})]})})})]}),e.jsxs(h,{children:[e.jsxs(p,{children:[e.jsx(f,{children:"Recent Interventions"}),e.jsx(N,{children:"Latest crisis support events (anonymized)"})]}),e.jsx(m,{children:e.jsx(ce,{className:"h-[300px]",children:c?e.jsx("div",{className:"space-y-2",children:[1,2,3,4,5].map(t=>e.jsx(u,{className:"h-16 w-full"},t))}):e.jsxs("div",{className:"space-y-2",children:[(n||[]).slice(0,20).map(t=>{var i;return e.jsx("div",{className:"p-3 rounded-lg border bg-card",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(g,{variant:"outline",className:"text-xs capitalize",children:((i=t.trigger_type)==null?void 0:i.replace(/_/g," "))||"Unknown"}),t.risk_score&&e.jsxs(g,{variant:t.risk_score>7?"destructive":"secondary",className:"text-xs",children:["Risk: ",t.risk_score,"/10"]}),t.was_acknowledged&&e.jsx(L,{className:"h-3 w-3 text-green-500"}),t.was_helpful===!0&&e.jsx(T,{className:"h-3 w-3 text-primary"}),t.was_helpful===!1&&e.jsx($,{className:"h-3 w-3 text-red-500"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:new Date(t.created_at).toLocaleStr
1ing()})]}),t.action_taken&&e.jsx(g,{variant:"secondary",className:"text-xs",children:t.action_taken})]})},t.id)}),(!n||n.length===0)&&e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(R,{className:"h-8 w-8 mx-auto mb-2 opacity-50"}),e.jsx("p",{children:"No interventions recorded yet"})]})]})})})]})]})}function je(r,d){const n=r.length,x=r.filter(a=>a.was_acknowledged).length,c=r.filter(a=>a.was_helpful===!0).length,s=r.filter(a=>a.was_helpful===!1).length,j=r.filter(a=>a.was_helpful===null).length,t=n>0?Math.round(x/n*100):0,i=n>0?Math.round(c/n*100):0,P=n>0?Math.round(s/n*100):0,U=n>0?Math.round(j/n*100):0,z=n>0?Math.round((c+s)/n*100):0,k=r.filter(a=>a.was_acknowledged&&a.acknowledged_at).map(a=>{const l=new Date(a.created_at);return(new Date(a.acknowledged_at).getTime()-l.getTime())/6e4}),G=k.length>0?Math.round(k.reduce((a,l)=>a+l,0)/k.length):0,o=r.map(a=>a.risk_score).filter(Boolean),X=o.length>0?o.reduce((a,l)=>a+l,0)/o.length:0,b=[{label:"Low (1-3)",count:o.filter(a=>a<=3).length,percentage:0},{label:"Medium (4-6)",count:o.filter(a=>a>3&&a<=6).length,percentage:0},{label:"High (7-8)",count:o.filter(a=>a>6&&a<=8).length,percentage:0},{label:"Critical (9-10)",count:o.filter(a=>a>8).length,percentage:0}],Y=Math.max(...b.map(a=>a.count),1);b.forEach(a=>a.percentage=a.count/Y*100);const J=new Date,I=[];for(let a=13;a>=0;a--){const l=new Date(J);l.setDate(l.getDate()-a),l.setHours(0,0,0,0);const w=new Date(l);w.setDate(w.getDate()+1);const V=r.filter(Z=>{const B=new Date(Z.created_at);return B>=l&&B<w}).length;I.push({date:l.toLocaleDateString("en-US",{month:"short",day:"numeric"}),count:V})}const C={};r.forEach(a=>{const l=a.trigger_type||"unknown";C[l]=(C[l]||0)+1});const Q=Object.entries(C).map(([a,l])=>({name:a.replace(/_/g," "),value:l})).sort((a,l)=>l.value-a.value),_={};r.forEach(a=>{const l=a.action_taken||"no action";_[l]=(_[l]||0)+1});const W=Object.entries(_).map(([a,l])=>({action:a.replace(/_/g," "),count:l})).sort((a,l)=>l.count-a.count).slice(0,6);return{totalInterventions:n,acknowledgedCount:x,acknowledgementRate:t,helpfulCount:c,notHelpfulCount:s,noFeedbackCount:j,helpfulRate:i,notHelpfulRate:P,noFeedbackRate:U,feedbackRate:z,avgTimeToAck:G,avgRiskScore:X,riskDistribution:b,interventionsOverTime:I,typeBreakdown:Q,actionsBreakdown:W}}function v({title:r,value:d,icon:n,loading:x,color:c}){return e.jsx(h,{children:e.jsxs(m,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground mb-2",children:[e.jsx(n,{className:`h-4 w-4 ${c||""}`}),e.jsx("span",{className:"text-sm",children:r})]}),x?e.jsx(u,{className:"h-8 w-20"}):e.jsx("p",{className:`text-2xl font-bold ${c||""}`,children:typeof d=="number"?d.toLocaleString():d})]})})}function F({label:r,count:d,percentage:n}){return e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-20 text-xs",children:r}),e.jsx("div",{className:"flex-1 h-6 bg-muted rounded overflow-hidden",children:e.jsx("div",{className:"h-full bg-primary/60 flex items-center justify-end pr-2",style:{width:`${n}%`},children:e.jsx("span",{className:"text-xs font-medium",children:d})})}),e.jsxs("span",{className:"w-10 text-xs text-right",children:[n,"%"]})]})}export{ye 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.