query_ctrl.ts 9.8 KB

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