Adding reload to datasourceSrv (#44217)

alerting-new-folder-modal
Timur Olzhabayev 3 years ago committed by GitHub
parent 732c2ebb37
commit c3f69cc4d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      packages/grafana-runtime/src/services/dataSourceSrv.ts
  2. 5
      public/app/features/alerting/getAlertingValidationMessage.test.ts
  3. 2
      public/app/features/alerting/unified/mocks.ts
  4. 1
      public/app/features/dashboard/containers/DashboardPage.test.tsx
  5. 18
      public/app/features/datasources/state/actions.ts
  6. 9
      public/app/features/plugins/datasource_srv.ts
  7. 29
      public/app/features/plugins/tests/datasource_srv.test.ts
  8. 1
      public/app/features/variables/state/initVariableTransaction.test.ts

@ -28,6 +28,11 @@ export interface DataSourceSrv {
ref?: DataSourceRef | string | null,
scopedVars?: ScopedVars
): DataSourceInstanceSettings | undefined;
/**
* Reloads the DataSourceSrv
*/
reload(): void;
}
/** @public */

@ -35,6 +35,7 @@ describe('getAlertingValidationMessage', () => {
return [];
},
getInstanceSettings: (() => {}) as any,
reload: () => jest.fn(),
};
const targets: ElasticsearchQuery[] = [
{ refId: 'A', query: '@hostname:$hostname' },
@ -77,6 +78,7 @@ describe('getAlertingValidationMessage', () => {
getList(): DataSourceInstanceSettings[] {
return [];
},
reload: () => jest.fn(),
};
const targets: any[] = [
{ refId: 'A', query: 'some query', datasource: 'alertingDatasource' },
@ -108,6 +110,7 @@ describe('getAlertingValidationMessage', () => {
getList(): DataSourceInstanceSettings[] {
return [];
},
reload: () => jest.fn(),
};
const targets: ElasticsearchQuery[] = [
{ refId: 'A', query: '@hostname:$hostname' },
@ -142,6 +145,7 @@ describe('getAlertingValidationMessage', () => {
getList(): DataSourceInstanceSettings[] {
return [];
},
reload: () => jest.fn(),
};
const targets: ElasticsearchQuery[] = [
{ refId: 'A', query: '@hostname:hostname' },
@ -175,6 +179,7 @@ describe('getAlertingValidationMessage', () => {
getList(): DataSourceInstanceSettings[] {
return [];
},
reload: () => jest.fn(),
};
const targets: ElasticsearchQuery[] = [
{ refId: 'A', query: '@hostname:hostname' },

@ -287,6 +287,8 @@ export class MockDataSourceSrv implements DataSourceSrv {
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
return DatasourceSrv.prototype.loadDatasource.call(this, name);
}
reload() {}
}
export const mockGrafanaReceiver = (

@ -220,6 +220,7 @@ describe('DashboardPage', () => {
get: jest.fn().mockResolvedValue({}),
getInstanceSettings: jest.fn().mockReturnValue({ meta: {} }),
getList: jest.fn(),
reload: jest.fn(),
});
ctx.setup(() => {

@ -8,8 +8,6 @@ import { importDataSourcePlugin } from 'app/features/plugins/plugin_loader';
import { getPluginSettings } from 'app/features/plugins/pluginSettings';
import { DataSourcePluginCategory, ThunkDispatch, ThunkResult } from 'app/types';
import config from '../../../core/config';
import { buildCategories } from './buildCategories';
import { buildNavModel } from './navModel';
import {
@ -218,7 +216,7 @@ export function addDataSource(plugin: DataSourcePluginMeta): ThunkResult<void> {
}
const result = await getBackendSrv().post('/api/datasources', newInstance);
await updateFrontendSettings();
await getDatasourceSrv().reload();
locationService.push(`/datasources/edit/${result.datasource.uid}`);
};
}
@ -235,7 +233,7 @@ export function loadDataSourcePlugins(): ThunkResult<void> {
export function updateDataSource(dataSource: DataSourceSettings): ThunkResult<void> {
return async (dispatch) => {
await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource); // by UID not yet supported
await updateFrontendSettings();
await getDatasourceSrv().reload();
return dispatch(loadDataSource(dataSource.uid));
};
}
@ -245,7 +243,7 @@ export function deleteDataSource(): ThunkResult<void> {
const dataSource = getStore().dataSources.dataSource;
await getBackendSrv().delete(`/api/datasources/${dataSource.id}`);
await updateFrontendSettings();
await getDatasourceSrv().reload();
locationService.push('/datasources');
};
@ -283,16 +281,6 @@ export function findNewName(dataSources: ItemWithName[], name: string) {
return name;
}
function updateFrontendSettings() {
return getBackendSrv()
.get('/api/frontend/settings')
.then((settings: any) => {
config.datasources = settings.datasources;
config.defaultDatasource = settings.defaultDatasource;
getDatasourceSrv().init(config.datasources, settings.defaultDatasource);
});
}
function nameHasSuffix(name: string) {
return name.endsWith('-', name.length - 1);
}

@ -7,6 +7,7 @@ import {
TemplateSrv,
getTemplateSrv,
getLegacyAngularInjector,
getBackendSrv,
} from '@grafana/runtime';
// Types
import {
@ -26,6 +27,7 @@ import {
import { DataSourceVariableModel } from '../variables/types';
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
import appEvents from 'app/core/app_events';
import config from 'app/core/config';
export class DatasourceSrv implements DataSourceService {
private datasources: Record<string, DataSourceApi> = {}; // UID
@ -324,6 +326,13 @@ export class DatasourceSrv implements DataSourceService {
};
});
}
async reload() {
const settings = await getBackendSrv().get('/api/frontend/settings');
config.datasources = settings.datasources;
config.defaultDatasource = settings.defaultDatasource;
this.init(settings.datasources, settings.defaultDatasource);
}
}
export function variableInterpolation(value: any[]) {

@ -46,7 +46,20 @@ jest.mock('../plugin_loader', () => ({
},
}));
const getBackendSrvGetMock = jest.fn();
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getBackendSrv: () => ({
get: getBackendSrvGetMock,
}),
}));
describe('datasource_srv', () => {
beforeEach(() => {
jest.resetModules();
});
const dataSourceSrv = new DatasourceSrv(templateSrv);
const dataSourceInit = {
mmm: {
@ -294,5 +307,21 @@ describe('datasource_srv', () => {
]
`);
});
it('Should reload the datasource', async () => {
// arrange
getBackendSrvGetMock.mockReturnValueOnce({
datasources: {
...dataSourceInit,
},
defaultDatasource: 'aaa',
});
const initMock = jest.spyOn(dataSourceSrv, 'init').mockImplementation(() => {});
// act
await dataSourceSrv.reload();
// assert
expect(getBackendSrvGetMock).toHaveBeenCalledWith('/api/frontend/settings');
expect(initMock).toHaveBeenCalledWith(dataSourceInit, 'aaa');
});
});
});

@ -47,6 +47,7 @@ function getTestContext(variables?: VariableModel[]) {
get: jest.fn().mockResolvedValue({}),
getList: jest.fn().mockReturnValue([]),
getInstanceSettings: getInstanceSettingsMock,
reload: jest.fn(),
});
const variableQueryRunner: any = {
cancelRequest: jest.fn(),

Loading…
Cancel
Save