query_part.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ = require('lodash');
  3. var index = [];
  4. var categories = {
  5. Aggregations: [],
  6. Selectors: [],
  7. Transformations: [],
  8. Math: [],
  9. Aliasing: [],
  10. Fields: [],
  11. };
  12. var groupByTimeFunctions = [];
  13. class QueryPartDef {
  14. type: string;
  15. params: any[];
  16. defaultParams: any[];
  17. renderer: any;
  18. category: any;
  19. addStrategy: any;
  20. constructor(options: any) {
  21. this.type = options.type;
  22. this.params = options.params;
  23. this.defaultParams = options.defaultParams;
  24. this.renderer = options.renderer;
  25. this.category = options.category;
  26. this.addStrategy = options.addStrategy;
  27. }
  28. static register(options: any) {
  29. index[options.type] = new QueryPartDef(options);
  30. options.category.push(index[options.type]);
  31. }
  32. }
  33. function functionRenderer(part, innerExpr) {
  34. var str = part.def.type + '(';
  35. var parameters = _.map(part.params, (value, index) => {
  36. var paramType = part.def.params[index];
  37. if (paramType.type === 'time') {
  38. if (value === 'auto') {
  39. value = '$interval';
  40. }
  41. }
  42. if (paramType.quote === 'single') {
  43. return "'" + value + "'";
  44. } else if (paramType.quote === 'double') {
  45. return '"' + value + '"';
  46. }
  47. return value;
  48. });
  49. if (innerExpr) {
  50. parameters.unshift(innerExpr);
  51. }
  52. return str + parameters.join(', ') + ')';
  53. }
  54. function aliasRenderer(part, innerExpr) {
  55. return innerExpr + ' AS ' + '"' + part.params[0] + '"';
  56. }
  57. function suffixRenderer(part, innerExpr) {
  58. return innerExpr + ' ' + part.params[0];
  59. }
  60. function identityRenderer(part, innerExpr) {
  61. return part.params[0];
  62. }
  63. function quotedIdentityRenderer(part, innerExpr) {
  64. return '"' + part.params[0] + '"';
  65. }
  66. function fieldRenderer(part, innerExpr) {
  67. if (part.params[0] === '*') {
  68. return '*';
  69. }
  70. return '"' + part.params[0] + '"';
  71. }
  72. function replaceAggregationAddStrategy(selectParts, partModel) {
  73. // look for existing aggregation
  74. for (var i = 0; i < selectParts.length; i++) {
  75. var part = selectParts[i];
  76. if (part.def.category === categories.Aggregations) {
  77. selectParts[i] = partModel;
  78. return;
  79. }
  80. if (part.def.category === categories.Selectors) {
  81. selectParts[i] = partModel;
  82. return;
  83. }
  84. }
  85. selectParts.splice(1, 0, partModel);
  86. }
  87. function addTransformationStrategy(selectParts, partModel) {
  88. var i;
  89. // look for index to add transformation
  90. for (i = 0; i < selectParts.length; i++) {
  91. var part = selectParts[i];
  92. if (part.def.category === categories.Math || part.def.category === categories.Aliasing) {
  93. break;
  94. }
  95. }
  96. selectParts.splice(i, 0, partModel);
  97. }
  98. function addMathStrategy(selectParts, partModel) {
  99. var partCount = selectParts.length;
  100. if (partCount > 0) {
  101. // if last is math, replace it
  102. if (selectParts[partCount-1].def.type === 'math') {
  103. selectParts[partCount-1] = partModel;
  104. return;
  105. }
  106. // if next to last is math, replace it
  107. if (selectParts[partCount-2].def.type === 'math') {
  108. selectParts[partCount-2] = partModel;
  109. return;
  110. }
  111. // if last is alias add it before
  112. else if (selectParts[partCount-1].def.type === 'alias') {
  113. selectParts.splice(partCount-1, 0, partModel);
  114. return;
  115. }
  116. }
  117. selectParts.push(partModel);
  118. }
  119. function addAliasStrategy(selectParts, partModel) {
  120. var partCount = selectParts.length;
  121. if (partCount > 0) {
  122. // if last is alias, replace it
  123. if (selectParts[partCount-1].def.type === 'alias') {
  124. selectParts[partCount-1] = partModel;
  125. return;
  126. }
  127. }
  128. selectParts.push(partModel);
  129. }
  130. function addFieldStrategy(selectParts, partModel, query) {
  131. // copy all parts
  132. var parts = _.map(selectParts, function(part: any) {
  133. return new QueryPart({type: part.def.type, params: _.clone(part.params)});
  134. });
  135. query.selectModels.push(parts);
  136. }
  137. QueryPartDef.register({
  138. type: 'field',
  139. addStrategy: addFieldStrategy,
  140. category: categories.Fields,
  141. params: [{type: 'field', dynamicLookup: true}],
  142. defaultParams: ['value'],
  143. renderer: fieldRenderer,
  144. });
  145. // Aggregations
  146. QueryPartDef.register({
  147. type: 'count',
  148. addStrategy: replaceAggregationAddStrategy,
  149. category: categories.Aggregations,
  150. params: [],
  151. defaultParams: [],
  152. renderer: functionRenderer,
  153. });
  154. QueryPartDef.register({
  155. type: 'distinct',
  156. addStrategy: replaceAggregationAddStrategy,
  157. category: categories.Aggregations,
  158. params: [],
  159. defaultParams: [],
  160. renderer: functionRenderer,
  161. });
  162. QueryPartDef.register({
  163. type: 'integral',
  164. addStrategy: replaceAggregationAddStrategy,
  165. category: categories.Aggregations,
  166. params: [],
  167. defaultParams: [],
  168. renderer: functionRenderer,
  169. });
  170. QueryPartDef.register({
  171. type: 'mean',
  172. addStrategy: replaceAggregationAddStrategy,
  173. category: categories.Aggregations,
  174. params: [],
  175. defaultParams: [],
  176. renderer: functionRenderer,
  177. });
  178. QueryPartDef.register({
  179. type: 'median',
  180. addStrategy: replaceAggregationAddStrategy,
  181. category: categories.Aggregations,
  182. params: [],
  183. defaultParams: [],
  184. renderer: functionRenderer,
  185. });
  186. QueryPartDef.register({
  187. type: 'sum',
  188. addStrategy: replaceAggregationAddStrategy,
  189. category: categories.Aggregations,
  190. params: [],
  191. defaultParams: [],
  192. renderer: functionRenderer,
  193. });
  194. // transformations
  195. QueryPartDef.register({
  196. type: 'derivative',
  197. addStrategy: addTransformationStrategy,
  198. category: categories.Transformations,
  199. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}],
  200. defaultParams: ['10s'],
  201. renderer: functionRenderer,
  202. });
  203. QueryPartDef.register({
  204. type: 'non_negative_derivative',
  205. addStrategy: addTransformationStrategy,
  206. category: categories.Transformations,
  207. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}],
  208. defaultParams: ['10s'],
  209. renderer: functionRenderer,
  210. });
  211. QueryPartDef.register({
  212. type: 'stddev',
  213. addStrategy: addTransformationStrategy,
  214. category: categories.Transformations,
  215. params: [],
  216. defaultParams: [],
  217. renderer: functionRenderer,
  218. });
  219. QueryPartDef.register({
  220. type: 'time',
  221. category: groupByTimeFunctions,
  222. params: [{ name: "interval", type: "time", options: ['auto', '1s', '10s', '1m', '5m', '10m', '15m', '1h'] }],
  223. defaultParams: ['auto'],
  224. renderer: functionRenderer,
  225. });
  226. QueryPartDef.register({
  227. type: 'fill',
  228. category: groupByTimeFunctions,
  229. params: [{ name: "fill", type: "string", options: ['none', 'null', '0', 'previous'] }],
  230. defaultParams: ['null'],
  231. renderer: functionRenderer,
  232. });
  233. // Selectors
  234. QueryPartDef.register({
  235. type: 'bottom',
  236. addStrategy: replaceAggregationAddStrategy,
  237. category: categories.Selectors,
  238. params: [{name: 'count', type: 'int'}],
  239. defaultParams: [3],
  240. renderer: functionRenderer,
  241. });
  242. QueryPartDef.register({
  243. type: 'first',
  244. addStrategy: replaceAggregationAddStrategy,
  245. category: categories.Selectors,
  246. params: [],
  247. defaultParams: [],
  248. renderer: functionRenderer,
  249. });
  250. QueryPartDef.register({
  251. type: 'last',
  252. addStrategy: replaceAggregationAddStrategy,
  253. category: categories.Selectors,
  254. params: [],
  255. defaultParams: [],
  256. renderer: functionRenderer,
  257. });
  258. QueryPartDef.register({
  259. type: 'max',
  260. addStrategy: replaceAggregationAddStrategy,
  261. category: categories.Selectors,
  262. params: [],
  263. defaultParams: [],
  264. renderer: functionRenderer,
  265. });
  266. QueryPartDef.register({
  267. type: 'min',
  268. addStrategy: replaceAggregationAddStrategy,
  269. category: categories.Selectors,
  270. params: [],
  271. defaultParams: [],
  272. renderer: functionRenderer,
  273. });
  274. QueryPartDef.register({
  275. type: 'percentile',
  276. addStrategy: replaceAggregationAddStrategy,
  277. category: categories.Selectors,
  278. params: [{name: 'nth', type: 'int'}],
  279. defaultParams: [95],
  280. renderer: functionRenderer,
  281. });
  282. QueryPartDef.register({
  283. type: 'top',
  284. addStrategy: replaceAggregationAddStrategy,
  285. category: categories.Selectors,
  286. params: [{name: 'count', type: 'int'}],
  287. defaultParams: [3],
  288. renderer: functionRenderer,
  289. });
  290. QueryPartDef.register({
  291. type: 'tag',
  292. category: groupByTimeFunctions,
  293. params: [{name: 'tag', type: 'string', dynamicLookup: true}],
  294. defaultParams: ['tag'],
  295. renderer: fieldRenderer,
  296. });
  297. QueryPartDef.register({
  298. type: 'math',
  299. addStrategy: addMathStrategy,
  300. category: categories.Math,
  301. params: [{ name: "expr", type: "string"}],
  302. defaultParams: [' / 100'],
  303. renderer: suffixRenderer,
  304. });
  305. QueryPartDef.register({
  306. type: 'alias',
  307. addStrategy: addAliasStrategy,
  308. category: categories.Aliasing,
  309. params: [{ name: "name", type: "string", quote: 'double'}],
  310. defaultParams: ['alias'],
  311. renderMode: 'suffix',
  312. renderer: aliasRenderer,
  313. });
  314. class QueryPart {
  315. part: any;
  316. def: QueryPartDef;
  317. params: any[];
  318. text: string;
  319. constructor(part: any) {
  320. this.part = part;
  321. this.def = index[part.type];
  322. if (!this.def) {
  323. throw {message: 'Could not find query part ' + part.type};
  324. }
  325. part.params = part.params || _.clone(this.def.defaultParams);
  326. this.params = part.params;
  327. this.updateText();
  328. }
  329. render(innerExpr: string) {
  330. return this.def.renderer(this, innerExpr);
  331. }
  332. hasMultipleParamsInString (strValue, index) {
  333. if (strValue.indexOf(',') === -1) {
  334. return false;
  335. }
  336. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  337. }
  338. updateParam (strValue, index) {
  339. // handle optional parameters
  340. // if string contains ',' and next param is optional, split and update both
  341. if (this.hasMultipleParamsInString(strValue, index)) {
  342. _.each(strValue.split(','), function(partVal: string, idx) {
  343. this.updateParam(partVal.trim(), idx);
  344. }, this);
  345. return;
  346. }
  347. if (strValue === '' && this.def.params[index].optional) {
  348. this.params.splice(index, 1);
  349. }
  350. else {
  351. this.params[index] = strValue;
  352. }
  353. this.part.params = this.params;
  354. this.updateText();
  355. }
  356. updateText() {
  357. if (this.params.length === 0) {
  358. this.text = this.def.type + '()';
  359. return;
  360. }
  361. var text = this.def.type + '(';
  362. text += this.params.join(', ');
  363. text += ')';
  364. this.text = text;
  365. }
  366. }
  367. export = {
  368. create: function(part): any {
  369. return new QueryPart(part);
  370. },
  371. getCategories: function() {
  372. return categories;
  373. }
  374. };