query_part.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ = require('lodash');
  3. var index = [];
  4. var categories = {
  5. Aggregations: [],
  6. Transformations: [],
  7. Math: [],
  8. Aliasing: [],
  9. Fields: [],
  10. };
  11. var groupByTimeFunctions = [];
  12. class QueryPartDef {
  13. type: string;
  14. params: any[];
  15. defaultParams: any[];
  16. renderer: any;
  17. category: any;
  18. addStrategy: any;
  19. constructor(options: any) {
  20. this.type = options.type;
  21. this.params = options.params;
  22. this.defaultParams = options.defaultParams;
  23. this.renderer = options.renderer;
  24. this.category = options.category;
  25. this.addStrategy = options.addStrategy;
  26. }
  27. static register(options: any) {
  28. index[options.type] = new QueryPartDef(options);
  29. options.category.push(index[options.type]);
  30. }
  31. }
  32. function functionRenderer(part, innerExpr) {
  33. var str = part.def.type + '(';
  34. var parameters = _.map(part.params, (value, index) => {
  35. var paramType = part.def.params[index];
  36. if (paramType.quote === 'single') {
  37. return "'" + value + "'";
  38. } else if (paramType.quote === 'double') {
  39. return '"' + value + '"';
  40. }
  41. return value;
  42. });
  43. if (innerExpr) {
  44. parameters.unshift(innerExpr);
  45. }
  46. return str + parameters.join(', ') + ')';
  47. }
  48. function aliasRenderer(part, innerExpr) {
  49. return innerExpr + ' AS ' + '"' + part.params[0] + '"';
  50. }
  51. function suffixRenderer(part, innerExpr) {
  52. return innerExpr + ' ' + part.params[0];
  53. }
  54. function identityRenderer(part, innerExpr) {
  55. return part.params[0];
  56. }
  57. function quotedIdentityRenderer(part, innerExpr) {
  58. return '"' + part.params[0] + '"';
  59. }
  60. function replaceAggregationAddStrategy(selectParts, partModel) {
  61. // look for existing aggregation
  62. for (var i = 0; i < selectParts.length; i++) {
  63. var part = selectParts[i];
  64. if (part.def.category === categories.Aggregations) {
  65. selectParts[i] = partModel;
  66. return;
  67. }
  68. }
  69. selectParts.splice(1, 0, partModel);
  70. }
  71. function addTransformationStrategy(selectParts, partModel) {
  72. var i;
  73. // look for index to add transformation
  74. for (i = 0; i < selectParts.length; i++) {
  75. var part = selectParts[i];
  76. if (part.def.category === categories.Math || part.def.category === categories.Aliasing) {
  77. break;
  78. }
  79. }
  80. selectParts.splice(i, 0, partModel);
  81. }
  82. function addMathStrategy(selectParts, partModel) {
  83. var partCount = selectParts.length;
  84. if (partCount > 0) {
  85. // if last is math, replace it
  86. if (selectParts[partCount-1].def.type === 'math') {
  87. selectParts[partCount-1] = partModel;
  88. return;
  89. }
  90. // if next to last is math, replace it
  91. if (selectParts[partCount-2].def.type === 'math') {
  92. selectParts[partCount-2] = partModel;
  93. return;
  94. }
  95. // if last is alias add it before
  96. else if (selectParts[partCount-1].def.type === 'alias') {
  97. selectParts.splice(partCount-1, 0, partModel);
  98. return;
  99. }
  100. }
  101. selectParts.push(partModel);
  102. }
  103. function addAliasStrategy(selectParts, partModel) {
  104. var partCount = selectParts.length;
  105. if (partCount > 0) {
  106. // if last is alias, replace it
  107. if (selectParts[partCount-1].def.type === 'alias') {
  108. selectParts[partCount-1] = partModel;
  109. return;
  110. }
  111. }
  112. selectParts.push(partModel);
  113. }
  114. function addFieldStrategy(selectParts, partModel, query) {
  115. // copy all parts
  116. var parts = _.map(selectParts, function(part: any) {
  117. return new QueryPart({type: part.def.type, params: _.clone(part.params)});
  118. });
  119. query.selectModels.push(parts);
  120. }
  121. QueryPartDef.register({
  122. type: 'field',
  123. addStrategy: addFieldStrategy,
  124. category: categories.Fields,
  125. params: [{type: 'field'}],
  126. defaultParams: ['value'],
  127. renderer: quotedIdentityRenderer,
  128. });
  129. QueryPartDef.register({
  130. type: 'mean',
  131. addStrategy: replaceAggregationAddStrategy,
  132. category: categories.Aggregations,
  133. params: [],
  134. defaultParams: [],
  135. renderer: functionRenderer,
  136. });
  137. QueryPartDef.register({
  138. type: 'sum',
  139. addStrategy: replaceAggregationAddStrategy,
  140. category: categories.Aggregations,
  141. params: [],
  142. defaultParams: [],
  143. renderer: functionRenderer,
  144. });
  145. QueryPartDef.register({
  146. type: 'derivative',
  147. addStrategy: addTransformationStrategy,
  148. category: categories.Transformations,
  149. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5min', '10m', '15m', '1h']}],
  150. defaultParams: ['10s'],
  151. renderer: functionRenderer,
  152. });
  153. QueryPartDef.register({
  154. type: 'time',
  155. category: groupByTimeFunctions,
  156. params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
  157. defaultParams: ['$interval'],
  158. renderer: functionRenderer,
  159. });
  160. QueryPartDef.register({
  161. type: 'math',
  162. addStrategy: addMathStrategy,
  163. category: categories.Math,
  164. params: [{ name: "expr", type: "string"}],
  165. defaultParams: [' / 100'],
  166. renderer: suffixRenderer,
  167. });
  168. QueryPartDef.register({
  169. type: 'alias',
  170. addStrategy: addAliasStrategy,
  171. category: categories.Aliasing,
  172. params: [{ name: "name", type: "string", quote: 'double'}],
  173. defaultParams: ['alias'],
  174. renderMode: 'suffix',
  175. renderer: aliasRenderer,
  176. });
  177. class QueryPart {
  178. part: any;
  179. def: QueryPartDef;
  180. params: any[];
  181. text: string;
  182. constructor(part: any) {
  183. this.part = part;
  184. this.def = index[part.type];
  185. if (!this.def) {
  186. throw {message: 'Could not find query part ' + part.type};
  187. }
  188. part.params = part.params || _.clone(this.def.defaultParams);
  189. this.params = part.params;
  190. this.updateText();
  191. }
  192. render(innerExpr: string) {
  193. return this.def.renderer(this, innerExpr);
  194. }
  195. hasMultipleParamsInString (strValue, index) {
  196. if (strValue.indexOf(',') === -1) {
  197. return false;
  198. }
  199. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  200. }
  201. updateParam (strValue, index) {
  202. // handle optional parameters
  203. // if string contains ',' and next param is optional, split and update both
  204. if (this.hasMultipleParamsInString(strValue, index)) {
  205. _.each(strValue.split(','), function(partVal: string, idx) {
  206. this.updateParam(partVal.trim(), idx);
  207. }, this);
  208. return;
  209. }
  210. if (strValue === '' && this.def.params[index].optional) {
  211. this.params.splice(index, 1);
  212. }
  213. else {
  214. this.params[index] = strValue;
  215. }
  216. this.part.params = this.params;
  217. this.updateText();
  218. }
  219. updateText() {
  220. if (this.params.length === 0) {
  221. this.text = this.def.type + '()';
  222. return;
  223. }
  224. var text = this.def.type + '(';
  225. text += this.params.join(', ');
  226. text += ')';
  227. this.text = text;
  228. }
  229. }
  230. export = {
  231. create: function(part): any {
  232. return new QueryPart(part);
  233. },
  234. getCategories: function() {
  235. return categories;
  236. }
  237. };