/usr/share/grafana/public/app/plugins/datasource/loki
import { firstValueFrom } from 'rxjs'; import { dateTime, getDefaultTimeRange } from '@grafana/data'; import { LokiVariableSupport } from './LokiVariableSupport'; import { LokiDatasource } from './datasource'; import { createLokiDatasource } from './mocks/datasource'; import { createMetadataRequest } from './mocks/metadataRequest'; import { LokiVariableQueryType } from './types'; describe('LokiVariableSupport', () => { let lokiVariableSupport: LokiVariableSupport; let datasource: LokiDatasource; beforeEach(() => { datasource = createLokiDatasource(); jest .spyOn(datasource, 'metadataRequest') .mockImplementation( createMetadataRequest( { label1: ['value1', 'value2'], label2: ['value3', 'value4'] }, { '{label1="value1", label2="value2"}': [{ label5: 'value5' }] } ) ); lokiVariableSupport = new LokiVariableSupport(datasource); }); it('should return label names for Loki', async () => { // label_names() const response = await lokiVariableSupport.execute( { refId: 'test', type: LokiVariableQueryType.LabelNames }, {}, getDefaultTimeRange() ); expect(response).toEqual([{ text: 'label1' }, { text: 'label2' }]); }); it('should return label values for Loki when no matcher', async () => { // label_values(label1) const response = await lokiVariableSupport.execute( { refId: 'test', type: LokiVariableQueryType.LabelValues, label: 'label1', }, {}, getDefaultTimeRange() ); expect(response).toEqual([{ text: 'value1' }, { text: 'value2' }]); }); it('should return label values for Loki with matcher', async () => { // label_values({label1="value1", label2="value2"},label5) const response = await lokiVariableSupport.execute( { refId: 'test', type: LokiVariableQueryType.LabelValues, stream: '{label1="value1", label2="value2"}', label: 'label5', }, {}, getDefaultTimeRange() ); expect(response).toEqual([{ text: 'value5' }]); }); it('should call `metricFindQuery` with the correct parameters', async () => { // label_values({label1="value1", label2="value2"},label5) const spy = jest.spyOn(datasource, 'metricFindQuery'); const range = getDefaultTimeRange(); const scopedVars = { foo: { value: 'bar' } }; range.from = dateTime(new Date('2020-01-01T00:00:00Z')); range.to = dateTime(new Date('2020-01-01T01:00:00Z')); await firstValueFrom( lokiVariableSupport.query({ targets: [ { refId: 'test', type: LokiVariableQueryType.LabelValues, stream: '{label1="value1", label2="value2"}', label: 'label5', }, ], range, scopedVars, requestId: 'test', interval: '1m', intervalMs: 60000, timezone: 'utc', app: 'explore', startTime: 0, }) ); expect(spy).toHaveBeenCalledWith( { refId: 'test', type: LokiVariableQueryType.LabelValues, stream: '{label1="value1", label2="value2"}', label: 'label5', }, { range, scopedVars, } ); spy.mockRestore(); }); });
.
Edit
..
Edit
CHANGELOG.md
Edit
LanguageProvider.test.ts
Edit
LanguageProvider.ts
Edit
LiveStreams.test.ts
Edit
LiveStreams.ts
Edit
LogContextProvider.test.ts
Edit
LogContextProvider.ts
Edit
LokiVariableSupport.test.ts
Edit
LokiVariableSupport.ts
Edit
README.md
Edit
backendResultTransformer.test.ts
Edit
backendResultTransformer.ts
Edit
components
Edit
configuration
Edit
dataquery.cue
Edit
dataquery.gen.ts
Edit
datasource.test.ts
Edit
datasource.ts
Edit
dist
Edit
docs
Edit
getDerivedFields.test.ts
Edit
getDerivedFields.ts
Edit
img
Edit
jest-setup.js
Edit
jest.config.js
Edit
languageUtils.test.ts
Edit
languageUtils.ts
Edit
language_utils.test.ts
Edit
lineParser.test.ts
Edit
lineParser.ts
Edit
liveStreamsResultTransformer.test.ts
Edit
liveStreamsResultTransformer.ts
Edit
logsTimeSplitting.test.ts
Edit
logsTimeSplitting.ts
Edit
makeTableFrames.test.ts
Edit
makeTableFrames.ts
Edit
mergeResponses.test.ts
Edit
mergeResponses.ts
Edit
metricTimeSplitting.test.ts
Edit
metricTimeSplitting.ts
Edit
migrations
Edit
mocks
Edit
modifyQuery.test.ts
Edit
modifyQuery.ts
Edit
module.test.ts
Edit
module.ts
Edit
package.json
Edit
plugin.json
Edit
project.json
Edit
queryHints.test.ts
Edit
queryHints.ts
Edit
querySplitting.test.ts
Edit
querySplitting.ts
Edit
queryUtils.test.ts
Edit
queryUtils.ts
Edit
querybuilder
Edit
responseUtils.test.ts
Edit
responseUtils.ts
Edit
shardQuerySplitting.test.ts
Edit
shardQuerySplitting.ts
Edit
sortDataFrame.test.ts
Edit
sortDataFrame.ts
Edit
streaming.test.ts
Edit
streaming.ts
Edit
syntax.test.ts
Edit
syntax.ts
Edit
tracking.test.ts
Edit
tracking.ts
Edit
tsconfig.json
Edit
types.ts
Edit
webpack.config.ts
Edit