query_ctrl.ts 12 KB

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