import{r as c,j as s}from"./vendor-react-wBh_31cN.js";import{j as Fe,u as E}from"./vendor-redux-CJZrIb6x.js";import{B as Se}from"./badge-UCnweWGd.js";import{D as ie}from"./data-table-Mt0DWBPO.js";import{u as Ne}from"./useNotifier-BGMn6HaH.js";import{s as we,a as Le}from"./jobAlert.selectors-DIpkTg6b.js";import{D as Pe,E as $e,A as ke,s as Oe,a as Me,b as Re,c as _e,d as y}from"./DeleteGlobalEventForm-eh21kZMM.js";import{A as m,s as Ve}from"./alertUtil-rW93jnda.js";import{r as R,s as le,u as oe,v as Be,w as Je,x as qe}from"./alertManagerConfUtil-D4yp8Ca8.js";import{a7 as Ue,aQ as L,a4 as P,a as ne,f as He,aX as Qe,W as Ke,p as We,aq as _,d as ce,b as Xe,R as V,G as de}from"./index-dNe6i2rS.js";import{a as me}from"./project-eQY1xQe-.js";import{V as $}from"./value-7N0Keti9.js";import{C as ve,a as Ae,b as ge,c as je}from"./card-BeQtvmcJ.js";import{C as Ye}from"./confirmation-dialog-C7dws5K4.js";import{c as ze}from"./vendor-router-D1GqFWiT.js";import{i as Ze}from"./includes-ucXR_9jq.js";const Ge=Ue.injectEndpoints({endpoints:v=>({getFeatureViewAlerts:v.query({query:({id:r,version:t,pId:l,fsId:d})=>({url:`
          project/${l}/
          featurestores/${d}/
          featureview/${r}/version/${t}/
          alerts
          `.replace(/\s/g,"")}),transformResponse:ne([],"items"),extraOptions:{skipError:!0},providesTags:L.cacheByIdArgProperty(P.ProjectTags.FeatureViewAlert)}),addFeatureViewAlerts:v.mutation({async queryFn({pId:r,fsId:t,alerts:l=[],id:d,version:g},i,o,A){const I=l.map(a=>A({url:`
            project/${r}/
            featurestores/${t}/
            featureview/${d}/version/${g}/
            alerts
            `.replace(/\s/g,""),method:"post",data:a})),x=await Promise.all(I),j=He(Qe(["error","response"]),x);return j||{data:x}},invalidatesTags:L.cacheByIdArgProperty(P.ProjectTags.FeatureViewAlert)}),testFeatureViewAlert:v.mutation({query:({id:r,version:t,pId:l,fsId:d,alertId:g})=>({url:`
          project/${l}/
          featurestores/${d}/
          featureview/${r}/version/${t}/
          alerts/${g}/test
          `.replace(/\s/g,""),method:"POST"}),transformResponse:ne([],"items")}),updateFeatureViewAlert:v.mutation({query:({id:r,version:t,pId:l,fsId:d,alertId:g,alert:i})=>({url:`
          project/${l}/
          featurestores/${d}/
          featureview/${r}/version/${t}/
          alerts/${g}
          `.replace(/\s/g,""),method:"PUT",data:i}),invalidatesTags:L.cacheByIdArgProperty(P.ProjectTags.FeatureViewAlert)}),deleteFeatureViewAlert:v.mutation({query:({id:r,version:t,pId:l,fsId:d,alertId:g})=>({url:`
          project/${l}/
          featurestores/${d}/
          featureview/${r}/version/${t}/
          alerts/${g}
          `.replace(/\s/g,""),method:"DELETE"}),invalidatesTags:L.cacheByIdArgProperty(P.ProjectTags.FeatureViewAlert)})})}),{useGetFeatureViewAlertsQuery:et,useAddFeatureViewAlertsMutation:tt,useTestFeatureViewAlertMutation:st,useUpdateFeatureViewAlertMutation:at,useDeleteFeatureViewAlertMutation:rt}=Ge,it=({isOpenPopup:v,isSubmiting:r,submit:t,handleTogglePopup:l})=>s.jsx(Ye,{open:v,onOpenChange:d=>l(d),title:"Delete a local event",description:"Once you delete an alert, it won't take effect anymore. Please be certain.",confirmLabel:"Delete",cancelLabel:"Back",confirmVariant:"destructive",isLoading:r,onConfirm:()=>t(void 0)}),lt=c.memo(it),ot=({opType:v,type:r,isOpenPopup:t,projectId:l,alertId:d,alert:g,receivers:i,threshold:o,isSubmitingAdd:A,isSubmitingUpdate:I,isSubmitingDelete:x,submit:j,handleTogglePopup:a,isAgentJob:k=!1})=>{switch(v){case m.ADD:case m.EDIT:return s.jsx(ke,{type:r,isOpenPopup:t,receivers:i||[],threshold:o||0,submit:j,handleTogglePopup:a,isSubmiting:A||I,initialValue:g,alertId:d,isAgentJob:k});case m.DELETE:return s.jsx(lt,{isOpenPopup:t,submit:j,handleTogglePopup:a,isSubmiting:x});case m.EDIT_PROJECT:return s.jsx($e,{projectId:l,isOpenPopup:t,projectAlertId:d,handleTogglePopup:a});case m.DELETE_PROJECT:return s.jsx(Pe,{projectId:l,isOpenPopup:t,projectAlertId:d,handleTogglePopup:a});default:return null}},nt=c.memo(ot),Tt=({type:v,job:r,featureGroup:t,featureView:l,className:d,isAgentJob:g=!1})=>{var re;const i=Fe(),{id:o,fsId:A,fvName:I,fvVersion:x}=ze(),j=Ne(),a=v==="job"?"job":t?"validation-fg":"validation-fv",{data:k}=Ke(),[B,{isLoading:fe}]=tt(),[J,{isLoading:pe}]=at(),[q]=st(),[U,{isLoading:he}]=rt(),ue=((re=E(Ve).receivers)==null?void 0:re.map(We("name")))??[],u=E(we),h=E(Oe),D=E(Me),{currentData:C,isLoading:Ee}=et({pId:+o,fsId:+A,id:I,version:+x},{skip:!l}),xe=E(Le),F=E(Re),ye=E(_e),[{opType:O,alertId:f},M]=c.useState({opType:m.ADD,alertId:-1}),[Ie,H]=c.useState(!1),p=c.useCallback(()=>H(e=>!e),[]),[b,Q]=c.useState([]);c.useEffect(()=>{a==="job"&&r&&r.name?i.jobAlert.fetch({projectId:+o,jobName:r.name}):a==="validation-fg"&&!F&&(t!=null&&t.featurestoreId)&&(t!=null&&t.id)&&h.validationAlerts===null&&i.validationAlert.fetch({projectId:+o,featureStoreId:t==null?void 0:t.featurestoreId,featureGroupId:t==null?void 0:t.id})},[i.jobAlert,i.validationAlert,a,t==null?void 0:t.featurestoreId,t==null?void 0:t.id,h,F,r,o,v]),c.useEffect(()=>()=>{var e,n;(n=(e=i.validationAlert).clear)==null||n.call(e)},[]),c.useEffect(()=>{i.projectAlert.fetch(+o)},[i.projectAlert,o]),c.useEffect(()=>{i.alerts.fetchReceivers(+o)},[i.alerts,o]);const T=c.useMemo(()=>a==="job"&&u&&u.jobAlerts?u.jobAlerts.length:a==="validation-fg"&&t&&h&&h.validationAlerts?h.validationAlerts.length:l&&C?C.length:0,[a,t,h,l,C,u]);c.useEffect(()=>{D.projectAlerts&&Q(a==="job"?D.projectAlerts.filter(e=>e.service===me.JOBS):D.projectAlerts.filter(e=>e.service===me.FEATURESTORE).filter(e=>a==="validation-fv"?Ze(e.status,[R.MONITORING_SHIFT_UNDETECTED,R.MONITORING_SHIFT_DETECTED,R.MONITORING_EMPTY_DETECTION_WINDOW]):!0))},[D,a]);const S=c.useCallback(e=>{e&&e.length>0&&e[0].receivers&&e[0].receivers.length>0?j.success({title:"Success",content:`Alert sent to receivers in ${e[0].receivers.map(n=>n.name)}`}):e&&e.length>0&&(!e[0].receivers||e[0].receivers.length<1)?j.error({title:"Error",content:"No receiver found for this alert."}):j.error({title:"Error",content:"Failed to send an alert."}),i.error.clearError()},[i.error,j]),K=c.useCallback(async e=>{let n;a==="job"&&r&&r.name?n=await i.jobAlert.test({projectId:+o,jobName:r.name,id:e}):a==="validation-fg"&&t?n=await i.validationAlert.test({projectId:+o,featureStoreId:t.featurestoreId,featureGroupId:t.id,id:e}):a==="validation-fv"&&(n=await q({pId:+o,fsId:+A,id:l.name,version:l.version,alertId:e}).unwrap()),S(n)},[i.jobAlert,i.validationAlert,a,t,l,A,r,o,S,q]),W=c.useCallback(async e=>{const n=await i.projectAlert.test({projectId:+o,id:e});S(n)},[i.projectAlert,o,S]),N=c.useCallback(e=>n=>{M({opType:e,alertId:n||-1}),p()},[p]),X=N(m.EDIT_PROJECT),Y=N(m.DELETE_PROJECT),z=N(m.EDIT),Z=N(m.DELETE),w={job:u==null?void 0:u.jobAlerts,"validation-fg":h==null?void 0:h.validationAlerts,"validation-fv":C}[a]??[],G=c.useCallback(async e=>{if(a==="job"&&r&&r.name){const n={...e.event?{status:le(e.event),severity:e.severity}:{},receiver:e.receiver,threshold:e.threshold,...e.passToAgent?{passToAgent:!0}:{}};await i.jobAlert.create({projectId:+o,jobName:r.name,jobAlert:n})}else{const n={status:oe(e.event),severity:e.severity,receiver:e.receiver};a==="validation-fg"?await i.validationAlert.create({projectId:+o,featureStoreId:t.featurestoreId,featureGroupId:t.id,validationAlert:n}):a==="validation-fv"&&await B({pId:+o,fsId:+A,id:l.name,version:l.version,alerts:[n]}).unwrap()}p()},[B,i.jobAlert,i.validationAlert,a,t,l,A,p,r,o]),ee=c.useCallback(async e=>{if(a==="job"&&r&&r.name){const n={...e.event?{status:le(e.event),severity:e.severity}:{},receiver:e.receiver,threshold:e.threshold,...e.passToAgent?{passToAgent:!0}:{}};await i.jobAlert.update({projectId:+o,jobName:r.name,id:f,jobAlert:n})}else{const n={status:oe(e.event),severity:e.severity,receiver:e.receiver};a==="validation-fg"?await i.validationAlert.update({projectId:+o,featureStoreId:t.featurestoreId,featureGroupId:t.id,id:f,validationAlert:n}):a==="validation-fv"&&await J({pId:+o,fsId:+A,id:l.name,version:l.version,alertId:f,alert:n}).unwrap()}p()},[f,i.jobAlert,i.validationAlert,a,t,l,A,p,r,o,J]),te=c.useCallback(async e=>{a==="job"&&r&&r.name?await i.jobAlert.remove({projectId:+o,jobName:r.name,id:f}):a==="validation-fg"?await i.validationAlert.remove({projectId:+o,featureStoreId:t.featurestoreId,featureGroupId:t.id,id:f}):a==="validation-fv"&&await U({pId:+o,fsId:+A,id:l.name,version:l.version,alertId:f}).unwrap(),p()},[f,U,i.jobAlert,i.validationAlert,a,t,l,A,p,r,o]),be=c.useMemo(()=>({[m.ADD]:G,[m.EDIT]:ee,[m.DELETE]:te,[m.EDIT_PROJECT]:()=>{},[m.DELETE_PROJECT]:()=>{}})[O],[O,G,te,ee]),se=[{accessorKey:"status",header:"Trigger on",cell:({row:e})=>s.jsx($,{children:Be(e.original.status)})},{accessorKey:"severity",header:"Severity",cell:({row:e})=>s.jsx(Se,{variant:qe(e.original.severity),children:Je(e.original.severity)})},{accessorKey:"receiver",header:"Receiver",cell:({row:e})=>s.jsx($,{children:e.original.receiver})}],Te=c.useMemo(()=>[...se,{id:"actions",header:"",cell:({row:e})=>s.jsxs("div",{className:"flex justify-end",children:[s.jsx(y,{type:"test","data-test":"test-alert-global",handleClick:()=>W(e.original.id)}),s.jsx(y,{type:"edit","data-test":"edit-alert-global",handleClick:()=>X(e.original.id)}),s.jsx(y,{type:"delete","data-test":"delete-alert-global",handleClick:()=>Y(e.original.id)})]})}],[W,X,Y]),De=c.useMemo(()=>[...se,{id:"actions",header:"",cell:({row:e})=>s.jsxs("div",{className:"flex justify-end",children:[s.jsx(y,{type:"test","data-test":"test-alert-local",handleClick:()=>K(e.original.id)}),s.jsx(y,{type:"edit","data-test":"edit-alert-local",handleClick:()=>z(e.original.id)}),s.jsx(y,{type:"delete","data-test":"delete-alert-local",handleClick:()=>Z(e.original.id)})]})}],[K,z,Z]);if(F)return s.jsxs(ve,{className:ce("w-full",d),children:[s.jsx(Ae,{children:s.jsx(ge,{children:"Alerts"})}),s.jsx(je,{children:s.jsx(_,{})})]});const Ce={job:"job","validation-fg":"feature group","validation-fv":"feature view"}[a],ae={job:"job execution","validation-fg":`data validation${k?" / feature monitoring":""}`,"validation-fv":"feature monitoring"}[a];return s.jsxs(s.Fragment,{children:[s.jsx(nt,{opType:O,type:a,isOpenPopup:Ie,projectId:+o,alertId:f,alert:w==null?void 0:w.find(Xe("id",f)),receivers:ue,submit:be,handleTogglePopup:H,isSubmitingAdd:fe,isSubmitingUpdate:pe,isSubmitingDelete:he,isAgentJob:g}),s.jsxs(ve,{className:ce("w-full",d),id:"alerts-container",children:[s.jsx(Ae,{children:s.jsx(ge,{children:"Alerts"})}),s.jsx(je,{children:T+b.length<1?s.jsxs("div",{className:"mb-5 mt-5 flex flex-col items-center justify-center",children:[s.jsx(V,{gray:!0,className:"text-lg",children:"No alerts defined"}),s.jsx(de,{className:"mt-5",onClick:()=>{M({opType:m.ADD,alertId:-1}),p()},children:"Add an alert"})]}):s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"flex items-center",children:[s.jsxs("div",{className:"flex gap-1",children:[s.jsx($,{primary:!0,children:T+b.length}),s.jsx($,{children:T+b.length>1?"alerts":"alert"})]}),s.jsx(de,{disabled:T>=5,onClick:()=>{M({opType:m.ADD,alertId:-1}),p()},className:"ml-auto",children:"Add an alert"})]}),b.length>0&&s.jsxs("div",{className:"flex flex-col gap-2",children:[s.jsxs(V,{className:"text-xs",gray:!0,children:["Global alerts (triggered on any ",ae,")."]}),ye?s.jsx(_,{}):s.jsx(ie,{columns:Te,data:b,emptyMessage:"No global alerts"})]}),T>0&&s.jsxs("div",{className:"flex flex-col gap-2",children:[s.jsxs(V,{className:"mt-2 text-xs",gray:!0,children:["Local alerts (triggered for this ",Ce," ",ae,"). Local alerts will take precedence over global alerts."]}),v==="job"&&xe||a==="validation-fg"&&F||a==="validation-fv"&&Ee?s.jsx(_,{}):s.jsx(ie,{columns:De,data:w??[],emptyMessage:"No local alerts"})]})]})})]})]})};export{Tt as S};
