query_part.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from '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. } else if (selectParts[partCount-1].def.type === 'alias') { // if last is alias add it before
  111. selectParts.splice(partCount-1, 0, partModel);
  112. return;
  113. }
  114. }
  115. selectParts.push(partModel);
  116. }
  117. function addAliasStrategy(selectParts, partModel) {
  118. var partCount = selectParts.length;
  119. if (partCount > 0) {
  120. // if last is alias, replace it
  121. if (selectParts[partCount-1].def.type === 'alias') {
  122. selectParts[partCount-1] = partModel;
  123. return;
  124. }
  125. }
  126. selectParts.push(partModel);
  127. }
  128. function addFieldStrategy(selectParts, partModel, query) {
  129. // copy all parts
  130. var parts = _.map(selectParts, function(part: any) {
  131. return new QueryPart({type: part.def.type, params: _.clone(part.params)});
  132. });
  133. query.selectModels.push(parts);
  134. }
  135. QueryPartDef.register({
  136. type: 'field',
  137. addStrategy: addFieldStrategy,
  138. category: categories.Fields,
  139. params: [{type: 'field', dynamicLookup: true}],
  140. defaultParams: ['value'],
  141. renderer: fieldRenderer,
  142. });
  143. // Aggregations
  144. QueryPartDef.register({
  145. type: 'count',
  146. addStrategy: replaceAggregationAddStrategy,
  147. category: categories.Aggregations,
  148. params: [],
  149. defaultParams: [],
  150. renderer: functionRenderer,
  151. });
  152. QueryPartDef.register({
  153. type: 'distinct',
  154. addStrategy: replaceAggregationAddStrategy,
  155. category: categories.Aggregations,
  156. params: [],
  157. defaultParams: [],
  158. renderer: functionRenderer,
  159. });
  160. QueryPartDef.register({
  161. type: 'integral',
  162. addStrategy: replaceAggregationAddStrategy,
  163. category: categories.Aggregations,
  164. params: [],
  165. defaultParams: [],
  166. renderer: functionRenderer,
  167. });
  168. QueryPartDef.register({
  169. type: 'mean',
  170. addStrategy: replaceAggregationAddStrategy,
  171. category: categories.Aggregations,
  172. params: [],
  173. defaultParams: [],
  174. renderer: functionRenderer,
  175. });
  176. QueryPartDef.register({
  177. type: 'median',
  178. addStrategy: replaceAggregationAddStrategy,
  179. category: categories.Aggregations,
  180. params: [],
  181. defaultParams: [],
  182. renderer: functionRenderer,
  183. });
  184. QueryPartDef.register({
  185. type: 'sum',
  186. addStrategy: replaceAggregationAddStrategy,
  187. category: categories.Aggregations,
  188. params: [],
  189. defaultParams: [],
  190. renderer: functionRenderer,
  191. });
  192. // transformations
  193. QueryPartDef.register({
  194. type: 'derivative',
  195. addStrategy: addTransformationStrategy,
  196. category: categories.Transformations,
  197. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}],
  198. defaultParams: ['10s'],
  199. renderer: functionRenderer,
  200. });
  201. QueryPartDef.register({
  202. type: 'non_negative_derivative',
  203. addStrategy: addTransformationStrategy,
  204. category: categories.Transformations,
  205. params: [{ name: "duration", type: "interval", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}],
  206. defaultParams: ['10s'],
  207. renderer: functionRenderer,
  208. });
  209. QueryPartDef.register({
  210. type: 'stddev',
  211. addStrategy: addTransformationStrategy,
  212. category: categories.Transformations,
  213. params: [],
  214. defaultParams: [],
  215. renderer: functionRenderer,
  216. });
  217. QueryPartDef.register({
  218. type: 'time',
  219. category: groupByTimeFunctions,
  220. params: [{ name: "interval", type: "time", options: ['auto', '1s', '10s', '1m', '5m', '10m', '15m', '1h'] }],
  221. defaultParams: ['auto'],
  222. renderer: functionRenderer,
  223. });
  224. QueryPartDef.register({
  225. type: 'fill',
  226. category: groupByTimeFunctions,
  227. params: [{ name: "fill", type: "string", options: ['none', 'null', '0', 'previous'] }],
  228. defaultParams: ['null'],
  229. renderer: functionRenderer,
  230. });
  231. // Selectors
  232. QueryPartDef.register({
  233. type: 'bottom',
  234. addStrategy: replaceAggregationAddStrategy,
  235. category: categories.Selectors,
  236. params: [{name: 'count', type: 'int'}],
  237. defaultParams: [3],
  238. renderer: functionRenderer,
  239. });
  240. QueryPartDef.register({
  241. type: 'first',
  242. addStrategy: replaceAggregationAddStrategy,
  243. category: categories.Selectors,
  244. params: [],
  245. defaultParams: [],
  246. renderer: functionRenderer,
  247. });
  248. QueryPartDef.register({
  249. type: 'last',
  250. addStrategy: replaceAggregationAddStrategy,
  251. category: categories.Selectors,
  252. params: [],
  253. defaultParams: [],
  254. renderer: functionRenderer,
  255. });
  256. QueryPartDef.register({
  257. type: 'max',
  258. addStrategy: replaceAggregationAddStrategy,
  259. category: categories.Selectors,
  260. params: [],
  261. defaultParams: [],
  262. renderer: functionRenderer,
  263. });
  264. QueryPartDef.register({
  265. type: 'min',
  266. addStrategy: replaceAggregationAddStrategy,
  267. category: categories.Selectors,
  268. params: [],
  269. defaultParams: [],
  270. renderer: functionRenderer,
  271. });
  272. QueryPartDef.register({
  273. type: 'percentile',
  274. addStrategy: replaceAggregationAddStrategy,
  275. category: categories.Selectors,
  276. params: [{name: 'nth', type: 'int'}],
  277. defaultParams: [95],
  278. renderer: functionRenderer,
  279. });
  280. QueryPartDef.register({
  281. type: 'top',
  282. addStrategy: replaceAggregationAddStrategy,
  283. category: categories.Selectors,
  284. params: [{name: 'count', type: 'int'}],
  285. defaultParams: [3],
  286. renderer: functionRenderer,
  287. });
  288. QueryPartDef.register({
  289. type: 'tag',
  290. category: groupByTimeFunctions,
  291. params: [{name: 'tag', type: 'string', dynamicLookup: true}],
  292. defaultParams: ['tag'],
  293. renderer: fieldRenderer,
  294. });
  295. QueryPartDef.register({
  296. type: 'math',
  297. addStrategy: addMathStrategy,
  298. category: categories.Math,
  299. params: [{ name: "expr", type: "string"}],
  300. defaultParams: [' / 100'],
  301. renderer: suffixRenderer,
  302. });
  303. QueryPartDef.register({
  304. type: 'alias',
  305. addStrategy: addAliasStrategy,
  306. category: categories.Aliasing,
  307. params: [{ name: "name", type: "string", quote: 'double'}],
  308. defaultParams: ['alias'],
  309. renderMode: 'suffix',
  310. renderer: aliasRenderer,
  311. });
  312. class QueryPart {
  313. part: any;
  314. def: QueryPartDef;
  315. params: any[];
  316. text: string;
  317. constructor(part: any) {
  318. this.part = part;
  319. this.def = index[part.type];
  320. if (!this.def) {
  321. throw {message: 'Could not find query part ' + part.type};
  322. }
  323. part.params = part.params || _.clone(this.def.defaultParams);
  324. this.params = part.params;
  325. this.updateText();
  326. }
  327. render(innerExpr: string) {
  328. return this.def.renderer(this, innerExpr);
  329. }
  330. hasMultipleParamsInString (strValue, index) {
  331. if (strValue.indexOf(',') === -1) {
  332. return false;
  333. }
  334. return this.def.params[index + 1] && this.def.params[index + 1].optional;
  335. }
  336. updateParam (strValue, index) {
  337. // handle optional parameters
  338. // if string contains ',' and next param is optional, split and update both
  339. if (this.hasMultipleParamsInString(strValue, index)) {
  340. _.each(strValue.split(','), function(partVal: string, idx) {
  341. this.updateParam(partVal.trim(), idx);
  342. }, this);
  343. return;
  344. }
  345. if (strValue === '' && this.def.params[index].optional) {
  346. this.params.splice(index, 1);
  347. } else {
  348. this.params[index] = strValue;
  349. }
  350. this.part.params = this.params;
  351. this.updateText();
  352. }
  353. updateText() {
  354. if (this.params.length === 0) {
  355. this.text = this.def.type + '()';
  356. return;
  357. }
  358. var text = this.def.type + '(';
  359. text += this.params.join(', ');
  360. text += ')';
  361. this.text = text;
  362. }
  363. }
  364. export default {
  365. create: function(part): any {
  366. return new QueryPart(part);
  367. },
  368. getCategories: function() {
  369. return categories;
  370. }
  371. };