From 0ffd9a9a3c5c632be811ea36364b1c76269ea184 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Sun, 4 Oct 2020 21:41:12 +0200 Subject: [PATCH] Loki: Run instant query only in Explore (#27974) * Run instant query only in Explore * Replace forEach with for loop --- .../datasource/loki/datasource.test.ts | 58 +++++++++++++++++-- .../app/plugins/datasource/loki/datasource.ts | 13 +++-- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 1b385ae4107..2071a657633 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -1,7 +1,15 @@ import { of, Subject } from 'rxjs'; import { first, last, take } from 'rxjs/operators'; import { omit } from 'lodash'; -import { AnnotationQueryRequest, DataFrame, DataQueryResponse, dateTime, FieldCache, TimeRange } from '@grafana/data'; +import { + AnnotationQueryRequest, + CoreApp, + DataFrame, + DataQueryResponse, + dateTime, + FieldCache, + TimeRange, +} from '@grafana/data'; import { BackendSrvRequest, FetchResponse } from '@grafana/runtime'; import LokiDatasource from './datasource'; @@ -130,7 +138,7 @@ describe('LokiDatasource', () => { observableTester().subscribeAndExpectOnComplete({ observable: ds.query(options).pipe(take(1)), expect: () => { - expect(fetchMock.mock.calls.length).toBe(2); + expect(fetchMock.mock.calls.length).toBe(1); expect(fetchMock.mock.calls[0][0].url).toContain(`limit=${expectedLimit}`); }, done, @@ -171,10 +179,11 @@ describe('LokiDatasource', () => { }); describe('when querying', () => { - it('should run range and instant query', done => { + it('should run range and instant query in Explore', done => { const ds = createLokiDSForTests(); const options = getQueryOptions({ targets: [{ expr: '{job="grafana"}', refId: 'B' }], + app: CoreApp.Explore, }); ds.runInstantQuery = jest.fn(() => of({ data: [] })); @@ -190,10 +199,29 @@ describe('LokiDatasource', () => { }); }); - it('should return series data', done => { + it('should run only range query in Dashboard', done => { + const ds = createLokiDSForTests(); + const options = getQueryOptions({ + targets: [{ expr: '{job="grafana"}', refId: 'B' }], + }); + + ds.runInstantQuery = jest.fn(() => of({ data: [] })); + ds.runRangeQuery = jest.fn(() => of({ data: [] })); + + observableTester().subscribeAndExpectOnComplete({ + observable: ds.query(options), + expect: () => { + expect(ds.runRangeQuery).toBeCalled(); + }, + done, + }); + }); + + it('should return series data for both queries in Explore', done => { const ds = createLokiDSForTests(); const options = getQueryOptions({ targets: [{ expr: '{job="grafana"} |= "foo"', refId: 'B' }], + app: CoreApp.Explore, }); observableTester().subscribeAndExpectOnNext({ @@ -223,6 +251,28 @@ describe('LokiDatasource', () => { fetchStream.next(omit(testResponse, 'data.status')); }); + it('should return series data for range query in Dashboard', done => { + const ds = createLokiDSForTests(); + const options = getQueryOptions({ + targets: [{ expr: '{job="grafana"} |= "foo"', refId: 'B' }], + }); + + observableTester().subscribeAndExpectOnNext({ + observable: ds.query(options).pipe(first()), // first result will come from runRangeQuery + expect: res => { + const dataFrame = res.data[0] as DataFrame; + const fieldCache = new FieldCache(dataFrame); + expect(fieldCache.getFieldByName('line')?.values.get(0)).toBe('hello'); + expect(dataFrame.meta?.limit).toBe(20); + expect(dataFrame.meta?.searchWords).toEqual(['foo']); + }, + done, + }); + + fetchStream.next(testResponse); + fetchStream.next(omit(testResponse, 'data.status')); + }); + it('should return custom error message when Loki returns escaping error', done => { const ds = createLokiDSForTests(); const options = getQueryOptions({ diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 486c7e80d7b..8606f95cb22 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -23,6 +23,7 @@ import { QueryResultMeta, ScopedVars, TimeRange, + CoreApp, } from '@grafana/data'; import { getTemplateSrv, TemplateSrv, BackendSrvRequest, FetchError, getBackendSrv } from '@grafana/runtime'; import { addLabelToQuery } from 'app/plugins/datasource/prometheus/add_label_to_query'; @@ -95,12 +96,12 @@ export class LokiDatasource extends DataSourceApi { expr: this.templateSrv.replace(target.expr, options.scopedVars, this.interpolateQueryExpr), })); - filteredTargets.forEach(target => - subQueries.push( - this.runInstantQuery(target, options, filteredTargets.length), - this.runRangeQuery(target, options, filteredTargets.length) - ) - ); + for (const target of filteredTargets) { + if (options.app === CoreApp.Explore) { + subQueries.push(this.runInstantQuery(target, options, filteredTargets.length)); + } + subQueries.push(this.runRangeQuery(target, options, filteredTargets.length)); + } // No valid targets, return the empty result to save a round trip. if (isEmpty(subQueries)) {