query_ctrl.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { auto } from 'angular';
  2. import { PanelEvents, QueryResultMeta } from '@grafana/data';
  3. import { QueryCtrl } from 'app/plugins/sdk';
  4. import { MssqlQuery } from './types';
  5. const defaultQuery = `SELECT
  6. $__timeEpoch(<time_column>),
  7. <value column> as value,
  8. <series name column> as metric
  9. FROM
  10. <table name>
  11. WHERE
  12. $__timeFilter(time_column)
  13. ORDER BY
  14. <time_column> ASC`;
  15. export class MssqlQueryCtrl extends QueryCtrl<MssqlQuery> {
  16. static templateUrl = 'partials/query.editor.html';
  17. formats: any[];
  18. lastQueryMeta?: QueryResultMeta;
  19. lastQueryError?: string;
  20. showHelp = false;
  21. /** @ngInject */
  22. constructor($scope: any, $injector: auto.IInjectorService) {
  23. super($scope, $injector);
  24. this.target.format = this.target.format || 'time_series';
  25. this.target.alias = '';
  26. this.formats = [
  27. { text: 'Time series', value: 'time_series' },
  28. { text: 'Table', value: 'table' },
  29. ];
  30. if (!this.target.rawSql) {
  31. // special handling when in table panel
  32. if (this.panelCtrl.panel.type === 'table') {
  33. this.target.format = 'table';
  34. this.target.rawSql = 'SELECT 1';
  35. } else {
  36. this.target.rawSql = defaultQuery;
  37. }
  38. }
  39. this.panelCtrl.events.on(PanelEvents.dataReceived, this.onDataReceived.bind(this), $scope);
  40. this.panelCtrl.events.on(PanelEvents.dataError, this.onDataError.bind(this), $scope);
  41. }
  42. onDataReceived(dataList: any) {
  43. this.lastQueryError = undefined;
  44. this.lastQueryMeta = dataList[0]?.meta;
  45. }
  46. onDataError(err: any) {
  47. if (err.data && err.data.results) {
  48. const queryRes = err.data.results[this.target.refId];
  49. if (queryRes) {
  50. this.lastQueryError = queryRes.error;
  51. }
  52. }
  53. }
  54. }