query_ctrl.ts 9.9 KB

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