mirror of https://github.com/grafana/grafana
Loki: Use label/<name>/values API instead of series API for label values discovery (#83044)
* Loki: Add label values API selector to setting and use label_values API when selected * remove CHANGELOG change * add docs * Support Loki 2.7+ only, so unconditionally use /labels API * Correct doc for fetchLabels and fetchLabelValues functions * Fixes after merge * Do not encode query parameter twice * return getLabelKeys in Completion Provider * docs * Add test for LabelParamEditor * Update public/app/plugins/datasource/loki/LogContextProvider.test.ts Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * toHaveBeenCalledWith --------- Co-authored-by: Matias Chomicki <matyax@gmail.com> Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>pull/87336/head
parent
9c254c7e1e
commit
33170c4d07
@ -0,0 +1,71 @@ |
||||
import { screen, render } from '@testing-library/react'; |
||||
import userEvent from '@testing-library/user-event'; |
||||
import React, { ComponentProps } from 'react'; |
||||
|
||||
import { DataSourceApi } from '@grafana/data'; |
||||
import { QueryBuilderOperation, QueryBuilderOperationParamDef } from '@grafana/experimental'; |
||||
import { config } from '@grafana/runtime'; |
||||
|
||||
import { createLokiDatasource } from '../../__mocks__/datasource'; |
||||
import { LokiDatasource } from '../../datasource'; |
||||
import { LokiQueryModeller } from '../LokiQueryModeller'; |
||||
import { LokiOperationId } from '../types'; |
||||
|
||||
import { LabelParamEditor } from './LabelParamEditor'; |
||||
|
||||
describe('LabelParamEditor', () => { |
||||
const queryHintsFeatureToggle = config.featureToggles.lokiQueryHints; |
||||
beforeAll(() => { |
||||
config.featureToggles.lokiQueryHints = true; |
||||
}); |
||||
afterAll(() => { |
||||
config.featureToggles.lokiQueryHints = queryHintsFeatureToggle; |
||||
}); |
||||
|
||||
it('shows label options', async () => { |
||||
const props = createProps({}, ['label1', 'label2']); |
||||
render(<LabelParamEditor {...props} />); |
||||
const input = screen.getByRole('combobox'); |
||||
await userEvent.click(input); |
||||
expect(screen.getByText('label1')).toBeInTheDocument(); |
||||
expect(screen.getByText('label2')).toBeInTheDocument(); |
||||
}); |
||||
|
||||
it('shows no label options if no samples are returned', async () => { |
||||
const props = createProps(); |
||||
render(<LabelParamEditor {...props} />); |
||||
const input = screen.getByRole('combobox'); |
||||
await userEvent.click(input); |
||||
expect(screen.getByText('No labels found')).toBeInTheDocument(); |
||||
}); |
||||
}); |
||||
|
||||
const createProps = (propsOverrides?: Partial<ComponentProps<typeof LabelParamEditor>>, mockedSample?: string[]) => { |
||||
const propsDefault = { |
||||
value: undefined, |
||||
onChange: jest.fn(), |
||||
onRunQuery: jest.fn(), |
||||
index: 1, |
||||
operationId: '1', |
||||
query: { |
||||
labels: [{ op: '=', label: 'foo', value: 'bar' }], |
||||
operations: [ |
||||
{ id: LokiOperationId.CountOverTime, params: ['5m'] }, |
||||
{ id: '__sum_by', params: ['job'] }, |
||||
], |
||||
}, |
||||
paramDef: {} as QueryBuilderOperationParamDef, |
||||
operation: {} as QueryBuilderOperation, |
||||
datasource: createLokiDatasource() as DataSourceApi, |
||||
queryModeller: { |
||||
renderLabels: jest.fn().mockReturnValue('sum by(job) (count_over_time({foo="bar"} [5m]))'), |
||||
} as unknown as LokiQueryModeller, |
||||
}; |
||||
const props = { ...propsDefault, ...propsOverrides }; |
||||
|
||||
if (props.datasource instanceof LokiDatasource) { |
||||
const resolvedValue = mockedSample ?? []; |
||||
props.datasource.languageProvider.fetchLabels = jest.fn().mockResolvedValue(resolvedValue); |
||||
} |
||||
return props; |
||||
}; |
Loading…
Reference in new issue