query_ctrl.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import queryPart from './query_part';
  4. import PostgresQuery from './postgres_query';
  5. export interface QueryMeta {
  6. sql: string;
  7. }
  8. const defaultQuery = `SELECT
  9. $__time(time_column),
  10. value1
  11. FROM
  12. metric_table
  13. WHERE
  14. $__timeFilter(time_column)
  15. `;
  16. export class PostgresQueryCtrl extends QueryCtrl {
  17. static templateUrl = 'partials/query.editor.html';
  18. showLastQuerySQL: boolean;
  19. formats: any[];
  20. queryModel: PostgresQuery;
  21. lastQueryMeta: QueryMeta;
  22. lastQueryError: string;
  23. showHelp: boolean;
  24. schemaSegment: any;
  25. tableSegment: any;
  26. timeColumnSegment: any;
  27. selectMenu: any;
  28. /** @ngInject **/
  29. constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
  30. super($scope, $injector);
  31. this.target = this.target;
  32. this.queryModel = new PostgresQuery(this.target, templateSrv, this.panel.scopedVars);
  33. this.formats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
  34. if (!this.target.rawSql) {
  35. // special handling when in table panel
  36. if (this.panelCtrl.panel.type === 'table') {
  37. this.target.format = 'table';
  38. this.target.rawSql = 'SELECT 1';
  39. } else {
  40. this.target.rawSql = defaultQuery;
  41. }
  42. }
  43. this.schemaSegment= uiSegmentSrv.newSegment(this.target.schema);
  44. if (!this.target.table) {
  45. this.tableSegment = uiSegmentSrv.newSegment({value: 'select table',fake: true});
  46. } else {
  47. this.tableSegment= uiSegmentSrv.newSegment(this.target.table);
  48. }
  49. this.timeColumnSegment = uiSegmentSrv.newSegment(this.target.timeColumn);
  50. this.buildSelectMenu();
  51. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  52. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  53. }
  54. buildSelectMenu() {
  55. var categories = queryPart.getCategories();
  56. this.selectMenu = _.reduce(
  57. categories,
  58. function(memo, cat, key) {
  59. var menu = {
  60. text: key,
  61. submenu: cat.map(item => {
  62. return { text: item.type, value: item.type };
  63. }),
  64. };
  65. memo.push(menu);
  66. return memo;
  67. },
  68. []
  69. );
  70. }
  71. toggleEditorMode() {
  72. try {
  73. // this.target.query = this.queryModel.render(false);
  74. } catch (err) {
  75. console.log('query render error');
  76. }
  77. this.target.rawQuery = !this.target.rawQuery;
  78. }
  79. getSchemaSegments() {
  80. var schemaQuery = "SELECT schema_name FROM information_schema.schemata WHERE";
  81. schemaQuery += " schema_name NOT LIKE 'pg_%' AND schema_name <> 'information_schema';";
  82. return this.datasource
  83. .metricFindQuery(schemaQuery)
  84. .then(this.transformToSegments(true))
  85. .catch(this.handleQueryError.bind(this));
  86. }
  87. getTableSegments() {
  88. var tableQuery = "SELECT table_name FROM information_schema.tables WHERE table_schema = '" + this.target.schema + "';";
  89. return this.datasource
  90. .metricFindQuery(tableQuery)
  91. .then(this.transformToSegments(true))
  92. .catch(this.handleQueryError.bind(this));
  93. }
  94. getTimeColumnSegments() {
  95. var columnQuery = "SELECT column_name FROM information_schema.columns WHERE ";
  96. columnQuery += " table_schema = '" + this.target.schema + "'";
  97. columnQuery += " AND table_name = '" + this.target.table + "'";
  98. columnQuery += " AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real');";
  99. return this.datasource
  100. .metricFindQuery(columnQuery)
  101. .then(this.transformToSegments(true))
  102. .catch(this.handleQueryError.bind(this));
  103. }
  104. getColumnSegments() {
  105. var columnQuery = "SELECT column_name FROM information_schema.columns WHERE ";
  106. columnQuery += " table_schema = '" + this.target.schema + "'";
  107. columnQuery += " AND table_name = '" + this.target.table + "'";
  108. columnQuery += " AND data_type IN ('bigint','integer','double precision','real');";
  109. return this.datasource
  110. .metricFindQuery(columnQuery)
  111. .then(this.transformToSegments(true))
  112. .catch(this.handleQueryError.bind(this));
  113. }
  114. tableChanged() {
  115. this.target.table = this.tableSegment.value;
  116. this.panelCtrl.refresh();
  117. }
  118. schemaChanged() {
  119. this.target.schema = this.schemaSegment.value;
  120. this.panelCtrl.refresh();
  121. }
  122. timeColumnChanged() {
  123. this.target.time = this.timeColumnSegment.value;
  124. this.panelCtrl.refresh();
  125. }
  126. onDataReceived(dataList) {
  127. this.lastQueryMeta = null;
  128. this.lastQueryError = null;
  129. let anySeriesFromQuery = _.find(dataList, { refId: this.target.refId });
  130. if (anySeriesFromQuery) {
  131. this.lastQueryMeta = anySeriesFromQuery.meta;
  132. }
  133. }
  134. onDataError(err) {
  135. if (err.data && err.data.results) {
  136. let queryRes = err.data.results[this.target.refId];
  137. if (queryRes) {
  138. this.lastQueryMeta = queryRes.meta;
  139. this.lastQueryError = queryRes.error;
  140. }
  141. }
  142. }
  143. transformToSegments(addTemplateVars) {
  144. return results => {
  145. var segments = _.map(results, segment => {
  146. return this.uiSegmentSrv.newSegment({
  147. value: segment.text,
  148. expandable: segment.expandable,
  149. });
  150. });
  151. if (addTemplateVars) {
  152. for (let variable of this.templateSrv.variables) {
  153. segments.unshift(
  154. this.uiSegmentSrv.newSegment({
  155. type: 'template',
  156. value: '/^$' + variable.name + '$/',
  157. expandable: true,
  158. })
  159. );
  160. }
  161. }
  162. return segments;
  163. };
  164. }
  165. addSelectPart(selectParts, cat, subitem) {
  166. this.queryModel.addSelectPart(selectParts, subitem.value);
  167. this.panelCtrl.refresh();
  168. }
  169. handleSelectPartEvent(selectParts, part, evt) {
  170. switch (evt.name) {
  171. case 'get-param-options': {
  172. var columnQuery = "SELECT column_name FROM information_schema.columns WHERE ";
  173. columnQuery += " table_schema = '" + this.target.schema + "'";
  174. columnQuery += " AND table_name = '" + this.target.table + "'";
  175. columnQuery += " AND data_type IN ('bigint','integer','double precision','real');";
  176. return this.datasource
  177. .metricFindQuery(columnQuery)
  178. .then(this.transformToSegments(true))
  179. .catch(this.handleQueryError.bind(this));
  180. }
  181. case 'part-param-changed': {
  182. this.panelCtrl.refresh();
  183. break;
  184. }
  185. case 'action': {
  186. this.queryModel.removeSelectPart(selectParts, part);
  187. this.panelCtrl.refresh();
  188. break;
  189. }
  190. case 'get-part-actions': {
  191. return this.$q.when([{ text: 'Remove', value: 'remove-part' }]);
  192. }
  193. }
  194. }
  195. handleQueryError(err) {
  196. this.error = err.message || 'Failed to issue metric query';
  197. return [];
  198. }
  199. }