query_part.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. define([
  2. 'lodash',
  3. 'jquery'
  4. ],
  5. function (_, $) {
  6. 'use strict';
  7. var index = [];
  8. var categories = {
  9. Combine: [],
  10. Transform: [],
  11. Calculate: [],
  12. Filter: [],
  13. Special: []
  14. };
  15. function addFuncDef(funcDef) {
  16. funcDef.params = funcDef.params || [];
  17. funcDef.defaultParams = funcDef.defaultParams || [];
  18. if (funcDef.category) {
  19. funcDef.category.push(funcDef);
  20. }
  21. index[funcDef.name] = funcDef;
  22. index[funcDef.shortName || funcDef.name] = funcDef;
  23. }
  24. addFuncDef({
  25. name: 'field',
  26. category: categories.Transform,
  27. params: [{type: 'field'}],
  28. defaultParams: ['value'],
  29. });
  30. addFuncDef({
  31. name: 'mean',
  32. category: categories.Transform,
  33. params: [],
  34. defaultParams: [],
  35. });
  36. addFuncDef({
  37. name: 'derivate',
  38. category: categories.Transform,
  39. params: [{ name: "rate", type: "interval", options: ['1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
  40. defaultParams: ['10s'],
  41. });
  42. addFuncDef({
  43. name: 'time',
  44. category: categories.Transform,
  45. params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
  46. defaultParams: ['$interval'],
  47. });
  48. addFuncDef({
  49. name: 'math',
  50. category: categories.Transform,
  51. params: [{ name: "expr", type: "string"}],
  52. defaultParams: [' / 100'],
  53. });
  54. addFuncDef({
  55. name: 'alias',
  56. category: categories.Transform,
  57. params: [{ name: "name", type: "string"}],
  58. defaultParams: ['alias'],
  59. });
  60. _.each(categories, function(funcList, catName) {
  61. categories[catName] = _.sortBy(funcList, 'name');
  62. });
  63. function FuncInstance(funcDef, options) {
  64. this.def = funcDef;
  65. this.params = [];
  66. if (options && options.withDefaultParams) {
  67. this.params = funcDef.defaultParams.slice(0);
  68. }
  69. this.updateText();
  70. }
  71. FuncInstance.prototype.render = function(metricExp) {
  72. var str = this.def.name + '(';
  73. var parameters = _.map(this.params, function(value, index) {
  74. var paramType = this.def.params[index].type;
  75. if (paramType === 'int' || paramType === 'value_or_series' || paramType === 'boolean') {
  76. return value;
  77. }
  78. else if (paramType === 'int_or_interval' && $.isNumeric(value)) {
  79. return value;
  80. }
  81. return "'" + value + "'";
  82. }, this);
  83. if (metricExp) {
  84. parameters.unshift(metricExp);
  85. }
  86. return str + parameters.join(', ') + ')';
  87. };
  88. FuncInstance.prototype._hasMultipleParamsInString = function(strValue, index) {
  89. if (strValue.indexOf(',') === -1) {
  90. return false;
  91. }
  92. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  93. };
  94. FuncInstance.prototype.updateParam = function(strValue, index) {
  95. // handle optional parameters
  96. // if string contains ',' and next param is optional, split and update both
  97. if (this._hasMultipleParamsInString(strValue, index)) {
  98. _.each(strValue.split(','), function(partVal, idx) {
  99. this.updateParam(partVal.trim(), idx);
  100. }, this);
  101. return;
  102. }
  103. if (strValue === '' && this.def.params[index].optional) {
  104. this.params.splice(index, 1);
  105. }
  106. else {
  107. this.params[index] = strValue;
  108. }
  109. this.updateText();
  110. };
  111. FuncInstance.prototype.updateText = function () {
  112. if (this.params.length === 0) {
  113. this.text = this.def.name + '()';
  114. return;
  115. }
  116. var text = this.def.name + '(';
  117. text += this.params.join(', ');
  118. text += ')';
  119. this.text = text;
  120. };
  121. return {
  122. create: function(funcDef, options) {
  123. if (_.isString(funcDef)) {
  124. if (!index[funcDef]) {
  125. throw { message: 'Method not found ' + name };
  126. }
  127. funcDef = index[funcDef];
  128. }
  129. return new FuncInstance(funcDef, options);
  130. },
  131. getFuncDef: function(name) {
  132. return index[name];
  133. },
  134. getCategories: function() {
  135. return categories;
  136. }
  137. };
  138. });