grafana/public/app/plugins/datasource/mssql/query_ctrl.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

66 lines
1.7 KiB
TypeScript

import { auto } from 'angular';
import { PanelEvents, QueryResultMeta } from '@grafana/data';
import { QueryCtrl } from 'app/plugins/sdk';
import { MssqlQuery } from './types';
const defaultQuery = `SELECT
$__timeEpoch(<time_column>),
<value column> as value,
<series name column> as metric
FROM
<table name>
WHERE
$__timeFilter(time_column)
ORDER BY
<time_column> ASC`;
export class MssqlQueryCtrl extends QueryCtrl<MssqlQuery> {
static templateUrl = 'partials/query.editor.html';
formats: any[];
lastQueryMeta?: QueryResultMeta;
lastQueryError?: string;
showHelp = false;
/** @ngInject */
constructor($scope: any, $injector: auto.IInjectorService) {
super($scope, $injector);
this.target.format = this.target.format || 'time_series';
this.target.alias = '';
this.formats = [
{ text: 'Time series', value: 'time_series' },
{ text: 'Table', value: 'table' },
];
if (!this.target.rawSql) {
// special handling when in table panel
if (this.panelCtrl.panel.type === 'table') {
this.target.format = 'table';
this.target.rawSql = 'SELECT 1';
} else {
this.target.rawSql = defaultQuery;
}
}
this.panelCtrl.events.on(PanelEvents.dataReceived, this.onDataReceived.bind(this), $scope);
this.panelCtrl.events.on(PanelEvents.dataError, this.onDataError.bind(this), $scope);
}
onDataReceived(dataList: any) {
this.lastQueryError = undefined;
this.lastQueryMeta = dataList[0]?.meta;
}
onDataError(err: any) {
if (err.data && err.data.results) {
const queryRes = err.data.results[this.target.refId];
if (queryRes) {
this.lastQueryError = queryRes.error;
}
}
}
}