query_ctrl.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import _ from 'lodash';
  2. import appEvents from 'app/core/app_events';
  3. import { PostgresMetaQuery } from './meta_query';
  4. import { QueryCtrl } from 'app/plugins/sdk';
  5. import { SqlPart } from 'app/core/components/sql_part/sql_part';
  6. import PostgresQuery from './postgres_query';
  7. import sqlPart from './sql_part';
  8. export interface QueryMeta {
  9. sql: string;
  10. }
  11. const defaultQuery = `SELECT
  12. $__time(time_column),
  13. value1
  14. FROM
  15. metric_table
  16. WHERE
  17. $__timeFilter(time_column)
  18. `;
  19. export class PostgresQueryCtrl extends QueryCtrl {
  20. static templateUrl = 'partials/query.editor.html';
  21. showLastQuerySQL: boolean;
  22. formats: any[];
  23. queryModel: PostgresQuery;
  24. metaBuilder: PostgresMetaQuery;
  25. lastQueryMeta: QueryMeta;
  26. lastQueryError: string;
  27. showHelp: boolean;
  28. tableSegment: any;
  29. whereAdd: any;
  30. timeColumnSegment: any;
  31. metricColumnSegment: any;
  32. selectMenu: any[];
  33. selectParts: SqlPart[][];
  34. groupParts: SqlPart[];
  35. whereParts: SqlPart[];
  36. groupAdd: any;
  37. /** @ngInject **/
  38. constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
  39. super($scope, $injector);
  40. this.target = this.target;
  41. this.queryModel = new PostgresQuery(this.target, templateSrv, this.panel.scopedVars);
  42. this.metaBuilder = new PostgresMetaQuery(this.target, this.queryModel);
  43. this.updateProjection();
  44. this.formats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
  45. if (!this.target.rawSql) {
  46. // special handling when in table panel
  47. if (this.panelCtrl.panel.type === 'table') {
  48. this.target.format = 'table';
  49. this.target.rawSql = 'SELECT 1';
  50. } else {
  51. this.target.rawSql = defaultQuery;
  52. }
  53. }
  54. if (!this.target.table) {
  55. this.tableSegment = uiSegmentSrv.newSegment({ value: 'select table', fake: true });
  56. } else {
  57. this.tableSegment = uiSegmentSrv.newSegment(this.target.table);
  58. }
  59. this.timeColumnSegment = uiSegmentSrv.newSegment(this.target.timeColumn);
  60. this.metricColumnSegment = uiSegmentSrv.newSegment(this.target.metricColumn);
  61. this.buildSelectMenu();
  62. this.whereAdd = this.uiSegmentSrv.newPlusButton();
  63. this.groupAdd = this.uiSegmentSrv.newPlusButton();
  64. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  65. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  66. }
  67. updateProjection() {
  68. this.selectParts = _.map(this.target.select, function(parts: any) {
  69. return _.map(parts, sqlPart.create).filter(n => n);
  70. });
  71. this.whereParts = _.map(this.target.where, sqlPart.create).filter(n => n);
  72. this.groupParts = _.map(this.target.group, sqlPart.create).filter(n => n);
  73. }
  74. updatePersistedParts() {
  75. this.target.select = _.map(this.selectParts, function(selectParts) {
  76. return _.map(selectParts, function(part: any) {
  77. return { type: part.def.type, params: part.params };
  78. });
  79. });
  80. this.target.where = _.map(this.whereParts, function(part: any) {
  81. return { type: part.def.type, name: part.name, params: part.params };
  82. });
  83. this.target.group = _.map(this.groupParts, function(part: any) {
  84. return { type: part.def.type, params: part.params };
  85. });
  86. }
  87. buildSelectMenu() {
  88. this.selectMenu = [];
  89. let aggregates = {
  90. text: 'Aggregate Functions',
  91. value: 'aggregate',
  92. submenu: [
  93. { text: 'Average', value: 'avg' },
  94. { text: 'Count', value: 'count' },
  95. { text: 'Maximum', value: 'max' },
  96. { text: 'Minimum', value: 'min' },
  97. { text: 'Sum', value: 'sum' },
  98. { text: 'Standard deviation', value: 'stddev' },
  99. { text: 'Variance', value: 'variance' },
  100. ],
  101. };
  102. // first and last aggregate are timescaledb specific
  103. if (this.datasource.jsonData.timescaledb === true) {
  104. aggregates.submenu.push({ text: 'First', value: 'first' });
  105. aggregates.submenu.push({ text: 'Last', value: 'last' });
  106. }
  107. this.selectMenu.push(aggregates);
  108. // ordered set aggregates require postgres 9.4+
  109. let aggregates2 = {
  110. text: 'Ordered-Set Aggregate Functions',
  111. value: 'percentile',
  112. submenu: [
  113. { text: 'Percentile (continuous)', value: 'percentile_cont' },
  114. { text: 'Percentile (discrete)', value: 'percentile_disc' },
  115. ],
  116. };
  117. this.selectMenu.push(aggregates2);
  118. let windows = {
  119. text: 'Window Functions',
  120. value: 'window',
  121. submenu: [
  122. { text: 'Increase', value: 'increase' },
  123. { text: 'Rate', value: 'rate' },
  124. { text: 'Sum', value: 'sum' },
  125. { text: 'Moving Average', value: 'avg', type: 'moving_window' },
  126. ],
  127. };
  128. this.selectMenu.push(windows);
  129. this.selectMenu.push({ text: 'Alias', value: 'alias' });
  130. this.selectMenu.push({ text: 'Column', value: 'column' });
  131. }
  132. toggleEditorMode() {
  133. if (this.target.rawQuery) {
  134. appEvents.emit('confirm-modal', {
  135. title: 'Warning',
  136. text2: 'Switching to query builder may overwrite your raw SQL.',
  137. icon: 'fa-exclamation',
  138. yesText: 'Switch',
  139. onConfirm: () => {
  140. this.target.rawQuery = !this.target.rawQuery;
  141. },
  142. });
  143. } else {
  144. this.target.rawQuery = !this.target.rawQuery;
  145. }
  146. }
  147. resetPlusButton(button) {
  148. let plusButton = this.uiSegmentSrv.newPlusButton();
  149. button.html = plusButton.html;
  150. button.value = plusButton.value;
  151. }
  152. getTableSegments() {
  153. return this.datasource
  154. .metricFindQuery(this.metaBuilder.buildTableQuery())
  155. .then(this.transformToSegments({}))
  156. .catch(this.handleQueryError.bind(this));
  157. }
  158. tableChanged() {
  159. this.target.table = this.tableSegment.value;
  160. this.panelCtrl.refresh();
  161. }
  162. getTimeColumnSegments() {
  163. return this.datasource
  164. .metricFindQuery(this.metaBuilder.buildColumnQuery('time'))
  165. .then(this.transformToSegments({}))
  166. .catch(this.handleQueryError.bind(this));
  167. }
  168. timeColumnChanged() {
  169. this.target.timeColumn = this.timeColumnSegment.value;
  170. this.datasource.metricFindQuery(this.metaBuilder.buildDatatypeQuery(this.target.timeColumn)).then(result => {
  171. if (result.length === 1) {
  172. this.target.timeColumnType = result[0];
  173. }
  174. });
  175. this.panelCtrl.refresh();
  176. }
  177. getMetricColumnSegments() {
  178. return this.datasource
  179. .metricFindQuery(this.metaBuilder.buildColumnQuery('metric'))
  180. .then(this.transformToSegments({ addNone: true }))
  181. .catch(this.handleQueryError.bind(this));
  182. }
  183. metricColumnChanged() {
  184. this.target.metricColumn = this.metricColumnSegment.value;
  185. this.panelCtrl.refresh();
  186. }
  187. onDataReceived(dataList) {
  188. this.lastQueryMeta = null;
  189. this.lastQueryError = null;
  190. let anySeriesFromQuery = _.find(dataList, { refId: this.target.refId });
  191. if (anySeriesFromQuery) {
  192. this.lastQueryMeta = anySeriesFromQuery.meta;
  193. }
  194. }
  195. onDataError(err) {
  196. if (err.data && err.data.results) {
  197. let queryRes = err.data.results[this.target.refId];
  198. if (queryRes) {
  199. this.lastQueryMeta = queryRes.meta;
  200. this.lastQueryError = queryRes.error;
  201. }
  202. }
  203. }
  204. transformToSegments(config) {
  205. return results => {
  206. let segments = _.map(results, segment => {
  207. return this.uiSegmentSrv.newSegment({
  208. value: segment.text,
  209. expandable: segment.expandable,
  210. });
  211. });
  212. if (config.addTemplateVars) {
  213. for (let variable of this.templateSrv.variables) {
  214. let value;
  215. value = '$' + variable.name;
  216. if (config.templateQuoter && variable.multi === false) {
  217. value = config.templateQuoter(value);
  218. }
  219. segments.unshift(
  220. this.uiSegmentSrv.newSegment({
  221. type: 'template',
  222. value: value,
  223. expandable: true,
  224. })
  225. );
  226. }
  227. }
  228. if (config.addNone) {
  229. segments.unshift(this.uiSegmentSrv.newSegment({ type: 'template', value: 'none', expandable: true }));
  230. }
  231. return segments;
  232. };
  233. }
  234. findAggregateIndex(selectParts) {
  235. return _.findIndex(selectParts, (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile');
  236. }
  237. findWindowIndex(selectParts) {
  238. return _.findIndex(selectParts, (p: any) => p.def.type === 'window' || p.def.type === 'moving_window');
  239. }
  240. addSelectPart(selectParts, item, subItem) {
  241. let partType = item.value;
  242. if (subItem && subItem.type) {
  243. partType = subItem.type;
  244. }
  245. let partModel = sqlPart.create({ type: partType });
  246. if (subItem) {
  247. partModel.params[0] = subItem.value;
  248. }
  249. let addAlias = false;
  250. switch (partType) {
  251. case 'column':
  252. let parts = _.map(selectParts, function(part: any) {
  253. return sqlPart.create({ type: part.def.type, params: _.clone(part.params) });
  254. });
  255. this.selectParts.push(parts);
  256. break;
  257. case 'percentile':
  258. case 'aggregate':
  259. // add group by if no group by yet
  260. if (this.target.group.length === 0) {
  261. this.addGroup('time', '1m');
  262. }
  263. let aggIndex = this.findAggregateIndex(selectParts);
  264. if (aggIndex !== -1) {
  265. // replace current aggregation
  266. selectParts[aggIndex] = partModel;
  267. } else {
  268. selectParts.splice(1, 0, partModel);
  269. }
  270. if (!_.find(selectParts, (p: any) => p.def.type === 'alias')) {
  271. addAlias = true;
  272. }
  273. break;
  274. case 'moving_window':
  275. case 'window':
  276. let windowIndex = this.findWindowIndex(selectParts);
  277. if (windowIndex !== -1) {
  278. // replace current window function
  279. selectParts[windowIndex] = partModel;
  280. } else {
  281. let aggIndex = this.findAggregateIndex(selectParts);
  282. if (aggIndex !== -1) {
  283. selectParts.splice(aggIndex + 1, 0, partModel);
  284. } else {
  285. selectParts.splice(1, 0, partModel);
  286. }
  287. }
  288. if (!_.find(selectParts, (p: any) => p.def.type === 'alias')) {
  289. addAlias = true;
  290. }
  291. break;
  292. case 'alias':
  293. addAlias = true;
  294. break;
  295. }
  296. if (addAlias) {
  297. // set initial alias name to column name
  298. partModel = sqlPart.create({ type: 'alias', params: [selectParts[0].params[0]] });
  299. if (selectParts[selectParts.length - 1].def.type === 'alias') {
  300. selectParts[selectParts.length - 1] = partModel;
  301. } else {
  302. selectParts.push(partModel);
  303. }
  304. }
  305. this.updatePersistedParts();
  306. this.panelCtrl.refresh();
  307. }
  308. removeSelectPart(selectParts, part) {
  309. if (part.def.type === 'column') {
  310. // remove all parts of column unless its last column
  311. if (this.selectParts.length > 1) {
  312. let modelsIndex = _.indexOf(this.selectParts, selectParts);
  313. this.selectParts.splice(modelsIndex, 1);
  314. }
  315. } else {
  316. let partIndex = _.indexOf(selectParts, part);
  317. selectParts.splice(partIndex, 1);
  318. }
  319. this.updatePersistedParts();
  320. }
  321. handleSelectPartEvent(selectParts, part, evt) {
  322. switch (evt.name) {
  323. case 'get-param-options': {
  324. switch (part.def.type) {
  325. case 'aggregate':
  326. return this.datasource
  327. .metricFindQuery(this.metaBuilder.buildAggregateQuery())
  328. .then(this.transformToSegments({}))
  329. .catch(this.handleQueryError.bind(this));
  330. case 'column':
  331. return this.datasource
  332. .metricFindQuery(this.metaBuilder.buildColumnQuery('value'))
  333. .then(this.transformToSegments({}))
  334. .catch(this.handleQueryError.bind(this));
  335. }
  336. }
  337. case 'part-param-changed': {
  338. this.panelCtrl.refresh();
  339. break;
  340. }
  341. case 'action': {
  342. this.removeSelectPart(selectParts, part);
  343. this.panelCtrl.refresh();
  344. break;
  345. }
  346. case 'get-part-actions': {
  347. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  348. }
  349. }
  350. }
  351. handleGroupPartEvent(part, index, evt) {
  352. switch (evt.name) {
  353. case 'get-param-options': {
  354. return this.datasource
  355. .metricFindQuery(this.metaBuilder.buildColumnQuery())
  356. .then(this.transformToSegments({}))
  357. .catch(this.handleQueryError.bind(this));
  358. }
  359. case 'part-param-changed': {
  360. this.panelCtrl.refresh();
  361. break;
  362. }
  363. case 'action': {
  364. this.removeGroup(part, index);
  365. this.panelCtrl.refresh();
  366. break;
  367. }
  368. case 'get-part-actions': {
  369. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  370. }
  371. }
  372. }
  373. addGroup(partType, value) {
  374. let params = [value];
  375. if (partType === 'time') {
  376. params = ['1m', 'none'];
  377. }
  378. let partModel = sqlPart.create({ type: partType, params: params });
  379. if (partType === 'time') {
  380. // put timeGroup at start
  381. this.groupParts.splice(0, 0, partModel);
  382. } else {
  383. this.groupParts.push(partModel);
  384. }
  385. // add aggregates when adding group by
  386. for (let selectParts of this.selectParts) {
  387. if (!selectParts.some(part => part.def.type === 'aggregate')) {
  388. let aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] });
  389. selectParts.splice(1, 0, aggregate);
  390. if (!selectParts.some(part => part.def.type === 'alias')) {
  391. let alias = sqlPart.create({ type: 'alias', params: [selectParts[0].part.params[0]] });
  392. selectParts.push(alias);
  393. }
  394. }
  395. }
  396. this.updatePersistedParts();
  397. }
  398. removeGroup(part, index) {
  399. if (part.def.type === 'time') {
  400. // remove aggregations
  401. this.selectParts = _.map(this.selectParts, (s: any) => {
  402. return _.filter(s, (part: any) => {
  403. if (part.def.type === 'aggregate' || part.def.type === 'percentile') {
  404. return false;
  405. }
  406. return true;
  407. });
  408. });
  409. }
  410. this.groupParts.splice(index, 1);
  411. this.updatePersistedParts();
  412. }
  413. handleWherePartEvent(whereParts, part, evt, index) {
  414. switch (evt.name) {
  415. case 'get-param-options': {
  416. switch (evt.param.name) {
  417. case 'left':
  418. return this.datasource
  419. .metricFindQuery(this.metaBuilder.buildColumnQuery())
  420. .then(this.transformToSegments({}))
  421. .catch(this.handleQueryError.bind(this));
  422. case 'right':
  423. return this.datasource
  424. .metricFindQuery(this.metaBuilder.buildValueQuery(part.params[0]))
  425. .then(this.transformToSegments({ addTemplateVars: true, templateQuoter: this.queryModel.quoteLiteral }))
  426. .catch(this.handleQueryError.bind(this));
  427. case 'op':
  428. return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN']));
  429. default:
  430. return this.$q.when([]);
  431. }
  432. }
  433. case 'part-param-changed': {
  434. this.panelCtrl.refresh();
  435. break;
  436. }
  437. case 'action': {
  438. // remove element
  439. whereParts.splice(index, 1);
  440. this.updatePersistedParts();
  441. this.panelCtrl.refresh();
  442. break;
  443. }
  444. case 'get-part-actions': {
  445. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  446. }
  447. }
  448. }
  449. getWhereOptions() {
  450. var options = [];
  451. options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' }));
  452. // options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' }));
  453. options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' }));
  454. return this.$q.when(options);
  455. }
  456. addWhereAction(part, index) {
  457. switch (this.whereAdd.type) {
  458. case 'macro': {
  459. this.whereParts.push(sqlPart.create({ type: 'macro', name: this.whereAdd.value, params: [] }));
  460. break;
  461. }
  462. default: {
  463. this.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] }));
  464. }
  465. }
  466. this.updatePersistedParts();
  467. this.resetPlusButton(this.whereAdd);
  468. this.panelCtrl.refresh();
  469. }
  470. getGroupOptions() {
  471. return this.datasource
  472. .metricFindQuery(this.metaBuilder.buildColumnQuery('group'))
  473. .then(tags => {
  474. var options = [];
  475. if (!this.queryModel.hasTimeGroup()) {
  476. options.push(this.uiSegmentSrv.newSegment({ type: 'time', value: 'time(1m,none)' }));
  477. }
  478. for (let tag of tags) {
  479. options.push(this.uiSegmentSrv.newSegment({ type: 'column', value: tag.text }));
  480. }
  481. return options;
  482. })
  483. .catch(this.handleQueryError.bind(this));
  484. }
  485. addGroupAction() {
  486. switch (this.groupAdd.value) {
  487. default: {
  488. this.addGroup(this.groupAdd.type, this.groupAdd.value);
  489. }
  490. }
  491. this.resetPlusButton(this.groupAdd);
  492. this.panelCtrl.refresh();
  493. }
  494. handleQueryError(err) {
  495. this.error = err.message || 'Failed to issue metric query';
  496. return [];
  497. }
  498. }