query_part.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. Fields: [],
  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 fieldRenderer(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 addFieldStrategy(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: 'field',
  90. addStrategy: addFieldStrategy,
  91. category: categories.Fields,
  92. params: [{ type: 'field', dynamicLookup: true }],
  93. defaultParams: ['value'],
  94. renderer: fieldRenderer,
  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: 'time',
  151. options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  152. },
  153. ],
  154. defaultParams: ['$__interval'],
  155. renderer: functionRenderer,
  156. });
  157. register({
  158. type: 'fill',
  159. category: groupByTimeFunctions,
  160. params: [
  161. {
  162. name: 'fill',
  163. type: 'string',
  164. options: ['none', 'null', '0', 'previous', 'linear'],
  165. },
  166. ],
  167. defaultParams: ['null'],
  168. renderer: functionRenderer,
  169. });
  170. // Selectors
  171. register({
  172. type: 'max',
  173. addStrategy: replaceAggregationAddStrategy,
  174. category: categories.Aggregations,
  175. params: [],
  176. defaultParams: [],
  177. renderer: functionRenderer,
  178. });
  179. register({
  180. type: 'min',
  181. addStrategy: replaceAggregationAddStrategy,
  182. category: categories.Aggregations,
  183. params: [],
  184. defaultParams: [],
  185. renderer: functionRenderer,
  186. });
  187. register({
  188. type: 'math',
  189. addStrategy: addMathStrategy,
  190. category: categories.Math,
  191. params: [{ name: 'expr', type: 'string' }],
  192. defaultParams: [' / 100'],
  193. renderer: suffixRenderer,
  194. });
  195. register({
  196. type: 'alias',
  197. addStrategy: addAliasStrategy,
  198. category: categories.Aliasing,
  199. params: [{ name: 'name', type: 'string', quote: 'double' }],
  200. defaultParams: ['alias'],
  201. renderMode: 'suffix',
  202. renderer: aliasRenderer,
  203. });
  204. export default {
  205. create: createPart,
  206. getCategories: function() {
  207. return categories;
  208. },
  209. };