query_part.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import _ from 'lodash';
  2. import { QueryPartDef, QueryPart, functionRenderer, suffixRenderer } from 'app/core/components/query_part/query_part';
  3. var index = [];
  4. var categories = {
  5. Aggregations: [],
  6. Math: [],
  7. Aliasing: [],
  8. Columns: [],
  9. };
  10. function createPart(part): any {
  11. var def = index[part.type];
  12. if (!def) {
  13. throw { message: 'Could not find query part ' + part.type };
  14. }
  15. return new QueryPart(part, def);
  16. }
  17. function register(options: any) {
  18. index[options.type] = new QueryPartDef(options);
  19. options.category.push(index[options.type]);
  20. }
  21. var groupByTimeFunctions = [];
  22. function aliasRenderer(part, innerExpr) {
  23. return innerExpr + ' AS ' + '"' + part.params[0] + '"';
  24. }
  25. function columnRenderer(part, innerExpr) {
  26. return '"' + part.params[0] + '"';
  27. }
  28. function replaceAggregationAddStrategy(selectParts, partModel) {
  29. // look for existing aggregation
  30. for (var i = 0; i < selectParts.length; i++) {
  31. var part = selectParts[i];
  32. if (part.def.category === categories.Aggregations) {
  33. selectParts[i] = partModel;
  34. return;
  35. }
  36. }
  37. selectParts.splice(1, 0, partModel);
  38. }
  39. function addTransformationStrategy(selectParts, partModel) {
  40. var i;
  41. // look for index to add transformation
  42. for (i = 0; i < selectParts.length; i++) {
  43. var part = selectParts[i];
  44. if (part.def.category === categories.Math || part.def.category === categories.Aliasing) {
  45. break;
  46. }
  47. }
  48. selectParts.splice(i, 0, partModel);
  49. }
  50. function addMathStrategy(selectParts, partModel) {
  51. var partCount = selectParts.length;
  52. if (partCount > 0) {
  53. // if last is math, replace it
  54. if (selectParts[partCount - 1].def.type === 'math') {
  55. selectParts[partCount - 1] = partModel;
  56. return;
  57. }
  58. // if next to last is math, replace it
  59. if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
  60. selectParts[partCount - 2] = partModel;
  61. return;
  62. } else if (selectParts[partCount - 1].def.type === 'alias') {
  63. // if last is alias add it before
  64. selectParts.splice(partCount - 1, 0, partModel);
  65. return;
  66. }
  67. }
  68. selectParts.push(partModel);
  69. }
  70. function addAliasStrategy(selectParts, partModel) {
  71. var partCount = selectParts.length;
  72. if (partCount > 0) {
  73. // if last is alias, replace it
  74. if (selectParts[partCount - 1].def.type === 'alias') {
  75. selectParts[partCount - 1] = partModel;
  76. return;
  77. }
  78. }
  79. selectParts.push(partModel);
  80. }
  81. function addColumnStrategy(selectParts, partModel, query) {
  82. // copy all parts
  83. var parts = _.map(selectParts, function(part: any) {
  84. return createPart({ type: part.def.type, params: _.clone(part.params) });
  85. });
  86. query.selectModels.push(parts);
  87. }
  88. register({
  89. type: 'column',
  90. addStrategy: addColumnStrategy,
  91. category: categories.Columns,
  92. params: [{ type: 'column', dynamicLookup: true }],
  93. defaultParams: ['value'],
  94. renderer: columnRenderer,
  95. });
  96. // Aggregations
  97. register({
  98. type: 'avg',
  99. addStrategy: replaceAggregationAddStrategy,
  100. category: categories.Aggregations,
  101. params: [],
  102. defaultParams: [],
  103. renderer: functionRenderer,
  104. });
  105. register({
  106. type: 'count',
  107. addStrategy: replaceAggregationAddStrategy,
  108. category: categories.Aggregations,
  109. params: [],
  110. defaultParams: [],
  111. renderer: functionRenderer,
  112. });
  113. register({
  114. type: 'sum',
  115. addStrategy: replaceAggregationAddStrategy,
  116. category: categories.Aggregations,
  117. params: [],
  118. defaultParams: [],
  119. renderer: functionRenderer,
  120. });
  121. // transformations
  122. register({
  123. type: 'non_negative_derivative',
  124. addStrategy: addTransformationStrategy,
  125. category: categories.Aggregations,
  126. params: [
  127. {
  128. name: 'duration',
  129. type: 'interval',
  130. options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  131. },
  132. ],
  133. defaultParams: ['10s'],
  134. renderer: functionRenderer,
  135. });
  136. register({
  137. type: 'stddev',
  138. addStrategy: addTransformationStrategy,
  139. category: categories.Aggregations,
  140. params: [],
  141. defaultParams: [],
  142. renderer: functionRenderer,
  143. });
  144. register({
  145. type: 'time',
  146. category: groupByTimeFunctions,
  147. params: [
  148. {
  149. name: 'interval',
  150. type: 'interval',
  151. options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  152. },
  153. {
  154. name: 'fill',
  155. type: 'string',
  156. options: ['none', 'null', '0'],
  157. },
  158. ],
  159. defaultParams: ['$__interval','none'],
  160. renderer: functionRenderer,
  161. });
  162. // Selectors
  163. register({
  164. type: 'max',
  165. addStrategy: replaceAggregationAddStrategy,
  166. category: categories.Aggregations,
  167. params: [],
  168. defaultParams: [],
  169. renderer: functionRenderer,
  170. });
  171. register({
  172. type: 'min',
  173. addStrategy: replaceAggregationAddStrategy,
  174. category: categories.Aggregations,
  175. params: [],
  176. defaultParams: [],
  177. renderer: functionRenderer,
  178. });
  179. register({
  180. type: 'math',
  181. addStrategy: addMathStrategy,
  182. category: categories.Math,
  183. params: [{ name: 'expr', type: 'string' }],
  184. defaultParams: [' / 100'],
  185. renderer: suffixRenderer,
  186. });
  187. register({
  188. type: 'alias',
  189. addStrategy: addAliasStrategy,
  190. category: categories.Aliasing,
  191. params: [{ name: 'name', type: 'string', quote: 'double' }],
  192. defaultParams: ['alias'],
  193. renderMode: 'suffix',
  194. renderer: aliasRenderer,
  195. });
  196. export default {
  197. create: createPart,
  198. getCategories: function() {
  199. return categories;
  200. },
  201. };