graphiteFuncs.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. define([
  2. 'underscore'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. var funcDefList = [];
  7. var index = [];
  8. function addFuncDef(funcDef) {
  9. funcDefList.push(funcDef);
  10. index[funcDef.name] = funcDef;
  11. index[funcDef.shortName || funcDef.name] = funcDef;
  12. }
  13. addFuncDef({
  14. name: 'scaleToSeconds',
  15. params: [ { name: 'seconds', type: 'int' } ],
  16. defaultParams: [1],
  17. });
  18. addFuncDef({
  19. name: "alias",
  20. params: [
  21. { name: "alias", type: 'string' }
  22. ],
  23. defaultParams: ['alias']
  24. });
  25. addFuncDef({
  26. name: 'sumSeries',
  27. shortName: 'sum',
  28. params: [],
  29. defaultParams: []
  30. });
  31. addFuncDef({
  32. name: "groupByNode",
  33. params: [
  34. {
  35. name: "node",
  36. type: "node",
  37. },
  38. {
  39. name: "function",
  40. type: "function",
  41. }
  42. ],
  43. defaultParams: [3, "sum"]
  44. });
  45. addFuncDef({
  46. name: 'aliasByNode',
  47. params: [ { name: "node", type: "node", } ],
  48. defaultParams: [3]
  49. });
  50. function FuncInstance(funcDef) {
  51. this.def = funcDef;
  52. this.params = funcDef.defaultParams.slice(0);
  53. this.updateText();
  54. }
  55. FuncInstance.prototype.updateText = function () {
  56. if (this.params.length === 0) {
  57. this.text = this.def.name + '()';
  58. return;
  59. }
  60. var text = this.def.name + '(';
  61. _.each(this.def.params, function(param, index) {
  62. text += this.params[index] + ', ';
  63. }, this);
  64. text = text.substring(0, text.length - 2);
  65. text += ')';
  66. this.text = text;
  67. };
  68. return {
  69. createFuncInstance: function(name) {
  70. if (_.isString(name)) {
  71. var funcDef = index[name];
  72. if (!funcDef) {
  73. throw { message: 'Method not found ' + name };
  74. }
  75. name = funcDef;
  76. }
  77. return new FuncInstance(name);
  78. },
  79. getDefList: function() {
  80. return funcDefList.slice(0);
  81. }
  82. };
  83. });