15 lines
12 KiB
JavaScript
15 lines
12 KiB
JavaScript
import{j as e,r as x}from"./vendor-codemirror-B1SGo4xl.js";import{D as v,a as k,b as w,c as S,d as _,e as C,B as A}from"./index-Dungj95X.js";import{I as h}from"./input-CXoUQQLm.js";import{L as c}from"./label-CMpUoC87.js";import{T as L}from"./textarea-BxfjmLq5.js";import{S as E}from"./switch-Bxiq4EHp.js";import{a as j}from"./alert-v1WaPeEX.js";import{t as F,ap as P,aq as I,ar as q,L as B}from"./vendor-icons-DmqcWdzX.js";function b(o){if(!o)return[];if(Array.isArray(o))return o;if(typeof o=="string"){try{const r=JSON.parse(o);if(Array.isArray(r))return r}catch{}return o.split(",").map(r=>r.trim()).filter(Boolean)}return[]}const T=["email","sms","dingtalk","wechat","webhook"],H={dingtalk:"钉钉",wechat:"企业微信",email:"邮件",sms:"短信",webhook:"Webhook"};function z(o,r){if(!o)return null;const a=r.find(i=>String(i.id)===String(o));return a?{type:a.type||"unknown",name:a.name||a.type||"未命名",isLegacy:!1}:T.includes(o)?{type:o,name:H[o]||o,isLegacy:!0}:null}const W=[{key:"dingtalk",label:"钉钉",placeholder:`{
|
|
"webhook": "https://oapi.dingtalk.com/robot/send?access_token=***",
|
|
"secret": "SECxxx"
|
|
}`},{key:"wechat",label:"企业微信",placeholder:`{
|
|
"webhook": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=***"
|
|
}`},{key:"email",label:"邮件",placeholder:`{
|
|
"to": "ops@ezops.io",
|
|
"subject": "告警通知"
|
|
}`},{key:"sms",label:"短信",placeholder:`{
|
|
"phones": "13800138000",
|
|
"sign": "AIOps"
|
|
}`},{key:"webhook",label:"Webhook",placeholder:`{
|
|
"url": "https://example.com/alert"
|
|
}`}];function y(o){return W.find(r=>r.key===o)?.label||o}function f(o){switch(o){case"email":return q;case"sms":return I;case"dingtalk":case"wechat":return P;default:return F}}function Z(o,r){const a=b(o.notify_channels);return a.length===0?e.jsx("span",{"code-path":"src/components/monitor/alertShared.tsx:76:33",className:"text-xs text-slate-600",children:"--"}):e.jsx("div",{"code-path":"src/components/monitor/alertShared.tsx:78:5",className:"flex flex-wrap gap-1",children:a.map(i=>{const m=z(i,r);if(!m)return null;const p=f(m.type);return e.jsxs("span",{"code-path":"src/components/monitor/alertShared.tsx:84:11",title:`${m.name} (${y(m.type)})`,className:"inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-slate-800 text-slate-300",children:[e.jsx(p,{"code-path":"src/components/monitor/alertShared.tsx:89:13",className:"h-3 w-3 text-slate-400"}),m.name]},i)})})}const D={name:"",description:"",type:"metric",source:"prometheus",expression:"",severity:"warning",duration:300,notify_channels:[]};function G({open:o,onOpenChange:r,editingRule:a,notifyChannels:i,onSaved:m,defaultType:p="metric"}){const[s,l]=x.useState({...D}),[u,g]=x.useState(!1);x.useEffect(()=>{if(o)if(a)l({name:a.name||"",description:a.description||"",type:a.type||"metric",source:a.source||"prometheus",expression:a.expression||"",severity:a.severity||"warning",duration:a.duration||300,notify_channels:b(a.notify_channels)});else{const t={...D,type:p};p==="log"&&(t.source="loki"),l(t)}},[o,a,p]);const N=t=>{l(n=>({...n,notify_channels:n.notify_channels.includes(t)?n.notify_channels.filter(d=>d!==t):[...n.notify_channels,t]}))},R=async()=>{if(!(!s.name.trim()||!s.expression.trim())){g(!0);try{const t={name:s.name.trim(),description:s.description.trim(),type:s.type,source:s.source,expression:s.expression.trim(),severity:s.severity,duration:Number(s.duration)||0,notify_channels:s.notify_channels};a?await j.updateRule(a.id,t):await j.createRule(t),r(!1),m()}catch(t){console.error("Failed to save rule:",t)}finally{g(!1)}}};return e.jsx(v,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:111:5",open:o,onOpenChange:r,children:e.jsxs(k,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:112:7",className:"bg-slate-900 border-slate-700 text-slate-100 max-w-lg",children:[e.jsxs(w,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:113:9",children:[e.jsx(S,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:114:11",className:"text-slate-100",children:a?"编辑告警规则":"新建告警规则"}),e.jsx(_,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:117:11",className:"text-slate-500",children:a?"修改告警规则的触发条件和通知方式":"配置监控指标的告警触发条件"})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:122:9",className:"space-y-4 max-h-[60vh] overflow-y-auto pr-1",children:[e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:123:11",className:"space-y-1.5",children:[e.jsxs(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:124:13",className:"text-slate-300",children:["规则名称 ",e.jsx("span",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:124:52",className:"text-red-400",children:"*"})]}),e.jsx(h,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:125:13",value:s.name,onChange:t=>l({...s,name:t.target.value}),placeholder:"如:CPU 使用率告警",className:"bg-slate-800/50 border-slate-700 text-slate-200 placeholder:text-slate-600"})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:133:11",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:134:13",className:"text-slate-300",children:"描述"}),e.jsx(L,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:135:13",value:s.description,onChange:t=>l({...s,description:t.target.value}),placeholder:"规则说明",rows:2,className:"bg-slate-800/50 border-slate-700 text-slate-200 placeholder:text-slate-600 resize-none"})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:144:11",className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:145:13",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:146:15",className:"text-slate-300",children:"规则类型"}),e.jsxs("select",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:147:15",value:s.type,onChange:t=>{const n=t.target.value;l({...s,type:n,source:n==="log"?"loki":"prometheus"})},className:"w-full h-9 px-3 rounded-md bg-slate-800/50 border border-slate-700 text-slate-200 text-sm",children:[e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:156:17",value:"metric",children:"指标监控"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:157:17",value:"log",children:"日志告警"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:158:17",value:"event",children:"事件告警"})]})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:161:13",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:162:15",className:"text-slate-300",children:"数据源"}),e.jsx("select",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:163:15",value:s.source,onChange:t=>l({...s,source:t.target.value}),className:"w-full h-9 px-3 rounded-md bg-slate-800/50 border border-slate-700 text-slate-200 text-sm",children:s.type==="log"?e.jsxs(e.Fragment,{children:[e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:170:21",value:"loki",children:"Loki"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:171:21",value:"elasticsearch",children:"Elasticsearch"})]}):e.jsxs(e.Fragment,{children:[e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:175:21",value:"prometheus",children:"Prometheus"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:176:21",value:"zabbix",children:"Zabbix"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:177:21",value:"custom",children:"自定义"})]})})]})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:184:11",className:"space-y-1.5",children:[e.jsxs(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:185:13",className:"text-slate-300",children:["触发表达式 ",e.jsx("span",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:185:53",className:"text-red-400",children:"*"})]}),e.jsx(h,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:186:13",value:s.expression,onChange:t=>l({...s,expression:t.target.value}),placeholder:s.type==="log"?'{app="api"} |= "error"':"cpu_usage > 80",className:"bg-slate-800/50 border-slate-700 text-slate-200 placeholder:text-slate-600 font-mono text-sm"}),e.jsx("p",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:192:13",className:"text-xs text-slate-600",children:s.type==="log"?s.source==="loki"?"LogQL 查询或 count_over_time 表达式":"Elasticsearch Lucene 查询":"PromQL 或指标表达式,例如 cpu_usage > 80"})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:199:11",className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:200:13",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:201:15",className:"text-slate-300",children:"告警级别"}),e.jsxs("select",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:202:15",value:s.severity,onChange:t=>l({...s,severity:t.target.value}),className:"w-full h-9 px-3 rounded-md bg-slate-800/50 border border-slate-700 text-slate-200 text-sm",children:[e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:207:17",value:"info",children:"提示"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:208:17",value:"warning",children:"警告"}),e.jsx("option",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:209:17",value:"critical",children:"严重"})]})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:212:13",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:213:15",className:"text-slate-300",children:"持续时间(秒)"}),e.jsx(h,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:214:15",type:"number",value:s.duration,onChange:t=>l({...s,duration:Number(t.target.value)}),className:"bg-slate-800/50 border-slate-700 text-slate-200"})]})]}),e.jsxs("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:223:11",className:"space-y-1.5",children:[e.jsx(c,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:224:13",className:"text-slate-300",children:"通知渠道"}),i.length===0?e.jsx("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:226:15",className:"text-xs text-amber-400/80 bg-amber-500/5 border border-amber-500/20 rounded-md px-3 py-2",children:"暂无已配置的通知渠道,请先在「通知渠道配置」中添加"}):e.jsx("div",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:230:15",className:"flex flex-wrap gap-3 pt-1",children:i.map(t=>{const n=f(t.type),d=s.notify_channels.includes(String(t.id));return e.jsxs("label",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:235:21",className:"flex items-center gap-2 cursor-pointer select-none text-sm text-slate-300",children:[e.jsx(E,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:239:23",checked:d,onCheckedChange:()=>N(String(t.id)),className:"data-[state=checked]:bg-blue-600"}),e.jsx(n,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:244:23",className:"h-4 w-4 text-slate-400"}),e.jsxs("span",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:245:23",children:[t.name,e.jsxs("span",{"code-path":"src/components/monitor/AlertRuleDialog.tsx:247:25",className:"text-xs text-slate-500 ml-1",children:["(",y(t.type),")"]})]})]},t.id)})})]})]}),e.jsxs(C,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:257:9",children:[e.jsx(A,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:258:11",variant:"outline",className:"border-slate-700 text-slate-300 hover:bg-slate-800",onClick:()=>r(!1),children:"取消"}),e.jsxs(A,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:265:11",className:"bg-blue-600 hover:bg-blue-700 text-white",disabled:!s.name.trim()||!s.expression.trim()||u,onClick:R,children:[u&&e.jsx(B,{"code-path":"src/components/monitor/AlertRuleDialog.tsx:270:28",className:"h-4 w-4 mr-2 animate-spin"}),a?"保存修改":"创建规则"]})]})]})})}export{G as A,W as C,y as g,Z as r};
|