queryBuilder.js 2.6 KB

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