query_ctrl.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import InfluxQueryBuilder from './query_builder';
  5. import InfluxQuery from './influx_query';
  6. import queryPart from './query_part';
  7. import {QueryCtrl} from 'app/plugins/sdk';
  8. export class InfluxQueryCtrl extends QueryCtrl {
  9. static templateUrl = 'partials/query.editor.html';
  10. queryModel: InfluxQuery;
  11. queryBuilder: any;
  12. groupBySegment: any;
  13. resultFormats: any[];
  14. policySegment: any;
  15. tagSegments: any[];
  16. selectMenu: any;
  17. measurementSegment: any;
  18. removeTagFilterSegment: any;
  19. /** @ngInject **/
  20. constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
  21. super($scope, $injector);
  22. this.target = this.target;
  23. this.queryModel = new InfluxQuery(this.target, templateSrv, this.panel.scopedVars);
  24. this.queryBuilder = new InfluxQueryBuilder(this.target, this.datasource.database);
  25. this.groupBySegment = this.uiSegmentSrv.newPlusButton();
  26. this.resultFormats = [
  27. {text: 'Time series', value: 'time_series'},
  28. {text: 'Table', value: 'table'},
  29. ];
  30. this.policySegment = uiSegmentSrv.newSegment(this.target.policy);
  31. if (!this.target.measurement) {
  32. this.measurementSegment = uiSegmentSrv.newSelectMeasurement();
  33. } else {
  34. this.measurementSegment = uiSegmentSrv.newSegment(this.target.measurement);
  35. }
  36. this.tagSegments = [];
  37. for (let tag of this.target.tags) {
  38. if (!tag.operator) {
  39. if (/^\/.*\/$/.test(tag.value)) {
  40. tag.operator = "=~";
  41. } else {
  42. tag.operator = '=';
  43. }
  44. }
  45. if (tag.condition) {
  46. this.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
  47. }
  48. this.tagSegments.push(uiSegmentSrv.newKey(tag.key));
  49. this.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
  50. this.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
  51. }
  52. this.fixTagSegments();
  53. this.buildSelectMenu();
  54. this.removeTagFilterSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove tag filter --'});
  55. }
  56. buildSelectMenu() {
  57. var categories = queryPart.getCategories();
  58. this.selectMenu = _.reduce(categories, function(memo, cat, key) {
  59. var menu = {
  60. text: key,
  61. submenu: cat.map(item => {
  62. return {text: item.type, value: item.type};
  63. }),
  64. };
  65. memo.push(menu);
  66. return memo;
  67. }, []);
  68. }
  69. getGroupByOptions() {
  70. var query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  71. return this.datasource.metricFindQuery(query).then(tags => {
  72. var options = [];
  73. if (!this.queryModel.hasFill()) {
  74. options.push(this.uiSegmentSrv.newSegment({value: 'fill(null)'}));
  75. }
  76. if (!this.queryModel.hasGroupByTime()) {
  77. options.push(this.uiSegmentSrv.newSegment({value: 'time($interval)'}));
  78. }
  79. for (let tag of tags) {
  80. options.push(this.uiSegmentSrv.newSegment({value: 'tag(' + tag.text + ')'}));
  81. }
  82. return options;
  83. }).catch(this.handleQueryError.bind(this));
  84. }
  85. groupByAction() {
  86. this.queryModel.addGroupBy(this.groupBySegment.value);
  87. var plusButton = this.uiSegmentSrv.newPlusButton();
  88. this.groupBySegment.value = plusButton.value;
  89. this.groupBySegment.html = plusButton.html;
  90. this.panelCtrl.refresh();
  91. }
  92. addSelectPart(selectParts, cat, subitem) {
  93. this.queryModel.addSelectPart(selectParts, subitem.value);
  94. this.panelCtrl.refresh();
  95. }
  96. handleSelectPartEvent(selectParts, part, evt) {
  97. switch (evt.name) {
  98. case "get-param-options": {
  99. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  100. return this.datasource.metricFindQuery(fieldsQuery)
  101. .then(this.transformToSegments(true))
  102. .catch(this.handleQueryError.bind(this));
  103. }
  104. case "part-param-changed": {
  105. this.panelCtrl.refresh();
  106. break;
  107. }
  108. case "action": {
  109. this.queryModel.removeSelectPart(selectParts, part);
  110. this.panelCtrl.refresh();
  111. break;
  112. }
  113. case "get-part-actions": {
  114. return this.$q.when([{text: 'Remove', value: 'remove-part'}]);
  115. }
  116. }
  117. }
  118. handleGroupByPartEvent(part, index, evt) {
  119. switch (evt.name) {
  120. case "get-param-options": {
  121. var tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  122. return this.datasource.metricFindQuery(tagsQuery)
  123. .then(this.transformToSegments(true))
  124. .catch(this.handleQueryError.bind(this));
  125. }
  126. case "part-param-changed": {
  127. this.panelCtrl.refresh();
  128. break;
  129. }
  130. case "action": {
  131. this.queryModel.removeGroupByPart(part, index);
  132. this.panelCtrl.refresh();
  133. break;
  134. }
  135. case "get-part-actions": {
  136. return this.$q.when([{text: 'Remove', value: 'remove-part'}]);
  137. }
  138. }
  139. }
  140. fixTagSegments() {
  141. var count = this.tagSegments.length;
  142. var lastSegment = this.tagSegments[Math.max(count-1, 0)];
  143. if (!lastSegment || lastSegment.type !== 'plus-button') {
  144. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  145. }
  146. }
  147. measurementChanged() {
  148. this.target.measurement = this.measurementSegment.value;
  149. this.panelCtrl.refresh();
  150. }
  151. getPolicySegments() {
  152. var policiesQuery = this.queryBuilder.buildExploreQuery('RETENTION POLICIES');
  153. return this.datasource.metricFindQuery(policiesQuery)
  154. .then(this.transformToSegments(false))
  155. .catch(this.handleQueryError.bind(this));
  156. }
  157. policyChanged() {
  158. this.target.policy = this.policySegment.value;
  159. this.panelCtrl.refresh();
  160. }
  161. toggleEditorMode() {
  162. try {
  163. this.target.query = this.queryModel.render(false);
  164. } catch (err) {
  165. console.log('query render error');
  166. }
  167. this.target.rawQuery = !this.target.rawQuery;
  168. }
  169. getMeasurements(measurementFilter) {
  170. var query = this.queryBuilder.buildExploreQuery('MEASUREMENTS', undefined, measurementFilter);
  171. return this.datasource.metricFindQuery(query)
  172. .then(this.transformToSegments(true))
  173. .catch(this.handleQueryError.bind(this));
  174. }
  175. handleQueryError(err) {
  176. this.error = err.message || 'Failed to issue metric query';
  177. return [];
  178. }
  179. transformToSegments(addTemplateVars) {
  180. return (results) => {
  181. var segments = _.map(results, segment => {
  182. return this.uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  183. });
  184. if (addTemplateVars) {
  185. for (let variable of this.templateSrv.variables) {
  186. segments.unshift(this.uiSegmentSrv.newSegment({ type: 'template', value: '/^$' + variable.name + '$/', expandable: true }));
  187. }
  188. }
  189. return segments;
  190. };
  191. }
  192. getTagsOrValues(segment, index) {
  193. if (segment.type === 'condition') {
  194. return this.$q.when([this.uiSegmentSrv.newSegment('AND'), this.uiSegmentSrv.newSegment('OR')]);
  195. }
  196. if (segment.type === 'operator') {
  197. var nextValue = this.tagSegments[index+1].value;
  198. if (/^\/.*\/$/.test(nextValue)) {
  199. return this.$q.when(this.uiSegmentSrv.newOperators(['=~', '!~']));
  200. } else {
  201. return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<>', '<', '>']));
  202. }
  203. }
  204. var query, addTemplateVars;
  205. if (segment.type === 'key' || segment.type === 'plus-button') {
  206. query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  207. addTemplateVars = false;
  208. } else if (segment.type === 'value') {
  209. query = this.queryBuilder.buildExploreQuery('TAG_VALUES', this.tagSegments[index-2].value);
  210. addTemplateVars = true;
  211. }
  212. return this.datasource.metricFindQuery(query)
  213. .then(this.transformToSegments(addTemplateVars))
  214. .then(results => {
  215. if (segment.type === 'key') {
  216. results.splice(0, 0, angular.copy(this.removeTagFilterSegment));
  217. }
  218. return results;
  219. })
  220. .catch(this.handleQueryError.bind(this));
  221. }
  222. getFieldSegments() {
  223. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  224. return this.datasource.metricFindQuery(fieldsQuery)
  225. .then(this.transformToSegments(false))
  226. .catch(this.handleQueryError);
  227. }
  228. tagSegmentUpdated(segment, index) {
  229. this.tagSegments[index] = segment;
  230. // handle remove tag condition
  231. if (segment.value === this.removeTagFilterSegment.value) {
  232. this.tagSegments.splice(index, 3);
  233. if (this.tagSegments.length === 0) {
  234. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  235. } else if (this.tagSegments.length > 2) {
  236. this.tagSegments.splice(Math.max(index-1, 0), 1);
  237. if (this.tagSegments[this.tagSegments.length-1].type !== 'plus-button') {
  238. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  239. }
  240. }
  241. } else {
  242. if (segment.type === 'plus-button') {
  243. if (index > 2) {
  244. this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
  245. }
  246. this.tagSegments.push(this.uiSegmentSrv.newOperator('='));
  247. this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  248. segment.type = 'key';
  249. segment.cssClass = 'query-segment-key';
  250. }
  251. if ((index+1) === this.tagSegments.length) {
  252. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  253. }
  254. }
  255. this.rebuildTargetTagConditions();
  256. }
  257. rebuildTargetTagConditions() {
  258. var tags = [];
  259. var tagIndex = 0;
  260. var tagOperator = "";
  261. _.each(this.tagSegments, (segment2, index) => {
  262. if (segment2.type === 'key') {
  263. if (tags.length === 0) {
  264. tags.push({});
  265. }
  266. tags[tagIndex].key = segment2.value;
  267. } else if (segment2.type === 'value') {
  268. tagOperator = this.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  269. if (tagOperator) {
  270. this.tagSegments[index-1] = this.uiSegmentSrv.newOperator(tagOperator);
  271. tags[tagIndex].operator = tagOperator;
  272. }
  273. tags[tagIndex].value = segment2.value;
  274. } else if (segment2.type === 'condition') {
  275. tags.push({ condition: segment2.value });
  276. tagIndex += 1;
  277. } else if (segment2.type === 'operator') {
  278. tags[tagIndex].operator = segment2.value;
  279. }
  280. });
  281. this.target.tags = tags;
  282. this.panelCtrl.refresh();
  283. }
  284. getTagValueOperator(tagValue, tagOperator) {
  285. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  286. return '=~';
  287. } else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  288. return '=';
  289. }
  290. }
  291. getCollapsedText() {
  292. return this.queryModel.render(false);
  293. }
  294. }