query_part.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 fieldRenderer(part, innerExpr) {
  61. if (part.params[0] === '*') {
  62. return '*';
  63. }
  64. return '"' + part.params[0] + '"';
  65. }
  66. function replaceAggregationAddStrategy(selectParts, partModel) {
  67. // look for existing aggregation
  68. for (var i = 0; i < selectParts.length; i++) {
  69. var part = selectParts[i];
  70. if (part.def.category === categories.Aggregations) {
  71. selectParts[i] = partModel;
  72. return;
  73. }
  74. }
  75. selectParts.splice(1, 0, partModel);
  76. }
  77. function addTransformationStrategy(selectParts, partModel) {
  78. var i;
  79. // look for index to add transformation
  80. for (i = 0; i < selectParts.length; i++) {
  81. var part = selectParts[i];
  82. if (part.def.category === categories.Math || part.def.category === categories.Aliasing) {
  83. break;
  84. }
  85. }
  86. selectParts.splice(i, 0, partModel);
  87. }
  88. function addMathStrategy(selectParts, partModel) {
  89. var partCount = selectParts.length;
  90. if (partCount > 0) {
  91. // if last is math, replace it
  92. if (selectParts[partCount-1].def.type === 'math') {
  93. selectParts[partCount-1] = partModel;
  94. return;
  95. }
  96. // if next to last is math, replace it
  97. if (selectParts[partCount-2].def.type === 'math') {
  98. selectParts[partCount-2] = partModel;
  99. return;
  100. }
  101. // if last is alias add it before
  102. else if (selectParts[partCount-1].def.type === 'alias') {
  103. selectParts.splice(partCount-1, 0, partModel);
  104. return;
  105. }
  106. }
  107. selectParts.push(partModel);
  108. }
  109. function addAliasStrategy(selectParts, partModel) {
  110. var partCount = selectParts.length;
  111. if (partCount > 0) {
  112. // if last is alias, replace it
  113. if (selectParts[partCount-1].def.type === 'alias') {
  114. selectParts[partCount-1] = partModel;
  115. return;
  116. }
  117. }
  118. selectParts.push(partModel);
  119. }
  120. function addFieldStrategy(selectParts, partModel, query) {
  121. // copy all parts
  122. var parts = _.map(selectParts, function(part: any) {
  123. return new QueryPart({type: part.def.type, params: _.clone(part.params)});
  124. });
  125. query.selectModels.push(parts);
  126. }
  127. QueryPartDef.register({
  128. type: 'field',
  129. addStrategy: addFieldStrategy,
  130. category: categories.Fields,
  131. params: [{type: 'field'}],
  132. defaultParams: ['value'],
  133. renderer: fieldRenderer,
  134. });
  135. QueryPartDef.register({
  136. type: 'mean',
  137. addStrategy: replaceAggregationAddStrategy,
  138. category: categories.Aggregations,
  139. params: [],
  140. defaultParams: [],
  141. renderer: functionRenderer,
  142. });
  143. QueryPartDef.register({
  144. type: 'sum',
  145. addStrategy: replaceAggregationAddStrategy,
  146. category: categories.Aggregations,
  147. params: [],
  148. defaultParams: [],
  149. renderer: functionRenderer,
  150. });
  151. QueryPartDef.register({
  152. type: 'derivative',
  153. addStrategy: addTransformationStrategy,
  154. category: categories.Transformations,
  155. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5min', '10m', '15m', '1h']}],
  156. defaultParams: ['10s'],
  157. renderer: functionRenderer,
  158. });
  159. QueryPartDef.register({
  160. type: 'time',
  161. category: groupByTimeFunctions,
  162. params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'] }],
  163. defaultParams: ['$interval'],
  164. renderer: functionRenderer,
  165. });
  166. QueryPartDef.register({
  167. type: 'fill',
  168. category: groupByTimeFunctions,
  169. params: [{ name: "fill", type: "string", options: ['none', 'null', '0', 'previous'] }],
  170. defaultParams: ['null'],
  171. renderer: functionRenderer,
  172. });
  173. QueryPartDef.register({
  174. type: 'tag',
  175. category: groupByTimeFunctions,
  176. params: [{name: 'tag', type: 'string'}],
  177. defaultParams: ['tag'],
  178. renderer: fieldRenderer,
  179. });
  180. QueryPartDef.register({
  181. type: 'math',
  182. addStrategy: addMathStrategy,
  183. category: categories.Math,
  184. params: [{ name: "expr", type: "string"}],
  185. defaultParams: [' / 100'],
  186. renderer: suffixRenderer,
  187. });
  188. QueryPartDef.register({
  189. type: 'alias',
  190. addStrategy: addAliasStrategy,
  191. category: categories.Aliasing,
  192. params: [{ name: "name", type: "string", quote: 'double'}],
  193. defaultParams: ['alias'],
  194. renderMode: 'suffix',
  195. renderer: aliasRenderer,
  196. });
  197. class QueryPart {
  198. part: any;
  199. def: QueryPartDef;
  200. params: any[];
  201. text: string;
  202. constructor(part: any) {
  203. this.part = part;
  204. this.def = index[part.type];
  205. if (!this.def) {
  206. throw {message: 'Could not find query part ' + part.type};
  207. }
  208. part.params = part.params || _.clone(this.def.defaultParams);
  209. this.params = part.params;
  210. this.updateText();
  211. }
  212. render(innerExpr: string) {
  213. return this.def.renderer(this, innerExpr);
  214. }
  215. hasMultipleParamsInString (strValue, index) {
  216. if (strValue.indexOf(',') === -1) {
  217. return false;
  218. }
  219. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  220. }
  221. updateParam (strValue, index) {
  222. // handle optional parameters
  223. // if string contains ',' and next param is optional, split and update both
  224. if (this.hasMultipleParamsInString(strValue, index)) {
  225. _.each(strValue.split(','), function(partVal: string, idx) {
  226. this.updateParam(partVal.trim(), idx);
  227. }, this);
  228. return;
  229. }
  230. if (strValue === '' && this.def.params[index].optional) {
  231. this.params.splice(index, 1);
  232. }
  233. else {
  234. this.params[index] = strValue;
  235. }
  236. this.part.params = this.params;
  237. this.updateText();
  238. }
  239. updateText() {
  240. if (this.params.length === 0) {
  241. this.text = this.def.type + '()';
  242. return;
  243. }
  244. var text = this.def.type + '(';
  245. text += this.params.join(', ');
  246. text += ')';
  247. this.text = text;
  248. }
  249. }
  250. export = {
  251. create: function(part): any {
  252. return new QueryPart(part);
  253. },
  254. getCategories: function() {
  255. return categories;
  256. }
  257. };