Browse Source

update SQL connector to support Grafana 9

pull/12/head
rozetko 2 years ago
parent
commit
7c89c455ed
  1. 17
      src/connectors/sql.ts
  2. 2
      src/connectors/utils.ts
  3. 20
      src/services/query_service/grafana.ts

17
src/connectors/sql.ts

@ -6,11 +6,13 @@ import * as _ from 'lodash';
// as of 26.09.2020, it works for all SQL datasources // as of 26.09.2020, it works for all SQL datasources
export class SqlConnector extends DatasourceConnector { export class SqlConnector extends DatasourceConnector {
private _targetName: string; //save first target name, while multi metric not implemented private _targetName: string; //save first target name, while multi metric not implemented
private url: string = 'api/tsdb/query'; private url: string;
constructor(datasource: Datasource, targets: any[]) { constructor(datasource: Datasource, targets: any[]) {
super(datasource, targets); super(datasource, targets);
this.url = datasource.url;
if(targets.length === 0) { if(targets.length === 0) {
throw Error('got empty targets list'); throw Error('got empty targets list');
} }
@ -34,7 +36,7 @@ export class SqlConnector extends DatasourceConnector {
data: { data: {
from: String(from), from: String(from),
to: String(to), to: String(to),
queries: queries queries: queries,
} }
} }
}; };
@ -53,16 +55,15 @@ export class SqlConnector extends DatasourceConnector {
// TODO: support more than 1 metric (each res.data.results item is a metric) // TODO: support more than 1 metric (each res.data.results item is a metric)
let results = res.data.results[this._targetName]; let results = res.data.results[this._targetName];
if (!results.series) { if (_.isEmpty(results.frames)) {
return emptyResult; return emptyResult;
} }
let points = results.series[0].points; const frame = results.frames[0];
points.forEach(p => p.reverse());
return { return {
columns: ['timestamp', results.series[0].name], columns: frame.schema.fields.map(field => field.name),
values: points // @ts-ignore
values: _.zip(...frame.data.values),
}; };
} }
} }

2
src/connectors/utils.ts

@ -32,7 +32,7 @@ export function processSQLLimitOffset(sql: string, limit: number, offset: number
} }
function ensureParentheses(regex: RegExp, str: string): { index: number, length: number } { function ensureParentheses(regex: RegExp, str: string): { index: number, length: number } {
let occurence: RegExpExecArray; let occurence: RegExpExecArray | null;
while((occurence = regex.exec(str)) !== null) { while((occurence = regex.exec(str)) !== null) {
let leftPart = str.slice(0, occurence.index) let leftPart = str.slice(0, occurence.index)
let rightPart = str.slice(occurence.index + occurence[0].length); let rightPart = str.slice(occurence.index + occurence[0].length);

20
src/services/query_service/grafana.ts

@ -16,9 +16,6 @@ export class GrafanaQueryService extends QueryService {
async query(query: DatasourceQuery, apiKey: string): Promise<AxiosResponse<any>> { async query(query: DatasourceQuery, apiKey: string): Promise<AxiosResponse<any>> {
let headers = { Authorization: `Bearer ${apiKey}` }; let headers = { Authorization: `Bearer ${apiKey}` };
const grafanaUrl = getGrafanaUrl(query.url);
query.url = `${grafanaUrl}/${query.url}`;
if(query.headers !== undefined) { if(query.headers !== undefined) {
_.merge(headers, query.headers); _.merge(headers, query.headers);
} }
@ -64,20 +61,3 @@ export class GrafanaQueryService extends QueryService {
} }
} }
} }
export function getGrafanaUrl(url: string): string {
const parsedUrl = new URL(url);
const path = parsedUrl.pathname;
const panelUrl = path.match(/^\/*([^\/]*)\/d\//);
if(panelUrl === null) {
return url;
}
const origin = parsedUrl.origin;
const grafanaSubPath = panelUrl[1];
if(grafanaSubPath.length > 0) {
return `${origin}/${grafanaSubPath}`;
}
return origin;
}

Loading…
Cancel
Save