query_ctrl.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import { InfluxQueryBuilder } from './query_builder';
  4. import InfluxQuery from './influx_query';
  5. import queryPart from './query_part';
  6. import { QueryCtrl } from 'app/plugins/sdk';
  7. export class InfluxQueryCtrl extends QueryCtrl {
  8. static templateUrl = 'partials/query.editor.html';
  9. queryModel: InfluxQuery;
  10. queryBuilder: any;
  11. groupBySegment: any;
  12. resultFormats: any[];
  13. orderByTime: 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 = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
  27. this.policySegment = uiSegmentSrv.newSegment(this.target.policy);
  28. if (!this.target.measurement) {
  29. this.measurementSegment = uiSegmentSrv.newSelectMeasurement();
  30. } else {
  31. this.measurementSegment = uiSegmentSrv.newSegment(this.target.measurement);
  32. }
  33. this.tagSegments = [];
  34. for (const tag of this.target.tags) {
  35. if (!tag.operator) {
  36. if (/^\/.*\/$/.test(tag.value)) {
  37. tag.operator = '=~';
  38. } else {
  39. tag.operator = '=';
  40. }
  41. }
  42. if (tag.condition) {
  43. this.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
  44. }
  45. this.tagSegments.push(uiSegmentSrv.newKey(tag.key));
  46. this.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
  47. this.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
  48. }
  49. this.fixTagSegments();
  50. this.buildSelectMenu();
  51. this.removeTagFilterSegment = uiSegmentSrv.newSegment({
  52. fake: true,
  53. value: '-- remove tag filter --',
  54. });
  55. }
  56. removeOrderByTime() {
  57. this.target.orderByTime = 'ASC';
  58. }
  59. buildSelectMenu() {
  60. const categories = queryPart.getCategories();
  61. this.selectMenu = _.reduce(
  62. categories,
  63. function(memo, cat, key) {
  64. const menu = {
  65. text: key,
  66. submenu: cat.map(item => {
  67. return { text: item.type, value: item.type };
  68. }),
  69. };
  70. memo.push(menu);
  71. return memo;
  72. },
  73. []
  74. );
  75. }
  76. getGroupByOptions() {
  77. const query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  78. return this.datasource
  79. .metricFindQuery(query)
  80. .then(tags => {
  81. const options = [];
  82. if (!this.queryModel.hasFill()) {
  83. options.push(this.uiSegmentSrv.newSegment({ value: 'fill(null)' }));
  84. }
  85. if (!this.target.limit) {
  86. options.push(this.uiSegmentSrv.newSegment({ value: 'LIMIT' }));
  87. }
  88. if (!this.target.slimit) {
  89. options.push(this.uiSegmentSrv.newSegment({ value: 'SLIMIT' }));
  90. }
  91. if (this.target.orderByTime === 'ASC') {
  92. options.push(this.uiSegmentSrv.newSegment({ value: 'ORDER BY time DESC' }));
  93. }
  94. if (!this.queryModel.hasGroupByTime()) {
  95. options.push(this.uiSegmentSrv.newSegment({ value: 'time($interval)' }));
  96. }
  97. for (const tag of tags) {
  98. options.push(this.uiSegmentSrv.newSegment({ value: 'tag(' + tag.text + ')' }));
  99. }
  100. return options;
  101. })
  102. .catch(this.handleQueryError.bind(this));
  103. }
  104. groupByAction() {
  105. switch (this.groupBySegment.value) {
  106. case 'LIMIT': {
  107. this.target.limit = 10;
  108. break;
  109. }
  110. case 'SLIMIT': {
  111. this.target.slimit = 10;
  112. break;
  113. }
  114. case 'ORDER BY time DESC': {
  115. this.target.orderByTime = 'DESC';
  116. break;
  117. }
  118. default: {
  119. this.queryModel.addGroupBy(this.groupBySegment.value);
  120. }
  121. }
  122. const plusButton = this.uiSegmentSrv.newPlusButton();
  123. this.groupBySegment.value = plusButton.value;
  124. this.groupBySegment.html = plusButton.html;
  125. this.panelCtrl.refresh();
  126. }
  127. addSelectPart(selectParts, cat, subitem) {
  128. this.queryModel.addSelectPart(selectParts, subitem.value);
  129. this.panelCtrl.refresh();
  130. }
  131. handleSelectPartEvent(selectParts, part, evt) {
  132. switch (evt.name) {
  133. case 'get-param-options': {
  134. const fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  135. return this.datasource
  136. .metricFindQuery(fieldsQuery)
  137. .then(this.transformToSegments(true))
  138. .catch(this.handleQueryError.bind(this));
  139. }
  140. case 'part-param-changed': {
  141. this.panelCtrl.refresh();
  142. break;
  143. }
  144. case 'action': {
  145. this.queryModel.removeSelectPart(selectParts, part);
  146. this.panelCtrl.refresh();
  147. break;
  148. }
  149. case 'get-part-actions': {
  150. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  151. }
  152. }
  153. }
  154. handleGroupByPartEvent(part, index, evt) {
  155. switch (evt.name) {
  156. case 'get-param-options': {
  157. const tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  158. return this.datasource
  159. .metricFindQuery(tagsQuery)
  160. .then(this.transformToSegments(true))
  161. .catch(this.handleQueryError.bind(this));
  162. }
  163. case 'part-param-changed': {
  164. this.panelCtrl.refresh();
  165. break;
  166. }
  167. case 'action': {
  168. this.queryModel.removeGroupByPart(part, index);
  169. this.panelCtrl.refresh();
  170. break;
  171. }
  172. case 'get-part-actions': {
  173. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  174. }
  175. }
  176. }
  177. fixTagSegments() {
  178. const count = this.tagSegments.length;
  179. const lastSegment = this.tagSegments[Math.max(count - 1, 0)];
  180. if (!lastSegment || lastSegment.type !== 'plus-button') {
  181. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  182. }
  183. }
  184. measurementChanged() {
  185. this.target.measurement = this.measurementSegment.value;
  186. this.panelCtrl.refresh();
  187. }
  188. getPolicySegments() {
  189. const policiesQuery = this.queryBuilder.buildExploreQuery('RETENTION POLICIES');
  190. return this.datasource
  191. .metricFindQuery(policiesQuery)
  192. .then(this.transformToSegments(false))
  193. .catch(this.handleQueryError.bind(this));
  194. }
  195. policyChanged() {
  196. this.target.policy = this.policySegment.value;
  197. this.panelCtrl.refresh();
  198. }
  199. toggleEditorMode() {
  200. try {
  201. this.target.query = this.queryModel.render(false);
  202. } catch (err) {
  203. console.log('query render error');
  204. }
  205. this.target.rawQuery = !this.target.rawQuery;
  206. }
  207. getMeasurements(measurementFilter) {
  208. const query = this.queryBuilder.buildExploreQuery('MEASUREMENTS', undefined, measurementFilter);
  209. return this.datasource
  210. .metricFindQuery(query)
  211. .then(this.transformToSegments(true))
  212. .catch(this.handleQueryError.bind(this));
  213. }
  214. handleQueryError(err) {
  215. this.error = err.message || 'Failed to issue metric query';
  216. return [];
  217. }
  218. transformToSegments(addTemplateVars) {
  219. return results => {
  220. const segments = _.map(results, segment => {
  221. return this.uiSegmentSrv.newSegment({
  222. value: segment.text,
  223. expandable: segment.expandable,
  224. });
  225. });
  226. if (addTemplateVars) {
  227. for (const variable of this.templateSrv.variables) {
  228. segments.unshift(
  229. this.uiSegmentSrv.newSegment({
  230. type: 'value',
  231. value: '/^$' + variable.name + '$/',
  232. expandable: true,
  233. })
  234. );
  235. }
  236. }
  237. return segments;
  238. };
  239. }
  240. getTagsOrValues(segment, index) {
  241. if (segment.type === 'condition') {
  242. return this.$q.when([this.uiSegmentSrv.newSegment('AND'), this.uiSegmentSrv.newSegment('OR')]);
  243. }
  244. if (segment.type === 'operator') {
  245. const nextValue = this.tagSegments[index + 1].value;
  246. if (/^\/.*\/$/.test(nextValue)) {
  247. return this.$q.when(this.uiSegmentSrv.newOperators(['=~', '!~']));
  248. } else {
  249. return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<>', '<', '>']));
  250. }
  251. }
  252. let query, addTemplateVars;
  253. if (segment.type === 'key' || segment.type === 'plus-button') {
  254. query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
  255. addTemplateVars = false;
  256. } else if (segment.type === 'value') {
  257. query = this.queryBuilder.buildExploreQuery('TAG_VALUES', this.tagSegments[index - 2].value);
  258. addTemplateVars = true;
  259. }
  260. return this.datasource
  261. .metricFindQuery(query)
  262. .then(this.transformToSegments(addTemplateVars))
  263. .then(results => {
  264. if (segment.type === 'key') {
  265. results.splice(0, 0, angular.copy(this.removeTagFilterSegment));
  266. }
  267. return results;
  268. })
  269. .catch(this.handleQueryError.bind(this));
  270. }
  271. getFieldSegments() {
  272. const fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
  273. return this.datasource
  274. .metricFindQuery(fieldsQuery)
  275. .then(this.transformToSegments(false))
  276. .catch(this.handleQueryError);
  277. }
  278. tagSegmentUpdated(segment, index) {
  279. this.tagSegments[index] = segment;
  280. // handle remove tag condition
  281. if (segment.value === this.removeTagFilterSegment.value) {
  282. this.tagSegments.splice(index, 3);
  283. if (this.tagSegments.length === 0) {
  284. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  285. } else if (this.tagSegments.length > 2) {
  286. this.tagSegments.splice(Math.max(index - 1, 0), 1);
  287. if (this.tagSegments[this.tagSegments.length - 1].type !== 'plus-button') {
  288. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  289. }
  290. }
  291. } else {
  292. if (segment.type === 'plus-button') {
  293. if (index > 2) {
  294. this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
  295. }
  296. this.tagSegments.push(this.uiSegmentSrv.newOperator('='));
  297. this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  298. segment.type = 'key';
  299. segment.cssClass = 'query-segment-key';
  300. }
  301. if (index + 1 === this.tagSegments.length) {
  302. this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
  303. }
  304. }
  305. this.rebuildTargetTagConditions();
  306. }
  307. rebuildTargetTagConditions() {
  308. const tags = [];
  309. let tagIndex = 0;
  310. let tagOperator = '';
  311. _.each(this.tagSegments, (segment2, index) => {
  312. if (segment2.type === 'key') {
  313. if (tags.length === 0) {
  314. tags.push({});
  315. }
  316. tags[tagIndex].key = segment2.value;
  317. } else if (segment2.type === 'value') {
  318. tagOperator = this.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  319. if (tagOperator) {
  320. this.tagSegments[index - 1] = this.uiSegmentSrv.newOperator(tagOperator);
  321. tags[tagIndex].operator = tagOperator;
  322. }
  323. tags[tagIndex].value = segment2.value;
  324. } else if (segment2.type === 'condition') {
  325. tags.push({ condition: segment2.value });
  326. tagIndex += 1;
  327. } else if (segment2.type === 'operator') {
  328. tags[tagIndex].operator = segment2.value;
  329. }
  330. });
  331. this.target.tags = tags;
  332. this.panelCtrl.refresh();
  333. }
  334. getTagValueOperator(tagValue, tagOperator): string {
  335. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  336. return '=~';
  337. } else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  338. return '=';
  339. }
  340. return null;
  341. }
  342. getCollapsedText() {
  343. return this.queryModel.render(false);
  344. }
  345. }