query_builder.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. define([
  2. ],
  3. function () {
  4. 'use strict';
  5. function InfluxQueryBuilder(target) {
  6. this.target = target;
  7. }
  8. var p = InfluxQueryBuilder.prototype;
  9. p.build = function() {
  10. return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
  11. };
  12. p._buildQuery = function() {
  13. var target = this.target;
  14. var query = 'select ';
  15. var seriesName = target.series;
  16. if(!seriesName.match('^/.*/') && !seriesName.match(/^merge\(.*\)/)) {
  17. seriesName = '"' + seriesName+ '"';
  18. }
  19. if (target.groupby_field) {
  20. query += target.groupby_field + ', ';
  21. }
  22. query += target.function + '(' + target.column + ')';
  23. query += ' from ' + seriesName + ' where $timeFilter';
  24. if (target.condition) {
  25. query += ' and ' + target.condition;
  26. }
  27. query += ' group by time($interval)';
  28. if (target.groupby_field) {
  29. query += ', ' + target.groupby_field;
  30. this.groupByField = target.groupby_field;
  31. }
  32. if (target.fill) {
  33. query += ' fill(' + target.fill + ')';
  34. }
  35. query += " order asc";
  36. target.query = query;
  37. return query;
  38. };
  39. p._modifyRawQuery = function () {
  40. var query = this.target.query.replace(";", "");
  41. var queryElements = query.split(" ");
  42. var lowerCaseQueryElements = query.toLowerCase().split(" ");
  43. if (lowerCaseQueryElements[1].indexOf(',') !== -1) {
  44. this.groupByField = lowerCaseQueryElements[1].replace(',', '');
  45. }
  46. return queryElements.join(" ");
  47. };
  48. return InfluxQueryBuilder;
  49. });