portal.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // - business function -
  2. function query_user() {
  3. var query = $.trim($("#query").val());
  4. var mine = document.getElementById('mine').checked ? 1 : 0;
  5. window.location.href = '/portal/hostgroup?q=' + query + '&mine=' + mine;
  6. }
  7. function create_hostgroup() {
  8. var name = $.trim($("#grp_name").val());
  9. $.post('/portal/group/create', {'grp_name': name}, function (json) {
  10. handle_quietly(json, function () {
  11. window.location.reload();
  12. });
  13. }, "json");
  14. }
  15. function delete_hostgroup(group_id) {
  16. my_confirm('确定要删除???', ['确定', '取消'], function () {
  17. $.getJSON('/portal/group/delete/' + group_id, {}, function (json) {
  18. handle_quietly(json, function () {
  19. location.reload();
  20. });
  21. });
  22. }, function () {
  23. return false;
  24. });
  25. }
  26. function edit_hostgroup(group_id, grp_name) {
  27. layer.prompt({title: 'input new name:', val: grp_name, length: 255}, function (val, index, elem) {
  28. $.post('/portal/group/update/' + group_id, {'new_name': val}, function (json) {
  29. handle_quietly(json, function () {
  30. location.reload();
  31. });
  32. }, "json");
  33. })
  34. }
  35. function rename_hostgroup() {
  36. var old_str = $.trim($('#old_str').val());
  37. var new_str = $.trim($('#new_str').val());
  38. $.post('/portal/group/rename', {'old_str': old_str, 'new_str': new_str}, function (json) {
  39. handle_quietly(json, function () {
  40. window.location.href = '/?q=' + new_str;
  41. });
  42. }, "json");
  43. }
  44. function bind_plugin(group_id) {
  45. var plugin_idr = $.trim($("#plugin_dir").val());
  46. $.post('/portal/plugin/bind', {'group_id': group_id, 'plugin_dir': plugin_idr}, function (json) {
  47. handle_quietly(json, function () {
  48. location.reload();
  49. });
  50. }, "json");
  51. }
  52. function unbind_plugin(plugin_id) {
  53. my_confirm('确定要解除绑定?', ['确定', '取消'], function () {
  54. $.getJSON('/portal/plugin/delete/' + plugin_id, {}, function (json) {
  55. handle_quietly(json, function () {
  56. location.reload();
  57. });
  58. });
  59. }, function () {
  60. return false;
  61. });
  62. }
  63. function query_host() {
  64. var xbox = $("#xbox").val();
  65. var group_id = $("#group_id").val();
  66. var query = $.trim($("#query").val());
  67. var limit = $("#limit").val();
  68. var maintaining = document.getElementById('maintaining').checked ? 1 : 0;
  69. window.location.href = '/portal/group/' + group_id + '/hosts?q=' + query + '&maintaining=' + maintaining + '&limit=' + limit + '&xbox=' + xbox;
  70. }
  71. function select_all() {
  72. var v = document.getElementById('chk').checked;
  73. $.each($("#hosts input[type=checkbox]"), function (i, n) {
  74. n.checked = v;
  75. });
  76. }
  77. function remove_hosts() {
  78. var ids = [];
  79. jQuery.each($("#hosts input[type=checkbox]"), function (i, n) {
  80. if (n.checked) {
  81. ids.push($(n).attr("hid"));
  82. }
  83. });
  84. if (ids.length == 0) {
  85. err_message_quietly('no hosts selected');
  86. return;
  87. }
  88. var group_id = $("#group_id").val();
  89. $.post("/portal/host/remove", {'host_ids': ids.join(","), 'grp_id': group_id}, function (json) {
  90. handle_quietly(json, function () {
  91. location.reload();
  92. });
  93. }, "json");
  94. }
  95. function maintain() {
  96. var ids = [];
  97. jQuery.each($("#hosts input[type=checkbox]"), function (i, n) {
  98. if (n.checked) {
  99. ids.push($(n).attr("hid"));
  100. }
  101. });
  102. if (ids.length == 0) {
  103. err_message_quietly('no hosts selected');
  104. return;
  105. }
  106. var begin = $.trim($("#begin").val());
  107. var end = $.trim($("#end").val());
  108. if (begin.length == 0 || end.length == 0) {
  109. err_message_quietly('begin time and end time are necessary');
  110. return false;
  111. }
  112. var b = moment(begin, "YYYY-MM-DD HH:mm").unix();
  113. var e = moment(end, "YYYY-MM-DD HH:mm").unix();
  114. $.post('/portal/host/maintain', {'begin': b, 'end': e, 'host_ids': ids.join(',')}, function (json) {
  115. handle_quietly(json, function () {
  116. location.reload();
  117. });
  118. }, "json");
  119. }
  120. function no_maintain() {
  121. var ids = [];
  122. jQuery.each($("#hosts input[type=checkbox]"), function (i, n) {
  123. if (n.checked) {
  124. ids.push($(n).attr("hid"));
  125. }
  126. });
  127. if (ids.length == 0) {
  128. err_message_quietly('no hosts selected');
  129. return;
  130. }
  131. $.post('/portal/host/reset', {'host_ids': ids.join(',')}, function (json) {
  132. handle_quietly(json, function () {
  133. location.reload();
  134. });
  135. }, "json");
  136. }
  137. function batch_add_host() {
  138. var hosts = $.trim($("#hosts").val());
  139. if (hosts.length == 0) {
  140. err_message_quietly('请填写机器列表,一行一个');
  141. return false;
  142. }
  143. $.post('/portal/host/add', {'group_id': $("#group_id").val(), 'hosts': hosts}, function (json) {
  144. if (json.msg.length > 0) {
  145. err_message_quietly(json.msg);
  146. } else {
  147. $("#message").html(json.data);
  148. }
  149. }, "json");
  150. }
  151. function host_unbind_group(host_id, group_id) {
  152. $.getJSON('/portal/host/unbind', {'host_id': host_id, 'group_id': group_id}, function (json) {
  153. handle_quietly(json, function () {
  154. location.reload();
  155. });
  156. })
  157. }
  158. function query_expression() {
  159. var query = $.trim($("#query").val());
  160. var mine = document.getElementById('mine').checked ? 1 : 0;
  161. window.location.href = '/portal/expression?q=' + query + '&mine=' + mine;
  162. }
  163. function delete_expression(id) {
  164. my_confirm('确定要删除???', ['确定', '取消'], function () {
  165. $.getJSON('/portal/expression/delete/' + id, {}, function (json) {
  166. handle_quietly(json, function () {
  167. location.reload();
  168. });
  169. })
  170. }, function () {
  171. return false;
  172. });
  173. }
  174. function update_expression() {
  175. var callback_url = $.trim($("#callback_url").val());
  176. var need_callback = callback_url.length > 0 ? 1 : 0;
  177. $.post(
  178. '/portal/expression/update',
  179. {
  180. 'expression': $.trim($("#expression").val()),
  181. 'func': $.trim($("#func").val()),
  182. 'op': $("#op").val(),
  183. 'right_value': $.trim($("#right_value").val()),
  184. 'uic': $.trim($("#uic").val()),
  185. 'max_step': $.trim($("#max_step").val()),
  186. 'priority': $.trim($("#priority").val()),
  187. 'note': $.trim($("#note").val()),
  188. 'url': callback_url,
  189. 'callback': need_callback,
  190. 'before_callback_sms': document.getElementById("before_callback_sms").checked ? 1 : 0,
  191. 'before_callback_mail': document.getElementById("before_callback_mail").checked ? 1 : 0,
  192. 'after_callback_sms': document.getElementById("after_callback_sms").checked ? 1 : 0,
  193. 'after_callback_mail': document.getElementById("after_callback_mail").checked ? 1 : 0,
  194. 'expression_id': $("#expression_id").val()
  195. },
  196. function (json) {
  197. handle_quietly(json);
  198. }, "json");
  199. }
  200. function pause_expression(id) {
  201. var pause = '1';
  202. if ($('#i-' + id).attr('class').indexOf('play') > 0) {
  203. // current: pause
  204. pause = '0'
  205. }
  206. $.getJSON("/portal/expression/pause", {'id': id, 'pause': pause}, function (json) {
  207. if (json.msg.length > 0) {
  208. err_message_quietly(json.msg);
  209. } else {
  210. if (pause == '1') {
  211. $('#i-' + id).attr('class', 'glyphicon glyphicon-play orange')
  212. } else {
  213. $('#i-' + id).attr('class', 'glyphicon glyphicon-pause orange')
  214. }
  215. }
  216. });
  217. }
  218. function make_select2_for_uic_group(selector) {
  219. $(selector).select2({
  220. placeholder: "input uic team name",
  221. allowClear: true,
  222. multiple: true,
  223. quietMillis: 100,
  224. minimumInputLength: 2,
  225. id: function (obj) {
  226. return obj.name;
  227. },
  228. ajax: {
  229. url: "/api/uic/group",
  230. dataType: 'json',
  231. data: function (term, page) {
  232. return {
  233. query: term,
  234. limit: 20
  235. };
  236. },
  237. results: function (json, page) {
  238. return {results: json.data};
  239. }
  240. },
  241. initSelection: function (element, callback) {
  242. var data = [];
  243. $($(element).val().split(",")).each(function () {
  244. data.push({id: this, name: this});
  245. });
  246. callback(data);
  247. },
  248. formatResult: function (obj) {
  249. return obj.name
  250. },
  251. formatSelection: function (obj) {
  252. return obj.name
  253. }
  254. });
  255. }
  256. function query_template() {
  257. var query = $.trim($("#query").val());
  258. var mine = document.getElementById('mine').checked ? 1 : 0;
  259. window.location.href = '/portal/template?q=' + query + '&mine=' + mine;
  260. }
  261. function delete_template(id) {
  262. my_confirm('确定要删除???', ['确定', '取消'], function () {
  263. $.getJSON('/portal/template/delete/' + id, {}, function (json) {
  264. handle_quietly(json, function () {
  265. location.reload();
  266. });
  267. })
  268. }, function () {
  269. return false;
  270. });
  271. }
  272. function create_template() {
  273. var tpl_name = $.trim($("#tpl_name").val());
  274. $.post('/portal/template/create', {'name': tpl_name}, function (json) {
  275. if (json.msg.length > 0) {
  276. err_message_quietly(json.msg);
  277. } else {
  278. location.href = '/portal/template/update/' + json.id;
  279. }
  280. }, "json");
  281. }
  282. function make_select2_for_template(selector) {
  283. $(selector).select2({
  284. placeholder: "input template name",
  285. allowClear: true,
  286. quietMillis: 100,
  287. minimumInputLength: 2,
  288. id: function (obj) {
  289. return obj.id;
  290. },
  291. ajax: {
  292. url: "/api/template/query",
  293. dataType: 'json',
  294. data: function (term, page) {
  295. return {
  296. query: term,
  297. limit: 10
  298. };
  299. },
  300. results: function (json, page) {
  301. return {results: json.data};
  302. }
  303. },
  304. initSelection: function (element, callback) {
  305. var tpl_id = $(element).val();
  306. $.getJSON("/api/template/" + tpl_id, function (json) {
  307. callback(json.data);
  308. });
  309. },
  310. formatResult: function (obj) {
  311. return obj.name
  312. },
  313. formatSelection: function (obj) {
  314. return obj.name
  315. }
  316. });
  317. }
  318. function make_select2_for_metric(selector) {
  319. $(selector).select2({
  320. placeholder: "input metric",
  321. allowClear: true,
  322. quietMillis: 100,
  323. minimumInputLength: 2,
  324. id: function (obj) {
  325. return obj.name;
  326. },
  327. ajax: {
  328. url: "/api/metric/query",
  329. dataType: 'json',
  330. data: function (term, page) {
  331. return {
  332. query: term,
  333. limit: 10
  334. };
  335. },
  336. results: function (json, page) {
  337. return {results: json.data};
  338. }
  339. },
  340. initSelection: function (element, callback) {
  341. var val = $(element).val();
  342. callback({id: val, name: val});
  343. },
  344. formatResult: function (obj) {
  345. return obj.name
  346. },
  347. formatSelection: function (obj) {
  348. return obj.name
  349. }
  350. });
  351. }
  352. function update_template() {
  353. var tpl_id = $('#tpl_id').val();
  354. var name = $.trim($("#name").val());
  355. var parent_id = $("#parent_id").val();
  356. $.post('/portal/template/rename/' + tpl_id, {'name': name, 'parent_id': parent_id}, function (json) {
  357. handle_quietly(json);
  358. }, "json");
  359. }
  360. function save_action_for_tpl(tpl_id) {
  361. var callback_url = $.trim($("#callback_url").val());
  362. var need_callback = callback_url.length > 0 ? 1 : 0;
  363. $.post(
  364. '/portal/template/action/update/' + tpl_id,
  365. {
  366. 'uic': $.trim($("#uic").val()),
  367. 'url': callback_url,
  368. 'callback': need_callback,
  369. 'before_callback_sms': document.getElementById("before_callback_sms").checked ? 1 : 0,
  370. 'before_callback_mail': document.getElementById("before_callback_mail").checked ? 1 : 0,
  371. 'after_callback_sms': document.getElementById("after_callback_sms").checked ? 1 : 0,
  372. 'after_callback_mail': document.getElementById("after_callback_mail").checked ? 1 : 0
  373. },
  374. function (json) {
  375. handle_quietly(json);
  376. }, "json");
  377. }
  378. function goto_strategy_add_div() {
  379. $("#add_div").show('fast');
  380. $("#current_sid").val('');
  381. location.href = "#add";
  382. }
  383. function save_strategy() {
  384. var sid = $("#current_sid").val();
  385. $.post('/portal/strategy/update', {
  386. 'sid': sid,
  387. 'metric': $.trim($("#metric").val()),
  388. 'tags': $.trim($("#tags").val()),
  389. 'max_step': $.trim($("#max_step").val()),
  390. 'priority': $.trim($("#priority").val()),
  391. 'note': $.trim($("#note").val()),
  392. 'func': $.trim($("#func").val()),
  393. 'op': $.trim($("#op").val()),
  394. 'right_value': $.trim($("#right_value").val()),
  395. 'run_begin': $.trim($("#run_begin").val()),
  396. 'run_end': $.trim($("#run_end").val()),
  397. 'tpl_id': $.trim($("#tpl_id").val())
  398. }, function (json) {
  399. handle_quietly(json, function () {
  400. location.reload();
  401. });
  402. }, "json")
  403. }
  404. function clone_strategy(sid) {
  405. $("#current_sid").val('');
  406. fill_fields(sid);
  407. }
  408. function modify_strategy(sid) {
  409. $("#current_sid").val(sid);
  410. fill_fields(sid);
  411. }
  412. function fill_fields(sid) {
  413. $("#add_div").show('fast');
  414. location.href = "#add";
  415. $.getJSON('/portal/strategy/' + sid, {}, function (json) {
  416. $("#metric").val(json.data.metric);
  417. $("#tags").val(json.data.tags);
  418. $("#max_step").val(json.data.max_step);
  419. $("#priority").val(json.data.priority);
  420. $("#note").val(json.data.note);
  421. $("#func").val(json.data.func);
  422. $("#op").val(json.data.op);
  423. $("#right_value").val(json.data.right_value);
  424. $("#run_begin").val(json.data.run_begin);
  425. $("#run_end").val(json.data.run_end);
  426. make_select2_for_metric("#metric");
  427. });
  428. }
  429. function delete_strategy(id) {
  430. my_confirm('确定要删除???', ['确定', '取消'], function () {
  431. $.getJSON('/portal/strategy/delete/' + id, {}, function (json) {
  432. handle_quietly(json, function () {
  433. location.reload();
  434. });
  435. })
  436. }, function () {
  437. return false;
  438. });
  439. }
  440. function tpl_unbind_group(tpl_id, grp_id) {
  441. my_confirm('确定要解除绑定关系?', ['确定', '取消'], function () {
  442. $.getJSON('/portal/template/unbind/group', {'tpl_id': tpl_id, 'grp_id': grp_id}, function (json) {
  443. handle_quietly(json, function () {
  444. location.reload();
  445. });
  446. })
  447. }, function () {
  448. return false;
  449. });
  450. }
  451. function fork_template(tpl_id) {
  452. $.getJSON('/portal/template/fork/' + tpl_id, {}, function (json) {
  453. if (json.msg.length > 0) {
  454. err_message_quietly(json.msg);
  455. } else {
  456. location.href = '/portal/template/update/' + json.id;
  457. }
  458. });
  459. }
  460. function bind_template(grp_id) {
  461. var tpl_id = $.trim($("#tpl_id").val());
  462. $.getJSON('/portal/group/bind/template', {'grp_id': grp_id, 'tpl_id': tpl_id}, function (json) {
  463. handle_quietly(json, function () {
  464. location.reload();
  465. })
  466. });
  467. }
  468. function node_unbind_tpl(grp_name, tpl_id) {
  469. my_confirm('确定要解除绑定关系?', ['确定', '取消'], function () {
  470. $.getJSON('/portal/template/unbind/node', {'tpl_id': tpl_id, 'grp_name': grp_name}, function (json) {
  471. handle_quietly(json, function () {
  472. location.reload();
  473. });
  474. })
  475. }, function () {
  476. return false;
  477. });
  478. }
  479. function node_bind_tpl() {
  480. var node = $.trim($("#node").val());
  481. var tpl_id = $("#tpl_id").val();
  482. $.post('/portal/template/bind/node', {'node': node, 'tpl_id': tpl_id}, function (json) {
  483. handle_quietly(json, function () {
  484. location.reload();
  485. });
  486. }, "json");
  487. }
  488. function create_cluster_monitor_metric(grp_id) {
  489. $.post('/portal/group/' + grp_id + '/cluster/creator', {
  490. 'numerator': $("#numerator").val(),
  491. 'denominator': $("#denominator").val(),
  492. 'endpoint': $("#endpoint").val(),
  493. 'metric': $("#metric").val(),
  494. 'tags': $("#tags").val(),
  495. 'step': $("#step").val()
  496. }, function (json) {
  497. handle_quietly(json, function () {
  498. location.href = "/portal/group/" + grp_id + "/cluster";
  499. });
  500. }, "json")
  501. }
  502. function update_cluster_monitor_metric(cluster_id, grp_id) {
  503. $.post('/portal/cluster/edit/' + cluster_id, {
  504. 'numerator': $("#numerator").val(),
  505. 'denominator': $("#denominator").val(),
  506. 'endpoint': $("#endpoint").val(),
  507. 'metric': $("#metric").val(),
  508. 'tags': $("#tags").val(),
  509. 'step': $("#step").val(),
  510. 'grp_id': grp_id
  511. }, function (json) {
  512. handle_quietly(json);
  513. }, "json");
  514. }
  515. function delete_cluster_monitor_item(cluster_id) {
  516. my_confirm('确定要删除???', ['确定', '取消'], function () {
  517. $.post('/portal/cluster/delete/' + cluster_id, {}, function (json) {
  518. handle_quietly(json, function () {
  519. location.reload();
  520. })
  521. }, "json");
  522. }, function () {
  523. return false;
  524. });
  525. }
  526. // - alarm-dash business function -
  527. function alarm_case_all_select() {
  528. var boxes = $("input[type=checkbox]");
  529. for (var i = 0; i < boxes.length; i++) {
  530. boxes[i].checked="checked";
  531. }
  532. }
  533. function alarm_case_event_all_select() {
  534. var boxes = $("input[type=checkbox]");
  535. for (var i = 0; i < boxes.length; i++) {
  536. boxes[i].checked="checked";
  537. }
  538. }
  539. function alarm_case_reverse_select() {
  540. var boxes = $("input[type=checkbox]");
  541. for (var i = 0; i < boxes.length; i++) {
  542. if (boxes[i].checked) {
  543. boxes[i].checked=""
  544. } else {
  545. boxes[i].checked="checked";
  546. }
  547. }
  548. }
  549. function alarm_case_event_reverse_select() {
  550. var boxes = $("input[type=checkbox]");
  551. for (var i = 0; i < boxes.length; i++) {
  552. if (boxes[i].checked) {
  553. boxes[i].checked=""
  554. } else {
  555. boxes[i].checked="checked";
  556. }
  557. }
  558. }
  559. function alarm_case_batch_rm() {
  560. var boxes = $("input[type=checkbox]");
  561. var ids = []
  562. for (var i = 0; i < boxes.length; i++) {
  563. if (boxes[i].checked) {
  564. ids.push($(boxes[i]).attr("alarm"))
  565. }
  566. }
  567. my_confirm('确定要删除???', ['确定', '取消'], function () {
  568. $.post('/portal/alarm-dash/case/delete', {"ids": ids.join(',')}, function (json) {
  569. handle_quietly(json, function () {
  570. location.reload();
  571. });
  572. }, "json");
  573. }, function () {
  574. return false;
  575. });
  576. }
  577. function alarm_case_rm(id) {
  578. my_confirm('确定要删除???', ['确定', '取消'], function () {
  579. $.post('/portal/alarm-dash/case/delete', {"ids": id}, function (json) {
  580. handle_quietly(json, function () {
  581. location.reload();
  582. });
  583. }, "json");
  584. }, function () {
  585. return false;
  586. });
  587. }
  588. function alarm_case_event_rm(id) {
  589. my_confirm('确定要删除???', ['确定', '取消'], function () {
  590. $.post('/portal/alarm-dash/case/event/delete', {"ids": id}, function (json) {
  591. handle_quietly(json, function () {
  592. location.reload();
  593. });
  594. }, "json");
  595. }, function () {
  596. return false;
  597. });
  598. }
  599. function alarm_case_event_batch_rm() {
  600. var boxes = $("input[type=checkbox]");
  601. var ids = []
  602. for (var i = 0; i < boxes.length; i++) {
  603. if (boxes[i].checked) {
  604. ids.push($(boxes[i]).attr("alarm"))
  605. }
  606. }
  607. my_confirm('确定要删除???', ['确定', '取消'], function () {
  608. $.post('/portal/alarm-dash/case/event/delete', {"ids": ids.join(',')}, function (json) {
  609. handle_quietly(json, function () {
  610. location.reload();
  611. });
  612. }, "json");
  613. }, function () {
  614. return false;
  615. });
  616. }