Skip to content

Commit

Permalink
Explore: fixes loading more logs in logs context view (#24135)
Browse files Browse the repository at this point in the history
(cherry picked from commit d3a8f6d)
  • Loading branch information
Lukas Siatka authored and aknuds1 committed May 15, 2020
1 parent c3bc99f commit ff8afd5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
@@ -1,5 +1,8 @@
import React from 'react';
import { FieldType, LogRowModel, MutableDataFrame, Labels, LogLevel, DataQueryResponse } from '@grafana/data';
import { getRowContexts } from './LogRowContextProvider';
import { getRowContexts, LogRowContextProvider } from './LogRowContextProvider';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';

describe('getRowContexts', () => {
describe('when called with a DataFrame and results are returned', () => {
Expand Down Expand Up @@ -84,6 +87,85 @@ describe('getRowContexts', () => {
});
});

describe('LogRowContextProvider', () => {
describe('when requesting longer context', () => {
it('can request more log lines', async () => {
const firstResult = new MutableDataFrame({
refId: 'B',
fields: [
{ name: 'ts', type: FieldType.time, values: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] },
{
name: 'line',
type: FieldType.string,
values: ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1'],
labels: {},
},
{
name: 'id',
type: FieldType.string,
values: ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1'],
labels: {},
},
],
});

const secondResult = new MutableDataFrame({
refId: 'B',
fields: [
{ name: 'ts', type: FieldType.time, values: [14, 13, 12] },
{ name: 'line', type: FieldType.string, values: ['14', '13', '12'], labels: {} },
{ name: 'id', type: FieldType.string, values: ['14', '13', '12'], labels: {} },
],
});

let called = false;
const getRowContextMock = (row: LogRowModel, options?: any): Promise<DataQueryResponse> => {
if (!called) {
called = true;
return Promise.resolve({ data: [firstResult] });
}
return Promise.resolve({ data: [secondResult] });
};
let wrapper: any;
await act(async () => {
wrapper = await mount(
<LogRowContextProvider row={row} getRowContext={getRowContextMock}>
{({ result, errors, hasMoreContextRows, updateLimit, limit }) => {
return (
<div>
<div className="result">
<p className="result-before">{result.before?.toString()}</p>
<p className="result-after">{result.after?.toString()}</p>
</div>
<div className="errors">
<p className="errors-before">{errors.before}</p>
<p className="errors-after">{errors.after}</p>
</div>
<div className="hasMoreContextRows">
<p className="hasMoreContextRows-before">{String(hasMoreContextRows.before)}</p>
<p className="hasMoreContextRows-after">{String(hasMoreContextRows.after)}</p>
</div>
<div className="limit">{limit}</div>
<button className="updateLimit" onClick={updateLimit}>
Update limit
</button>
</div>
);
}}
</LogRowContextProvider>
);
});
expect(wrapper.find('.hasMoreContextRows-before').text()).toBe('true');
expect(wrapper.find('.hasMoreContextRows-after').text()).toBe('true');
expect(wrapper.find('.limit').text()).toBe('10');
await act(async () => wrapper.find('.updateLimit').simulate('click'));
expect(wrapper.find('.limit').text()).toBe('20');
expect(wrapper.find('.hasMoreContextRows-before').text()).toBe('true');
expect(wrapper.find('.hasMoreContextRows-after').text()).toBe('false');
});
});
});

const row: LogRowModel = {
entryFieldIndex: 0,
rowIndex: 0,
Expand Down
14 changes: 12 additions & 2 deletions packages/grafana-ui/src/components/Logs/LogRowContextProvider.tsx
Expand Up @@ -37,6 +37,7 @@ interface LogRowContextProviderProps {
errors: LogRowContextQueryErrors;
hasMoreContextRows: HasMoreContextRows;
updateLimit: () => void;
limit: number;
}) => JSX.Element;
}

Expand Down Expand Up @@ -160,11 +161,19 @@ export const LogRowContextProvider: React.FunctionComponent<LogRowContextProvide
let hasMoreLogsBefore = true,
hasMoreLogsAfter = true;

if (currentResult && currentResult.data[0].length === value.data[0].length) {
const currentResultBefore = currentResult?.data[0][0];
const currentResultAfter = currentResult?.data[1][0];
const valueBefore = value.data[0][0];
const valueAfter = value.data[1][0];

// checks if there are more log rows in a given direction
// if after fetching additional rows the length of result is the same,
// we can assume there are no logs in that direction within a given time range
if (currentResult && (!valueBefore || currentResultBefore.length === valueBefore.length)) {
hasMoreLogsBefore = false;
}

if (currentResult && currentResult.data[1].length === value.data[1].length) {
if (currentResult && (!valueAfter || currentResultAfter.length === valueAfter.length)) {
hasMoreLogsAfter = false;
}

Expand All @@ -189,5 +198,6 @@ export const LogRowContextProvider: React.FunctionComponent<LogRowContextProvide
},
hasMoreContextRows,
updateLimit: () => setLimit(limit + 10),
limit,
});
};

0 comments on commit ff8afd5

Please sign in to comment.