Postgres/MySQL/MSSQL: Remove usage of legacy timeSrv (#45241)

This commit is contained in:
Marcus Efraimsson
2022-02-11 13:36:11 +01:00
committed by GitHub
parent fed112ef3b
commit 0e8a5407d1
6 changed files with 19 additions and 57 deletions

View File

@ -2,12 +2,11 @@ import { map as _map } from 'lodash';
import { lastValueFrom, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { BackendDataSourceResponse, DataSourceWithBackend, FetchResponse, getBackendSrv } from '@grafana/runtime';
import { AnnotationEvent, DataSourceInstanceSettings, MetricFindValue, ScopedVars } from '@grafana/data';
import { AnnotationEvent, DataSourceInstanceSettings, MetricFindValue, ScopedVars, TimeRange } from '@grafana/data';
import ResponseParser from './response_parser';
import PostgresQueryModel from 'app/plugins/datasource/postgres/postgres_query_model';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
//Types
import { PostgresOptions, PostgresQuery, PostgresQueryForInterpolation } from './types';
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
@ -23,8 +22,7 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
constructor(
instanceSettings: DataSourceInstanceSettings<PostgresOptions>,
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
private readonly timeSrv: TimeSrv = getTimeSrv()
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super(instanceSettings);
this.name = instanceSettings.name;
@ -142,7 +140,7 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
format: 'table',
};
const range = this.timeSrv.timeRange();
const range = optionalOptions?.range as TimeRange;
return lastValueFrom(
getBackendSrv()
@ -150,8 +148,8 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
url: '/api/ds/query',
method: 'POST',
data: {
from: range.from.valueOf().toString(),
to: range.to.valueOf().toString(),
from: range?.from?.valueOf()?.toString(),
to: range?.to?.valueOf()?.toString(),
queries: [interpolatedQuery],
},
requestId: refId,
@ -169,7 +167,6 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
private _metaRequest(rawSql: string) {
const refId = 'meta';
const range = this.timeSrv.timeRange();
const query = {
refId: refId,
datasource: this.getRef(),
@ -180,8 +177,6 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
url: '/api/ds/query',
method: 'POST',
data: {
from: range.from.valueOf().toString(),
to: range.to.valueOf().toString(),
queries: [query],
},
requestId: refId,

View File

@ -7,14 +7,12 @@ import {
DataSourceInstanceSettings,
dateTime,
MutableDataFrame,
toUtc,
} from '@grafana/data';
import { PostgresDatasource } from '../datasource';
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
import { TemplateSrv } from 'app/features/templating/template_srv';
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv';
import { PostgresOptions, PostgresQuery } from '../types';
jest.mock('@grafana/runtime', () => ({
@ -43,21 +41,10 @@ describe('PostgreSQLDatasource', () => {
},
} as unknown as DataSourceInstanceSettings<PostgresOptions>;
const templateSrv: TemplateSrv = new TemplateSrv();
const raw = {
from: toUtc('2018-04-25 10:00'),
to: toUtc('2018-04-25 11:00'),
};
const timeSrvMock = {
timeRange: () => ({
from: raw.from,
to: raw.to,
raw: raw,
}),
} as unknown as TimeSrv;
const variable = { ...initialCustomVariableModelState };
const ds = new PostgresDatasource(instanceSettings, templateSrv, timeSrvMock);
const ds = new PostgresDatasource(instanceSettings, templateSrv);
return { ds, templateSrv, timeSrvMock, variable };
return { ds, templateSrv, variable };
};
// https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing