query_ctrl.ts 11 KB

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