Explore/Loki: Fix defaulting to instant query when switching from Prometheus (#40216)

* Loki: Pass only wanted query props when importing from Prometheus

* Update

* Update public/app/plugins/datasource/loki/language_provider.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Add test

* Fix strict error

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
This commit is contained in:
Ivana Huckova 2021-10-12 13:05:57 +02:00 committed by GitHub
parent b1f56b4863
commit 2f0eccb421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -254,6 +254,18 @@ describe('Query imports', () => {
});
describe('prometheus query imports', () => {
it('always results in range query type', async () => {
const instance = new LanguageProvider(datasource);
const result = await instance.importQueries(
[{ refId: 'bar', expr: '{job="grafana"}', instant: true, range: false } as DataQuery],
{
meta: { id: 'prometheus' },
} as DataSourceApi
);
expect(result).toEqual([{ refId: 'bar', expr: '{job="grafana"}', range: true }]);
expect(result).not.toHaveProperty('instant');
});
it('returns empty query from metric-only query', async () => {
const instance = new LanguageProvider(datasource);
const result = await instance.importPrometheusQuery('foo');

View File

@ -15,6 +15,7 @@ import syntax, { FUNCTIONS, PIPE_PARSERS, PIPE_OPERATORS } from './syntax';
import { LokiQuery } from './types';
import { dateTime, AbsoluteTimeRange, LanguageProvider, HistoryItem, DataQuery, DataSourceApi } from '@grafana/data';
import { PromQuery } from '../prometheus/types';
import { GraphiteQuery } from '../graphite/types';
import LokiDatasource from './datasource';
import { CompletionItem, TypeaheadInput, TypeaheadOutput, CompletionItemGroup } from '@grafana/ui';
@ -334,16 +335,20 @@ export default class LokiLanguageProvider extends LanguageProvider {
return { context, suggestions };
}
async importQueries(queries: DataQuery[], originDataSource: DataSourceApi): Promise<LokiQuery[]> {
async importQueries(
queries: PromQuery[] | GraphiteQuery[] | DataQuery[],
originDataSource: DataSourceApi
): Promise<LokiQuery[]> {
const datasourceType = originDataSource.meta.id;
if (datasourceType === 'prometheus') {
return Promise.all(
queries.map(async (query) => {
const expr = await this.importPrometheusQuery((query as PromQuery).expr);
const { ...rest } = query as PromQuery;
[...(queries as PromQuery[])].map(async (query) => {
const expr = await this.importPrometheusQuery(query.expr);
const { refId } = query;
return {
...rest,
expr,
refId,
range: true,
};
})
);