Migrate dataSourceSrv to getDataSourceSrv
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { getDataSourceSrv } from '@grafana/runtime';
|
||||
import { migrateDSConfig } from './migrations';
|
||||
|
||||
const SUPPORTED_SQL_DS = ['mysql', 'postgres', 'influxdb'];
|
||||
@@ -23,9 +24,7 @@ const defaultConfig = {
|
||||
export class ZabbixDSConfigController {
|
||||
|
||||
/** @ngInject */
|
||||
constructor($scope, $injector, datasourceSrv) {
|
||||
this.datasourceSrv = datasourceSrv;
|
||||
|
||||
constructor() {
|
||||
this.current.jsonData = migrateDSConfig(this.current.jsonData);
|
||||
_.defaults(this.current.jsonData, defaultConfig);
|
||||
|
||||
@@ -39,7 +38,7 @@ export class ZabbixDSConfigController {
|
||||
}
|
||||
|
||||
getSupportedDBDataSources() {
|
||||
let datasources = this.datasourceSrv.getAll();
|
||||
let datasources = getDataSourceSrv().getAll();
|
||||
return _.filter(datasources, ds => {
|
||||
return _.includes(SUPPORTED_SQL_DS, ds.type);
|
||||
});
|
||||
@@ -53,7 +52,7 @@ export class ZabbixDSConfigController {
|
||||
|
||||
loadCurrentDBDatasource() {
|
||||
const dsName= this.current.jsonData.dbConnectionDatasourceName;
|
||||
this.datasourceSrv.loadDatasource(dsName)
|
||||
getDataSourceSrv().loadDatasource(dsName)
|
||||
.then(ds => {
|
||||
if (ds) {
|
||||
this.dbConnectionDatasourceId = ds.id;
|
||||
@@ -66,7 +65,7 @@ export class ZabbixDSConfigController {
|
||||
return;
|
||||
}
|
||||
|
||||
this.datasourceSrv.loadDatasource(this.current.name)
|
||||
getDataSourceSrv().loadDatasource(this.current.name)
|
||||
.then(ds => {
|
||||
return ds.getVersion();
|
||||
})
|
||||
|
||||
@@ -16,7 +16,7 @@ const DEFAULT_ZABBIX_VERSION = 3;
|
||||
export class ZabbixDatasource {
|
||||
|
||||
/** @ngInject */
|
||||
constructor(instanceSettings, templateSrv, datasourceSrv, zabbixAlertingSrv) {
|
||||
constructor(instanceSettings, templateSrv, zabbixAlertingSrv) {
|
||||
this.templateSrv = templateSrv;
|
||||
this.zabbixAlertingSrv = zabbixAlertingSrv;
|
||||
|
||||
@@ -75,7 +75,7 @@ export class ZabbixDatasource {
|
||||
dbConnectionRetentionPolicy: this.dbConnectionRetentionPolicy,
|
||||
};
|
||||
|
||||
this.zabbix = new Zabbix(zabbixOptions, datasourceSrv);
|
||||
this.zabbix = new Zabbix(zabbixOptions);
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import mocks from '../../test-setup/mocks';
|
||||
import { DBConnector } from '../zabbix/connectors/dbConnector';
|
||||
|
||||
const loadDatasourceMock = jest.fn().mockResolvedValue({ id: 42, name: 'foo', meta: {} });
|
||||
const getAllMock = jest.fn().mockReturnValue([{ id: 42, name: 'foo', meta: {} }]);
|
||||
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
getDataSourceSrv: () => ({
|
||||
loadDatasource: loadDatasourceMock,
|
||||
getAll: getAllMock
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('DBConnector', () => {
|
||||
let ctx = {};
|
||||
const datasourceSrv = mocks.datasourceSrvMock;
|
||||
datasourceSrv.loadDatasource.mockResolvedValue({ id: 42, name: 'foo', meta: {} });
|
||||
datasourceSrv.getAll.mockReturnValue([{ id: 42, name: 'foo' }]);
|
||||
const ctx: any = {};
|
||||
|
||||
describe('When init DB connector', () => {
|
||||
beforeEach(() => {
|
||||
@@ -13,34 +19,34 @@ describe('DBConnector', () => {
|
||||
datasourceId: 42,
|
||||
datasourceName: undefined
|
||||
};
|
||||
|
||||
loadDatasourceMock.mockClear();
|
||||
getAllMock.mockClear();
|
||||
});
|
||||
|
||||
it('should try to load datasource by name first', () => {
|
||||
ctx.options = {
|
||||
datasourceName: 'bar'
|
||||
};
|
||||
const dbConnector = new DBConnector(ctx.options, datasourceSrv);
|
||||
const dbConnector = new DBConnector({ datasourceName: 'bar' });
|
||||
dbConnector.loadDBDataSource();
|
||||
expect(datasourceSrv.getAll).not.toHaveBeenCalled();
|
||||
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('bar');
|
||||
expect(getAllMock).not.toHaveBeenCalled();
|
||||
expect(loadDatasourceMock).toHaveBeenCalledWith('bar');
|
||||
});
|
||||
|
||||
it('should load datasource by id if name not present', () => {
|
||||
const dbConnector = new DBConnector(ctx.options, datasourceSrv);
|
||||
const dbConnector = new DBConnector({ datasourceId: 42 });
|
||||
dbConnector.loadDBDataSource();
|
||||
expect(datasourceSrv.getAll).toHaveBeenCalled();
|
||||
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('foo');
|
||||
expect(getAllMock).toHaveBeenCalled();
|
||||
expect(loadDatasourceMock).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
it('should throw error if no name and id specified', () => {
|
||||
ctx.options = {};
|
||||
const dbConnector = new DBConnector(ctx.options, datasourceSrv);
|
||||
const dbConnector = new DBConnector(ctx.options);
|
||||
return expect(dbConnector.loadDBDataSource()).rejects.toBe('Data Source name should be specified');
|
||||
});
|
||||
|
||||
it('should throw error if datasource with given id is not found', () => {
|
||||
ctx.options.datasourceId = 45;
|
||||
const dbConnector = new DBConnector(ctx.options, datasourceSrv);
|
||||
const dbConnector = new DBConnector(ctx.options);
|
||||
return expect(dbConnector.loadDBDataSource()).rejects.toBe('Data Source with ID 45 not found');
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,20 @@
|
||||
import { InfluxDBConnector } from '../zabbix/connectors/influxdb/influxdbConnector';
|
||||
import { compactQuery } from '../utils';
|
||||
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
getDataSourceSrv: jest.fn(() => ({
|
||||
loadDatasource: jest.fn().mockResolvedValue(
|
||||
{ id: 42, name: 'InfluxDB DS', meta: {} }
|
||||
),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('InfluxDBConnector', () => {
|
||||
let ctx = {};
|
||||
|
||||
beforeEach(() => {
|
||||
ctx.options = { datasourceName: 'InfluxDB DS', retentionPolicy: 'longterm' };
|
||||
ctx.datasourceSrvMock = {
|
||||
loadDatasource: jest.fn().mockResolvedValue(
|
||||
{ id: 42, name: 'InfluxDB DS', meta: {} }
|
||||
),
|
||||
};
|
||||
ctx.influxDBConnector = new InfluxDBConnector(ctx.options, ctx.datasourceSrvMock);
|
||||
ctx.influxDBConnector = new InfluxDBConnector(ctx.options);
|
||||
ctx.influxDBConnector.invokeInfluxDBQuery = jest.fn().mockResolvedValue([]);
|
||||
ctx.defaultQueryParams = {
|
||||
itemids: ['123', '234'],
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { getDataSourceSrv } from '@grafana/runtime';
|
||||
|
||||
export const DEFAULT_QUERY_LIMIT = 10000;
|
||||
export const HISTORY_TO_TABLE_MAP = {
|
||||
@@ -34,31 +35,30 @@ export const consolidateByTrendColumns = {
|
||||
* `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API.
|
||||
*/
|
||||
export class DBConnector {
|
||||
constructor(options, datasourceSrv) {
|
||||
this.datasourceSrv = datasourceSrv;
|
||||
constructor(options) {
|
||||
this.datasourceId = options.datasourceId;
|
||||
this.datasourceName = options.datasourceName;
|
||||
this.datasourceTypeId = null;
|
||||
this.datasourceTypeName = null;
|
||||
}
|
||||
|
||||
static loadDatasource(dsId, dsName, datasourceSrv) {
|
||||
static loadDatasource(dsId, dsName) {
|
||||
if (!dsName && dsId !== undefined) {
|
||||
let ds = _.find(datasourceSrv.getAll(), {'id': dsId});
|
||||
let ds = _.find(getDataSourceSrv().getAll(), {'id': dsId});
|
||||
if (!ds) {
|
||||
return Promise.reject(`Data Source with ID ${dsId} not found`);
|
||||
}
|
||||
dsName = ds.name;
|
||||
}
|
||||
if (dsName) {
|
||||
return datasourceSrv.loadDatasource(dsName);
|
||||
return getDataSourceSrv().loadDatasource(dsName);
|
||||
} else {
|
||||
return Promise.reject(`Data Source name should be specified`);
|
||||
}
|
||||
}
|
||||
|
||||
loadDBDataSource() {
|
||||
return DBConnector.loadDatasource(this.datasourceId, this.datasourceName, this.datasourceSrv)
|
||||
return DBConnector.loadDatasource(this.datasourceId, this.datasourceName)
|
||||
.then(ds => {
|
||||
this.datasourceTypeId = ds.meta.id;
|
||||
this.datasourceTypeName = ds.meta.name;
|
||||
|
||||
@@ -11,8 +11,8 @@ const consolidateByFunc = {
|
||||
};
|
||||
|
||||
export class InfluxDBConnector extends DBConnector {
|
||||
constructor(options, datasourceSrv) {
|
||||
super(options, datasourceSrv);
|
||||
constructor(options) {
|
||||
super(options);
|
||||
this.retentionPolicy = options.retentionPolicy;
|
||||
super.loadDBDataSource().then(ds => {
|
||||
this.influxDS = ds;
|
||||
|
||||
@@ -11,8 +11,8 @@ const supportedDatabases = {
|
||||
};
|
||||
|
||||
export class SQLConnector extends DBConnector {
|
||||
constructor(options, datasourceSrv) {
|
||||
super(options, datasourceSrv);
|
||||
constructor(options) {
|
||||
super(options);
|
||||
|
||||
this.limit = options.limit || DEFAULT_QUERY_LIMIT;
|
||||
this.sqlDialect = null;
|
||||
|
||||
@@ -25,7 +25,7 @@ const REQUESTS_TO_BIND = [
|
||||
];
|
||||
|
||||
export class Zabbix {
|
||||
constructor(options, datasourceSrv) {
|
||||
constructor(options) {
|
||||
let {
|
||||
url,
|
||||
username,
|
||||
@@ -57,7 +57,7 @@ export class Zabbix {
|
||||
|
||||
if (enableDirectDBConnection) {
|
||||
const connectorOptions = { dbConnectionRetentionPolicy };
|
||||
this.initDBConnector(dbConnectionDatasourceId, dbConnectionDatasourceName, datasourceSrv, connectorOptions)
|
||||
this.initDBConnector(dbConnectionDatasourceId, dbConnectionDatasourceName, connectorOptions)
|
||||
.then(() => {
|
||||
this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector);
|
||||
this.getTrendsDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getTrends, 'getTrends', this.dbConnector);
|
||||
@@ -65,15 +65,15 @@ export class Zabbix {
|
||||
}
|
||||
}
|
||||
|
||||
initDBConnector(datasourceId, datasourceName, datasourceSrv, options) {
|
||||
return DBConnector.loadDatasource(datasourceId, datasourceName, datasourceSrv)
|
||||
initDBConnector(datasourceId, datasourceName, options) {
|
||||
return DBConnector.loadDatasource(datasourceId, datasourceName)
|
||||
.then(ds => {
|
||||
let connectorOptions = { datasourceId, datasourceName };
|
||||
if (ds.type === 'influxdb') {
|
||||
connectorOptions.retentionPolicy = options.dbConnectionRetentionPolicy;
|
||||
this.dbConnector = new InfluxDBConnector(connectorOptions, datasourceSrv);
|
||||
this.dbConnector = new InfluxDBConnector(connectorOptions);
|
||||
} else {
|
||||
this.dbConnector = new SQLConnector(connectorOptions, datasourceSrv);
|
||||
this.dbConnector = new SQLConnector(connectorOptions);
|
||||
}
|
||||
return this.dbConnector;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user