query_ctrl.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. 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. buildSelectMenu() {
  57. var categories = queryPart.getCategories();
  58. this.selectMenu = _.reduce(categories, function(memo, cat, key) {
  59. var menu = {
  60. text: key,
  61. submenu: cat.map(item => {
  62. return {text: item.type, value: item.type};
  63. }),
  64. };
  65. memo.push(menu);
  66. return memo;
  67. }, []);
  68. }
  69. getGroupByOptions() {
  70. var query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  71. return this.datasource.metricFindQuery(query).then(tags => {
  72. var options = [];
  73. if (!this.queryModel.hasFill()) {
  74. options.push(this.uiSegmentSrv.newSegment({value: 'fill(null)'}));
  75. }
  76. if (!this.queryModel.hasGroupByTime()) {
  77. options.push(this.uiSegmentSrv.newSegment({value: 'time($interval)'}));
  78. }
  79. for (let tag of tags) {
  80. options.push(this.uiSegmentSrv.newSegment({value: 'tag(' + tag.text + ')'}));
  81. }
  82. return options;
  83. }).catch(this.handleQueryError.bind(this));
  84. }
  85. groupByAction() {
  86. this.queryModel.addGroupBy(this.groupBySegment.value);
  87. var plusButton = this.uiSegmentSrv.newPlusButton();
  88. this.groupBySegment.value = plusButton.value;
  89. this.groupBySegment.html = plusButton.html;
  90. this.panelCtrl.refresh();
  91. }
  92. removeGroupByPart(part, index) {
  93. this.queryModel.removeGroupByPart(part, index);
  94. this.panelCtrl.refresh();
  95. }
  96. addSelectPart(selectParts, cat, subitem) {
  97. this.queryModel.addSelectPart(selectParts, subitem.value);
  98. this.panelCtrl.refresh();
  99. }
  100. removeSelectPart(selectParts, part) {
  101. this.queryModel.removeSelectPart(selectParts, part);
  102. this.panelCtrl.refresh();
  103. }
  104. selectPartUpdated() {
  105. this.panelCtrl.refresh();
  106. }
  107. fixTagSegments() {
  108. var count = this.tagSegments.length;
  109. var lastSegment = this.tagSegments[Math.max(count-1, 0)];
  110. if (!lastSegment || lastSegment.type !== 'plus-button') {
  111. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  112. }
  113. }
  114. measurementChanged() {
  115. this.target.measurement = this.measurementSegment.value;
  116. this.panelCtrl.refresh();
  117. }
  118. getPolicySegments() {
  119. var policiesQuery = this.queryBuilder.buildExploreQuery('RETENTION POLICIES');
  120. return this.datasource.metricFindQuery(policiesQuery)
  121. .then(this.transformToSegments(false))
  122. .catch(this.handleQueryError.bind(this));
  123. }
  124. policyChanged() {
  125. this.target.policy = this.policySegment.value;
  126. this.panelCtrl.refresh();
  127. }
  128. toggleEditorMode() {
  129. try {
  130. this.target.query = this.queryModel.render(false);
  131. } catch (err) {
  132. console.log('query render error');
  133. }
  134. this.target.rawQuery = !this.target.rawQuery;
  135. }
  136. getMeasurements() {
  137. var query = this.queryBuilder.buildExploreQuery('MEASUREMENTS');
  138. return this.datasource.metricFindQuery(query)
  139. .then(this.transformToSegments(true))
  140. .catch(this.handleQueryError.bind(this));
  141. }
  142. getPartOptions(part) {
  143. if (part.def.type === 'field') {
  144. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  145. return this.datasource.metricFindQuery(fieldsQuery)
  146. .then(this.transformToSegments(true))
  147. .catch(this.handleQueryError.bind(this));
  148. }
  149. if (part.def.type === 'tag') {
  150. var tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  151. return this.datasource.metricFindQuery(tagsQuery)
  152. .then(this.transformToSegments(true))
  153. .catch(this.handleQueryError.bind(true));
  154. }
  155. }
  156. handleQueryError(err) {
  157. this.error = err.message || 'Failed to issue metric query';
  158. return [];
  159. }
  160. transformToSegments(addTemplateVars) {
  161. return (results) => {
  162. var segments = _.map(results, segment => {
  163. return this.uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  164. });
  165. if (addTemplateVars) {
  166. for (let variable of this.templateSrv.variables) {
  167. segments.unshift(this.uiSegmentSrv.newSegment({ type: 'template', value: '/^$' + variable.name + '$/', expandable: true }));
  168. }
  169. }
  170. return segments;
  171. };
  172. }
  173. getTagsOrValues(segment, index) {
  174. if (segment.type === 'condition') {
  175. return this.$q.when([this.uiSegmentSrv.newSegment('AND'), this.uiSegmentSrv.newSegment('OR')]);
  176. }
  177. if (segment.type === 'operator') {
  178. var nextValue = this.tagSegments[index+1].value;
  179. if (/^\/.*\/$/.test(nextValue)) {
  180. return this.$q.when(this.uiSegmentSrv.newOperators(['=~', '!~']));
  181. } else {
  182. return this.$q.when(this.uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  183. }
  184. }
  185. var query, addTemplateVars;
  186. if (segment.type === 'key' || segment.type === 'plus-button') {
  187. query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  188. addTemplateVars = false;
  189. } else if (segment.type === 'value') {
  190. query = this.queryBuilder.buildExploreQuery('TAG_VALUES', this.tagSegments[index-2].value);
  191. addTemplateVars = true;
  192. }
  193. return this.datasource.metricFindQuery(query)
  194. .then(this.transformToSegments(addTemplateVars))
  195. .then(results => {
  196. if (segment.type === 'key') {
  197. results.splice(0, 0, angular.copy(this.removeTagFilterSegment));
  198. }
  199. return results;
  200. })
  201. .catch(this.handleQueryError.bind(this));
  202. }
  203. getFieldSegments() {
  204. var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  205. return this.datasource.metricFindQuery(fieldsQuery)
  206. .then(this.transformToSegments(false))
  207. .catch(this.handleQueryError);
  208. }
  209. setFill(fill) {
  210. this.target.fill = fill;
  211. this.panelCtrl.refresh();
  212. }
  213. tagSegmentUpdated(segment, index) {
  214. this.tagSegments[index] = segment;
  215. // handle remove tag condition
  216. if (segment.value === this.removeTagFilterSegment.value) {
  217. this.tagSegments.splice(index, 3);
  218. if (this.tagSegments.length === 0) {
  219. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  220. } else if (this.tagSegments.length > 2) {
  221. this.tagSegments.splice(Math.max(index-1, 0), 1);
  222. if (this.tagSegments[this.tagSegments.length-1].type !== 'plus-button') {
  223. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  224. }
  225. }
  226. } else {
  227. if (segment.type === 'plus-button') {
  228. if (index > 2) {
  229. this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
  230. }
  231. this.tagSegments.push(this.uiSegmentSrv.newOperator('='));
  232. this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  233. segment.type = 'key';
  234. segment.cssClass = 'query-segment-key';
  235. }
  236. if ((index+1) === this.tagSegments.length) {
  237. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  238. }
  239. }
  240. this.rebuildTargetTagConditions();
  241. }
  242. rebuildTargetTagConditions() {
  243. var tags = [];
  244. var tagIndex = 0;
  245. var tagOperator = "";
  246. _.each(this.tagSegments, (segment2, index) => {
  247. if (segment2.type === 'key') {
  248. if (tags.length === 0) {
  249. tags.push({});
  250. }
  251. tags[tagIndex].key = segment2.value;
  252. } else if (segment2.type === 'value') {
  253. tagOperator = this.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  254. if (tagOperator) {
  255. this.tagSegments[index-1] = this.uiSegmentSrv.newOperator(tagOperator);
  256. tags[tagIndex].operator = tagOperator;
  257. }
  258. tags[tagIndex].value = segment2.value;
  259. } else if (segment2.type === 'condition') {
  260. tags.push({ condition: segment2.value });
  261. tagIndex += 1;
  262. } else if (segment2.type === 'operator') {
  263. tags[tagIndex].operator = segment2.value;
  264. }
  265. });
  266. this.target.tags = tags;
  267. this.panelCtrl.refresh();
  268. }
  269. getTagValueOperator(tagValue, tagOperator) {
  270. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  271. return '=~';
  272. } else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  273. return '=';
  274. }
  275. }
  276. getCollapsedText() {
  277. return this.queryModel.render(false);
  278. }
  279. }