alertrouter_edit.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {{template "main" .}}
  2. <!-- Navbar -->
  3. <nav class="main-header navbar navbar-expand navbar-white navbar-light">
  4. <!-- Left navbar links -->
  5. <ul class="navbar-nav">
  6. <li class="nav-item">
  7. <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
  8. </li>
  9. <li class="nav-item d-none d-sm-inline-block">
  10. <a href="/alertrouter" class="nav-link">告警管理</a>
  11. </li>
  12. </ul>
  13. </nav>
  14. <!-- /.navbar -->
  15. <!-- Content Wrapper. Contains page content -->
  16. <div class="content-wrapper">
  17. <!-- Content Header (Page header) -->
  18. <section class="content-header">
  19. <div class="container-fluid">
  20. <div class="row mb-2">
  21. <div class="col-sm-6">
  22. <h1>编辑告警路由</h1>
  23. </div>
  24. </div>
  25. </div><!-- /.container-fluid -->
  26. </section>
  27. <!-- Main content -->
  28. <section class="content">
  29. <!-- Default box -->
  30. <div class="card">
  31. <div class="card-header">
  32. <h3 class="card-title"></h3>
  33. <div class="card-tools">
  34. <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
  35. <i class="fas fa-minus"></i>
  36. </button>
  37. <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
  38. <i class="fas fa-times"></i>
  39. </button>
  40. </div>
  41. </div>
  42. <div class="card-body">
  43. <form class="form-horizontal" method="POST" action="##" enctype="multipart/form-data" onsubmit="return false" id="formtpl">
  44. <input id="id" type="hidden" name="id" value="{{.AlertRouter.Id}}">
  45. <div class="row">
  46. <div class="col-sm-6">
  47. <!-- text input -->
  48. <div class="form-group">
  49. <label>路由名称</label>
  50. <input type="text" id="name" class="form-control" name="name" value="{{.AlertRouter.Name}}">
  51. </div>
  52. </div>
  53. <div class="col-sm-6">
  54. <!-- select -->
  55. <div class="form-group">
  56. <label>生效模版</label>
  57. <select class="form-control" name="tpl_id" id="tpl_id">
  58. <option value="{{.AlertRouter.Tpl.Id}}">{{.AlertRouter.Tpl.Tplname}}</option>
  59. </select>
  60. </div>
  61. </div>
  62. </div>
  63. <div class="row" style="display:none;">
  64. <div class="col-sm-6">
  65. <!-- text input -->
  66. <div class="form-group">
  67. <label>模版类型</label>
  68. <input type="text" id="tpltype" class="form-control" name="tpltype" value="{{.AlertRouter.Tpl.Tpltype}}" disabled>
  69. </div>
  70. </div>
  71. </div>
  72. <br />
  73. <div id="labelx"></div>
  74. <div class="row">
  75. <div class="col-sm-6">
  76. <!-- text input -->
  77. <div class="form-group">
  78. <button id='labeladd' type="button" class="btn btn-info btn-flat"><i class="far fa-plus-square"></i></button>
  79. </div>
  80. </div>
  81. </div>
  82. <br />
  83. <div class="row">
  84. <div id="Purl" class="col-sm-6">
  85. <!-- text input -->
  86. <div class="form-group">
  87. <label id="typestr">钉钉机器人地址</label>
  88. <input type="text" id="purl" class="form-control" name="purl" value="{{.AlertRouter.UrlOrPhone}}">
  89. </div>
  90. </div>
  91. <div id="Pat" class="col-sm-6">
  92. <!-- text input -->
  93. <div class="form-group">
  94. <label id="typestr">@某人</label>
  95. <input type="text" id="pat" class="form-control" name="pat" value="{{.AlertRouter.AtSomeOne}}" placeholder="钉钉@使用的是手机号码,企业微信机器人@使用的是用户帐号,">
  96. <!-- <input type="text" id="pat" class="form-control" name="pat" value="" placeholder="钉钉@使用的是手机号码,企业微信机器人@使用的是用户帐号,飞书V2 @使用的是用户Email邮箱"> -->
  97. </div>
  98. </div>
  99. <div id="Party" class="col-sm-6" style="display:none">
  100. <!-- text input -->
  101. <div class="form-group">
  102. <label id="typestr">接受部门</label>
  103. <input type="text" id="pparty" class="form-control" name="pparty" value="">
  104. </div>
  105. </div>
  106. <div id="Tag" class="col-sm-6" style="display:none">
  107. <!-- text input -->
  108. <div class="form-group">
  109. <label id="typestr">接受标签</label>
  110. <input type="text" id="ptag" class="form-control" name="ptag" value="">
  111. </div>
  112. </div>
  113. </div>
  114. <br />
  115. <div class="text-right">
  116. <a class="btn btn-app bg-success" onclick="savetpl();">
  117. <i class="fas fa-save"></i> 保存路由
  118. </a>
  119. </div>
  120. </form>
  121. </div>
  122. <!-- /.card-body -->
  123. <div class="card-footer">
  124. </div>
  125. <!-- /.card-footer-->
  126. </div>
  127. <!-- /.card -->
  128. </section>
  129. <!-- /.content -->
  130. </div>
  131. <!-- /.content-wrapper -->
  132. {{template "endhtml"}}
  133. <script>
  134. $(function () {
  135. typeChange();
  136. loadRules();
  137. $('#labeladd').click(function(){
  138. var labeldiv='<div class="row"> <div class="col-3"> <div class="form-group"> <label>Label Name</label> <input type="text" id="rules" class="form-control" name="rules" value="" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-3"> <div class="form-group"> <label>Label Value</label> <input type="text" id="rulesvalue" class="form-control" name="rulesvalue" value="" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-1 d-flex align-items-center"> <div class="form-check"> <input id="rulesregex" class="form-check-input" name="rulesregex" type="checkbox"> <label class="form-check-label">Regex</label> </div> </div> <div class="col-sm-1 d-flex align-items-center"> <div class="form-group"> <button id="labeldel" type="button" class="btn btn-danger btn-flat"><i class="fa fa-trash"></i></button> </div> </div></div> <br />';
  139. $('#labelx').append(labeldiv)
  140. });
  141. $("#labelx").delegate('#labeldel','click',function(){
  142. $(this).parents(".row").remove();
  143. });
  144. })
  145. function loadRules(){
  146. var rules_str={{.AlertRouter.Rules}};
  147. var rules_data = JSON.parse(rules_str);
  148. rules_data.forEach(function(rule_data){
  149. var ischeck=""
  150. if (rule_data.Regex){
  151. ischeck="checked"
  152. } else {
  153. ischeck=""
  154. }
  155. var labeldiv='<div class="row"> <div class="col-3"> <div class="form-group"> <label>Label Name</label> <input type="text" id="rules" class="form-control" name="rules" value="'+rule_data.Name+'" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-3"> <div class="form-group"> <label>Label Value</label> <input type="text" id="rulesvalue" class="form-control" name="rulesvalue" value="'+rule_data.Value+'" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-1 d-flex align-items-center"> <div class="form-check"> <input id="rulesregex" class="form-check-input" name="rulesregex" type="checkbox" '+ischeck+'> <label class="form-check-label">Regex</label> </div> </div> <div class="col-sm-1 d-flex align-items-center"> <div class="form-group"> <button id="labeldel" type="button" class="btn btn-danger btn-flat"><i class="fa fa-trash"></i></button> </div> </div></div> <br />';
  156. $('#labelx').append(labeldiv)
  157. });
  158. }
  159. function typeChange(){
  160. var tpl_id = document.getElementById("tpl_id");
  161. var tpltype = document.getElementById("tpltype");
  162. var typeStr = document.getElementById("typestr");
  163. var tpl_idSelect = tpl_id.options[tpl_id.selectedIndex].value; //get id
  164. if (tpltype.value=="dd"){
  165. typeStr.innerText="钉钉机器人地址(多个请以,隔开)";
  166. document.getElementById("Purl").style.display="";
  167. document.getElementById("Pat").style.display="";
  168. document.getElementById("Party").style.display="none";
  169. document.getElementById("Tag").style.display="none";
  170. } else if(tpltype.value=="wx"){
  171. typeStr.innerText="微信机器人地址(多个请以,隔开)";
  172. document.getElementById("Purl").style.display="";
  173. document.getElementById("Pat").style.display="";
  174. document.getElementById("Party").style.display="none";
  175. document.getElementById("Tag").style.display="none";
  176. } else if(tpltype.value=="fs"){
  177. typeStr.innerText="飞书机器人地址(多个请以,隔开)";
  178. document.getElementById("Purl").style.display="";
  179. document.getElementById("Pat").style.display="";
  180. document.getElementById("Party").style.display="none";
  181. document.getElementById("Tag").style.display="none";
  182. } else if(tpltype.value=="fsapp"){
  183. typeStr.innerText="飞书 用户open_id、user_id、union_ids、部门open_department_id";
  184. document.getElementById("Purl").style.display="none";
  185. document.getElementById("Pat").style.display="";
  186. document.getElementById("Party").style.display="none";
  187. document.getElementById("Tag").style.display="none";
  188. } else if(tpltype.value=="voice"){
  189. typeStr.innerText="";
  190. document.getElementById("Purl").style.display="none";
  191. document.getElementById("Pat").style.display="none";
  192. document.getElementById("Party").style.display="none";
  193. document.getElementById("Tag").style.display="none";
  194. } else if(tpltype.value=="webhook"){
  195. typeStr.innerText="webhook地址(多个请以,隔开)";
  196. document.getElementById("Purl").style.display="";
  197. document.getElementById("Pat").style.display="none";
  198. document.getElementById("Party").style.display="none";
  199. document.getElementById("Tag").style.display="none";
  200. } else if(tpltype.value=="email"){
  201. typeStr.innerText="邮箱(多个请以,隔开)";
  202. document.getElementById("Purl").style.display="";
  203. document.getElementById("Pat").style.display="none";
  204. document.getElementById("Party").style.display="none";
  205. document.getElementById("Tag").style.display="none";
  206. } else if(tpltype.value=="rl"){
  207. typeStr.innerText="百度Hi(如流)群id";
  208. document.getElementById("Purl").style.display="";
  209. document.getElementById("Pat").style.display="none";
  210. document.getElementById("Party").style.display="none";
  211. document.getElementById("Tag").style.display="none";
  212. } else if(tpltype.value=="workwechat"){
  213. alert("workwechat暂不支持告警路由,待后期开启!");
  214. /*typeStr.innerText="接受用户";
  215. document.getElementById("Pat").style.display="none";
  216. document.getElementById("Party").style.display="";
  217. document.getElementById("Tag").style.display="";*/
  218. } else{
  219. typeStr.innerText="手机号(多个请以,隔开)";
  220. document.getElementById("Purl").style.display="";
  221. document.getElementById("Pat").style.display="none";
  222. document.getElementById("Party").style.display="none";
  223. document.getElementById("Tag").style.display="none";
  224. }
  225. }
  226. function savetpl(){
  227. var uname=document.getElementById("rules");
  228. if (uname.value.length==0){
  229. alert('路由规则不能为空');
  230. return
  231. };
  232. var ucontent=document.getElementById("purl");
  233. if (ucontent.value.length==0){
  234. alert('url 或 手机号参数不能为空');
  235. return
  236. };
  237. //取到所有rules
  238. var RouterRules =new Array();
  239. $("#labelx > div").each(function() {
  240. var LabelMap={
  241. Name: $(this).find("#rules").val(),
  242. Value: $(this).find("#rulesvalue").val(),
  243. Regex: $(this).find("#rulesregex").is(":checked")
  244. };
  245. RouterRules.push(LabelMap);
  246. });
  247. var alert_router_json_str={
  248. RouterId: $('#id').val(),
  249. RouterName: $('#name').val(),
  250. RouterTplId: $('#tpl_id').val(),
  251. RouterPurl: $('#purl').val(),
  252. RouterPat: $('#pat').val(),
  253. Rules: RouterRules
  254. };
  255. var json_data = JSON.stringify(alert_router_json_str);
  256. $.ajax({
  257. type: "POST",
  258. dataType: "json",
  259. url: '{{ urlfor "MainController.AddRouter"}}',
  260. data: json_data,
  261. success: function (result) {
  262. console.log(result);
  263. if (result == null) {
  264. alert("告警路由保存成功!");
  265. window.location.replace("/alertrouter")
  266. } else {
  267. alert("告警路由保存失败!");
  268. };
  269. },
  270. });
  271. }
  272. </script>
  273. {{template "jshtml"}}