query_part.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 addMathStrategy(selectParts, partModel) {
  40. var partCount = selectParts.length;
  41. if (partCount > 0) {
  42. // if last is math, replace it
  43. if (selectParts[partCount - 1].def.type === 'math') {
  44. selectParts[partCount - 1] = partModel;
  45. return;
  46. }
  47. // if next to last is math, replace it
  48. if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
  49. selectParts[partCount - 2] = partModel;
  50. return;
  51. } else if (selectParts[partCount - 1].def.type === 'alias') {
  52. // if last is alias add it before
  53. selectParts.splice(partCount - 1, 0, partModel);
  54. return;
  55. }
  56. }
  57. selectParts.push(partModel);
  58. }
  59. function addAliasStrategy(selectParts, partModel) {
  60. var partCount = selectParts.length;
  61. if (partCount > 0) {
  62. // if last is alias, replace it
  63. if (selectParts[partCount - 1].def.type === 'alias') {
  64. selectParts[partCount - 1] = partModel;
  65. return;
  66. }
  67. }
  68. selectParts.push(partModel);
  69. }
  70. function addColumnStrategy(selectParts, partModel, query) {
  71. // copy all parts
  72. var parts = _.map(selectParts, function(part: any) {
  73. return createPart({ type: part.def.type, params: _.clone(part.params) });
  74. });
  75. query.selectModels.push(parts);
  76. }
  77. register({
  78. type: 'column',
  79. addStrategy: addColumnStrategy,
  80. category: categories.Columns,
  81. params: [{ type: 'column', dynamicLookup: true }],
  82. defaultParams: ['value'],
  83. renderer: columnRenderer,
  84. });
  85. // Aggregations
  86. register({
  87. type: 'avg',
  88. addStrategy: replaceAggregationAddStrategy,
  89. category: categories.Aggregations,
  90. params: [],
  91. defaultParams: [],
  92. renderer: functionRenderer,
  93. });
  94. register({
  95. type: 'count',
  96. addStrategy: replaceAggregationAddStrategy,
  97. category: categories.Aggregations,
  98. params: [],
  99. defaultParams: [],
  100. renderer: functionRenderer,
  101. });
  102. register({
  103. type: 'sum',
  104. addStrategy: replaceAggregationAddStrategy,
  105. category: categories.Aggregations,
  106. params: [],
  107. defaultParams: [],
  108. renderer: functionRenderer,
  109. });
  110. register({
  111. type: 'stddev',
  112. addStrategy: replaceAggregationAddStrategy,
  113. category: categories.Aggregations,
  114. params: [],
  115. defaultParams: [],
  116. renderer: functionRenderer,
  117. });
  118. register({
  119. type: 'min',
  120. addStrategy: replaceAggregationAddStrategy,
  121. category: categories.Aggregations,
  122. params: [],
  123. defaultParams: [],
  124. renderer: functionRenderer,
  125. });
  126. register({
  127. type: 'max',
  128. addStrategy: replaceAggregationAddStrategy,
  129. category: categories.Aggregations,
  130. params: [],
  131. defaultParams: [],
  132. renderer: functionRenderer,
  133. });
  134. register({
  135. type: 'math',
  136. addStrategy: addMathStrategy,
  137. category: categories.Math,
  138. params: [{ name: 'expr', type: 'string' }],
  139. defaultParams: [' / 100'],
  140. renderer: suffixRenderer,
  141. });
  142. register({
  143. type: 'alias',
  144. addStrategy: addAliasStrategy,
  145. category: categories.Aliasing,
  146. params: [{ name: 'name', type: 'string', quote: 'double' }],
  147. defaultParams: ['alias'],
  148. renderMode: 'suffix',
  149. renderer: aliasRenderer,
  150. });
  151. register({
  152. type: 'time',
  153. category: groupByTimeFunctions,
  154. params: [
  155. {
  156. name: 'interval',
  157. type: 'interval',
  158. options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  159. },
  160. {
  161. name: 'fill',
  162. type: 'string',
  163. options: ['none', 'NULL', '0'],
  164. },
  165. ],
  166. defaultParams: ['$__interval','none'],
  167. renderer: functionRenderer,
  168. });
  169. export default {
  170. create: createPart,
  171. getCategories: function() {
  172. return categories;
  173. },
  174. };