param_flow_service.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Parameter flow control service.
  3. *
  4. * @author Eric Zhao
  5. */
  6. angular.module('sentinelDashboardApp').service('ParamFlowService', ['$http', function ($http) {
  7. this.queryMachineRules = function(app, ip, port) {
  8. var param = {
  9. app: app,
  10. ip: ip,
  11. port: port
  12. };
  13. return $http({
  14. url: '/paramFlow/rules',
  15. params: param,
  16. method: 'GET'
  17. });
  18. };
  19. this.addNewRule = function(rule) {
  20. return $http({
  21. url: '/paramFlow/rule',
  22. data: rule,
  23. method: 'POST'
  24. });
  25. };
  26. this.saveRule = function (entity) {
  27. return $http({
  28. url: '/paramFlow/rule/' + entity.id,
  29. data: entity,
  30. method: 'PUT'
  31. });
  32. };
  33. this.deleteRule = function (entity) {
  34. return $http({
  35. url: '/paramFlow/rule/' + entity.id,
  36. method: 'DELETE'
  37. });
  38. };
  39. function isNumberClass(classType) {
  40. return classType === 'int' || classType === 'double' ||
  41. classType === 'float' || classType === 'long' || classType === 'short';
  42. }
  43. function isByteClass(classType) {
  44. return classType === 'byte';
  45. }
  46. function notNumberAtLeastZero(num) {
  47. return num === undefined || num === '' || isNaN(num) || num < 0;
  48. }
  49. function notGoodNumber(num) {
  50. return num === undefined || num === '' || isNaN(num);
  51. }
  52. function notGoodNumberBetweenExclusive(num, l ,r) {
  53. return num === undefined || num === '' || isNaN(num) || num < l || num > r;
  54. }
  55. function notValidParamItem(curExItem) {
  56. if (isNumberClass(curExItem.classType) && notGoodNumber(curExItem.object)) {
  57. return true;
  58. }
  59. if (isByteClass(curExItem.classType) && notGoodNumberBetweenExclusive(curExItem.object, -128, 127)) {
  60. return true;
  61. }
  62. return curExItem.object === undefined || curExItem.classType === undefined ||
  63. notNumberAtLeastZero(curExItem.count);
  64. }
  65. this.checkRuleValid = function (rule) {
  66. if (!rule.resource || rule.resource === '') {
  67. alert('资源名称不能为空');
  68. return false;
  69. }
  70. if (rule.grade != 1) {
  71. alert('未知的限流模式');
  72. return false;
  73. }
  74. if (rule.count < 0) {
  75. alert('限流阈值必须大于等于 0');
  76. return false;
  77. }
  78. if (rule.paramIdx === undefined || rule.paramIdx === '' || isNaN(rule.paramIdx) || rule.paramIdx < 0) {
  79. alert('热点参数索引必须大于等于 0');
  80. return false;
  81. }
  82. if (rule.paramFlowItemList !== undefined) {
  83. for (var i = 0; i < rule.paramFlowItemList.length; i++) {
  84. var item = rule.paramFlowItemList[i];
  85. if (notValidParamItem(item)) {
  86. alert('热点参数例外项不合法,请检查值和类型是否正确:参数为 ' + item.object + ', 类型为 ' +
  87. item.classType + ', 限流阈值为 ' + item.count);
  88. return false;
  89. }
  90. }
  91. }
  92. return true;
  93. };
  94. }]);