portal.js 18 KB

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