query_ctrl.ts 10 KB

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