query_ctrl.ts 11 KB

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