mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 02:32:53 +08:00

* Chore: Remove angular dependency from backendSrv * Refactor: Naive soultion for logging out unauthorized users * Refactor: Restructures to different streams * Refactor: Restructures datasourceRequest * Refactor: Flipped back if statement * Refactor: Extracted getFromFetchStream * Refactor: Extracts toFailureStream operation * Refactor: Fixes issue when options.params contains arrays * Refactor: Fixes broken test (but we need a lot more) * Refactor: Adds explaining comments * Refactor: Adds latest RxJs version so cancellations work * Refactor: Cleans up the takeUntil code * Refactor: Adds tests for request function * Refactor: Separates into smaller functions * Refactor: Adds last error tests * Started to changed so we require getBackendSrv from the @grafana-runtime when applicable. * Using the getBackendSrv from @grafana/runtime. * Changed so we use the getBackendSrv from the @grafana-runtime when possible. * Fixed so Server Admin -> Orgs works again. * Removed unused dependency. * Fixed digest issues on the Server Admin -> Users page. * Fix: Fixes digest problems in Playlists * Fix: Fixes digest issues in VersionHistory * Tests: Fixes broken tests * Fix: Fixes digest issues in Alerting => Notification channels * Fixed digest issues on the Intive page. * Fixed so we run digest after password reset email sent. * Fixed digest issue when trying to sign up account. * Fixed so the Server Admin -> Edit Org works with backendSrv * Fixed so Server Admin -> Users works with backend srv. * Fixed digest issues in Server Admin -> Orgs * Fix: Fixes digest issues in DashList plugin * Fixed digest issues on Server Admin -> users. * Fix: Fixes digest issues with Snapshots * Fixed digest issue when deleting a user. * Fix: Fixes digest issues with dashLink * Chore: Changes RxJs version to 6.5.4 which includes the same cancellation fix * Fix: Fixes digest issue when toggling folder in manage dashboards * Fix: Fixes bug in executeInOrder * Fix: Fixes digest issue with CreateFolderCtrl and FolderDashboardsCtrl * Fix: Fixes tslint error in test * Refactor: Changes default behaviour for emitted messages as before migration * Fix: Fixes various digest issues when saving, starring or deleting dashboards * Fix: Fixes digest issues with FolderPickerCtrl * Fixed digest issue. * Fixed digest issues. * Fixed issues with angular digest. * Removed the this.digest pattern. Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com> Co-authored-by: Marcus Andersson <systemvetaren@gmail.com>
200 lines
5.7 KiB
TypeScript
200 lines
5.7 KiB
TypeScript
import _ from 'lodash';
|
|
import ResponseParser from './response_parser';
|
|
import PostgresQuery from 'app/plugins/datasource/postgres/postgres_query';
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
//Types
|
|
import { PostgresQueryForInterpolation } from './types';
|
|
import { getSearchFilterScopedVar } from '../../../features/templating/variable';
|
|
|
|
export class PostgresDatasource {
|
|
id: any;
|
|
name: any;
|
|
jsonData: any;
|
|
responseParser: ResponseParser;
|
|
queryModel: PostgresQuery;
|
|
interval: string;
|
|
|
|
/** @ngInject */
|
|
constructor(
|
|
instanceSettings: { name: any; id?: any; jsonData?: any },
|
|
private templateSrv: TemplateSrv,
|
|
private timeSrv: TimeSrv
|
|
) {
|
|
this.name = instanceSettings.name;
|
|
this.id = instanceSettings.id;
|
|
this.jsonData = instanceSettings.jsonData;
|
|
this.responseParser = new ResponseParser();
|
|
this.queryModel = new PostgresQuery({});
|
|
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
|
|
}
|
|
|
|
interpolateVariable = (value: string, variable: { multi: any; includeAll: any }) => {
|
|
if (typeof value === 'string') {
|
|
if (variable.multi || variable.includeAll) {
|
|
return this.queryModel.quoteLiteral(value);
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
if (typeof value === 'number') {
|
|
return value;
|
|
}
|
|
|
|
const quotedValues = _.map(value, v => {
|
|
return this.queryModel.quoteLiteral(v);
|
|
});
|
|
return quotedValues.join(',');
|
|
};
|
|
|
|
interpolateVariablesInQueries(queries: PostgresQueryForInterpolation[]): PostgresQueryForInterpolation[] {
|
|
let expandedQueries = queries;
|
|
if (queries && queries.length > 0) {
|
|
expandedQueries = queries.map(query => {
|
|
const expandedQuery = {
|
|
...query,
|
|
datasource: this.name,
|
|
rawSql: this.templateSrv.replace(query.rawSql, {}, this.interpolateVariable),
|
|
};
|
|
return expandedQuery;
|
|
});
|
|
}
|
|
return expandedQueries;
|
|
}
|
|
|
|
query(options: any) {
|
|
const queries = _.filter(options.targets, target => {
|
|
return target.hide !== true;
|
|
}).map(target => {
|
|
const queryModel = new PostgresQuery(target, this.templateSrv, options.scopedVars);
|
|
|
|
return {
|
|
refId: target.refId,
|
|
intervalMs: options.intervalMs,
|
|
maxDataPoints: options.maxDataPoints,
|
|
datasourceId: this.id,
|
|
rawSql: queryModel.render(this.interpolateVariable),
|
|
format: target.format,
|
|
};
|
|
});
|
|
|
|
if (queries.length === 0) {
|
|
return Promise.resolve({ data: [] });
|
|
}
|
|
|
|
return getBackendSrv()
|
|
.datasourceRequest({
|
|
url: '/api/tsdb/query',
|
|
method: 'POST',
|
|
data: {
|
|
from: options.range.from.valueOf().toString(),
|
|
to: options.range.to.valueOf().toString(),
|
|
queries: queries,
|
|
},
|
|
})
|
|
.then(this.responseParser.processQueryResult);
|
|
}
|
|
|
|
annotationQuery(options: any) {
|
|
if (!options.annotation.rawQuery) {
|
|
return Promise.reject({
|
|
message: 'Query missing in annotation definition',
|
|
});
|
|
}
|
|
|
|
const query = {
|
|
refId: options.annotation.name,
|
|
datasourceId: this.id,
|
|
rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
|
|
format: 'table',
|
|
};
|
|
|
|
return getBackendSrv()
|
|
.datasourceRequest({
|
|
url: '/api/tsdb/query',
|
|
method: 'POST',
|
|
data: {
|
|
from: options.range.from.valueOf().toString(),
|
|
to: options.range.to.valueOf().toString(),
|
|
queries: [query],
|
|
},
|
|
})
|
|
.then((data: any) => this.responseParser.transformAnnotationResponse(options, data));
|
|
}
|
|
|
|
metricFindQuery(query: string, optionalOptions: { variable?: any; searchFilter?: string }) {
|
|
let refId = 'tempvar';
|
|
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
|
refId = optionalOptions.variable.name;
|
|
}
|
|
|
|
const rawSql = this.templateSrv.replace(
|
|
query,
|
|
getSearchFilterScopedVar({ query, wildcardChar: '%', options: optionalOptions }),
|
|
this.interpolateVariable
|
|
);
|
|
|
|
const interpolatedQuery = {
|
|
refId: refId,
|
|
datasourceId: this.id,
|
|
rawSql,
|
|
format: 'table',
|
|
};
|
|
|
|
const range = this.timeSrv.timeRange();
|
|
const data = {
|
|
queries: [interpolatedQuery],
|
|
from: range.from.valueOf().toString(),
|
|
to: range.to.valueOf().toString(),
|
|
};
|
|
|
|
return getBackendSrv()
|
|
.datasourceRequest({
|
|
url: '/api/tsdb/query',
|
|
method: 'POST',
|
|
data: data,
|
|
})
|
|
.then((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data));
|
|
}
|
|
|
|
getVersion() {
|
|
return this.metricFindQuery("SELECT current_setting('server_version_num')::int/100", {});
|
|
}
|
|
|
|
getTimescaleDBVersion() {
|
|
return this.metricFindQuery("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'", {});
|
|
}
|
|
|
|
testDatasource() {
|
|
return this.metricFindQuery('SELECT 1', {})
|
|
.then((res: any) => {
|
|
return { status: 'success', message: 'Database Connection OK' };
|
|
})
|
|
.catch((err: any) => {
|
|
console.log(err);
|
|
if (err.data && err.data.message) {
|
|
return { status: 'error', message: err.data.message };
|
|
} else {
|
|
return { status: 'error', message: err.status };
|
|
}
|
|
});
|
|
}
|
|
|
|
targetContainsTemplate(target: any) {
|
|
let rawSql = '';
|
|
|
|
if (target.rawQuery) {
|
|
rawSql = target.rawSql;
|
|
} else {
|
|
const query = new PostgresQuery(target);
|
|
rawSql = query.buildQuery();
|
|
}
|
|
|
|
rawSql = rawSql.replace('$__', '');
|
|
|
|
return this.templateSrv.variableExists(rawSql);
|
|
}
|
|
}
|