query_part.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. throw { message: 'Could not find query part ' + part.type };
  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. // look for existing aggregation
  25. for (var i = 0; i < selectParts.length; i++) {
  26. var part = selectParts[i];
  27. if (part.def.type === "aggregate") {
  28. selectParts[i] = partModel;
  29. return;
  30. }
  31. }
  32. selectParts.splice(1, 0, partModel);
  33. }
  34. function addMathStrategy(selectParts, partModel) {
  35. var partCount = selectParts.length;
  36. if (partCount > 0) {
  37. // if last is math, replace it
  38. if (selectParts[partCount - 1].def.type === 'math') {
  39. selectParts[partCount - 1] = partModel;
  40. return;
  41. }
  42. // if next to last is math, replace it
  43. if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
  44. selectParts[partCount - 2] = partModel;
  45. return;
  46. } else if (selectParts[partCount - 1].def.type === 'alias') {
  47. // if last is alias add it before
  48. selectParts.splice(partCount - 1, 0, partModel);
  49. return;
  50. }
  51. }
  52. selectParts.push(partModel);
  53. }
  54. function addAliasStrategy(selectParts, partModel) {
  55. var partCount = selectParts.length;
  56. if (partCount > 0) {
  57. // if last is alias, replace it
  58. if (selectParts[partCount - 1].def.type === 'alias') {
  59. selectParts[partCount - 1] = partModel;
  60. return;
  61. }
  62. }
  63. selectParts.push(partModel);
  64. }
  65. function addColumnStrategy(selectParts, partModel, query) {
  66. // copy all parts
  67. var parts = _.map(selectParts, function(part: any) {
  68. return createPart({ type: part.def.type, params: _.clone(part.params) });
  69. });
  70. query.selectModels.push(parts);
  71. }
  72. register({
  73. type: 'column',
  74. label: 'Column:',
  75. addStrategy: addColumnStrategy,
  76. params: [{ type: 'column', dynamicLookup: true }],
  77. defaultParams: ['value'],
  78. renderer: columnRenderer,
  79. });
  80. register({
  81. type: 'aggregate',
  82. label: 'Aggregate:',
  83. addStrategy: replaceAggregationAddStrategy,
  84. params: [{name: 'name', type: 'string', dynamicLookup: true}],
  85. defaultParams: ['avg'],
  86. renderer: aggregateRenderer,
  87. });
  88. register({
  89. type: 'math',
  90. label: 'Math:',
  91. addStrategy: addMathStrategy,
  92. params: [{ name: 'expr', type: 'string' }],
  93. defaultParams: [' / 100'],
  94. renderer: suffixRenderer,
  95. });
  96. register({
  97. type: 'alias',
  98. label: 'Alias:',
  99. addStrategy: addAliasStrategy,
  100. params: [{ name: 'name', type: 'string', quote: 'double' }],
  101. defaultParams: ['alias'],
  102. renderMode: 'suffix',
  103. renderer: aliasRenderer,
  104. });
  105. register({
  106. type: 'time',
  107. label: 'time',
  108. params: [
  109. {
  110. name: 'interval',
  111. type: 'interval',
  112. options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
  113. },
  114. {
  115. name: 'fill',
  116. type: 'string',
  117. options: ['none', 'NULL', '0'],
  118. },
  119. ],
  120. defaultParams: ['$__interval','none'],
  121. renderer: functionRenderer,
  122. });
  123. export default {
  124. create: createPart,
  125. };