query_part.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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: 'tag',
  162. category: groupByTimeFunctions,
  163. params: [{name: 'tag', type: 'string'}],
  164. defaultParams: ['tag'],
  165. renderer: quotedIdentityRenderer,
  166. });
  167. QueryPartDef.register({
  168. type: 'math',
  169. addStrategy: addMathStrategy,
  170. category: categories.Math,
  171. params: [{ name: "expr", type: "string"}],
  172. defaultParams: [' / 100'],
  173. renderer: suffixRenderer,
  174. });
  175. QueryPartDef.register({
  176. type: 'alias',
  177. addStrategy: addAliasStrategy,
  178. category: categories.Aliasing,
  179. params: [{ name: "name", type: "string", quote: 'double'}],
  180. defaultParams: ['alias'],
  181. renderMode: 'suffix',
  182. renderer: aliasRenderer,
  183. });
  184. class QueryPart {
  185. part: any;
  186. def: QueryPartDef;
  187. params: any[];
  188. text: string;
  189. constructor(part: any) {
  190. this.part = part;
  191. this.def = index[part.type];
  192. if (!this.def) {
  193. throw {message: 'Could not find query part ' + part.type};
  194. }
  195. part.params = part.params || _.clone(this.def.defaultParams);
  196. this.params = part.params;
  197. this.updateText();
  198. }
  199. render(innerExpr: string) {
  200. return this.def.renderer(this, innerExpr);
  201. }
  202. hasMultipleParamsInString (strValue, index) {
  203. if (strValue.indexOf(',') === -1) {
  204. return false;
  205. }
  206. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  207. }
  208. updateParam (strValue, index) {
  209. // handle optional parameters
  210. // if string contains ',' and next param is optional, split and update both
  211. if (this.hasMultipleParamsInString(strValue, index)) {
  212. _.each(strValue.split(','), function(partVal: string, idx) {
  213. this.updateParam(partVal.trim(), idx);
  214. }, this);
  215. return;
  216. }
  217. if (strValue === '' && this.def.params[index].optional) {
  218. this.params.splice(index, 1);
  219. }
  220. else {
  221. this.params[index] = strValue;
  222. }
  223. this.part.params = this.params;
  224. this.updateText();
  225. }
  226. updateText() {
  227. if (this.params.length === 0) {
  228. this.text = this.def.type + '()';
  229. return;
  230. }
  231. var text = this.def.type + '(';
  232. text += this.params.join(', ');
  233. text += ')';
  234. this.text = text;
  235. }
  236. }
  237. export = {
  238. create: function(part): any {
  239. return new QueryPart(part);
  240. },
  241. getCategories: function() {
  242. return categories;
  243. }
  244. };