query_ctrl.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. import { auto } from 'angular';
  2. import { clone, filter, find, findIndex, indexOf, map } from 'lodash';
  3. import { PanelEvents, QueryResultMeta } from '@grafana/data';
  4. import { TemplateSrv } from '@grafana/runtime';
  5. import { SqlPart } from 'app/angular/components/sql_part/sql_part';
  6. import appEvents from 'app/core/app_events';
  7. import { VariableWithMultiSupport } from 'app/features/variables/types';
  8. import { QueryCtrl } from 'app/plugins/sdk';
  9. import { ShowConfirmModalEvent } from 'app/types/events';
  10. import { PostgresMetaQuery } from './meta_query';
  11. import PostgresQueryModel from './postgres_query_model';
  12. import sqlPart from './sql_part';
  13. const defaultQuery = `SELECT
  14. $__time(time_column),
  15. value1
  16. FROM
  17. metric_table
  18. WHERE
  19. $__timeFilter(time_column)
  20. `;
  21. export class PostgresQueryCtrl extends QueryCtrl {
  22. static templateUrl = 'partials/query.editor.html';
  23. formats: any[];
  24. queryModel: PostgresQueryModel;
  25. metaBuilder: PostgresMetaQuery;
  26. lastQueryMeta?: QueryResultMeta;
  27. lastQueryError?: string;
  28. showHelp = false;
  29. tableSegment: any;
  30. whereAdd: any;
  31. timeColumnSegment: any;
  32. metricColumnSegment: any;
  33. selectMenu: any[] = [];
  34. selectParts: SqlPart[][] = [[]];
  35. groupParts: SqlPart[] = [];
  36. whereParts: SqlPart[] = [];
  37. groupAdd: any;
  38. /** @ngInject */
  39. constructor(
  40. $scope: any,
  41. $injector: auto.IInjectorService,
  42. private templateSrv: TemplateSrv,
  43. private uiSegmentSrv: any
  44. ) {
  45. super($scope, $injector);
  46. this.target = this.target;
  47. this.queryModel = new PostgresQueryModel(this.target, templateSrv, this.panel.scopedVars);
  48. this.metaBuilder = new PostgresMetaQuery(this.target, this.queryModel);
  49. this.updateProjection();
  50. this.formats = [
  51. { text: 'Time series', value: 'time_series' },
  52. { text: 'Table', value: 'table' },
  53. ];
  54. if (!this.target.rawSql) {
  55. // special handling when in table panel
  56. if (this.panelCtrl.panel.type === 'table') {
  57. this.target.format = 'table';
  58. this.target.rawSql = 'SELECT 1';
  59. this.target.rawQuery = true;
  60. } else {
  61. this.target.rawSql = defaultQuery;
  62. this.datasource.metricFindQuery(this.metaBuilder.findMetricTable()).then((result: any) => {
  63. if (result.length > 0) {
  64. this.target.table = result[0].text;
  65. let segment = this.uiSegmentSrv.newSegment(this.target.table);
  66. this.tableSegment.html = segment.html;
  67. this.tableSegment.value = segment.value;
  68. this.target.timeColumn = result[1].text;
  69. segment = this.uiSegmentSrv.newSegment(this.target.timeColumn);
  70. this.timeColumnSegment.html = segment.html;
  71. this.timeColumnSegment.value = segment.value;
  72. this.target.timeColumnType = 'timestamp';
  73. this.target.select = [[{ type: 'column', params: [result[2].text] }]];
  74. this.updateProjection();
  75. this.updateRawSqlAndRefresh();
  76. }
  77. });
  78. }
  79. }
  80. if (!this.target.table) {
  81. this.tableSegment = uiSegmentSrv.newSegment({ value: 'select table', fake: true });
  82. } else {
  83. this.tableSegment = uiSegmentSrv.newSegment(this.target.table);
  84. }
  85. this.timeColumnSegment = uiSegmentSrv.newSegment(this.target.timeColumn);
  86. this.metricColumnSegment = uiSegmentSrv.newSegment(this.target.metricColumn);
  87. this.buildSelectMenu();
  88. this.whereAdd = this.uiSegmentSrv.newPlusButton();
  89. this.groupAdd = this.uiSegmentSrv.newPlusButton();
  90. this.panelCtrl.events.on(PanelEvents.dataReceived, this.onDataReceived.bind(this), $scope);
  91. this.panelCtrl.events.on(PanelEvents.dataError, this.onDataError.bind(this), $scope);
  92. }
  93. updateRawSqlAndRefresh() {
  94. if (!this.target.rawQuery) {
  95. this.target.rawSql = this.queryModel.buildQuery();
  96. }
  97. this.panelCtrl.refresh();
  98. }
  99. timescaleAggCheck() {
  100. const aggIndex = this.findAggregateIndex(this.selectParts[0]);
  101. // add or remove TimescaleDB aggregate functions as needed
  102. if (aggIndex !== -1) {
  103. const baseOpts = this.selectParts[0][aggIndex].def.params[0].baseOptions;
  104. const timescaleOpts = baseOpts.concat(this.selectParts[0][aggIndex].def.params[0].timescaleOptions);
  105. if (this.datasource.jsonData.timescaledb === true) {
  106. this.selectParts[0][aggIndex].def.params[0].options = timescaleOpts;
  107. } else {
  108. this.selectParts[0][aggIndex].def.params[0].options = baseOpts;
  109. }
  110. }
  111. }
  112. updateProjection() {
  113. this.selectParts = map(this.target.select, (parts: any) => {
  114. return map(parts, sqlPart.create).filter((n) => n);
  115. });
  116. this.timescaleAggCheck();
  117. this.whereParts = map(this.target.where, sqlPart.create).filter((n) => n);
  118. this.groupParts = map(this.target.group, sqlPart.create).filter((n) => n);
  119. }
  120. updatePersistedParts() {
  121. this.target.select = map(this.selectParts, (selectParts) => {
  122. return map(selectParts, (part: any) => {
  123. return { type: part.def.type, datatype: part.datatype, params: part.params };
  124. });
  125. });
  126. this.timescaleAggCheck();
  127. this.target.where = map(this.whereParts, (part: any) => {
  128. return { type: part.def.type, datatype: part.datatype, name: part.name, params: part.params };
  129. });
  130. this.target.group = map(this.groupParts, (part: any) => {
  131. return { type: part.def.type, datatype: part.datatype, params: part.params };
  132. });
  133. }
  134. buildSelectMenu() {
  135. this.selectMenu = [];
  136. const aggregates = {
  137. text: 'Aggregate Functions',
  138. value: 'aggregate',
  139. submenu: [
  140. { text: 'Average', value: 'avg' },
  141. { text: 'Count', value: 'count' },
  142. { text: 'Maximum', value: 'max' },
  143. { text: 'Minimum', value: 'min' },
  144. { text: 'Sum', value: 'sum' },
  145. { text: 'Standard deviation', value: 'stddev' },
  146. { text: 'Variance', value: 'variance' },
  147. ],
  148. };
  149. // first and last aggregate are timescaledb specific
  150. if (this.datasource.jsonData.timescaledb === true) {
  151. aggregates.submenu.push({ text: 'First', value: 'first' });
  152. aggregates.submenu.push({ text: 'Last', value: 'last' });
  153. }
  154. this.selectMenu.push(aggregates);
  155. // ordered set aggregates require postgres 9.4+
  156. if (this.datasource.jsonData.postgresVersion >= 904) {
  157. const aggregates2 = {
  158. text: 'Ordered-Set Aggregate Functions',
  159. value: 'percentile',
  160. submenu: [
  161. { text: 'Percentile (continuous)', value: 'percentile_cont' },
  162. { text: 'Percentile (discrete)', value: 'percentile_disc' },
  163. ],
  164. };
  165. this.selectMenu.push(aggregates2);
  166. }
  167. const windows = {
  168. text: 'Window Functions',
  169. value: 'window',
  170. submenu: [
  171. { text: 'Delta', value: 'delta' },
  172. { text: 'Increase', value: 'increase' },
  173. { text: 'Rate', value: 'rate' },
  174. { text: 'Sum', value: 'sum' },
  175. { text: 'Moving Average', value: 'avg', type: 'moving_window' },
  176. ],
  177. };
  178. this.selectMenu.push(windows);
  179. this.selectMenu.push({ text: 'Alias', value: 'alias' });
  180. this.selectMenu.push({ text: 'Column', value: 'column' });
  181. }
  182. toggleEditorMode() {
  183. if (this.target.rawQuery) {
  184. appEvents.publish(
  185. new ShowConfirmModalEvent({
  186. title: 'Warning',
  187. text2: 'Switching to query builder may overwrite your raw SQL.',
  188. icon: 'exclamation-triangle',
  189. yesText: 'Switch',
  190. onConfirm: () => {
  191. // This could be called from React, so wrap in $evalAsync.
  192. // Will then either run as part of the current digest cycle or trigger a new one.
  193. this.$scope.$evalAsync(() => {
  194. this.target.rawQuery = !this.target.rawQuery;
  195. });
  196. },
  197. })
  198. );
  199. } else {
  200. // This could be called from React, so wrap in $evalAsync.
  201. // Will then either run as part of the current digest cycle or trigger a new one.
  202. this.$scope.$evalAsync(() => {
  203. this.target.rawQuery = !this.target.rawQuery;
  204. });
  205. }
  206. }
  207. resetPlusButton(button: { html: any; value: any; type: any; fake: any }) {
  208. const plusButton = this.uiSegmentSrv.newPlusButton();
  209. button.html = plusButton.html;
  210. button.value = plusButton.value;
  211. button.type = plusButton.type;
  212. button.fake = plusButton.fake;
  213. }
  214. getTableSegments() {
  215. return this.datasource
  216. .metricFindQuery(this.metaBuilder.buildTableQuery())
  217. .then(this.transformToSegments({}))
  218. .catch(this.handleQueryError.bind(this));
  219. }
  220. tableChanged() {
  221. this.target.table = this.tableSegment.value;
  222. this.target.where = [];
  223. this.target.group = [];
  224. this.updateProjection();
  225. const segment = this.uiSegmentSrv.newSegment('none');
  226. this.metricColumnSegment.html = segment.html;
  227. this.metricColumnSegment.value = segment.value;
  228. this.target.metricColumn = 'none';
  229. const task1 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('time')).then((result: any) => {
  230. // check if time column is still valid
  231. if (result.length > 0 && !find(result, (r: any) => r.text === this.target.timeColumn)) {
  232. const segment = this.uiSegmentSrv.newSegment(result[0].text);
  233. this.timeColumnSegment.html = segment.html;
  234. this.timeColumnSegment.value = segment.value;
  235. }
  236. return this.timeColumnChanged(false);
  237. });
  238. const task2 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('value')).then((result: any) => {
  239. if (result.length > 0) {
  240. this.target.select = [[{ type: 'column', params: [result[0].text] }]];
  241. this.updateProjection();
  242. }
  243. });
  244. Promise.all([task1, task2]).then(() => {
  245. this.updateRawSqlAndRefresh();
  246. });
  247. }
  248. getTimeColumnSegments() {
  249. return this.datasource
  250. .metricFindQuery(this.metaBuilder.buildColumnQuery('time'))
  251. .then(this.transformToSegments({}))
  252. .catch(this.handleQueryError.bind(this));
  253. }
  254. timeColumnChanged(refresh?: boolean) {
  255. this.target.timeColumn = this.timeColumnSegment.value;
  256. return this.datasource
  257. .metricFindQuery(this.metaBuilder.buildDatatypeQuery(this.target.timeColumn))
  258. .then((result: any) => {
  259. if (result.length === 1) {
  260. if (this.target.timeColumnType !== result[0].text) {
  261. this.target.timeColumnType = result[0].text;
  262. }
  263. let partModel;
  264. if (this.queryModel.hasUnixEpochTimecolumn()) {
  265. partModel = sqlPart.create({ type: 'macro', name: '$__unixEpochFilter', params: [] });
  266. } else {
  267. partModel = sqlPart.create({ type: 'macro', name: '$__timeFilter', params: [] });
  268. }
  269. if (this.whereParts.length >= 1 && this.whereParts[0].def.type === 'macro') {
  270. // replace current macro
  271. this.whereParts[0] = partModel;
  272. } else {
  273. this.whereParts.splice(0, 0, partModel);
  274. }
  275. }
  276. this.updatePersistedParts();
  277. if (refresh !== false) {
  278. this.updateRawSqlAndRefresh();
  279. }
  280. });
  281. }
  282. getMetricColumnSegments() {
  283. return this.datasource
  284. .metricFindQuery(this.metaBuilder.buildColumnQuery('metric'))
  285. .then(this.transformToSegments({ addNone: true }))
  286. .catch(this.handleQueryError.bind(this));
  287. }
  288. metricColumnChanged() {
  289. this.target.metricColumn = this.metricColumnSegment.value;
  290. this.updateRawSqlAndRefresh();
  291. }
  292. onDataReceived(dataList: any) {
  293. this.lastQueryError = undefined;
  294. this.lastQueryMeta = dataList[0]?.meta;
  295. }
  296. onDataError(err: any) {
  297. if (err.data && err.data.results) {
  298. const queryRes = err.data.results[this.target.refId];
  299. if (queryRes) {
  300. this.lastQueryError = queryRes.error;
  301. }
  302. }
  303. }
  304. transformToSegments(config: { addNone?: any; addTemplateVars?: any; templateQuoter?: any }) {
  305. return (results: any) => {
  306. const segments = map(results, (segment) => {
  307. return this.uiSegmentSrv.newSegment({
  308. value: segment.text,
  309. expandable: segment.expandable,
  310. });
  311. });
  312. if (config.addTemplateVars) {
  313. for (const variable of this.templateSrv.getVariables()) {
  314. let value;
  315. value = '$' + variable.name;
  316. if (config.templateQuoter && (variable as unknown as VariableWithMultiSupport).multi === false) {
  317. value = config.templateQuoter(value);
  318. }
  319. segments.unshift(
  320. this.uiSegmentSrv.newSegment({
  321. type: 'template',
  322. value: value,
  323. expandable: true,
  324. })
  325. );
  326. }
  327. }
  328. if (config.addNone) {
  329. segments.unshift(this.uiSegmentSrv.newSegment({ type: 'template', value: 'none', expandable: true }));
  330. }
  331. return segments;
  332. };
  333. }
  334. findAggregateIndex(selectParts: any) {
  335. return findIndex(selectParts, (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile');
  336. }
  337. findWindowIndex(selectParts: any) {
  338. return findIndex(selectParts, (p: any) => p.def.type === 'window' || p.def.type === 'moving_window');
  339. }
  340. addSelectPart(selectParts: any[], item: { value: any }, subItem: { type: any; value: any }) {
  341. let partType = item.value;
  342. if (subItem && subItem.type) {
  343. partType = subItem.type;
  344. }
  345. let partModel = sqlPart.create({ type: partType });
  346. if (subItem) {
  347. partModel.params[0] = subItem.value;
  348. }
  349. let addAlias = false;
  350. switch (partType) {
  351. case 'column':
  352. const parts = map(selectParts, (part: any) => {
  353. return sqlPart.create({ type: part.def.type, params: clone(part.params) });
  354. });
  355. this.selectParts.push(parts);
  356. break;
  357. case 'percentile':
  358. case 'aggregate':
  359. // add group by if no group by yet
  360. if (this.target.group.length === 0) {
  361. this.addGroup('time', '$__interval');
  362. }
  363. const aggIndex = this.findAggregateIndex(selectParts);
  364. if (aggIndex !== -1) {
  365. // replace current aggregation
  366. selectParts[aggIndex] = partModel;
  367. } else {
  368. selectParts.splice(1, 0, partModel);
  369. }
  370. if (!find(selectParts, (p: any) => p.def.type === 'alias')) {
  371. addAlias = true;
  372. }
  373. break;
  374. case 'moving_window':
  375. case 'window':
  376. const windowIndex = this.findWindowIndex(selectParts);
  377. if (windowIndex !== -1) {
  378. // replace current window function
  379. selectParts[windowIndex] = partModel;
  380. } else {
  381. const aggIndex = this.findAggregateIndex(selectParts);
  382. if (aggIndex !== -1) {
  383. selectParts.splice(aggIndex + 1, 0, partModel);
  384. } else {
  385. selectParts.splice(1, 0, partModel);
  386. }
  387. }
  388. if (!find(selectParts, (p: any) => p.def.type === 'alias')) {
  389. addAlias = true;
  390. }
  391. break;
  392. case 'alias':
  393. addAlias = true;
  394. break;
  395. }
  396. if (addAlias) {
  397. // set initial alias name to column name
  398. partModel = sqlPart.create({ type: 'alias', params: [selectParts[0].params[0].replace(/"/g, '')] });
  399. if (selectParts[selectParts.length - 1].def.type === 'alias') {
  400. selectParts[selectParts.length - 1] = partModel;
  401. } else {
  402. selectParts.push(partModel);
  403. }
  404. }
  405. this.updatePersistedParts();
  406. this.updateRawSqlAndRefresh();
  407. }
  408. removeSelectPart(selectParts: any, part: { def: { type: string } }) {
  409. if (part.def.type === 'column') {
  410. // remove all parts of column unless its last column
  411. if (this.selectParts.length > 1) {
  412. const modelsIndex = indexOf(this.selectParts, selectParts);
  413. this.selectParts.splice(modelsIndex, 1);
  414. }
  415. } else {
  416. const partIndex = indexOf(selectParts, part);
  417. selectParts.splice(partIndex, 1);
  418. }
  419. this.updatePersistedParts();
  420. }
  421. handleSelectPartEvent(selectParts: any, part: { def: any }, evt: { name: any }) {
  422. switch (evt.name) {
  423. case 'get-param-options': {
  424. switch (part.def.type) {
  425. case 'aggregate':
  426. return this.datasource
  427. .metricFindQuery(this.metaBuilder.buildAggregateQuery())
  428. .then(this.transformToSegments({}))
  429. .catch(this.handleQueryError.bind(this));
  430. case 'column':
  431. return this.datasource
  432. .metricFindQuery(this.metaBuilder.buildColumnQuery('value'))
  433. .then(this.transformToSegments({}))
  434. .catch(this.handleQueryError.bind(this));
  435. }
  436. }
  437. case 'part-param-changed': {
  438. this.updatePersistedParts();
  439. this.updateRawSqlAndRefresh();
  440. break;
  441. }
  442. case 'action': {
  443. this.removeSelectPart(selectParts, part);
  444. this.updateRawSqlAndRefresh();
  445. break;
  446. }
  447. case 'get-part-actions': {
  448. return Promise.resolve([{ text: 'Remove', value: 'remove-part' }]);
  449. }
  450. }
  451. }
  452. handleGroupPartEvent(part: any, index: any, evt: { name: any }) {
  453. switch (evt.name) {
  454. case 'get-param-options': {
  455. return this.datasource
  456. .metricFindQuery(this.metaBuilder.buildColumnQuery())
  457. .then(this.transformToSegments({}))
  458. .catch(this.handleQueryError.bind(this));
  459. }
  460. case 'part-param-changed': {
  461. this.updatePersistedParts();
  462. this.updateRawSqlAndRefresh();
  463. break;
  464. }
  465. case 'action': {
  466. this.removeGroup(part, index);
  467. this.updateRawSqlAndRefresh();
  468. break;
  469. }
  470. case 'get-part-actions': {
  471. return Promise.resolve([{ text: 'Remove', value: 'remove-part' }]);
  472. }
  473. }
  474. }
  475. addGroup(partType: string, value: string) {
  476. let params = [value];
  477. if (partType === 'time') {
  478. params = ['$__interval', 'none'];
  479. }
  480. const partModel = sqlPart.create({ type: partType, params: params });
  481. if (partType === 'time') {
  482. // put timeGroup at start
  483. this.groupParts.splice(0, 0, partModel);
  484. } else {
  485. this.groupParts.push(partModel);
  486. }
  487. // add aggregates when adding group by
  488. for (const selectParts of this.selectParts) {
  489. if (!selectParts.some((part) => part.def.type === 'aggregate')) {
  490. const aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] });
  491. selectParts.splice(1, 0, aggregate);
  492. if (!selectParts.some((part) => part.def.type === 'alias')) {
  493. const alias = sqlPart.create({ type: 'alias', params: [selectParts[0].part.params[0]] });
  494. selectParts.push(alias);
  495. }
  496. }
  497. }
  498. this.updatePersistedParts();
  499. }
  500. removeGroup(part: { def: { type: string } }, index: number) {
  501. if (part.def.type === 'time') {
  502. // remove aggregations
  503. this.selectParts = map(this.selectParts, (s: any) => {
  504. return filter(s, (part: any) => {
  505. if (part.def.type === 'aggregate' || part.def.type === 'percentile') {
  506. return false;
  507. }
  508. return true;
  509. });
  510. });
  511. }
  512. this.groupParts.splice(index, 1);
  513. this.updatePersistedParts();
  514. }
  515. handleWherePartEvent(whereParts: any, part: any, evt: any, index: any) {
  516. switch (evt.name) {
  517. case 'get-param-options': {
  518. switch (evt.param.name) {
  519. case 'left':
  520. return this.datasource
  521. .metricFindQuery(this.metaBuilder.buildColumnQuery())
  522. .then(this.transformToSegments({}))
  523. .catch(this.handleQueryError.bind(this));
  524. case 'right':
  525. if (['int4', 'int8', 'float4', 'float8', 'timestamp', 'timestamptz'].indexOf(part.datatype) > -1) {
  526. // don't do value lookups for numerical fields
  527. return Promise.resolve([]);
  528. } else {
  529. return this.datasource
  530. .metricFindQuery(this.metaBuilder.buildValueQuery(part.params[0]))
  531. .then(
  532. this.transformToSegments({
  533. addTemplateVars: true,
  534. templateQuoter: (v: string) => {
  535. return this.queryModel.quoteLiteral(v);
  536. },
  537. })
  538. )
  539. .catch(this.handleQueryError.bind(this));
  540. }
  541. case 'op':
  542. return Promise.resolve(this.uiSegmentSrv.newOperators(this.metaBuilder.getOperators(part.datatype)));
  543. default:
  544. return Promise.resolve([]);
  545. }
  546. }
  547. case 'part-param-changed': {
  548. this.updatePersistedParts();
  549. this.datasource.metricFindQuery(this.metaBuilder.buildDatatypeQuery(part.params[0])).then((d: any) => {
  550. if (d.length === 1) {
  551. part.datatype = d[0].text;
  552. }
  553. });
  554. this.updateRawSqlAndRefresh();
  555. break;
  556. }
  557. case 'action': {
  558. // remove element
  559. whereParts.splice(index, 1);
  560. this.updatePersistedParts();
  561. this.updateRawSqlAndRefresh();
  562. break;
  563. }
  564. case 'get-part-actions': {
  565. return Promise.resolve([{ text: 'Remove', value: 'remove-part' }]);
  566. }
  567. }
  568. }
  569. getWhereOptions() {
  570. const options = [];
  571. if (this.queryModel.hasUnixEpochTimecolumn()) {
  572. options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' }));
  573. } else {
  574. options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' }));
  575. }
  576. options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' }));
  577. return Promise.resolve(options);
  578. }
  579. addWhereAction(part: any, index: any) {
  580. switch (this.whereAdd.type) {
  581. case 'macro': {
  582. const partModel = sqlPart.create({ type: 'macro', name: this.whereAdd.value, params: [] });
  583. if (this.whereParts.length >= 1 && this.whereParts[0].def.type === 'macro') {
  584. // replace current macro
  585. this.whereParts[0] = partModel;
  586. } else {
  587. this.whereParts.splice(0, 0, partModel);
  588. }
  589. break;
  590. }
  591. default: {
  592. this.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] }));
  593. }
  594. }
  595. this.updatePersistedParts();
  596. this.resetPlusButton(this.whereAdd);
  597. this.updateRawSqlAndRefresh();
  598. }
  599. getGroupOptions() {
  600. return this.datasource
  601. .metricFindQuery(this.metaBuilder.buildColumnQuery('group'))
  602. .then((tags: any) => {
  603. const options = [];
  604. if (!this.queryModel.hasTimeGroup()) {
  605. options.push(this.uiSegmentSrv.newSegment({ type: 'time', value: 'time($__interval,none)' }));
  606. }
  607. for (const tag of tags) {
  608. options.push(this.uiSegmentSrv.newSegment({ type: 'column', value: tag.text }));
  609. }
  610. return options;
  611. })
  612. .catch(this.handleQueryError.bind(this));
  613. }
  614. addGroupAction() {
  615. this.addGroup(this.groupAdd.type, this.groupAdd.value);
  616. this.resetPlusButton(this.groupAdd);
  617. this.updateRawSqlAndRefresh();
  618. }
  619. handleQueryError(err: any): any[] {
  620. this.error = err.message || 'Failed to issue metric query';
  621. return [];
  622. }
  623. }