Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/public/utilities/formatting/formatLogSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

const DEFAULT_MAX_LENGTH = 120;

/*
* Only the beginning of the content can end up in the snippet, so the rest is not parsed: the markdown stripping has a quadratic cost on
* long lines, which would freeze the overview for any log with a huge content
*/
const MAX_PARSED_LENGTH = 2000;

const CODE_PLACEHOLDER = '\u0000';
const CODE_PLACEHOLDER_REGEX = new RegExp(`${CODE_PLACEHOLDER}(\\d+)${CODE_PLACEHOLDER}`, 'g');

Expand Down Expand Up @@ -62,7 +68,7 @@ export const formatLogSnippet = (text, title = '', maxLength = DEFAULT_MAX_LENGT

let inCodeFence = false;
const lines = [];
for (const rawLine of String(text).split(/\r?\n|\\n/)) {
for (const rawLine of String(text).slice(0, MAX_PARSED_LENGTH).split(/\r?\n|\\n/)) {
if (rawLine.trim().startsWith('```')) {
inCodeFence = !inCodeFence;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ module.exports = () => {
expect(formatLogSnippet('Other\nRun 1 failed', 'Run 1 failed')).to.equal('Other Run 1 failed');
});

it('should not take a long time to process a huge content', () => {
const start = Date.now();
const snippet = formatLogSnippet(' __a b'.repeat(100000));
expect(Date.now() - start).to.be.lessThan(500);
expect(snippet.length).to.be.lessThanOrEqual(120);
});

it('should truncate on a word boundary with an ellipsis', () => {
const snippet = formatLogSnippet('word '.repeat(50), '', 20);
expect(snippet).to.equal('word word word…');
Expand Down
Loading