queryBuilder.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. define([
  2. 'lodash'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. function InfluxQueryBuilder(target) {
  7. this.target = target;
  8. }
  9. function renderTagCondition (tag, index) {
  10. var str = "";
  11. if (index > 0) {
  12. str = (tag.condition || 'AND') + ' ';
  13. }
  14. if (tag.value && tag.value[0] === '/' && tag.value[tag.value.length - 1] === '/') {
  15. return str + tag.key + ' =~ ' + tag.value;
  16. }
  17. return str + tag.key + " = '" + tag.value + "'";
  18. }
  19. var p = InfluxQueryBuilder.prototype;
  20. p.build = function() {
  21. return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
  22. };
  23. p.buildExploreQuery = function(type, withKey) {
  24. var query;
  25. var measurement;
  26. if (type === 'TAG_KEYS') {
  27. query = 'SHOW TAG KEYS';
  28. measurement= this.target.measurement;
  29. } else if (type === 'TAG_VALUES') {
  30. query = 'SHOW TAG VALUES';
  31. measurement= this.target.measurement;
  32. } else if (type === 'MEASUREMENTS') {
  33. query = 'SHOW MEASUREMENTS';
  34. }
  35. if (measurement) {
  36. query += ' FROM "' + measurement + '"';
  37. }
  38. if (withKey) {
  39. query += ' WITH KEY = "' + withKey + '"';
  40. }
  41. if (this.target.tags && this.target.tags.length > 0) {
  42. var whereConditions = _.reduce(this.target.tags, function(memo, tag) {
  43. // do not add a condition for the key we want to explore for
  44. if (tag.key === withKey) {
  45. return memo;
  46. }
  47. memo.push(renderTagCondition(tag, memo.length));
  48. return memo;
  49. }, []);
  50. if (whereConditions.length > 0) {
  51. query += ' WHERE ' + whereConditions.join(' ');
  52. }
  53. }
  54. return query;
  55. };
  56. p._buildQuery = function() {
  57. var target = this.target;
  58. if (!target.measurement) {
  59. throw "Metric measurement is missing";
  60. }
  61. var query = 'SELECT ';
  62. var measurement = target.measurement;
  63. var aggregationFunc = target.function || 'mean';
  64. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  65. measurement = '"' + measurement+ '"';
  66. }
  67. query += aggregationFunc + '(value)';
  68. query += ' FROM ' + measurement + ' WHERE ';
  69. var conditions = _.map(target.tags, function(tag, index) {
  70. return renderTagCondition(tag, index);
  71. });
  72. query += conditions.join(' ');
  73. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  74. query += ' GROUP BY time($interval)';
  75. if (target.groupByTags && target.groupByTags.length > 0) {
  76. query += ', ' + target.groupByTags.join();
  77. }
  78. if (target.fill) {
  79. query += ' fill(' + target.fill + ')';
  80. }
  81. query += " ORDER BY asc";
  82. target.query = query;
  83. return query;
  84. };
  85. p._modifyRawQuery = function () {
  86. var query = this.target.query.replace(";", "");
  87. return query;
  88. };
  89. return InfluxQueryBuilder;
  90. });