query_ctrl.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.rawQuery = !this.target.rawQuery;
  132. }
  133. getMeasurements() {
  134. var query = this.queryBuilder.buildExploreQuery('MEASUREMENTS');
  135. return this.datasource.metricFindQuery(query)
  136. .then(this.transformToSegments(true))
  137. .catch(this.handleQueryError.bind(this));
  138. }
  139. getPartOptions(part) {
  140. if (part.def.type === 'field') {
  141. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  142. return this.datasource.metricFindQuery(fieldsQuery)
  143. .then(this.transformToSegments(true))
  144. .catch(this.handleQueryError.bind(this));
  145. }
  146. if (part.def.type === 'tag') {
  147. var tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  148. return this.datasource.metricFindQuery(tagsQuery)
  149. .then(this.transformToSegments(true))
  150. .catch(this.handleQueryError.bind(true));
  151. }
  152. }
  153. handleQueryError(err) {
  154. this.error = err.message || 'Failed to issue metric query';
  155. return [];
  156. }
  157. transformToSegments(addTemplateVars) {
  158. return (results) => {
  159. var segments = _.map(results, segment => {
  160. return this.uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  161. });
  162. if (addTemplateVars) {
  163. for (let variable of this.templateSrv.variables) {
  164. segments.unshift(this.uiSegmentSrv.newSegment({ type: 'template', value: '/$' + variable.name + '$/', expandable: true }));
  165. }
  166. }
  167. return segments;
  168. };
  169. }
  170. getTagsOrValues(segment, index) {
  171. if (segment.type === 'condition') {
  172. return this.$q.when([this.uiSegmentSrv.newSegment('AND'), this.uiSegmentSrv.newSegment('OR')]);
  173. }
  174. if (segment.type === 'operator') {
  175. var nextValue = this.tagSegments[index+1].value;
  176. if (/^\/.*\/$/.test(nextValue)) {
  177. return this.$q.when(this.uiSegmentSrv.newOperators(['=~', '!~']));
  178. } else {
  179. return this.$q.when(this.uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  180. }
  181. }
  182. var query, addTemplateVars;
  183. if (segment.type === 'key' || segment.type === 'plus-button') {
  184. query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  185. addTemplateVars = false;
  186. } else if (segment.type === 'value') {
  187. query = this.queryBuilder.buildExploreQuery('TAG_VALUES', this.tagSegments[index-2].value);
  188. addTemplateVars = true;
  189. }
  190. return this.datasource.metricFindQuery(query)
  191. .then(this.transformToSegments(addTemplateVars))
  192. .then(results => {
  193. if (segment.type === 'key') {
  194. results.splice(0, 0, angular.copy(this.removeTagFilterSegment));
  195. }
  196. return results;
  197. })
  198. .catch(this.handleQueryError.bind(this));
  199. }
  200. getFieldSegments() {
  201. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  202. return this.datasource.metricFindQuery(fieldsQuery)
  203. .then(this.transformToSegments(false))
  204. .catch(this.handleQueryError);
  205. }
  206. setFill(fill) {
  207. this.target.fill = fill;
  208. this.panelCtrl.refresh();
  209. }
  210. tagSegmentUpdated(segment, index) {
  211. this.tagSegments[index] = segment;
  212. // handle remove tag condition
  213. if (segment.value === this.removeTagFilterSegment.value) {
  214. this.tagSegments.splice(index, 3);
  215. if (this.tagSegments.length === 0) {
  216. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  217. } else if (this.tagSegments.length > 2) {
  218. this.tagSegments.splice(Math.max(index-1, 0), 1);
  219. if (this.tagSegments[this.tagSegments.length-1].type !== 'plus-button') {
  220. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  221. }
  222. }
  223. } else {
  224. if (segment.type === 'plus-button') {
  225. if (index > 2) {
  226. this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
  227. }
  228. this.tagSegments.push(this.uiSegmentSrv.newOperator('='));
  229. this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  230. segment.type = 'key';
  231. segment.cssClass = 'query-segment-key';
  232. }
  233. if ((index+1) === this.tagSegments.length) {
  234. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  235. }
  236. }
  237. this.rebuildTargetTagConditions();
  238. }
  239. rebuildTargetTagConditions() {
  240. var tags = [];
  241. var tagIndex = 0;
  242. var tagOperator = "";
  243. _.each(this.tagSegments, (segment2, index) => {
  244. if (segment2.type === 'key') {
  245. if (tags.length === 0) {
  246. tags.push({});
  247. }
  248. tags[tagIndex].key = segment2.value;
  249. } else if (segment2.type === 'value') {
  250. tagOperator = this.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  251. if (tagOperator) {
  252. this.tagSegments[index-1] = this.uiSegmentSrv.newOperator(tagOperator);
  253. tags[tagIndex].operator = tagOperator;
  254. }
  255. tags[tagIndex].value = segment2.value;
  256. } else if (segment2.type === 'condition') {
  257. tags.push({ condition: segment2.value });
  258. tagIndex += 1;
  259. } else if (segment2.type === 'operator') {
  260. tags[tagIndex].operator = segment2.value;
  261. }
  262. });
  263. this.target.tags = tags;
  264. this.panelCtrl.refresh();
  265. }
  266. getTagValueOperator(tagValue, tagOperator) {
  267. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  268. return '=~';
  269. } else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  270. return '=';
  271. }
  272. }
  273. }