Commit fcad439c by kay delaney Committed by GitHub

Chore: Remove angular dependency from prometheus datasource (#20536)

* Chore: Remove angular dependency from prometheus datasource
parent 37051cd8
const backendSrv = {
/**
* Creates a pretty bogus prom response. Definitelly needs more work but right now we do not test the contents of the
* messages anyway.
*/
function makePromResponse() {
return {
data: {
data: {
result: [
{
metric: {
__name__: 'test_metric',
},
values: [[1568369640, 1]],
},
],
resultType: 'matrix',
},
},
};
}
export const backendSrv = {
get: jest.fn(),
getDashboard: jest.fn(),
getDashboardByUid: jest.fn(),
getFolderByUid: jest.fn(),
post: jest.fn(),
resolveCancelerIfExists: jest.fn(),
datasourceRequest: jest.fn(() => Promise.resolve(makePromResponse())),
};
export function getBackendSrv() {
return backendSrv;
}
export const getBackendSrv = jest.fn().mockReturnValue(backendSrv);
......@@ -2,46 +2,51 @@ import { PrometheusDatasource } from './datasource';
import { DataSourceInstanceSettings } from '@grafana/data';
import { PromContext, PromOptions } from './types';
import { dateTime, LoadingState } from '@grafana/data';
import { getBackendSrv, backendSrv } from 'app/core/services/__mocks__/backend_srv';
jest.mock('app/core/services/backend_srv');
jest.mock('app/features/dashboard/services/TimeSrv', () => ({
__esModule: true,
getTimeSrv: jest.fn().mockReturnValue({
timeRange(): any {
return {
from: dateTime(),
to: dateTime(),
};
},
}),
}));
jest.mock('app/features/templating/template_srv', () => {
return {
replace: jest.fn(() => null),
getAdhocFilters: jest.fn((): any[] => []),
};
});
beforeEach(() => {
getBackendSrv.mockClear();
for (const method in backendSrv) {
(backendSrv as any)[method].mockClear();
}
});
const defaultInstanceSettings: DataSourceInstanceSettings<PromOptions> = {
url: 'test_prom',
jsonData: {},
} as any;
const backendSrvMock: any = {
datasourceRequest: jest.fn(),
};
const templateSrvMock: any = {
replace(): null {
return null;
},
getAdhocFilters(): any[] {
return [];
},
};
const timeSrvMock: any = {
timeRange(): any {
return {
from: dateTime(),
to: dateTime(),
};
},
};
describe('datasource', () => {
describe('query', () => {
const ds = new PrometheusDatasource(
defaultInstanceSettings,
{} as any,
backendSrvMock,
templateSrvMock,
timeSrvMock
);
let ds: PrometheusDatasource;
beforeEach(() => {
ds = new PrometheusDatasource(defaultInstanceSettings);
});
it('returns empty array when no queries', done => {
expect.assertions(2);
ds.query(makeQuery([])).subscribe({
next(next) {
expect(next.data).toEqual([]);
......@@ -55,7 +60,7 @@ describe('datasource', () => {
it('performs time series queries', done => {
expect.assertions(2);
backendSrvMock.datasourceRequest.mockReturnValueOnce(Promise.resolve(makePromResponse()));
ds.query(makeQuery([{}])).subscribe({
next(next) {
expect(next.data.length).not.toBe(0);
......@@ -69,7 +74,7 @@ describe('datasource', () => {
it('with 2 queries and used from Explore, sends results as they arrive', done => {
expect.assertions(4);
backendSrvMock.datasourceRequest.mockReturnValue(Promise.resolve(makePromResponse()));
const responseStatus = [LoadingState.Loading, LoadingState.Done];
ds.query(makeQuery([{ context: PromContext.Explore }, { context: PromContext.Explore }])).subscribe({
next(next) {
......@@ -84,7 +89,6 @@ describe('datasource', () => {
it('with 2 queries and used from Panel, waits for all to finish until sending Done status', done => {
expect.assertions(2);
backendSrvMock.datasourceRequest.mockReturnValue(Promise.resolve(makePromResponse()));
ds.query(makeQuery([{ context: PromContext.Panel }, { context: PromContext.Panel }])).subscribe({
next(next) {
expect(next.data.length).not.toBe(0);
......@@ -117,25 +121,3 @@ function makeQuery(targets: any[]): any {
interval: '15s',
};
}
/**
* Creates a pretty bogus prom response. Definitelly needs more work but right now we do not test the contents of the
* messages anyway.
*/
function makePromResponse() {
return {
data: {
data: {
result: [
{
metric: {
__name__: 'test_metric',
},
values: [[1568369640, 1]],
},
],
resultType: 'matrix',
},
},
};
}
import _ from 'lodash';
import { TimeRange } from '@grafana/data';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { PrometheusDatasource, PromDataQueryResponse } from './datasource';
import { PromQueryRequest } from './types';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
export default class PrometheusMetricFindQuery {
range: TimeRange;
constructor(private datasource: PrometheusDatasource, private query: string, timeSrv: TimeSrv) {
constructor(private datasource: PrometheusDatasource, private query: string) {
this.datasource = datasource;
this.query = query;
this.range = timeSrv.timeRange();
this.range = getTimeSrv().timeRange();
}
process() {
......@@ -18,7 +18,6 @@ export default class PrometheusMetricFindQuery {
const labelValuesRegex = /^label_values\((?:(.+),\s*)?([a-zA-Z_][a-zA-Z0-9_]*)\)\s*$/;
const metricNamesRegex = /^metrics\((.+)\)\s*$/;
const queryResultRegex = /^query_result\((.+)\)\s*$/;
const labelNamesQuery = this.query.match(labelNamesRegex);
if (labelNamesQuery) {
return this.labelNamesQuery();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment