query_ctrl.ts 11 KB

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