sql_part.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import _ from 'lodash';
  2. import { SqlPartDef, SqlPart, functionRenderer, suffixRenderer } from 'app/core/components/sql_part/sql_part';
  3. var index = [];
  4. function createPart(part): any {
  5. var def = index[part.type];
  6. if (!def) {
  7. return null;
  8. }
  9. return new SqlPart(part, def);
  10. }
  11. function register(options: any) {
  12. index[options.type] = new SqlPartDef(options);
  13. }
  14. function aliasRenderer(part, innerExpr) {
  15. return innerExpr + ' AS ' + '"' + part.params[0] + '"';
  16. }
  17. function aggregateRenderer(part, innerExpr) {
  18. return part.params[0] + '(' + innerExpr + ')';
  19. }
  20. function columnRenderer(part, innerExpr) {
  21. return part.params[0];
  22. }
  23. function replaceAggregationAddStrategy(selectParts, partModel) {
  24. var hasAlias = false;
  25. // look for existing aggregation
  26. for (var i = 0; i < selectParts.length; i++) {
  27. var part = selectParts[i];
  28. if (part.def.type === 'aggregate') {
  29. selectParts[i] = partModel;
  30. return;
  31. }
  32. if (part.def.type === 'alias') {
  33. hasAlias = true;
  34. }
  35. }
  36. // add alias if none exists yet
  37. if (!hasAlias) {
  38. var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
  39. selectParts.push(aliasModel);
  40. }
  41. selectParts.splice(1, 0, partModel);
  42. }
  43. function addMathStrategy(selectParts, partModel) {
  44. var partCount = selectParts.length;
  45. if (partCount > 0) {
  46. // if last is math, replace it
  47. if (selectParts[partCount - 1].def.type === 'math') {
  48. selectParts[partCount - 1] = partModel;
  49. return;
  50. }
  51. // if next to last is math, replace it
  52. if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
  53. selectParts[partCount - 2] = partModel;
  54. return;
  55. } else if (selectParts[partCount - 1].def.type === 'alias') {
  56. // if last is alias add it before
  57. selectParts.splice(partCount - 1, 0, partModel);
  58. return;
  59. }
  60. }
  61. selectParts.push(partModel);
  62. }
  63. function addAliasStrategy(selectParts, partModel) {
  64. var partCount = selectParts.length;
  65. if (partCount > 0) {
  66. // if last is alias, replace it
  67. if (selectParts[partCount - 1].def.type === 'alias') {
  68. selectParts[partCount - 1] = partModel;
  69. return;
  70. }
  71. }
  72. selectParts.push(partModel);
  73. }
  74. function addColumnStrategy(selectParts, partModel, query) {
  75. // copy all parts
  76. var parts = _.map(selectParts, function(part: any) {
  77. return createPart({ type: part.def.type, params: _.clone(part.params) });
  78. });
  79. query.selectModels.push(parts);
  80. }
  81. function addExpressionStrategy(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. style: 'label',
  91. addStrategy: addColumnStrategy,
  92. params: [{ type: 'column', dynamicLookup: true }],
  93. defaultParams: ['value'],
  94. renderer: columnRenderer,
  95. });
  96. register({
  97. type: 'expression',
  98. style: 'expression',
  99. label: 'Expr:',
  100. addStrategy: addExpressionStrategy,
  101. params: [
  102. { name: 'left', type: 'string', dynamicLookup: true },
  103. { name: 'op', type: 'string', dynamicLookup: true },
  104. { name: 'right', type: 'string', dynamicLookup: true },
  105. ],
  106. defaultParams: ['value', '=', 'value'],
  107. renderer: columnRenderer,
  108. });
  109. register({
  110. type: 'macro',
  111. style: 'label',
  112. label: 'Macro:',
  113. addStrategy: addExpressionStrategy,
  114. params: [],
  115. defaultParams: [],
  116. renderer: columnRenderer,
  117. });
  118. register({
  119. type: 'aggregate',
  120. style: 'label',
  121. addStrategy: replaceAggregationAddStrategy,
  122. params: [{ name: 'name', type: 'string', dynamicLookup: true }],
  123. defaultParams: ['avg'],
  124. renderer: aggregateRenderer,
  125. });
  126. register({
  127. type: 'math',
  128. style: 'label',
  129. addStrategy: addMathStrategy,
  130. params: [{ name: 'expr', type: 'string' }],
  131. defaultParams: [' / 100'],
  132. renderer: suffixRenderer,
  133. });
  134. register({
  135. type: 'alias',
  136. style: 'label',
  137. addStrategy: addAliasStrategy,
  138. params: [{ name: 'name', type: 'string', quote: 'double' }],
  139. defaultParams: ['alias'],
  140. renderMode: 'suffix',
  141. renderer: aliasRenderer,
  142. });
  143. register({
  144. type: 'time',
  145. style: 'function',
  146. label: 'time',
  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. export default {
  163. create: createPart,
  164. };