mirror of https://github.com/grafana/grafana
Loki: Improve the display of loki query stats (#63623)
* fix: refresh query stats on timerange change * partial fix: request stats update on type in code mode * complete fix: request stats update on type in code mode * fix: update failing tests and props * refactor: pass only essential query string to getQueryStats * refactor: remove unused variables * test: fix datasource.getTimeRange is not a function * refactor: use lodash debounce instead of setTimeout * refactor: add suggestions from code review * test: shouldUpdateStats and makeStatsRequest * refactor: use more appropriate variable names * refactor: make setQueryStats required instead of optional * refactor: move setQueryStats into LokiQueryEditor * fix: add missing props to LokiQueryField * revert changes * refactor: use inversion of control to request stats * refactor: remove unnecessary codepull/64932/head
parent
8f0a9729f0
commit
c16280a4e8
@ -0,0 +1,59 @@ |
|||||||
|
import { TimeRange } from '@grafana/data'; |
||||||
|
|
||||||
|
import { createLokiDatasource } from '../mocks'; |
||||||
|
|
||||||
|
import { getStats, shouldUpdateStats } from './stats'; |
||||||
|
|
||||||
|
describe('shouldUpdateStats', () => { |
||||||
|
it('should return true if the query has changed', () => { |
||||||
|
const query = '{job="grafana"}'; |
||||||
|
const prevQuery = '{job="not-grafana"}'; |
||||||
|
const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; |
||||||
|
const prevTimerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; |
||||||
|
expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(true); |
||||||
|
}); |
||||||
|
it('should return true if the timerange has changed', () => { |
||||||
|
const query = '{job="grafana"}'; |
||||||
|
const prevQuery = '{job="grafana"}'; |
||||||
|
const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; |
||||||
|
const prevTimerange = { raw: { from: 'now-2h', to: 'now' } } as TimeRange; |
||||||
|
expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(true); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should return false if the query and timerange have not changed', () => { |
||||||
|
const query = '{job="grafana"}'; |
||||||
|
const prevQuery = '{job="grafana"}'; |
||||||
|
const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; |
||||||
|
const prevTimerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; |
||||||
|
expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(false); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe('makeStatsRequest', () => { |
||||||
|
const datasource = createLokiDatasource(); |
||||||
|
|
||||||
|
it('should return undefined if there is no query', () => { |
||||||
|
const query = ''; |
||||||
|
expect(getStats(datasource, query)).resolves.toBe(undefined); // change
|
||||||
|
}); |
||||||
|
|
||||||
|
it('should return undefined if the response has no data', () => { |
||||||
|
const query = '{job="grafana"}'; |
||||||
|
datasource.getQueryStats = jest.fn().mockResolvedValue({ streams: 0, chunks: 0, bytes: 0, entries: 0 }); |
||||||
|
expect(getStats(datasource, query)).resolves.toBe(undefined); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should return the stats if the response has data', () => { |
||||||
|
const query = '{job="grafana"}'; |
||||||
|
|
||||||
|
datasource.getQueryStats = jest |
||||||
|
.fn() |
||||||
|
.mockResolvedValue({ streams: 1, chunks: 12611, bytes: 12913664, entries: 78344 }); |
||||||
|
expect(getStats(datasource, query)).resolves.toEqual({ |
||||||
|
streams: 1, |
||||||
|
chunks: 12611, |
||||||
|
bytes: 12913664, |
||||||
|
entries: 78344, |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
import { TimeRange } from '@grafana/data'; |
||||||
|
|
||||||
|
import { LokiDatasource } from '../datasource'; |
||||||
|
import { QueryStats } from '../types'; |
||||||
|
|
||||||
|
export async function getStats(datasource: LokiDatasource, query: string): Promise<QueryStats | undefined> { |
||||||
|
if (!query) { |
||||||
|
return undefined; |
||||||
|
} |
||||||
|
|
||||||
|
const response = await datasource.getQueryStats(query); |
||||||
|
return Object.values(response).every((v) => v === 0) ? undefined : response; |
||||||
|
} |
||||||
|
|
||||||
|
export function shouldUpdateStats( |
||||||
|
query: string, |
||||||
|
prevQuery: string | undefined, |
||||||
|
timerange: TimeRange, |
||||||
|
prevTimerange: TimeRange | undefined |
||||||
|
): boolean { |
||||||
|
if ( |
||||||
|
query === prevQuery && |
||||||
|
timerange.raw.from === prevTimerange?.raw.from && |
||||||
|
timerange.raw.to === prevTimerange?.raw.to |
||||||
|
) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
Loading…
Reference in new issue