mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Postgres/MySQL/MSSQL: Remove usage of legacy timeSrv (#45241)
This commit is contained in:
parent
fed112ef3b
commit
0e8a5407d1
@ -2,12 +2,11 @@ import { map as _map } from 'lodash';
|
|||||||
import { lastValueFrom, of } from 'rxjs';
|
import { lastValueFrom, of } from 'rxjs';
|
||||||
import { catchError, map, mapTo } from 'rxjs/operators';
|
import { catchError, map, mapTo } from 'rxjs/operators';
|
||||||
import { BackendDataSourceResponse, DataSourceWithBackend, FetchResponse, getBackendSrv } from '@grafana/runtime';
|
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 ResponseParser from './response_parser';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { MssqlOptions, MssqlQuery, MssqlQueryForInterpolation } from './types';
|
import { MssqlOptions, MssqlQuery, MssqlQueryForInterpolation } from './types';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
||||||
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
|
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
|
||||||
|
|
||||||
export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOptions> {
|
export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOptions> {
|
||||||
@ -18,8 +17,7 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: DataSourceInstanceSettings<MssqlOptions>,
|
instanceSettings: DataSourceInstanceSettings<MssqlOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv()
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
|
||||||
) {
|
) {
|
||||||
super(instanceSettings);
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@ -123,7 +121,7 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
|
|||||||
refId = optionalOptions.variable.name;
|
refId = optionalOptions.variable.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
const range = optionalOptions?.range as TimeRange;
|
||||||
|
|
||||||
const interpolatedQuery = {
|
const interpolatedQuery = {
|
||||||
refId: refId,
|
refId: refId,
|
||||||
@ -138,8 +136,8 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
|
|||||||
url: '/api/ds/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: range.from.valueOf().toString(),
|
from: range?.from?.valueOf()?.toString(),
|
||||||
to: range.to.valueOf().toString(),
|
to: range?.to?.valueOf()?.toString(),
|
||||||
queries: [interpolatedQuery],
|
queries: [interpolatedQuery],
|
||||||
},
|
},
|
||||||
requestId: refId,
|
requestId: refId,
|
||||||
|
@ -6,7 +6,6 @@ import { TemplateSrv } from 'app/features/templating/template_srv';
|
|||||||
import { backendSrv } from 'app/core/services/backend_srv';
|
import { backendSrv } from 'app/core/services/backend_srv';
|
||||||
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
||||||
import { createFetchResponse } from 'test/helpers/createFetchResponse';
|
import { createFetchResponse } from 'test/helpers/createFetchResponse';
|
||||||
import { TimeSrvStub } from 'test/specs/helpers';
|
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
...(jest.requireActual('@grafana/runtime') as unknown as object),
|
...(jest.requireActual('@grafana/runtime') as unknown as object),
|
||||||
@ -17,15 +16,13 @@ describe('MSSQLDatasource', () => {
|
|||||||
const templateSrv: TemplateSrv = new TemplateSrv();
|
const templateSrv: TemplateSrv = new TemplateSrv();
|
||||||
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
||||||
|
|
||||||
const ctx: any = {
|
const ctx: any = {};
|
||||||
timeSrv: new TimeSrvStub(),
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
|
|
||||||
ctx.instanceSettings = { name: 'mssql' };
|
ctx.instanceSettings = { name: 'mssql' };
|
||||||
ctx.ds = new MssqlDatasource(ctx.instanceSettings, templateSrv, ctx.timeSrv);
|
ctx.ds = new MssqlDatasource(ctx.instanceSettings, templateSrv);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing annotationQuery', () => {
|
describe('When performing annotationQuery', () => {
|
||||||
@ -255,7 +252,6 @@ describe('MSSQLDatasource', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.timeSrv.setTime(time);
|
|
||||||
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
||||||
|
|
||||||
return ctx.ds.metricFindQuery(query, { range: time });
|
return ctx.ds.metricFindQuery(query, { range: time });
|
||||||
|
@ -9,7 +9,6 @@ import ResponseParser from './response_parser';
|
|||||||
import { MySQLOptions, MySQLQuery, MysqlQueryForInterpolation } from './types';
|
import { MySQLOptions, MySQLQuery, MysqlQueryForInterpolation } from './types';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
||||||
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
|
import { toTestingStatus } from '@grafana/runtime/src/utils/queryResponse';
|
||||||
|
|
||||||
export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOptions> {
|
export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOptions> {
|
||||||
@ -21,8 +20,7 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: DataSourceInstanceSettings<MySQLOptions>,
|
instanceSettings: DataSourceInstanceSettings<MySQLOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv()
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
|
||||||
) {
|
) {
|
||||||
super(instanceSettings);
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@ -140,7 +138,7 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
|
|||||||
format: 'table',
|
format: 'table',
|
||||||
};
|
};
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
const range = optionalOptions?.range;
|
||||||
|
|
||||||
return lastValueFrom(
|
return lastValueFrom(
|
||||||
getBackendSrv()
|
getBackendSrv()
|
||||||
@ -148,8 +146,8 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
|
|||||||
url: '/api/ds/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: range.from.valueOf().toString(),
|
from: range?.from?.valueOf()?.toString(),
|
||||||
to: range.to.valueOf().toString(),
|
to: range?.to?.valueOf()?.toString(),
|
||||||
queries: [interpolatedQuery],
|
queries: [interpolatedQuery],
|
||||||
},
|
},
|
||||||
requestId: refId,
|
requestId: refId,
|
||||||
|
@ -5,7 +5,6 @@ import {
|
|||||||
DataSourceInstanceSettings,
|
DataSourceInstanceSettings,
|
||||||
dateTime,
|
dateTime,
|
||||||
MutableDataFrame,
|
MutableDataFrame,
|
||||||
toUtc,
|
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
|
|
||||||
import { MysqlDatasource } from '../datasource';
|
import { MysqlDatasource } from '../datasource';
|
||||||
@ -27,20 +26,9 @@ describe('MySQLDatasource', () => {
|
|||||||
} as unknown as DataSourceInstanceSettings<MySQLOptions>;
|
} as unknown as DataSourceInstanceSettings<MySQLOptions>;
|
||||||
const templateSrv: TemplateSrv = new TemplateSrv();
|
const templateSrv: TemplateSrv = new TemplateSrv();
|
||||||
const variable = { ...initialCustomVariableModelState };
|
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)));
|
fetchMock.mockImplementation((options) => of(createFetchResponse(response)));
|
||||||
|
|
||||||
const ds = new MysqlDatasource(instanceSettings, templateSrv, timeSrvMock);
|
const ds = new MysqlDatasource(instanceSettings, templateSrv);
|
||||||
|
|
||||||
return { ds, variable, templateSrv, fetchMock };
|
return { ds, variable, templateSrv, fetchMock };
|
||||||
};
|
};
|
||||||
|
@ -2,12 +2,11 @@ import { map as _map } from 'lodash';
|
|||||||
import { lastValueFrom, of } from 'rxjs';
|
import { lastValueFrom, of } from 'rxjs';
|
||||||
import { map, catchError } from 'rxjs/operators';
|
import { map, catchError } from 'rxjs/operators';
|
||||||
import { BackendDataSourceResponse, DataSourceWithBackend, FetchResponse, getBackendSrv } from '@grafana/runtime';
|
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 ResponseParser from './response_parser';
|
||||||
import PostgresQueryModel from 'app/plugins/datasource/postgres/postgres_query_model';
|
import PostgresQueryModel from 'app/plugins/datasource/postgres/postgres_query_model';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
||||||
//Types
|
//Types
|
||||||
import { PostgresOptions, PostgresQuery, PostgresQueryForInterpolation } from './types';
|
import { PostgresOptions, PostgresQuery, PostgresQueryForInterpolation } from './types';
|
||||||
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
||||||
@ -23,8 +22,7 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: DataSourceInstanceSettings<PostgresOptions>,
|
instanceSettings: DataSourceInstanceSettings<PostgresOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv()
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
|
||||||
) {
|
) {
|
||||||
super(instanceSettings);
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@ -142,7 +140,7 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
format: 'table',
|
format: 'table',
|
||||||
};
|
};
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
const range = optionalOptions?.range as TimeRange;
|
||||||
|
|
||||||
return lastValueFrom(
|
return lastValueFrom(
|
||||||
getBackendSrv()
|
getBackendSrv()
|
||||||
@ -150,8 +148,8 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
url: '/api/ds/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: range.from.valueOf().toString(),
|
from: range?.from?.valueOf()?.toString(),
|
||||||
to: range.to.valueOf().toString(),
|
to: range?.to?.valueOf()?.toString(),
|
||||||
queries: [interpolatedQuery],
|
queries: [interpolatedQuery],
|
||||||
},
|
},
|
||||||
requestId: refId,
|
requestId: refId,
|
||||||
@ -169,7 +167,6 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
|
|
||||||
private _metaRequest(rawSql: string) {
|
private _metaRequest(rawSql: string) {
|
||||||
const refId = 'meta';
|
const refId = 'meta';
|
||||||
const range = this.timeSrv.timeRange();
|
|
||||||
const query = {
|
const query = {
|
||||||
refId: refId,
|
refId: refId,
|
||||||
datasource: this.getRef(),
|
datasource: this.getRef(),
|
||||||
@ -180,8 +177,6 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
url: '/api/ds/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: range.from.valueOf().toString(),
|
|
||||||
to: range.to.valueOf().toString(),
|
|
||||||
queries: [query],
|
queries: [query],
|
||||||
},
|
},
|
||||||
requestId: refId,
|
requestId: refId,
|
||||||
|
@ -7,14 +7,12 @@ import {
|
|||||||
DataSourceInstanceSettings,
|
DataSourceInstanceSettings,
|
||||||
dateTime,
|
dateTime,
|
||||||
MutableDataFrame,
|
MutableDataFrame,
|
||||||
toUtc,
|
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
|
|
||||||
import { PostgresDatasource } from '../datasource';
|
import { PostgresDatasource } from '../datasource';
|
||||||
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
||||||
import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv';
|
|
||||||
import { PostgresOptions, PostgresQuery } from '../types';
|
import { PostgresOptions, PostgresQuery } from '../types';
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
@ -43,21 +41,10 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
},
|
},
|
||||||
} as unknown as DataSourceInstanceSettings<PostgresOptions>;
|
} as unknown as DataSourceInstanceSettings<PostgresOptions>;
|
||||||
const templateSrv: TemplateSrv = new TemplateSrv();
|
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 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
|
// https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing
|
||||||
|
Loading…
Reference in New Issue
Block a user