query_ctrl.ts 11 KB

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