mirror of https://github.com/grafana/grafana
InfluxDB: Template variable support for SQL language (#77799)
* Only run through with classicQuery if the language is influxql and backend migration is disabled * Add variable editor * Simple template variable support * Show template variables in the drowdowns * better imports * unit tests * it is now 11 just because we add rawSql interpolation in datasource.ts applyVariables method * fixpull/77873/head
parent
a46ff31c4f
commit
3cb92e3460
@ -0,0 +1,44 @@ |
||||
import { lastValueFrom } from 'rxjs'; |
||||
|
||||
import config from 'app/core/config'; |
||||
|
||||
import { SQLQuery } from '../../../features/plugins/sql'; |
||||
|
||||
import InfluxDatasource from './datasource'; |
||||
import { |
||||
getMockDSInstanceSettings, |
||||
mockBackendService, |
||||
mockInfluxQueryRequest, |
||||
mockInfluxSQLFetchResponse, |
||||
mockTemplateSrv, |
||||
} from './mocks'; |
||||
import { InfluxVersion } from './types'; |
||||
|
||||
config.featureToggles.influxdbBackendMigration = true; |
||||
mockBackendService(mockInfluxSQLFetchResponse); |
||||
|
||||
describe('InfluxDB SQL Support', () => { |
||||
const replaceMock = jest.fn(); |
||||
const templateSrv = mockTemplateSrv(jest.fn(), replaceMock); |
||||
|
||||
let sqlQuery: SQLQuery; |
||||
|
||||
beforeEach(() => { |
||||
sqlQuery = { |
||||
refId: 'x', |
||||
rawSql: |
||||
'SELECT "$interpolationVar2", time FROM iox.$interpolationVar WHERE time >= $__timeFrom AND time <= $__timeTo', |
||||
}; |
||||
}); |
||||
|
||||
describe('interpolate variables', () => { |
||||
const ds = new InfluxDatasource(getMockDSInstanceSettings({ version: InfluxVersion.SQL }), templateSrv); |
||||
|
||||
it('should call replace template variables for rawSql', async () => { |
||||
await lastValueFrom(ds.query(mockInfluxQueryRequest([sqlQuery]))); |
||||
expect(replaceMock.mock.calls[1][0]).toBe( |
||||
`SELECT "$interpolationVar2", time FROM iox.$interpolationVar WHERE time >= $__timeFrom AND time <= $__timeTo` |
||||
); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,49 @@ |
||||
import { TemplateSrv } from '@grafana/runtime'; |
||||
|
||||
import { getMockDSInstanceSettings, mockBackendService, mockInfluxSQLVariableFetchResponse } from '../mocks'; |
||||
|
||||
import { FlightSQLDatasource } from './datasource.flightsql'; |
||||
|
||||
mockBackendService(mockInfluxSQLVariableFetchResponse); |
||||
describe('flightsql datasource', () => { |
||||
const templateSrv: TemplateSrv = { |
||||
containsTemplate: jest.fn(), |
||||
replace: jest.fn().mockImplementation((val: string) => val), |
||||
updateTimeRange: jest.fn(), |
||||
getVariables: jest.fn().mockReturnValue([ |
||||
{ |
||||
name: 'templateVar', |
||||
text: 'templateVar', |
||||
value: 'templateVar', |
||||
type: '', |
||||
label: 'templateVar', |
||||
}, |
||||
]), |
||||
}; |
||||
const mockInstanceSettings = getMockDSInstanceSettings(); |
||||
const instanceSettings = { |
||||
...mockInstanceSettings, |
||||
jsonData: { |
||||
...mockInstanceSettings.jsonData, |
||||
allowCleartextPasswords: false, |
||||
tlsAuth: false, |
||||
tlsAuthWithCACert: false, |
||||
tlsSkipVerify: false, |
||||
maxIdleConns: 1, |
||||
maxOpenConns: 1, |
||||
maxIdleConnsAuto: true, |
||||
connMaxLifetime: 1, |
||||
timezone: '', |
||||
user: '', |
||||
database: '', |
||||
url: '', |
||||
timeInterval: '', |
||||
}, |
||||
}; |
||||
const ds = new FlightSQLDatasource(instanceSettings, templateSrv); |
||||
|
||||
it('should add template variables to the responses', async () => { |
||||
const fields = await ds.fetchFields({ dataset: 'test', table: 'table' }); |
||||
expect(fields[0].name).toBe('$templateVar'); |
||||
}); |
||||
}); |
Loading…
Reference in new issue