sql_part.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import _ from 'lodash';
  2. import { SqlPartDef, SqlPart } 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 replaceAggregationAddStrategy(selectParts, partModel) {
  15. var hasAlias = false;
  16. // look for existing aggregation
  17. for (var i = 0; i < selectParts.length; i++) {
  18. var part = selectParts[i];
  19. if (part.def.type === 'aggregate') {
  20. selectParts[i] = partModel;
  21. return;
  22. }
  23. if (part.def.type === 'alias') {
  24. hasAlias = true;
  25. }
  26. }
  27. // add alias if none exists yet
  28. if (!hasAlias) {
  29. var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
  30. selectParts.push(aliasModel);
  31. }
  32. selectParts.splice(1, 0, partModel);
  33. }
  34. function replaceSpecialAddStrategy(selectParts, partModel) {
  35. var hasAlias = false;
  36. // look for existing aggregation
  37. for (var i = 0; i < selectParts.length; i++) {
  38. var part = selectParts[i];
  39. if (part.def.type === 'special') {
  40. selectParts[i] = partModel;
  41. return;
  42. }
  43. if (part.def.type === 'alias') {
  44. hasAlias = true;
  45. }
  46. }
  47. // add alias if none exists yet
  48. if (!hasAlias) {
  49. var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
  50. selectParts.push(aliasModel);
  51. }
  52. selectParts.splice(1, 0, partModel);
  53. }
  54. function addMathStrategy(selectParts, partModel) {
  55. var partCount = selectParts.length;
  56. if (partCount > 0) {
  57. // if last is math, replace it
  58. if (selectParts[partCount - 1].def.type === 'math') {
  59. selectParts[partCount - 1] = partModel;
  60. return;
  61. }
  62. // if next to last is math, replace it
  63. if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
  64. selectParts[partCount - 2] = partModel;
  65. return;
  66. } else if (selectParts[partCount - 1].def.type === 'alias') {
  67. // if last is alias add it before
  68. selectParts.splice(partCount - 1, 0, partModel);
  69. return;
  70. }
  71. }
  72. selectParts.push(partModel);
  73. }
  74. function addAliasStrategy(selectParts, partModel) {
  75. var partCount = selectParts.length;
  76. if (partCount > 0) {
  77. // if last is alias, replace it
  78. if (selectParts[partCount - 1].def.type === 'alias') {
  79. selectParts[partCount - 1] = partModel;
  80. return;
  81. }
  82. }
  83. selectParts.push(partModel);
  84. }
  85. function addColumnStrategy(selectParts, partModel, query) {
  86. // copy all parts
  87. var parts = _.map(selectParts, function(part: any) {
  88. return createPart({ type: part.def.type, params: _.clone(part.params) });
  89. });
  90. query.selectModels.push(parts);
  91. }
  92. function addExpressionStrategy(selectParts, partModel, query) {
  93. // copy all parts
  94. var parts = _.map(selectParts, function(part: any) {
  95. return createPart({ type: part.def.type, params: _.clone(part.params) });
  96. });
  97. query.selectModels.push(parts);
  98. }
  99. register({
  100. type: 'column',
  101. style: 'label',
  102. addStrategy: addColumnStrategy,
  103. params: [{ type: 'column', dynamicLookup: true }],
  104. defaultParams: ['value'],
  105. });
  106. register({
  107. type: 'expression',
  108. style: 'expression',
  109. label: 'Expr:',
  110. addStrategy: addExpressionStrategy,
  111. params: [
  112. { name: 'left', type: 'string', dynamicLookup: true },
  113. { name: 'op', type: 'string', dynamicLookup: true },
  114. { name: 'right', type: 'string', dynamicLookup: true },
  115. ],
  116. defaultParams: ['value', '=', 'value'],
  117. });
  118. register({
  119. type: 'macro',
  120. style: 'label',
  121. label: 'Macro:',
  122. addStrategy: addExpressionStrategy,
  123. params: [],
  124. defaultParams: [],
  125. });
  126. register({
  127. type: 'aggregate',
  128. style: 'label',
  129. addStrategy: replaceAggregationAddStrategy,
  130. params: [{ name: 'name', type: 'string', dynamicLookup: true }],
  131. defaultParams: ['avg'],
  132. });
  133. register({
  134. type: 'math',
  135. style: 'label',
  136. addStrategy: addMathStrategy,
  137. params: [{ name: 'expr', type: 'string' }],
  138. defaultParams: [' / 100'],
  139. });
  140. register({
  141. type: 'alias',
  142. style: 'label',
  143. addStrategy: addAliasStrategy,
  144. params: [{ name: 'name', type: 'string', quote: 'double' }],
  145. defaultParams: ['alias'],
  146. });
  147. register({
  148. type: 'time',
  149. style: 'function',
  150. label: 'time',
  151. params: [
  152. {
  153. name: 'interval',
  154. type: 'interval',
  155. options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  156. },
  157. {
  158. name: 'fill',
  159. type: 'string',
  160. options: ['none', 'NULL', '0'],
  161. },
  162. ],
  163. defaultParams: ['$__interval', 'none'],
  164. });
  165. register({
  166. type: 'special',
  167. style: 'label',
  168. params: [
  169. {
  170. name: 'function',
  171. type: 'string',
  172. options: ['increase', 'rate'],
  173. },
  174. ],
  175. defaultParams: ['increase'],
  176. addStrategy: replaceSpecialAddStrategy,
  177. });
  178. export default {
  179. create: createPart,
  180. };