query_ctrl.ts 16 KB

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