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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 { catchError, map, mapTo } 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 { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import { MssqlOptions, MssqlQuery, MssqlQueryForInterpolation } from './types';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOptions> {
@ -18,8 +17,7 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
constructor(
instanceSettings: DataSourceInstanceSettings<MssqlOptions>,
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
private readonly timeSrv: TimeSrv = getTimeSrv()
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super(instanceSettings);
this.name = instanceSettings.name;
@ -123,7 +121,7 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
refId = optionalOptions.variable.name;
}
const range = this.timeSrv.timeRange();
const range = optionalOptions?.range as TimeRange;
const interpolatedQuery = {
refId: refId,
@ -138,8 +136,8 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
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,

View File

@ -6,7 +6,6 @@ import { TemplateSrv } from 'app/features/templating/template_srv';
import { backendSrv } from 'app/core/services/backend_srv';
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
import { createFetchResponse } from 'test/helpers/createFetchResponse';
import { TimeSrvStub } from 'test/specs/helpers';
jest.mock('@grafana/runtime', () => ({
...(jest.requireActual('@grafana/runtime') as unknown as object),
@ -17,15 +16,13 @@ describe('MSSQLDatasource', () => {
const templateSrv: TemplateSrv = new TemplateSrv();
const fetchMock = jest.spyOn(backendSrv, 'fetch');
const ctx: any = {
timeSrv: new TimeSrvStub(),
};
const ctx: any = {};
beforeEach(() => {
jest.clearAllMocks();
ctx.instanceSettings = { name: 'mssql' };
ctx.ds = new MssqlDatasource(ctx.instanceSettings, templateSrv, ctx.timeSrv);
ctx.ds = new MssqlDatasource(ctx.instanceSettings, templateSrv);
});
describe('When performing annotationQuery', () => {
@ -255,7 +252,6 @@ describe('MSSQLDatasource', () => {
};
beforeEach(() => {
ctx.timeSrv.setTime(time);
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
return ctx.ds.metricFindQuery(query, { range: time });

View File

@ -9,7 +9,6 @@ import ResponseParser from './response_parser';
import { MySQLOptions, MySQLQuery, MysqlQueryForInterpolation } from './types';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOptions> {
@ -21,8 +20,7 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
constructor(
instanceSettings: DataSourceInstanceSettings<MySQLOptions>,
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
private readonly timeSrv: TimeSrv = getTimeSrv()
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super(instanceSettings);
this.name = instanceSettings.name;
@ -140,7 +138,7 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
format: 'table',
};
const range = this.timeSrv.timeRange();
const range = optionalOptions?.range;
return lastValueFrom(
getBackendSrv()
@ -148,8 +146,8 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
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,

View File

@ -5,7 +5,6 @@ import {
DataSourceInstanceSettings,
dateTime,
MutableDataFrame,
toUtc,
} from '@grafana/data';
import { MysqlDatasource } from '../datasource';
@ -27,20 +26,9 @@ describe('MySQLDatasource', () => {
} as unknown as DataSourceInstanceSettings<MySQLOptions>;
const templateSrv: TemplateSrv = new TemplateSrv();
const variable = { ...initialCustomVariableModelState };
const raw = {
from: toUtc('2018-04-25 10:00'),
to: toUtc('2018-04-25 11:00'),
};
const timeSrvMock: any = {
timeRange: () => ({
from: raw.from,
to: raw.to,
raw: raw,
}),
};
fetchMock.mockImplementation((options) => of(createFetchResponse(response)));
const ds = new MysqlDatasource(instanceSettings, templateSrv, timeSrvMock);
const ds = new MysqlDatasource(instanceSettings, templateSrv);
return { ds, variable, templateSrv, fetchMock };
};

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